2013-03-25 00:08:01 +08:00
|
|
|
import datetime
|
|
|
|
|
|
|
|
from django.db import models
|
|
|
|
from django.db.models.fields.related import ReverseSingleRelatedObjectDescriptor
|
|
|
|
from django.utils.encoding import python_2_unicode_compatible
|
|
|
|
from django.utils.translation import get_language
|
|
|
|
|
2013-11-03 12:36:09 +08:00
|
|
|
|
2013-08-06 14:58:07 +08:00
|
|
|
@python_2_unicode_compatible
|
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-08-06 14:58:07 +08:00
|
|
|
@python_2_unicode_compatible
|
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(
|
|
|
|
Country, from_fields=['person_country_id'], to_fields=['id'])
|
|
|
|
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-08-06 14:58:07 +08:00
|
|
|
@python_2_unicode_compatible
|
2013-03-25 00:08:01 +08:00
|
|
|
class Group(models.Model):
|
|
|
|
# Table Column Fields
|
|
|
|
name = models.CharField(max_length=128)
|
|
|
|
group_country = models.ForeignKey(Country)
|
|
|
|
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
|
|
|
|
|
|
|
|
|
2013-08-06 14:58:07 +08:00
|
|
|
@python_2_unicode_compatible
|
2013-03-25 00:08:01 +08:00
|
|
|
class Membership(models.Model):
|
|
|
|
# Table Column Fields
|
|
|
|
membership_country = models.ForeignKey(Country)
|
|
|
|
date_joined = models.DateTimeField(default=datetime.datetime.now)
|
|
|
|
invite_reason = models.CharField(max_length=64, null=True)
|
|
|
|
person_id = models.IntegerField()
|
|
|
|
group_id = models.IntegerField()
|
|
|
|
|
|
|
|
# Relation Fields
|
2013-08-06 14:58:07 +08:00
|
|
|
person = models.ForeignObject(
|
|
|
|
Person,
|
2013-03-25 00:08:01 +08:00
|
|
|
from_fields=['membership_country', 'person_id'],
|
|
|
|
to_fields=['person_country_id', 'id'])
|
2013-08-06 14:58:07 +08:00
|
|
|
group = models.ForeignObject(
|
|
|
|
Group,
|
2013-03-25 00:08:01 +08:00
|
|
|
from_fields=['membership_country', 'group_id'],
|
|
|
|
to_fields=['group_country', 'id'])
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
ordering = ('date_joined', 'invite_reason')
|
|
|
|
|
2013-08-06 14:58:07 +08:00
|
|
|
def __str__(self):
|
2013-03-25 00:08:01 +08:00
|
|
|
return "%s is a member of %s" % (self.person.name, self.group.name)
|
|
|
|
|
|
|
|
|
|
|
|
class Friendship(models.Model):
|
|
|
|
# Table Column Fields
|
|
|
|
from_friend_country = models.ForeignKey(Country, related_name="from_friend_country")
|
|
|
|
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,
|
2013-03-25 00:08:01 +08:00
|
|
|
from_fields=['from_friend_country', 'from_friend_id'],
|
|
|
|
to_fields=['person_country_id', 'id'],
|
|
|
|
related_name='from_friend')
|
|
|
|
|
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'],
|
|
|
|
related_name='to_friend_country')
|
|
|
|
|
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'],
|
|
|
|
related_name='to_friend')
|
|
|
|
|
2013-11-03 12:36:09 +08:00
|
|
|
|
2013-03-25 00:08:01 +08:00
|
|
|
class ArticleTranslationDescriptor(ReverseSingleRelatedObjectDescriptor):
|
|
|
|
"""
|
|
|
|
The set of articletranslation should not set any local fields.
|
|
|
|
"""
|
|
|
|
def __set__(self, instance, value):
|
|
|
|
if instance is None:
|
|
|
|
raise AttributeError("%s must be accessed via instance" % self.field.name)
|
|
|
|
setattr(instance, self.cache_name, value)
|
|
|
|
if value is not None and not self.field.rel.multiple:
|
|
|
|
setattr(value, self.field.related.get_cache_name(), instance)
|
|
|
|
|
2013-11-03 12:36:09 +08:00
|
|
|
|
2013-03-25 00:08:01 +08:00
|
|
|
class ColConstraint(object):
|
2014-03-02 22:25:53 +08:00
|
|
|
# Anything with as_sql() method works in get_extra_restriction().
|
2013-03-25 00:08:01 +08:00
|
|
|
def __init__(self, alias, col, value):
|
|
|
|
self.alias, self.col, self.value = alias, col, value
|
|
|
|
|
|
|
|
def as_sql(self, qn, connection):
|
|
|
|
return '%s.%s = %%s' % (qn(self.alias), qn(self.col)), [self.value]
|
|
|
|
|
2013-11-03 12:36:09 +08:00
|
|
|
|
2013-03-25 00:08:01 +08:00
|
|
|
class ActiveTranslationField(models.ForeignObject):
|
|
|
|
"""
|
|
|
|
This field will allow querying and fetching the currently active translation
|
|
|
|
for Article from ArticleTranslation.
|
|
|
|
"""
|
|
|
|
requires_unique_target = False
|
|
|
|
|
|
|
|
def get_extra_restriction(self, where_class, alias, related_alias):
|
|
|
|
return ColConstraint(alias, 'lang', get_language())
|
|
|
|
|
|
|
|
def get_extra_descriptor_filter(self):
|
|
|
|
return {'lang': get_language()}
|
|
|
|
|
|
|
|
def contribute_to_class(self, cls, name):
|
|
|
|
super(ActiveTranslationField, self).contribute_to_class(cls, name)
|
|
|
|
setattr(cls, self.name, ArticleTranslationDescriptor(self))
|
|
|
|
|
2013-11-03 12:36:09 +08:00
|
|
|
|
2013-03-25 00:08:01 +08:00
|
|
|
@python_2_unicode_compatible
|
|
|
|
class Article(models.Model):
|
|
|
|
active_translation = ActiveTranslationField(
|
|
|
|
'ArticleTranslation',
|
|
|
|
from_fields=['id'],
|
|
|
|
to_fields=['article'],
|
|
|
|
related_name='+',
|
|
|
|
null=True)
|
|
|
|
pub_date = models.DateField()
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
try:
|
|
|
|
return self.active_translation.title
|
|
|
|
except ArticleTranslation.DoesNotExist:
|
|
|
|
return '[No translation found]'
|
|
|
|
|
2013-11-03 12:36:09 +08:00
|
|
|
|
2013-07-25 21:25:23 +08:00
|
|
|
class NewsArticle(Article):
|
|
|
|
pass
|
|
|
|
|
2013-11-03 12:36:09 +08:00
|
|
|
|
2013-03-25 00:08:01 +08:00
|
|
|
class ArticleTranslation(models.Model):
|
|
|
|
article = models.ForeignKey(Article)
|
2013-12-02 03:21:57 +08:00
|
|
|
lang = models.CharField(max_length=2)
|
2013-03-25 00:08:01 +08:00
|
|
|
title = models.CharField(max_length=100)
|
|
|
|
body = models.TextField()
|
|
|
|
abstract = models.CharField(max_length=400, null=True)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
unique_together = ('article', 'lang')
|
|
|
|
ordering = ('active_translation__title',)
|
2013-06-27 21:44:21 +08:00
|
|
|
|
2013-11-03 12:36:09 +08:00
|
|
|
|
2013-06-27 21:44:21 +08:00
|
|
|
class ArticleTag(models.Model):
|
|
|
|
article = models.ForeignKey(Article, related_name="tags", related_query_name="tag")
|
|
|
|
name = models.CharField(max_length=255)
|
|
|
|
|
2013-11-03 12:36:09 +08:00
|
|
|
|
2013-06-27 21:44:21 +08:00
|
|
|
class ArticleIdea(models.Model):
|
2013-08-06 14:58:07 +08:00
|
|
|
articles = models.ManyToManyField(Article, related_name="ideas",
|
|
|
|
related_query_name="idea_things")
|
2013-06-27 21:44:21 +08:00
|
|
|
name = models.CharField(max_length=255)
|