Skip to content

Commit

Permalink
ledger.accounts.models: cherry-pick updates from disturbances branch
Browse files Browse the repository at this point in the history
  • Loading branch information
Scott Percival committed Feb 7, 2018
1 parent 6dd671d commit 3404af2
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 10 deletions.
65 changes: 65 additions & 0 deletions ledger/accounts/migrations/0013_auto_20180207_1210.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.10.8 on 2018-02-07 04:10
from __future__ import unicode_literals

from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
import django_countries.fields


class Migration(migrations.Migration):

dependencies = [
('accounts', '0012_auto_20180118_1505'),
]

operations = [
migrations.CreateModel(
name='Organisation',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=128, unique=True)),
('abn', models.CharField(blank=True, max_length=50, null=True, verbose_name='ABN')),
('identification', models.FileField(blank=True, null=True, upload_to='uploads/%Y/%m/%d')),
],
),
migrations.CreateModel(
name='OrganisationAddress',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('line1', models.CharField(max_length=255, verbose_name='Line 1')),
('line2', models.CharField(blank=True, max_length=255, verbose_name='Line 2')),
('line3', models.CharField(blank=True, max_length=255, verbose_name='Line 3')),
('locality', models.CharField(max_length=255, verbose_name='Suburb / Town')),
('state', models.CharField(blank=True, default='WA', max_length=255)),
('country', django_countries.fields.CountryField(default='AU', max_length=2)),
('postcode', models.CharField(max_length=10)),
('search_text', models.TextField(editable=False)),
('hash', models.CharField(db_index=True, editable=False, max_length=255)),
('organisation', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='adresses', to='accounts.Organisation')),
],
options={
'verbose_name_plural': 'organisation addresses',
},
),
migrations.AlterField(
model_name='address',
name='user',
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='profile_adresses', to=settings.AUTH_USER_MODEL),
),
migrations.AddField(
model_name='organisation',
name='billing_address',
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='org_billing_address', to='accounts.OrganisationAddress'),
),
migrations.AddField(
model_name='organisation',
name='postal_address',
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='org_postal_address', to='accounts.OrganisationAddress'),
),
migrations.AlterUniqueTogether(
name='organisationaddress',
unique_together=set([('organisation', 'hash')]),
),
]
43 changes: 33 additions & 10 deletions ledger/accounts/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ def _post_save(sender, instance, **kwargs):


@python_2_unicode_compatible
class Address(models.Model):
class BaseAddress(models.Model):
"""Generic address model, intended to provide billing and shipping
addresses.
Taken from django-oscar address AbstrastAddress class.
Expand All @@ -134,16 +134,13 @@ class Address(models.Model):
postcode = models.CharField(max_length=10)
# A field only used for searching addresses.
search_text = models.TextField(editable=False)
oscar_address = models.ForeignKey(UserAddress, related_name='profile_addresses')
user = models.ForeignKey('EmailUser', related_name='profile_addresses')
hash = models.CharField(max_length=255, db_index=True, editable=False)

def __str__(self):
return self.summary

class Meta:
verbose_name_plural = 'addresses'
unique_together = ('user','hash')
abstract = True

def clean(self):
# Strip all whitespace
Expand All @@ -155,7 +152,7 @@ def clean(self):
def save(self, *args, **kwargs):
self._update_search_text()
self.hash = self.generate_hash()
super(Address, self).save(*args, **kwargs)
super(BaseAddress, self).save(*args, **kwargs)

def _update_search_text(self):
search_fields = filter(
Expand Down Expand Up @@ -194,6 +191,14 @@ def generate_hash(self):
"""
return zlib.crc32(self.summary.strip().upper().encode('UTF8'))

class Address(BaseAddress):
user = models.ForeignKey('EmailUser', related_name='profile_adresses')
oscar_address = models.ForeignKey(UserAddress, related_name='profile_addresses')
class Meta:
verbose_name_plural = 'addresses'
unique_together = ('user','hash')


@python_2_unicode_compatible
class EmailIdentity(models.Model):
"""Table used for matching access email address with EmailUser.
Expand Down Expand Up @@ -275,10 +280,6 @@ def __str__(self):

def clean(self):
super(EmailUser, self).clean()

if (self.dob and self.dob < date(1900, 1, 1)):
raise ValidationError({'dob': 'Date of birth cannot be before 01/01/1900'})

self.email = self.email.lower() if self.email else self.email
post_clean.send(sender=self.__class__, instance=self)

Expand Down Expand Up @@ -458,6 +459,26 @@ def __str__(self):
else:
return '{}'.format(self.email)

@python_2_unicode_compatible
class Organisation(models.Model):
"""This model represents the details of a company or other organisation.
Management of these objects will be delegated to 0+ EmailUsers.
"""
name = models.CharField(max_length=128, unique=True)
abn = models.CharField(max_length=50, null=True, blank=True, verbose_name='ABN')
# TODO: business logic related to identification file upload/changes.
identification = models.FileField(upload_to='uploads/%Y/%m/%d', null=True, blank=True)
postal_address = models.ForeignKey('OrganisationAddress', related_name='org_postal_address', blank=True, null=True, on_delete=models.SET_NULL)
billing_address = models.ForeignKey('OrganisationAddress', related_name='org_billing_address', blank=True, null=True, on_delete=models.SET_NULL)

def __str__(self):
return self.name

class OrganisationAddress(BaseAddress):
organisation = models.ForeignKey(Organisation, null=True,blank=True, related_name='adresses')
class Meta:
verbose_name_plural = 'organisation addresses'
unique_together = ('organisation','hash')

class ProfileListener(object):
"""
Expand Down Expand Up @@ -644,3 +665,5 @@ def __str__(self):
class Meta:
managed = False
db_table = 'accounts_emailuser_report_v'


0 comments on commit 3404af2

Please sign in to comment.