Browse Source

Don't delete the posts and topics a user has created when deleting him.

sh4nks 11 years ago
parent
commit
4b1e085598

+ 5 - 1
flaskbb/forum/models.py

@@ -25,7 +25,8 @@ class Post(db.Model):
                                                    name="fk_topic_id",
                                                    ondelete="CASCADE"))
     user_id = db.Column(db.Integer, db.ForeignKey("users.id"))
-    content = db.Column(db.Text)
+    username = db.Column(db.String, nullable=False)
+    content = db.Column(db.Text, nullable=False)
     date_created = db.Column(db.DateTime, default=datetime.utcnow())
     date_modified = db.Column(db.DateTime)
 
@@ -55,6 +56,7 @@ class Post(db.Model):
         # Adding a new post
         if user and topic:
             self.user_id = user.id
+            self.username = user.username
             self.topic_id = topic.id
             self.date_created = datetime.utcnow()
 
@@ -115,6 +117,7 @@ class Topic(db.Model):
                                                    name="fk_forum_id"))
     title = db.Column(db.String)
     user_id = db.Column(db.Integer, db.ForeignKey("users.id"))
+    username = db.Column(db.String, nullable=False)
     date_created = db.Column(db.DateTime, default=datetime.utcnow())
     last_updated = db.Column(db.DateTime, default=datetime.utcnow())
     locked = db.Column(db.Boolean, default=False)
@@ -176,6 +179,7 @@ class Topic(db.Model):
         # Set the forum and user id
         self.forum_id = forum.id
         self.user_id = user.id
+        self.username = user.username
 
         # Insert and commit the topic
         db.session.add(self)

+ 5 - 1
flaskbb/templates/forum/category.html

@@ -55,7 +55,11 @@
                 </a>
                 <br />
                 {{ forum[0].last_post.date_created|time_since }}<br />
-                by <a href="{{ url_for('user.profile', username=forum[0].last_post.user.username) }}">{{ forum[0].last_post.user.username }}</a>
+                    {% if forum[0].last_post.user_id %}
+                    by <a href="{{ url_for('user.profile', username=forum[0].last_post.user.username) }}">{{ forum[0].last_post.user.username }}</a>
+                    {% else %}
+                    {{ forum[0].last_post.username }}
+                    {% endif %}
                 {% else %}
                 No posts
                 {% endif %}

+ 9 - 0
flaskbb/templates/forum/forum.html

@@ -69,7 +69,11 @@
                     <!-- Topic Pagination -->
                     {{ topic_pages(topic, config["POSTS_PER_PAGE"]) }}
                     <br />
+                    {% if topic.user_id %}
                     <small>by <a href="{{ url_for('user.profile', username=topic.user.username) }}">{{ topic.user.username }}</a></small>
+                    {% else %}
+                    <small>by {{ topic.username }}</small>
+                    {% endif %}
                 </div>
             </td>
             <td>
@@ -80,7 +84,12 @@
             </td>
             <td>
                 <a href="{{ url_for('forum.view_post', post_id=topic.last_post.id) }}">{{ topic.last_post.date_created|time_since }}</a><br />
+
+                {% if topic.last_post.user_id %}
                 <small>by <a href="{{ url_for('user.profile', username=topic.last_post.user.username) }}">{{ topic.last_post.user.username }}</a></small>
+                {% else %}
+                <small>{{ topic.last_post.username }}</small>
+                {% endif %}
             </td>
         </tr>
         {% else %}

+ 7 - 1
flaskbb/templates/forum/index.html

@@ -54,7 +54,13 @@
                 </a>
                 <br />
                 {{ forum[0].last_post.date_created|time_since }}<br />
-                by <a href="{{ url_for('user.profile', username=forum[0].last_post.user.username) }}">{{ forum[0].last_post.user.username }}</a>
+
+                    {% if forum[0].last_post.user_id %}
+                    by <a href="{{ url_for('user.profile', username=forum[0].last_post.user.username) }}">{{ forum[0].last_post.user.username }}</a>
+                    {% else %}
+                    {{ forum[0].last_post.username }}
+                    {% endif %}
+
                 {% else %}
                 No posts
                 {% endif %}

+ 19 - 11
flaskbb/templates/forum/topic.html

@@ -60,7 +60,7 @@
                         {{ url_for('forum.view_topic', topic_id=topic.id) }}#pid{{ post.id }}
                     {% endif %}
                         ">{{ post.date_created|format_date('%d %B %Y') }}</a>
-                    {% if post.date_modified %}
+                    {% if post.user_id and post.date_modified %}
                     <small>
                         (Last modified: {{ post.date_modified|format_date }} by <a href="{{ url_for('user.profile', username=post.user.username) }}">{{ post.user.username }}</a>.)
                     </small>
@@ -73,6 +73,7 @@
         <td>
             <table class="table table-borderless">
                 <tr>
+                {% if post.user_id %}
                     {% if post.user.avatar %}
                     <td width="1">
                         <img src="{{ post.user.avatar }}" alt="Avatar" height="100" width="100">
