From 09ab33aeed0152c5ba9d6adf082a21b1782a2671 Mon Sep 17 00:00:00 2001 From: Jarrett Jackson Date: Sun, 12 Mar 2023 20:42:51 -0400 Subject: [PATCH] Added view for archived posts Added option in the user profile to view their archived posts. Added _archive.html for displaying archived posts. --- app/main/routes.py | 35 ++++++++++++++++++++++++++++--- app/models.py | 18 +++++++++++----- app/templates/_archive.html | 27 ++++++++++++++++++++++++ app/templates/_post.html | 2 +- app/templates/archived_posts.html | 14 +++++++++++++ app/templates/user.html | 4 ++-- 6 files changed, 89 insertions(+), 11 deletions(-) create mode 100644 app/templates/_archive.html create mode 100644 app/templates/archived_posts.html diff --git a/app/main/routes.py b/app/main/routes.py index 8eaa09f..82b10fb 100644 --- a/app/main/routes.py +++ b/app/main/routes.py @@ -108,10 +108,31 @@ def edit_profile(): return render_template('edit_profile.html', title=_('Edit Profile'), form=form) -@bp.route('/archive////') +@bp.route('/archived/') @login_required -def archive(post_id, post_b, post_user, post_time): - current_user.archive(post_id, post_b, post_user, post_time) +def view_archive(username): + no_posts = False + user = User.query.filter_by(username=username).first_or_404() + page = request.args.get('page', 1, type=int) + posts = user.archived.order_by(Archive.timestamp.desc()).paginate( + page=page, per_page=current_app.config['POSTS_PER_PAGE'], + error_out=False) + if not current_user.has_archived_posts(): + no_posts = True + flash(_('You have no archived posts!')) + + next_url = url_for('main.view_archive', username=user.username, + page=posts.next_num) if posts.has_next else None + prev_url = url_for('main.view_archive', username=user.username, + page=posts.prev_num) if posts.has_prev else None + form = EmptyForm() + return render_template('archived_posts.html', user=user, archived=posts.items, none_archived=no_posts, + next_url=next_url, prev_url=prev_url, form=form) + +@bp.route('/archive/////') +@login_required +def archive(post_id, post_b, user_id, post_user, post_time): + current_user.archive(post_id, post_b, user_id, post_user, post_time) db.session.commit() flash(_('You have archived %(username)s post!', username=post_user)) return redirect(url_for('main.explore')) @@ -124,6 +145,14 @@ def archive_remove(post_user, post_id): flash(_('You have removed %(username)s post from your archive!', username=post_user)) return redirect(url_for('main.explore')) +@bp.route('/archived//') +@login_required +def archive_remove_user(post_user, post_id): + current_user.archive_remove(post_id) + db.session.commit() + flash(_('You have removed %(username)s post from your archive!', username=post_user)) + return redirect(url_for('main.view_archive', username=current_user.username)) + @bp.route('/follow/', methods=['POST']) @login_required diff --git a/app/models.py b/app/models.py index cd864dc..b7c8c8d 100644 --- a/app/models.py +++ b/app/models.py @@ -12,6 +12,7 @@ import redis import rq from app import db, login from app.search import add_to_index, remove_from_index, query_index +from datetime import datetime class SearchableMixin(object): @@ -97,7 +98,8 @@ class User(UserMixin, PaginatedAPIMixin, db.Model): password_hash = db.Column(db.String(128)) posts = db.relationship('Post', backref='author', lazy='dynamic') # link archive class to user - archived = db.relationship('Archive', backref='archivee', lazy='dynamic') + archived = db.relationship('Archive', foreign_keys='Archive.archived_by', backref='archivee', lazy='dynamic') + archived_other_user = db.relationship('Archive', foreign_keys='Archive.archived_owner', backref='own', lazy='dynamic') about_me = db.Column(db.String(140)) last_seen = db.Column(db.DateTime, default=datetime.utcnow) token = db.Column(db.String(32), index=True, unique=True) @@ -133,8 +135,9 @@ class User(UserMixin, PaginatedAPIMixin, db.Model): digest, size) # Create new achive entry - def archive(self, post_id, post_body, post_user, post_time): - a = Archive(id=post_id, body=post_body, author=post_user, archived_by=self.id) + def archive(self, post_id, post_body, user_id, post_user, post_time): + time_obj = datetime.strptime(post_time, '%Y-%m-%d %H:%M:%S.%f') + a = Archive(id=post_id, body=post_body, author=post_user, archived_by=self.id, archived_owner=user_id, timestamp=time_obj) db.session.add(a) print("Archived!") @@ -143,7 +146,10 @@ class User(UserMixin, PaginatedAPIMixin, db.Model): print("Removed Archive!") def has_archived_post(self, post_id): - return Archive.query.filter_by(id=post_id).count() > 0 + return Archive.query.filter_by(id=post_id, archived_by=self.id).count() > 0 + + def has_archived_posts(self): + return Archive.query.filter_by(archived_by=self.id).count() > 0 def follow(self, user): if not self.is_following(user): @@ -269,10 +275,12 @@ class Post(SearchableMixin, db.Model): # Archive class for archived posts class Archive(db.Model): - id = db.Column(db.Integer, primary_key=True) + archive_id = db.Column(db.Integer, primary_key=True) + id = db.Column(db.Integer) body = db.Column(db.String(140)) timestamp = db.Column(db.DateTime, index=True, default=datetime.utcnow) author = db.Column(db.String(50)) + archived_owner = db.Column(db.Integer, db.ForeignKey('user.id')) archived_by = db.Column(db.Integer, db.ForeignKey('user.id')) language = db.Column(db.String(5)) diff --git a/app/templates/_archive.html b/app/templates/_archive.html new file mode 100644 index 0000000..aced240 --- /dev/null +++ b/app/templates/_archive.html @@ -0,0 +1,27 @@ + + + + + +
+ + + + + {% set user_link %} + + + {{ archive.author }} + + + {% endset %} + {{ _('%(username)s said %(when)s', + username=user_link, when=moment(archive.timestamp).fromNow()) }} +
+ {{ archive.body }} +
+ + {% if archive.author != current_user.username %} + Remove from Archive + {% endif %} +
\ No newline at end of file diff --git a/app/templates/_post.html b/app/templates/_post.html index 564e559..cded509 100644 --- a/app/templates/_post.html +++ b/app/templates/_post.html @@ -31,7 +31,7 @@ {% endif %} {% if post.author.username != current_user.username %} {% if not current_user.has_archived_post(post.id) %} - Archive + Archive {% else %} Remove from Archive {% endif %} diff --git a/app/templates/archived_posts.html b/app/templates/archived_posts.html new file mode 100644 index 0000000..df30112 --- /dev/null +++ b/app/templates/archived_posts.html @@ -0,0 +1,14 @@ +{% extends "base.html" %} + +{% block app_content %} + {% if none_archived == false %} +

{{ _('Your Archived Posts') }}

+ {% endif %} +
+
+ {% for archive in archived %} + {% include '_archive.html' %} + {% endfor %} +
+
+{% endblock %} diff --git a/app/templates/user.html b/app/templates/user.html index a0a088c..9f889c1 100644 --- a/app/templates/user.html +++ b/app/templates/user.html @@ -13,8 +13,9 @@

{{ _('%(count)d followers', count=user.followers.count()) }}, {{ _('%(count)d following', count=user.followed.count()) }}

{% if user == current_user %}

{{ _('Edit your profile') }}

+

{{ _('View Archived Posts') }}

{% if not current_user.get_task_in_progress('export_posts') %} -

{{ _('Export your posts') }}

+

{{ _('Export your posts') }}

{% endif %} {% elif not current_user.is_following(user) %}

@@ -34,7 +35,6 @@ {% if user != current_user %}

{{ _('Send private message') }}

{% endif %} -