diff --git a/app/main/routes.py b/app/main/routes.py index 9c990ef..8eaa09f 100644 --- a/app/main/routes.py +++ b/app/main/routes.py @@ -108,14 +108,21 @@ def edit_profile(): return render_template('edit_profile.html', title=_('Edit Profile'), form=form) -# -@bp.route('/archive///') +@bp.route('/archive////') @login_required -def archive(post_b, post_user, post_time): - current_user.archive(post_b, post_user, post_time) +def archive(post_id, post_b, post_user, post_time): + current_user.archive(post_id, post_b, post_user, post_time) db.session.commit() flash(_('You have archived %(username)s post!', username=post_user)) - return redirect(url_for('main.index')) + return redirect(url_for('main.explore')) + +@bp.route('/archive//') +@login_required +def archive_remove(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.explore')) @bp.route('/follow/', methods=['POST']) diff --git a/app/models.py b/app/models.py index 33b545f..cd864dc 100644 --- a/app/models.py +++ b/app/models.py @@ -97,7 +97,7 @@ 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 - archive = db.relationship('Archive', backref='archivee', lazy='dynamic') + archived = db.relationship('Archive', backref='archivee', 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,10 +133,17 @@ class User(UserMixin, PaginatedAPIMixin, db.Model): digest, size) # Create new achive entry - def archive(self, post_body, post_user, post_time): - a = Archive(body=post_body, author=post_user, archived_by=self.id) + 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) db.session.add(a) print("Archived!") + + def archive_remove(self, post_id): + self.archived.filter_by(id=post_id).delete() + print("Removed Archive!") + + def has_archived_post(self, post_id): + return Archive.query.filter_by(id=post_id).count() > 0 def follow(self, user): if not self.is_following(user): diff --git a/app/templates/_post.html b/app/templates/_post.html index 616fae6..564e559 100644 --- a/app/templates/_post.html +++ b/app/templates/_post.html @@ -30,7 +30,11 @@ {% endif %} {% if post.author.username != current_user.username %} - Archive + {% if not current_user.has_archived_post(post.id) %} + Archive + {% else %} + Remove from Archive + {% endif %} {% endif %} diff --git a/app/templates/user.html b/app/templates/user.html index 397eb1e..a0a088c 100644 --- a/app/templates/user.html +++ b/app/templates/user.html @@ -34,6 +34,7 @@ {% if user != current_user %}

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

{% endif %} +