@@ -80,20 +81,27 @@
                     {% endif %}
                     <td>
                         <a href="{{ url_for('user.profile', username=post.user.username) }}"><span style="color: green;"><strong><em>{{ post.user.username }}</em></strong></span></a>
-                        {% if post.user|is_online %}
-                        <span class="label label-success">Online</span>
-                        {% else %}
-                        <span class="label label-default">Offline</span>
-                        {% endif %}
-                        <br />
-                        {{ post.user.primary_group.name }}<br />
+
+                            {% if post.user|is_online %}
+                            <span class="label label-success">Online</span>
+                            {% else %}
+                            <span class="label label-default">Offline</span>
+                            {% endif %}
+                            <br />
+                            {{ post.user.primary_group.name }}<br />
                     </td>
 
                     <td class="pull-right">
                         Posts: {{ post.user.post_count }}<br />
                         Registered since: {{ post.user.date_joined|format_date('%b %d %Y') }}<br />
                     </td>
-
+                {% else %}
+                    <td>
+                        <strong>{{ post.username }}</strong>
+                        <br />
+                        Guest
+                    </td>
+                {% endif %}
                 </tr>
             </table>
         </td>
@@ -105,7 +113,7 @@
                 {% autoescape false %}
                     {{ post.content|markup }}
                     <!-- Signaure Begin -->
-                    {% if post.user.signature %}
+                    {% if post.user_id and post.user.signature %}
                     <hr>
                     {{ post.user.signature|markup }}
                     {% endif %}
@@ -117,7 +125,7 @@
         <tr>
             <td>
                 <span class="pull-left">
-                    {% if current_user.is_authenticated %}
+                    {% if current_user.is_authenticated and post.user_id %}
                     <a href="{{ url_for('user.new_message') }}?to_user={{ post.user.username }}">PM</a>
                     {% endif %}
                     {% if post.user.website %}

+ 9 - 1
flaskbb/templates/forum/topictracker.html

@@ -38,7 +38,7 @@
         <tr>
             <td width="4%">
 
-            {% if topicread|is_unread(topic.last_post) %}
+            {% if topic|topic_is_unread(topicread, current_user) %}
                 <span class="fa fa-comment" style="font-size: 2em"></span>
             {% else %}
                 <span class="fa fa-comment-o" style="font-size: 2em"></span>
@@ -48,7 +48,11 @@
             <td>
                 <div>
                     <a href="{{ url_for('forum.view_topic', topic_id=topic.id) }}">{{ topic.title }}</a> <br />
+                    {% if topic.user_id %}
                     <small>by <a href="{{ url_for('user.profile', username=topic.user.username) }}">{{ topic.user.username }}</a></small>
+                    {% else %}
+                    <small>by {{ topic.username }}</small>
+                    {% endif %}
                 </div>
             </td>
             <td>
@@ -59,7 +63,11 @@
             </td>
             <td>
                 <a href="{{ url_for('forum.view_post', post_id=topic.last_post.id) }}">{{ topic.last_post.date_created|time_since }}</a><br />
+                {% if topic.last_post.user_id %}
                 <small>by <a href="{{ url_for('user.profile', username=topic.last_post.user.username) }}">{{ topic.last_post.user.username }}</a></small>
+                {% else %}
+                {{ topic.last_post.username }}
+                {% endif %}
             </td>
         </tr>
         {% else %}

+ 5 - 6
flaskbb/user/models.py

@@ -84,10 +84,8 @@ class User(db.Model, UserMixin):
     avatar = db.Column(db.String)
     notes = db.Column(db.Text(5000))
 
-    posts = db.relationship("Post", backref="user", lazy="dynamic",
-                            cascade="all, delete-orphan")
-    topics = db.relationship("Topic", backref="user", lazy="dynamic",
-                             cascade="all, delete-orphan")
+    posts = db.relationship("Post", backref="user", lazy="dynamic")
+    topics = db.relationship("Topic", backref="user", lazy="dynamic")
 
     post_count = db.Column(db.Integer, default=0)
 
@@ -296,10 +294,11 @@ class User(db.Model, UserMixin):
         db.session.commit()
         return self
 
-    def delete(self):
-        # TODO: Recount posts in the involved forums and topics
+    def delete(self, forums=None):
+        """Deletes the User."""
         db.session.delete(self)
         db.session.commit()
+
         return self
 
 

+ 6 - 5
flaskbb/utils/helpers.py

@@ -77,12 +77,13 @@ def topic_is_unread(topic, topicsread, user, forumsread=None):
 
     :param topicsread: The topicsread object for the topic
 
-    :param user: The user who should be checked if he has read the topic
+    :param user: The user who should be checked if he has read the last post
+                 in the topic
 
-    :param forumsread: The forumsread object in which the topic is. You do
-                       not have to pass this - only if you want to include the
-                       cleared attribute from the ForumsRead object.
-                       This will also check if the forum is marked as clear.
+    :param forumsread: The forumsread object in which the topic is. If you
+                       also want to check if the user has marked the forum as
+                       read, than you will also need to pass an forumsread
+                       object.
     """
     if not user.is_authenticated():
         return False