Users can now archive posts
Added option to posts to archive a post.
This commit is contained in:
		
							parent
							
								
									3472187155
								
							
						
					
					
						commit
						d3bfb13dbf
					
				|  | @ -7,9 +7,10 @@ from langdetect import detect, LangDetectException | |||
| from app import db | ||||
| from app.main.forms import EditProfileForm, EmptyForm, PostForm, SearchForm, \ | ||||
|     MessageForm | ||||
| from app.models import User, Post, Message, Notification | ||||
| from app.models import User, Post, Message, Notification, Archive | ||||
| from app.translate import translate | ||||
| from app.main import bp | ||||
| from sqlalchemy import select | ||||
| 
 | ||||
| 
 | ||||
| @bp.before_app_request | ||||
|  | @ -19,6 +20,7 @@ def before_request(): | |||
|         db.session.commit() | ||||
|         g.search_form = SearchForm() | ||||
|     g.locale = str(get_locale()) | ||||
|     db.create_all() | ||||
| 
 | ||||
| 
 | ||||
| @bp.route('/', methods=['GET', 'POST']) | ||||
|  | @ -82,7 +84,6 @@ def user(username): | |||
|     return render_template('user.html', user=user, posts=posts.items, | ||||
|                            next_url=next_url, prev_url=prev_url, form=form) | ||||
| 
 | ||||
| 
 | ||||
| @bp.route('/user/<username>/popup') | ||||
| @login_required | ||||
| def user_popup(username): | ||||
|  | @ -107,6 +108,15 @@ def edit_profile(): | |||
|     return render_template('edit_profile.html', title=_('Edit Profile'), | ||||
|                            form=form) | ||||
| 
 | ||||
| #  | ||||
| @bp.route('/archive/<post_b>/<post_user>/<post_time>') | ||||
| @login_required | ||||
| def archive(post_b, post_user, post_time): | ||||
|     current_user.archive(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')) | ||||
| 
 | ||||
| 
 | ||||
| @bp.route('/follow/<username>', methods=['POST']) | ||||
| @login_required | ||||
|  |  | |||
|  | @ -89,12 +89,15 @@ followers = db.Table( | |||
| ) | ||||
| 
 | ||||
| 
 | ||||
| 
 | ||||
| class User(UserMixin, PaginatedAPIMixin, db.Model): | ||||
|     id = db.Column(db.Integer, primary_key=True) | ||||
|     username = db.Column(db.String(64), index=True, unique=True) | ||||
|     email = db.Column(db.String(120), index=True, unique=True) | ||||
|     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') | ||||
|     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) | ||||
|  | @ -128,7 +131,13 @@ class User(UserMixin, PaginatedAPIMixin, db.Model): | |||
|         digest = md5(self.email.lower().encode('utf-8')).hexdigest() | ||||
|         return 'https://www.gravatar.com/avatar/{}?d=identicon&s={}'.format( | ||||
|             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) | ||||
|         db.session.add(a) | ||||
|         print("Archived!") | ||||
|      | ||||
|     def follow(self, user): | ||||
|         if not self.is_following(user): | ||||
|             self.followed.append(user) | ||||
|  | @ -251,6 +260,18 @@ class Post(SearchableMixin, db.Model): | |||
|     def __repr__(self): | ||||
|         return '<Post {}>'.format(self.body) | ||||
| 
 | ||||
| # Archive class for archived posts     | ||||
| class Archive(db.Model): | ||||
|     id = db.Column(db.Integer, primary_key=True) | ||||
|     body = db.Column(db.String(140)) | ||||
|     timestamp = db.Column(db.DateTime, index=True, default=datetime.utcnow) | ||||
|     author = db.Column(db.String(50)) | ||||
|     archived_by = db.Column(db.Integer, db.ForeignKey('user.id')) | ||||
|     language = db.Column(db.String(5)) | ||||
| 
 | ||||
|     def __repr__(self): | ||||
|         return '<Archive {}>'.format(self.body) | ||||
| 
 | ||||
| 
 | ||||
| class Message(db.Model): | ||||
|     id = db.Column(db.Integer, primary_key=True) | ||||
|  |  | |||
|  | @ -1,4 +1,5 @@ | |||
|     <table class="table table-hover"> | ||||
| 
 | ||||
| <table class="table table-hover"> | ||||
|         <tr> | ||||
|             <td width="70px"> | ||||
|                 <a href="{{ url_for('main.user', username=post.author.username) }}"> | ||||
|  | @ -17,6 +18,7 @@ | |||
|                     username=user_link, when=moment(post.timestamp).fromNow()) }} | ||||
|                 <br> | ||||
|                 <span id="post{{ post.id }}">{{ post.body }}</span> | ||||
|                  | ||||
|                 {% if post.language and post.language != g.locale %} | ||||
|                 <br><br> | ||||
|                 <span id="translation{{ post.id }}"> | ||||
|  | @ -27,6 +29,12 @@ | |||
|                                 '{{ g.locale }}');">{{ _('Translate') }}</a> | ||||
|                 </span> | ||||
|                 {% endif %} | ||||
|                 {% if post.author.username != current_user.username %} | ||||
|                     <a href="{{ url_for('main.archive', post_b=post.body, post_user=post.author.username, post_time=post.timestamp) }}">Archive</a> | ||||
|                 {% endif %} | ||||
|             </td> | ||||
|         </tr> | ||||
|     </table> | ||||
| 
 | ||||
|      | ||||
| 
 | ||||
|  |  | |||
|  | @ -25,7 +25,7 @@ | |||
|                 </p> | ||||
|                 {% else %} | ||||
|                 <p> | ||||
|                     <form action="{{ url_for('main.unfollow', username=user.username) }}" method="post"> | ||||
|                     <form action="{{ url_for('main.unfollow', username=user.username) }}"> | ||||
|                         {{ form.hidden_tag() }} | ||||
|                         {{ form.submit(value=_('Unfollow'), class_='btn btn-default') }} | ||||
|                     </form> | ||||
|  |  | |||
|  | @ -18,7 +18,7 @@ Flask-Migrate==3.0.1 | |||
| Flask-Moment==1.0.1 | ||||
| Flask-SQLAlchemy==2.5.1 | ||||
| Flask-WTF==0.15.1 | ||||
| greenlet==1.1.0 | ||||
| greenlet==2.0.1 | ||||
| httpie==2.4.0 | ||||
| idna==2.10 | ||||
| itsdangerous==2.0.1 | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue