2013-03-25 00:08:01 +08:00
|
|
|
import datetime
|
|
|
|
|
|
|
|
from django.db import models
|
|
|
|
|
2013-11-03 12:36:09 +08:00
|
|
|
|
2013-03-25 00:08:01 +08:00
|
|
|
class Country(models.Model):
|
|
|
|
# Table Column Fields
|
|
|
|
name = models.CharField(max_length=50)
|
|
|
|
|
2013-08-06 14:58:07 +08:00
|
|
|
def __str__(self):
|
2013-03-25 00:08:01 +08:00
|
|
|
return self.name
|
|
|
|
|
2013-11-03 12:36:09 +08:00
|
|
|
|
2013-03-25 00:08:01 +08: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 22:43:21 +08:00
|
|
|
Country,
|
|
|
|
from_fields=["person_country_id"],
|
|
|
|
to_fields=["id"],
|
|
|
|
on_delete=models.CASCADE,
|
|
|
|
)
|
2013-03-25 00:08:01 +08:00
|
|
|
friends = models.ManyToManyField("self", through="Friendship", symmetrical=False)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
ordering = ("name",)
|
|
|
|
|
2013-08-06 14:58:07 +08:00
|
|
|
def __str__(self):
|
2013-03-25 00:08:01 +08:00
|
|
|
return self.name
|
|
|
|
|
2013-11-03 12:36:09 +08:00
|
|
|
|
2013-03-25 00:08:01 +08:00
|
|
|
class Group(models.Model):
|
|
|
|
# Table Column Fields
|
|
|
|
name = models.CharField(max_length=128)
|
2015-07-22 22:43:21 +08:00
|
|
|
group_country = models.ForeignKey(Country, models.CASCADE)
|
2013-03-25 00:08:01 +08:00
|
|
|
members = models.ManyToManyField(
|
|
|
|
Person, related_name="groups", through="Membership"
|
|
|
|
)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
ordering = ("name",)
|
|
|
|
|
2013-08-06 14:58:07 +08:00
|
|
|
def __str__(self):
|
2013-03-25 00:08:01 +08:00
|
|
|
return self.name
|
|
|
|
|
|
|
|
|
|
|
|
class Membership(models.Model):
|
|
|
|
# Table Column Fields
|
2015-07-22 22:43:21 +08:00
|
|
|
membership_country = models.ForeignKey(Country, models.CASCADE)
|
2013-03-25 00:08:01 +08:00
|
|
|
date_joined = models.DateTimeField(default=datetime.datetime.now)
|
|
|
|
invite_reason = models.CharField(max_length=64, null=True)
|
|
|
|
person_id = models.IntegerField()
|
2015-12-23 07:22:54 +08:00
|
|
|
group_id = models.IntegerField(blank=True, null=True)
|
2013-03-25 00:08:01 +08:00
|
|
|
|
|
|
|
# Relation Fields
|
2013-08-06 14:58:07 +08:00
|
|
|
person = models.ForeignObject(
|
|
|
|
Person,
|
2015-12-23 07:22:54 +08:00
|
|
|
from_fields=["person_id", "membership_country"],
|
|
|
|
to_fields=["id", "person_country_id"],
|
2015-07-22 22:43:21 +08:00
|
|
|
on_delete=models.CASCADE,
|
|
|
|
)
|
2013-08-06 14:58:07 +08:00
|
|
|
group = models.ForeignObject(
|
|
|
|
Group,
|
2015-12-23 07:22:54 +08:00
|
|
|
from_fields=["group_id", "membership_country"],
|
|
|
|
to_fields=["id", "group_country"],
|
2015-07-22 22:43:21 +08:00
|
|
|
on_delete=models.CASCADE,
|
|
|
|
)
|
2013-03-25 00:08:01 +08:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
ordering = ("date_joined", "invite_reason")
|
|
|
|
|
2013-08-06 14:58:07 +08:00
|
|
|
def __str__(self):
|
2015-12-23 07:22:54 +08: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-25 00:08:01 +08:00
|
|
|
|
|
|
|
|
|
|
|
class Friendship(models.Model):
|
|
|
|
# Table Column Fields
|
2015-07-22 22:43:21 +08:00
|
|
|
from_friend_country = models.ForeignKey(
|
|
|
|
Country, models.CASCADE, related_name="from_friend_country"
|
|
|
|
)
|
2013-03-25 00:08:01 +08:00
|
|
|
from_friend_id = models.IntegerField()
|
|
|
|
to_friend_country_id = models.IntegerField()
|
|
|
|
to_friend_id = models.IntegerField()
|
|
|
|
|
|
|
|
# Relation Fields
|
2013-08-06 14:58:07 +08:00
|
|
|
from_friend = models.ForeignObject(
|
|
|
|
Person,
|
2015-07-22 22:43:21 +08:00
|
|
|
on_delete=models.CASCADE,
|
2013-03-25 00:08:01 +08:00
|
|
|
from_fields=["from_friend_country", "from_friend_id"],
|
|
|
|
to_fields=["person_country_id", "id"],
|
2018-03-16 17:54:34 +08:00
|
|
|
related_name="from_friend",
|
|
|
|
)
|
2013-03-25 00:08:01 +08:00
|
|
|
|
2013-08-06 14:58:07 +08:00
|
|
|
to_friend_country = models.ForeignObject(
|
|
|
|
Country,
|
2013-03-25 00:08:01 +08:00
|
|
|
from_fields=["to_friend_country_id"],
|
|
|
|
to_fields=["id"],
|
2015-07-22 22:43:21 +08:00
|
|
|
related_name="to_friend_country",
|
|
|
|
on_delete=models.CASCADE,
|
|
|
|
)
|
2013-03-25 00:08:01 +08:00
|
|
|
|
2013-08-06 14:58:07 +08:00
|
|
|
to_friend = models.ForeignObject(
|
|
|
|
Person,
|
2013-03-25 00:08:01 +08:00
|
|
|
from_fields=["to_friend_country_id", "to_friend_id"],
|
|
|
|
to_fields=["person_country_id", "id"],
|
2015-07-22 22:43:21 +08:00
|
|
|
related_name="to_friend",
|
|
|
|
on_delete=models.CASCADE,
|
|
|
|
)
|