Luke Plant avatar Luke Plant committed 19d9bc0

Added contacts app

Comments (0)

Files changed (7)

christchurch/settings.py

     'south',
     'christchurch',
     'sermons',
+    'contacts',
     'cms',
     'menus',
     'mptt',

Empty file added.

contacts/admin.py

+from django.contrib import admin
+
+from .models import Contact, HomeGroup
+
+
+class ContactAdmin(admin.ModelAdmin):
+    def address(obj):
+        return obj.address.replace('\n', ', ')
+    list_display = ['name', address, 'post_code', 'phone_number', 'mobile_number', 'email', 'church_member', ]
+    list_filter = ['church_member', 'home_group']
+
+
+class HomeGroupAdmin(admin.ModelAdmin):
+    list_display = ['name', 'group_email']
+
+admin.site.register(Contact, ContactAdmin)
+admin.site.register(HomeGroup, HomeGroupAdmin)

contacts/migrations/0001_initial.py

+# encoding: utf-8
+import datetime
+from south.db import db
+from south.v2 import SchemaMigration
+from django.db import models
+
+class Migration(SchemaMigration):
+
+    def forwards(self, orm):
+        
+        # Adding model 'HomeGroup'
+        db.create_table('contacts_homegroup', (
+            ('id', self.gf('django.db.models.fields.AutoField')(primary_key=True)),
+            ('name', self.gf('django.db.models.fields.CharField')(max_length=100)),
+            ('group_email', self.gf('django.db.models.fields.EmailField')(max_length=75, blank=True)),
+        ))
+        db.send_create_signal('contacts', ['HomeGroup'])
+
+        # Adding model 'Contact'
+        db.create_table('contacts_contact', (
+            ('id', self.gf('django.db.models.fields.AutoField')(primary_key=True)),
+            ('name', self.gf('django.db.models.fields.CharField')(max_length=100)),
+            ('address', self.gf('django.db.models.fields.TextField')(blank=True)),
+            ('post_code', self.gf('django.db.models.fields.CharField')(max_length=10, blank=True)),
+            ('phone_number', self.gf('django.db.models.fields.CharField')(max_length=22, blank=True)),
+            ('mobile_number', self.gf('django.db.models.fields.CharField')(max_length=22, blank=True)),
+            ('email', self.gf('django.db.models.fields.EmailField')(max_length=75, blank=True)),
+            ('home_group', self.gf('django.db.models.fields.related.ForeignKey')(to=orm['contacts.HomeGroup'], null=True, blank=True)),
+            ('church_member', self.gf('django.db.models.fields.BooleanField')(default=False)),
+        ))
+        db.send_create_signal('contacts', ['Contact'])
+
+
+    def backwards(self, orm):
+        
+        # Deleting model 'HomeGroup'
+        db.delete_table('contacts_homegroup')
+
+        # Deleting model 'Contact'
+        db.delete_table('contacts_contact')
+
+
+    models = {
+        'contacts.contact': {
+            'Meta': {'ordering': "['name']", 'object_name': 'Contact'},
+            'address': ('django.db.models.fields.TextField', [], {'blank': 'True'}),
+            'church_member': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
+            'email': ('django.db.models.fields.EmailField', [], {'max_length': '75', 'blank': 'True'}),
+            'home_group': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['contacts.HomeGroup']", 'null': 'True', 'blank': 'True'}),
+            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+            'mobile_number': ('django.db.models.fields.CharField', [], {'max_length': '22', 'blank': 'True'}),
+            'name': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
+            'phone_number': ('django.db.models.fields.CharField', [], {'max_length': '22', 'blank': 'True'}),
+            'post_code': ('django.db.models.fields.CharField', [], {'max_length': '10', 'blank': 'True'})
+        },
+        'contacts.homegroup': {
+            'Meta': {'ordering': "['name']", 'object_name': 'HomeGroup'},
+            'group_email': ('django.db.models.fields.EmailField', [], {'max_length': '75', 'blank': 'True'}),
+            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+            'name': ('django.db.models.fields.CharField', [], {'max_length': '100'})
+        }
+    }
+
+    complete_apps = ['contacts']
Add a comment to this file

contacts/migrations/__init__.py

Empty file added.

contacts/models.py

+from django.db import models
+
+
+class HomeGroup(models.Model):
+    name = models.CharField(max_length=100)
+    group_email = models.EmailField(blank=True)
+
+    def __unicode__(self):
+        return self.name
+
+    class Meta:
+        ordering = ['name']
+
+
+class Contact(models.Model):
+    name = models.CharField(max_length=100)
+    address = models.TextField(blank=True)
+    post_code = models.CharField(max_length=10, blank=True)
+    phone_number = models.CharField(max_length=22, blank=True)
+    mobile_number = models.CharField(max_length=22, blank=True)
+    email = models.EmailField(blank=True)
+    home_group = models.ForeignKey(HomeGroup, null=True, blank=True)
+    church_member = models.BooleanField()
+
+    def __unicode__(self):
+        return self.name
+
+    class Meta:
+        ordering = ['name']

contacts/scripts/import.py

+#!/usr/bin/env python
+from __future__ import print_function
+
+import os
+import sys
+import re
+
+from contacts.models import Contact
+
+def parse(data):
+    # One-off parsing function, for data in a simple text file with double
+    # line separating contacts.
+    groups = data.split('\n\n')
+    items = []
+    for g in groups:
+        item = {}
+        lines = g.strip().split('\n')
+        # Name is always first
+        item['name'] = lines[0]
+        lines.pop(0)
+        for i, l in reversed(list(enumerate(lines))):
+            if '@' in l:
+                item['email'] = l
+                lines.pop(i)
+            elif l.startswith('01'):
+                item['phone_number'] = l
+                lines.pop(i)
+            elif l.startswith('07'):
+                item['mobile_number'] = l
+                lines.pop(i)
+            elif re.match(r'^(BD|LS|LE)\d+ .*', l):
+                item['post_code'] = l.strip().strip('.')
+                lines.pop(i)
+        if len(lines) > 1:
+            print('\n'.join(lines))
+            print(g)
+            sys.exit()
+
+        elif len(lines) == 1:
+            item['address'] = lines[0].replace(', ', '\n').replace(',', '\n')
+        items.append(item)
+    return items
+
+
+def import_data(data):
+    Contact.objects.all().delete()
+    items = parse(data)
+    for item in items:
+        c = Contact(name=item['name'],
+                    address=item.get('address', ''),
+                    post_code=item.get('post_code', ''),
+                    phone_number=item.get('phone_number', ''),
+                    mobile_number=item.get('mobile_number', ''),
+                    email=item.get('email', ''),
+                    church_member=False)
+        c.save()
+
+
+if __name__ == '__main__':
+    import_data(sys.stdin.read())
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.