class DeletedPost(models.Model): """ Deleted Post model for post deleted by group owner or super user """ id = models.CharField(max_length=50, primary_key=True) user = models.ForeignKey(FBUser, related_name='delete_posts') created_time = models.DateTimeField() updated_time = models.DateTimeField() message = models.TextField(null=True, blank=True) picture = models.CharField(max_length=2083, null=True, blank=True) comment_count = models.IntegerField(default=0) like_count = models.IntegerField(default=0) share_count = models.IntegerField(default=0) group = models.ForeignKey(Group, related_name='delete_posts') # Enroll field in Mezzanine Search Engine objects = SearchableManager() search_fields = ("message", ) def __str__(self): return self.id def is_updated(self, new_updated_time): """ Check post is updated. :param new_updated_time: json datetime :return: True is updated and False is not updated """ return self.updated_time.strftime( '%Y-%m-%dT%H:%M:%S') != new_updated_time.split('+')[0] def get_diff_cre_time(self): """ Get different time from created time :return: different time """ return get_different_time(self.created_time) def get_diff_upe_time(self): """ Get different time from updated time :return: different time """ return get_different_time(self.updated_time) @classmethod def create(cls, post): return cls(id=post.id, user=post.user, created_time=post.created_time, updated_time=post.updated_time, message=post.message, picture=post.picture, comment_count=post.comment_count, like_count=post.like_count, share_count=post.share_count, group=post.group)
class Comment(models.Model): """ Facebook Comment model """ id = models.CharField(max_length=20, primary_key=True) user = models.ForeignKey(FBUser, related_name='comments') created_time = models.DateTimeField() message = models.TextField(null=True, blank=True) like_count = models.IntegerField(default=0) comment_count = models.IntegerField(default=0) post = models.ForeignKey(Post, related_name='comments') parent = models.ForeignKey('Comment', null=True, related_name='comments') group = models.ForeignKey(Group, related_name='comments') is_show = models.BooleanField(default=True) # Enroll field in Mezzanine Search Engine objects = SearchableManager() search_fields = ("message", ) def __str__(self): return self.id def get_diff_cre_time(self): """ Get different time from created time :return: different time """ return get_different_time(self.created_time)
class FBUser(models.Model): """ Facebook User model """ id = models.CharField(max_length=20, primary_key=True) name = models.CharField(max_length=50) picture = models.CharField(max_length=2083, null=True, blank=True) groups = models.ManyToManyField(Group) updated_time = models.DateTimeField(auto_now_add=True) # Enroll field in Mezzanine Search Engine objects = SearchableManager() search_fields = ("name", ) def __str__(self): return self.id def is_update(self): """ Check update is possible. (per 1 day) :return: """ now = timezone.now() diff = now - self.updated_time if diff.days >= 1: return True return False
class Searchable(models.Model): """ An abstract base class to extend django Model classes from to make them searchable. Requires that the base class defines search_fields. """ objects = SearchableManager() class Meta(): abstract = True
class Author(models.Model): english_name = models.CharField(max_length=30, null=True, verbose_name=u"英文姓名", unique=True) chinese_name = models.CharField(max_length=40, null=True, verbose_name=u"中文姓名") country = models.CharField(max_length=40, null=True, verbose_name=u"国家") description = models.TextField(verbose_name=u"作者简介", null=True) objects = SearchableManager() search_fields = ("english_name", "chinese_name") def __str__(self): return self.english_name + '---' + self.chinese_name class Meta: verbose_name = u'作者' verbose_name_plural = u'作者'
class Post(models.Model): """ Facebook Post model """ id = models.CharField(max_length=50, primary_key=True) user = models.ForeignKey(FBUser, related_name='posts') created_time = models.DateTimeField() updated_time = models.DateTimeField() message = models.TextField(null=True, blank=True) picture = models.CharField(max_length=2083, null=True, blank=True) comment_count = models.IntegerField(default=0) like_count = models.IntegerField(default=0) share_count = models.IntegerField(default=0) group = models.ForeignKey(Group, related_name='posts') is_show = models.BooleanField(default=True) # Enroll field in Mezzanine Search Engine objects = SearchableManager() search_fields = ("message", ) def __str__(self): return self.id def is_updated(self, new_updated_time): """ Check post is updated. :param new_updated_time: json datetime :return: True is updated and False is not updated """ return self.updated_time.strftime( '%Y-%m-%dT%H:%M:%S') != new_updated_time.split('+')[0] def get_diff_cre_time(self): """ Get different time from created time :return: different time """ return get_different_time(self.created_time) def get_diff_upe_time(self): """ Get different time from updated time :return: different time """ return get_different_time(self.updated_time)
class Book(BlogPost): chinese_title = models.CharField(max_length=100, null=True, verbose_name=u"中文书名") isbn = models.CharField(max_length=100, null=True, verbose_name="ISBN") author = models.ForeignKey("Author", verbose_name=u"作者") publisher = models.ForeignKey(Publisher, null=True, verbose_name=u"出版社") publication_date = models.DateField(null=True, verbose_name=u"出版日期") cover = models.FileField(upload_to="covers", verbose_name=u"图片", null=True) # cover = ThumbnailerImageField(upload_to="covers", verbose_name=u"图片", null=True) # photo = ThumbnailerImageField(upload_to='cover_photos', blank=True) objects = SearchableManager() search_fields = ("title", "description") class Meta: verbose_name = u'图书' verbose_name_plural = u'图书'
class Attachment(models.Model): """ Facebook Attachment model that is part of Post and Comment """ post = models.ForeignKey(Post, null=True, related_name='attachments') comment = models.ForeignKey(Comment, null=True, related_name='attachments') url = models.CharField(max_length=2083, null=True, blank=True) title = models.CharField(max_length=255, null=True, blank=True) description = models.TextField(null=True, blank=True) type = models.CharField(max_length=30, null=True, blank=True) # photo, share, unavailable, album(sub), video_autoplay, multi_share(sub), video_share_youtube, note media = models.ForeignKey(Media, null=True, related_name='media') objects = SearchableManager() search_fields = ("description", )
class DeletedComment(models.Model): """ Deleted Comment model for comment deleted by group owner or super user """ id = models.CharField(max_length=20, primary_key=True) user = models.ForeignKey(FBUser, related_name='delete_comments') created_time = models.DateTimeField() message = models.TextField(null=True, blank=True) like_count = models.IntegerField(default=0) comment_count = models.IntegerField(default=0) post = models.CharField(max_length=50) parent = models.CharField(max_length=20, null=True) group = models.ForeignKey(Group, related_name='delete_comments') # Enroll field in Mezzanine Search Engine objects = SearchableManager() search_fields = ("message", ) def __str__(self): return self.id def get_diff_cre_time(self): """ Get different time from created time :return: different time """ return get_different_time(self.created_time) @classmethod def create(cls, comment): if comment.parent: parent = comment.parent.id else: parent = None return cls(id=comment.id, user=comment.user, created_time=comment.created_time, message=comment.message, like_count=comment.like_count, comment_count=comment.comment_count, post=comment.post.id, parent=parent, group=comment.group)
class MediaFile(Orderable, Displayable): """Single file to add in a library.""" library = models.ForeignKey(MediaLibrary, related_name="files") file = FileField(_("File"), max_length=200, upload_to=upload_to("galleries.GalleryImage.file", "downloads")) #title = models.CharField(_("Title"), max_length=50, blank=True) #description = models.CharField(_("Description"), max_length=1000, blank=True) objects = SearchableManager() search_fields = {"title": 5, "description": 1} class Meta: verbose_name = _("Arquivo") verbose_name_plural = _("Arquivos") def __unicode__(self): return self.description def get_absolute_url(self): """ """ return "%s%s" % (settings.MEDIA_URL, self.file) # return reverse("page", kwargs={"slug": slug}) def save(self, *args, **kwargs): """ If no description is given when created, create one from the file name. """ if not self.id and not self.description: name = unquote(self.file.url).split("/")[-1].rsplit(".", 1)[0] name = name.replace("'", "") name = "".join([c if c not in punctuation else " " for c in name]) # str.title() doesn't deal with unicode very well. # http://bugs.python.org/issue6412 name = "".join([ s.upper() if i == 0 or name[i - 1] == " " else s for i, s in enumerate(name) ]) self.description = name super(MediaFile, self).save(*args, **kwargs)
class Person( TitledSlugged, MetaData, TimeStamped, AdminThumbMixin, Address, TeamOwnable ): """(Person description)""" objects = SearchableManager() search_fields = {"title": 5} user = models.OneToOneField( User, verbose_name=_('user'), blank=True, null=True, on_delete=models.SET_NULL ) person_title = models.CharField( _('title'), max_length=16, choices=TITLE_CHOICES, blank=True ) gender = models.CharField( _('gender'), max_length=16, choices=GENDER_CHOICES, blank=True ) first_name = models.CharField( _('first name'), max_length=255, blank=True, null=True ) last_name = models.CharField( _('last name'), max_length=255, blank=True, null=True ) email = models.EmailField( _('email'), blank=True, null=True ) telephone = models.CharField( _('telephone'), max_length=64, blank=True, null=True ) register_id = models.CharField( _('register ID'), blank=True, null=True, max_length=128 ) birthday = models.DateField( _('birthday'), blank=True, null=True ) bio = RichTextField( _('biography'), blank=True ) role = models.CharField( _('role'), max_length=256, blank=True, null=True ) external_id = models.CharField( _('external ID'), blank=True, null=True, max_length=128 ) hal_url = models.URLField( _('HAL url'), max_length=512, blank=True ) karma = models.IntegerField( default=0, editable=False ) search_fields = {"title": 1} is_referenced = models.BooleanField( _('Is Referenced'), default=True, help_text=_("Determine if the Person has to be referenced on search") ) class Meta: verbose_name = _('person') ordering = ['last_name', ] permissions = TeamOwnable.Meta.permissions def __str__(self): return self.title def get_absolute_url(self): return reverse("organization_network-person-detail", kwargs={'slug': self.slug}) def set_names(self): names = self.title.split(' ') if len(names) == 1: self.first_name = '' self.last_name = names[0] elif len(names) == 2: self.first_name = names[0] self.last_name = names[1] else: self.first_name = names[0] self.last_name = ' '.join(names[1:]) def save(self, *args, **kwargs): self.clean() if self.id and not self.user: old = Person.objects.get(id=self.id) self.user = old.user if self.first_name and self.last_name and (not self.title or self.title == '-'): self.title = self.first_name + ' ' + self.last_name super(Person, self).save(*args, **kwargs) for activity in self.activities.all(): update_activity(activity)
class Group(models.Model): """ Facebook Group model """ id = models.CharField(max_length=20, primary_key=True) name = models.CharField(max_length=100) description = models.TextField(null=True, blank=True) updated_time = models.DateTimeField() privacy = models.CharField(max_length=30) is_stored = models.BooleanField(default=False) post_count = models.IntegerField(default=0) comment_count = models.IntegerField(default=0) owner = models.ForeignKey('FBUser', null=True, related_name='group_owner') # Enroll field in Mezzanine Search Engine objects = SearchableManager() search_fields = ("name", ) def __str__(self): return self.id def is_updated(self, new_updated_time): """ Check group is updated. :param new_updated_time: json datetime :return: True is updated and False is not updated """ return self.updated_time.strftime( '%Y-%m-%dT%H:%M:%S') != new_updated_time.split('+')[0] def get_diff_time(self): """ Get different time from updated time :return: different time """ return get_different_time(self.updated_time) @staticmethod def get_unit(number): """ Get unit number :param number: number :return: unit number """ if number > 1000: return str(int(number / 1000)) + 'K' else: return number def get_post_count_unit(self): """ Get post count unit :return: post count unit number """ return Group.get_unit(self.post_count) def get_comment_count_unit(self): """ Get comment count unit :return: comment count unit number """ return Group.get_unit(self.comment_count)