Просмотр исходного кода

Initial post and topic objects with some default data

sh4nks 8 лет назад
Родитель
Сommit
9efc71fed9
1 измененных файлов с 28 добавлено и 2 удалено
  1. 28 2
      flaskbb/forum/models.py

+ 28 - 2
flaskbb/forum/models.py

@@ -153,10 +153,25 @@ class Post(db.Model, CRUDMixin):
         return url_for("forum.view_post", post_id=self.id)
         return url_for("forum.view_post", post_id=self.id)
 
 
     # Methods
     # Methods
-    def __init__(self, content=None):
+    def __init__(self, content=None, user=None, topic=None):
+        """Creates a post object with some initial values.
+
+        :param content: The content of the post.
+        :param user: The user of the post.
+        :param topic: Can either be the topic_id or the topic object.
+        """
         if content:
         if content:
             self.content = content
             self.content = content
 
 
+        if user:
+            self.user_id = user.id
+            self.username = user.username
+
+        if topic:
+            self.topic_id = topic if isinstance(topic, int) else topic.id
+
+        self.date_created = time_utcnow()
+
     def __repr__(self):
     def __repr__(self):
         """
         """
         Set to a unique key specific to the object in the database.
         Set to a unique key specific to the object in the database.
@@ -311,10 +326,21 @@ class Topic(db.Model, CRUDMixin):
         return url_for("forum.view_topic", topic_id=self.id, slug=self.slug)
         return url_for("forum.view_topic", topic_id=self.id, slug=self.slug)
 
 
     # Methods
     # Methods
-    def __init__(self, title=None):
+    def __init__(self, title=None, user=None):
+        """Creates a topic object with some initial values.
+
+        :param title: The title of the topic.
+        :param user: The user of the post.
+        """
         if title:
         if title:
             self.title = title
             self.title = title
 
 
+        if user:
+            self.user_id = user.id
+            self.username = user.username
+
+        self.date_created = self.last_updated = time_utcnow()
+
     def __repr__(self):
     def __repr__(self):
         """
         """
         Set to a unique key specific to the object in the database.
         Set to a unique key specific to the object in the database.