2013-03-24 16:08:01 +00:00
|
|
|
import datetime
|
|
|
|
|
|
|
|
from django.db import models
|
|
|
|
from django.utils.encoding import python_2_unicode_compatible
|
|
|
|
|
2013-11-03 04:36:09 +00:00
|
|
|
|
2013-08-06 06:58:07 +00:00
|
|
|
@python_2_unicode_compatible
|
2013-03-24 16:08:01 +00:00
|
|
|
class Country(models.Model):
|
|
|
|
# Table Column Fields
|
|
|
|
name = models.CharField(max_length=50)
|
|
|
|
|
2013-08-06 06:58:07 +00:00
|
|
|
def __str__(self):
|
2013-03-24 16:08:01 +00:00
|
|
|
return self.name
|
|
|
|
|
2013-11-03 04:36:09 +00:00
|
|
|
|
2013-08-06 06:58:07 +00:00
|
|
|
@python_2_unicode_compatible
|
2013-03-24 16:08:01 +00:00
|
|
|
class Person(models.Model):
|
|
|
|
# Table Column Fields
|
|
|
|
name = models.CharField(max_length=128)
|
|
|
|
person_country_id = models.IntegerField()
|
|
|
|
|
|
|
|
# Relation Fields
|
|
|
|
person_country = models.ForeignObject(
|
2015-07-22 14:43:21 +00:00
|
|
|
Country,
|
|
|
|
from_fields=['person_country_id'],
|
|
|
|
to_fields=['id'],
|
|
|
|
on_delete=models.CASCADE,
|
|
|
|
)
|
2013-03-24 16:08:01 +00:00
|
|
|
friends = models.ManyToManyField('self', through='Friendship', symmetrical=False)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
ordering = ('name',)
|
|
|
|
|
2013-08-06 06:58:07 +00:00
|
|
|
def __str__(self):
|
2013-03-24 16:08:01 +00:00
|
|
|
return self.name
|
|
|
|
|
2013-11-03 04:36:09 +00:00
|
|
|
|
2013-08-06 06:58:07 +00:00
|
|
|
@python_2_unicode_compatible
|
2013-03-24 16:08:01 +00:00
|
|
|
class Group(models.Model):
|
|
|
|
# Table Column Fields
|
|
|
|
name = models.CharField(max_length=128)
|
2015-07-22 14:43:21 +00:00
|
|
|
group_country = models.ForeignKey(Country, models.CASCADE)
|
2013-03-24 16:08:01 +00:00
|
|
|
members = models.ManyToManyField(Person, related_name='groups', through='Membership')
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
ordering = ('name',)
|
|
|
|
|
2013-08-06 06:58:07 +00:00
|
|
|
def __str__(self):
|
2013-03-24 16:08:01 +00:00
|
|
|
return self.name
|
|
|
|
|
|
|
|
|
2013-08-06 06:58:07 +00:00
|
|
|
@python_2_unicode_compatible
|
2013-03-24 16:08:01 +00:00
|
|
|
class Membership(models.Model):
|
|
|
|
# Table Column Fields
|
2015-07-22 14:43:21 +00:00
|
|
|
membership_country = models.ForeignKey(Country, models.CASCADE)
|
2013-03-24 16:08:01 +00:00
|
|
|
date_joined = models.DateTimeField(default=datetime.datetime.now)
|
|
|
|
invite_reason = models.CharField(max_length=64, null=True)
|
|
|
|
person_id = models.IntegerField()
|
2015-12-22 23:22:54 +00:00
|
|
|
group_id = models.IntegerField(blank=True, null=True)
|
2013-03-24 16:08:01 +00:00
|
|
|
|
|
|
|
# Relation Fields
|
2013-08-06 06:58:07 +00:00
|
|
|
person = models.ForeignObject(
|
|
|
|
Person,
|
2015-12-22 23:22:54 +00:00
|
|
|
from_fields=['person_id', 'membership_country'],
|
|
|
|
to_fields=['id', 'person_country_id'],
|
2015-07-22 14:43:21 +00:00
|
|
|
on_delete=models.CASCADE,
|
|
|
|
)
|
2013-08-06 06:58:07 +00:00
|
|
|
group = models.ForeignObject(
|
|
|
|
Group,
|
2015-12-22 23:22:54 +00:00
|
|
|
from_fields=['group_id', 'membership_country'],
|
|
|
|
to_fields=['id', 'group_country'],
|
2015-07-22 14:43:21 +00:00
|
|
|
on_delete=models.CASCADE,
|
|
|
|
)
|
2013-03-24 16:08:01 +00:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
ordering = ('date_joined', 'invite_reason')
|
|
|
|
|
2013-08-06 06:58:07 +00:00
|
|
|
def __str__(self):
|
2015-12-22 23:22:54 +00:00
|
|
|
group_name = self.group.name if self.group_id else 'NULL'
|
|
|
|
return "%s is a member of %s" % (self.person.name, group_name)
|
2013-03-24 16:08:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Friendship(models.Model):
|
|
|
|
# Table Column Fields
|
2015-07-22 14:43:21 +00:00
|
|
|
from_friend_country = models.ForeignKey(Country, models.CASCADE, related_name="from_friend_country")
|
2013-03-24 16:08:01 +00:00
|
|
|
from_friend_id = models.IntegerField()
|
|
|
|
to_friend_country_id = models.IntegerField()
|
|
|
|
to_friend_id = models.IntegerField()
|
|
|
|
|
|
|
|
# Relation Fields
|
2013-08-06 06:58:07 +00:00
|
|
|
from_friend = models.ForeignObject(
|
|
|
|
Person,
|
2015-07-22 14:43:21 +00:00
|
|
|
on_delete=models.CASCADE,
|
2013-03-24 16:08:01 +00:00
|
|
|
from_fields=['from_friend_country', 'from_friend_id'],
|
|
|
|
to_fields=['person_country_id', 'id'],
|
|
|
|
related_name='from_friend')
|
|
|
|
|
2013-08-06 06:58:07 +00:00
|
|
|
to_friend_country = models.ForeignObject(
|
|
|
|
Country,
|
2013-03-24 16:08:01 +00:00
|
|
|
from_fields=['to_friend_country_id'],
|
|
|
|
to_fields=['id'],
|
2015-07-22 14:43:21 +00:00
|
|
|
related_name='to_friend_country',
|
|
|
|
on_delete=models.CASCADE,
|
|
|
|
)
|
2013-03-24 16:08:01 +00:00
|
|
|
|
2013-08-06 06:58:07 +00:00
|
|
|
to_friend = models.ForeignObject(
|
|
|
|
Person,
|
2013-03-24 16:08:01 +00:00
|
|
|
from_fields=['to_friend_country_id', 'to_friend_id'],
|
|
|
|
to_fields=['person_country_id', 'id'],
|
2015-07-22 14:43:21 +00:00
|
|
|
related_name='to_friend',
|
|
|
|
on_delete=models.CASCADE,
|
|
|
|
)
|