From f918d21c30370e5a9dfbdfaac6aed695b0af0722 Mon Sep 17 00:00:00 2001 From: Marnanel Thurman Date: Sat, 4 Apr 2020 01:37:26 +0100 Subject: [PATCH] split "models" into separate files --- kepi/trilby_api/models/__init__.py | 14 + kepi/trilby_api/models/follow.py | 54 ++++ kepi/trilby_api/models/like.py | 35 +++ kepi/trilby_api/models/notification.py | 74 +++++ .../{models.py => models/person.py} | 272 +----------------- kepi/trilby_api/models/status.py | 145 ++++++++++ 6 files changed, 325 insertions(+), 269 deletions(-) create mode 100644 kepi/trilby_api/models/__init__.py create mode 100644 kepi/trilby_api/models/follow.py create mode 100644 kepi/trilby_api/models/like.py create mode 100644 kepi/trilby_api/models/notification.py rename kepi/trilby_api/{models.py => models/person.py} (52%) create mode 100644 kepi/trilby_api/models/status.py diff --git a/kepi/trilby_api/models/__init__.py b/kepi/trilby_api/models/__init__.py new file mode 100644 index 0000000..7c41e2f --- /dev/null +++ b/kepi/trilby_api/models/__init__.py @@ -0,0 +1,14 @@ +from .person import * +from .status import * +from .notification import * +from .like import * +from .follow import * + +__all__ = [ + 'TrilbyUser', + 'Person', + 'Status', + 'Notification', + 'Like', + 'Follow', + ] diff --git a/kepi/trilby_api/models/follow.py b/kepi/trilby_api/models/follow.py new file mode 100644 index 0000000..b09f67d --- /dev/null +++ b/kepi/trilby_api/models/follow.py @@ -0,0 +1,54 @@ +from django.db import models +from django.db.models.constraints import UniqueConstraint +from django.contrib.auth.models import AbstractUser +from django.conf import settings +from kepi.bowler_pub.create import create +import kepi.bowler_pub.crypto as crypto +from kepi.bowler_pub.utils import uri_to_url +from django.utils.timezone import now +from django.core.exceptions import ValidationError +import logging + +logger = logging.Logger('kepi') + +class Follow(models.Model): + + follower = models.ForeignKey( + 'Person', + on_delete = models.DO_NOTHING, + related_name = 'following', + ) + + following = models.ForeignKey( + 'Person', + on_delete = models.DO_NOTHING, + related_name = 'followers', + ) + + requested = models.BooleanField( + default=True, + ) + + show_reblogs = models.BooleanField( + default=True, + ) + + class Meta: + constraints = [ + UniqueConstraint( + fields = ['follower', 'following'], + name = 'follow_only_once', + ), + ] + + def __str__(self): + if self.requested: + return '[%s requests to follow %s]' % ( + follower, + following, + ) + else: + return '[%s follows %s]' % ( + follower, + following, + ) diff --git a/kepi/trilby_api/models/like.py b/kepi/trilby_api/models/like.py new file mode 100644 index 0000000..7d2d92c --- /dev/null +++ b/kepi/trilby_api/models/like.py @@ -0,0 +1,35 @@ +from django.db import models +from django.db.models.constraints import UniqueConstraint +from django.contrib.auth.models import AbstractUser +from django.conf import settings +from kepi.bowler_pub.create import create +import kepi.bowler_pub.crypto as crypto +from kepi.bowler_pub.utils import uri_to_url +from django.utils.timezone import now +from django.core.exceptions import ValidationError +import logging + +logger = logging.Logger('kepi') + +class Like(models.Model): + + liker = models.ForeignKey( + 'Person', + on_delete = models.DO_NOTHING, + ) + + liked = models.ForeignKey( + 'Status', + on_delete = models.DO_NOTHING, + ) + + class Meta: + constraints = [ + UniqueConstraint( + fields = ['liker', 'liked'], + name = 'i_cant_like_this_enough', + ), + ] + + def __str__(self): + return '[%s likes %s]' % (liker, liked) diff --git a/kepi/trilby_api/models/notification.py b/kepi/trilby_api/models/notification.py new file mode 100644 index 0000000..e97ae55 --- /dev/null +++ b/kepi/trilby_api/models/notification.py @@ -0,0 +1,74 @@ +from django.db import models +from django.db.models.constraints import UniqueConstraint +from django.contrib.auth.models import AbstractUser +from django.conf import settings +from kepi.bowler_pub.create import create +import kepi.bowler_pub.crypto as crypto +from kepi.bowler_pub.utils import uri_to_url +from django.utils.timezone import now +from django.core.exceptions import ValidationError +import logging + +logger = logging.Logger('kepi') + +class Notification(models.Model): + + FOLLOW = 'F' + MENTION = 'M' + REBLOG = 'R' + FAVOURITE = 'L' + + TYPE_CHOICES = [ + (FOLLOW, 'follow'), + (MENTION, 'mention'), + (REBLOG, 'reblog'), + (FAVOURITE, 'favourite'), + ] + + notification_type = models.CharField( + max_length = 1, + choices = TYPE_CHOICES, + ) + + created_at = models.DateTimeField( + default = now, + ) + + for_account = models.ForeignKey( + 'Person', + on_delete = models.DO_NOTHING, + related_name = 'notifications_for', + ) + + about_account = models.ForeignKey( + 'Person', + on_delete = models.DO_NOTHING, + related_name = 'notifications_about', + blank = True, + null = True, + ) + + status = models.ForeignKey( + 'Status', + on_delete = models.DO_NOTHING, + blank = True, + null = True, + ) + + def __str__(self): + + if self.notification_type == self.FOLLOW: + detail = '%s has followed you' % (self.about_account,) + elif self.notification_type == self.MENTION: + detail = '%s has mentioned you' % (self.about_account,) + elif self.notification_type == self.REBLOG: + detail = '%s has reblogged you' % (self.about_account,) + elif self.notification_type == self.FAVOURITE: + detail = '%s has favourited your status' % (self.about_account,) + else: + detail = '(%s?)' % (self.notification_type,) + + return '[%s: %s]' % ( + self.for_account.id, + detail, + ) diff --git a/kepi/trilby_api/models.py b/kepi/trilby_api/models/person.py similarity index 52% rename from kepi/trilby_api/models.py rename to kepi/trilby_api/models/person.py index 373824a..74bcf8d 100644 --- a/kepi/trilby_api/models.py +++ b/kepi/trilby_api/models/person.py @@ -243,8 +243,10 @@ class Person(models.Model): @property def inbox(self): + import kepi.trilby_api.models as trilby_models + def inbox_generator(): - for status in Status.objects.all(): + for status in trilby_models.Status.objects.all(): yield status return inbox_generator() @@ -258,271 +260,3 @@ class Person(models.Model): return self.remote_url else: return self.local_user - -################### - -class Status(models.Model): - - # TODO: The original design has the serial number - # monotonically but unpredictably increasing. - - remote_url = models.URLField( - max_length = 255, - null = True, - blank = True, - unique = True, - ) - - account = models.ForeignKey( - 'Person', - on_delete = models.DO_NOTHING, - ) - - in_reply_to_id = models.ForeignKey( - 'self', - on_delete = models.DO_NOTHING, - null = True, - blank = True, - ) - - content = models.TextField( - ) - - created_at = models.DateTimeField( - default = now, - ) - - # TODO Media - - sensitive = models.BooleanField( - default = False, - ) - - spoiler_text = models.CharField( - max_length = 255, - null = True, - blank = True, - default = '', - ) - - visibility = models.CharField( - max_length = 255, - default = 'public', - ) - - language = models.CharField( - max_length = 255, - null = True, - default = settings.KEPI['LANGUAGES'][0], - ) - - idempotency_key = models.CharField( - max_length = 255, - null = True, - default = None, - ) - - @property - def emojis(self): - return [] # TODO - - @property - def reblogs_count(self): - return 0 # FIXME - - @property - def favourites_count(self): - return 0 # FIXME - - @property - def reblogged(self): - return False # FIXME - - @property - def favourited(self): - return False # FIXME - - @property - def muted(self): - return False # FIXME - - @property - def pinned(self): - return False # FIXME - - @property - def media_attachments(self): - return [] # FIXME - - @property - def mentions(self): - return [] # FIXME - - @property - def tags(self): - return [] # FIXME - - @property - def card(self): - return None # FIXME - - @property - def poll(self): - return None # FIXME - - @property - def application(self): - return None # FIXME - - @property - def in_reply_to_account_id(self): - return None # FIXME - - @property - def uri(self): - return 'FIXME' # FIXME - - @property - def url(self): - return 'FIXME' # FIXME - - @property - def ancestors(self): - return [] # FIXME - - @property - def descendants(self): - return [] # FIXME - -################### - -class Notification(models.Model): - - FOLLOW = 'F' - MENTION = 'M' - REBLOG = 'R' - FAVOURITE = 'L' - - TYPE_CHOICES = [ - (FOLLOW, 'follow'), - (MENTION, 'mention'), - (REBLOG, 'reblog'), - (FAVOURITE, 'favourite'), - ] - - notification_type = models.CharField( - max_length = 1, - choices = TYPE_CHOICES, - ) - - created_at = models.DateTimeField( - default = now, - ) - - for_account = models.ForeignKey( - Person, - on_delete = models.DO_NOTHING, - related_name = 'notifications_for', - ) - - about_account = models.ForeignKey( - Person, - on_delete = models.DO_NOTHING, - related_name = 'notifications_about', - blank = True, - null = True, - ) - - status = models.ForeignKey( - Status, - on_delete = models.DO_NOTHING, - blank = True, - null = True, - ) - - def __str__(self): - - if self.notification_type == self.FOLLOW: - detail = '%s has followed you' % (self.about_account,) - elif self.notification_type == self.MENTION: - detail = '%s has mentioned you' % (self.about_account,) - elif self.notification_type == self.REBLOG: - detail = '%s has reblogged you' % (self.about_account,) - elif self.notification_type == self.FAVOURITE: - detail = '%s has favourited your status' % (self.about_account,) - else: - detail = '(%s?)' % (self.notification_type,) - - return '[%s: %s]' % ( - self.for_account.id, - detail, - ) - -##################################### - -class Like(models.Model): - - liker = models.ForeignKey( - 'Person', - on_delete = models.DO_NOTHING, - ) - - liked = models.ForeignKey( - 'Status', - on_delete = models.DO_NOTHING, - ) - - class Meta: - constraints = [ - UniqueConstraint( - fields = ['liker', 'liked'], - name = 'i_cant_like_this_enough', - ), - ] - - def __str__(self): - return '[%s likes %s]' % (liker, liked) - -##################################### - -class Follow(models.Model): - - follower = models.ForeignKey( - 'Person', - on_delete = models.DO_NOTHING, - related_name = 'following', - ) - - following = models.ForeignKey( - 'Person', - on_delete = models.DO_NOTHING, - related_name = 'followers', - ) - - requested = models.BooleanField( - default=True, - ) - - show_reblogs = models.BooleanField( - default=True, - ) - - class Meta: - constraints = [ - UniqueConstraint( - fields = ['follower', 'following'], - name = 'follow_only_once', - ), - ] - - def __str__(self): - if self.requested: - return '[%s requests to follow %s]' % ( - follower, - following, - ) - else: - return '[%s follows %s]' % ( - follower, - following, - ) diff --git a/kepi/trilby_api/models/status.py b/kepi/trilby_api/models/status.py new file mode 100644 index 0000000..e042394 --- /dev/null +++ b/kepi/trilby_api/models/status.py @@ -0,0 +1,145 @@ +from django.db import models +from django.db.models.constraints import UniqueConstraint +from django.contrib.auth.models import AbstractUser +from django.conf import settings +from kepi.bowler_pub.create import create +import kepi.bowler_pub.crypto as crypto +from kepi.bowler_pub.utils import uri_to_url +from django.utils.timezone import now +from django.core.exceptions import ValidationError +import logging + +logger = logging.Logger('kepi') + +class Status(models.Model): + + # TODO: The original design has the serial number + # monotonically but unpredictably increasing. + + remote_url = models.URLField( + max_length = 255, + null = True, + blank = True, + unique = True, + ) + + account = models.ForeignKey( + 'Person', + on_delete = models.DO_NOTHING, + ) + + in_reply_to_id = models.ForeignKey( + 'self', + on_delete = models.DO_NOTHING, + null = True, + blank = True, + ) + + content = models.TextField( + ) + + created_at = models.DateTimeField( + default = now, + ) + + # TODO Media + + sensitive = models.BooleanField( + default = False, + ) + + spoiler_text = models.CharField( + max_length = 255, + null = True, + blank = True, + default = '', + ) + + visibility = models.CharField( + max_length = 255, + default = 'public', + ) + + language = models.CharField( + max_length = 255, + null = True, + default = settings.KEPI['LANGUAGES'][0], + ) + + idempotency_key = models.CharField( + max_length = 255, + null = True, + default = None, + ) + + @property + def emojis(self): + return [] # TODO + + @property + def reblogs_count(self): + return 0 # FIXME + + @property + def favourites_count(self): + return 0 # FIXME + + @property + def reblogged(self): + return False # FIXME + + @property + def favourited(self): + return False # FIXME + + @property + def muted(self): + return False # FIXME + + @property + def pinned(self): + return False # FIXME + + @property + def media_attachments(self): + return [] # FIXME + + @property + def mentions(self): + return [] # FIXME + + @property + def tags(self): + return [] # FIXME + + @property + def card(self): + return None # FIXME + + @property + def poll(self): + return None # FIXME + + @property + def application(self): + return None # FIXME + + @property + def in_reply_to_account_id(self): + return None # FIXME + + @property + def uri(self): + return 'FIXME' # FIXME + + @property + def url(self): + return 'FIXME' # FIXME + + @property + def ancestors(self): + return [] # FIXME + + @property + def descendants(self): + return [] # FIXME