Сделаны лайки

This commit is contained in:
mrmur 2022-04-14 16:57:18 +05:00
parent 39c4475fba
commit 7bd4121354
9 changed files with 55 additions and 87 deletions

View File

@ -1 +1 @@
from . import users, diary_post, questions, answer_quest, like, dislike from . import users, diary_post, questions, answer_quest, like

View File

@ -1,16 +0,0 @@
import sqlalchemy
from flask_login import UserMixin
from .db_session import SqlAlchemyBase
class Dislike(SqlAlchemyBase, UserMixin):
__tablename__ = 'dislike'
id = sqlalchemy.Column(sqlalchemy.Integer,
primary_key=True, autoincrement=True)
user = sqlalchemy.Column(sqlalchemy.Integer,
sqlalchemy.ForeignKey("users.id"), nullable=True, default=None)
post = sqlalchemy.Column(sqlalchemy.Integer,
sqlalchemy.ForeignKey("posts.id"), nullable=True, default=None)
date = sqlalchemy.Column(sqlalchemy.DateTime, nullable=True, default=None)

Binary file not shown.

35
main.py
View File

@ -11,7 +11,6 @@ from data import db_session
from data.answer_quest import Answer from data.answer_quest import Answer
from data.diary_post import DiaryPost from data.diary_post import DiaryPost
from data.like import Like from data.like import Like
from data.dislike import Dislike
from data.questions import Quest from data.questions import Quest
from data.users import User from data.users import User
from forms.add_question import AddQuest from forms.add_question import AddQuest
@ -72,21 +71,15 @@ def main_page():
return render_template('base.html', title='moona') return render_template('base.html', title='moona')
@app.route('/new_dislike/<int:user_id>/<int:post_id>/<string:ret_href>')
def new_dislike(user_id, post_id, ret_href):
session = db_session.create_session()
dislike = Dislike()
dislike.user = user_id
dislike.post = post_id
dislike.date = datetime.datetime.now()
session.add(dislike)
session.commit()
return redirect(f"/{ret_href}")
@app.route('/new_like/<int:user_id>/<int:post_id>/<string:ret_href>') @app.route('/new_like/<int:user_id>/<int:post_id>/<string:ret_href>')
def new_like(user_id, post_id, ret_href): def new_like(user_id, post_id, ret_href):
session = db_session.create_session() session = db_session.create_session()
find = session.query(Like).filter(Like.post == post_id, Like.user == user_id).first()
if find:
session.delete(find)
session.commit()
return redirect(f"/{ret_href}")
else:
like = Like() like = Like()
like.user = user_id like.user = user_id
like.post = post_id like.post = post_id
@ -103,7 +96,7 @@ def publications():
emotion_fresh = [] emotion_fresh = []
for i in fresh_posts: for i in fresh_posts:
emotion = {id: i.id, 'pos_emot': [], 'nig_emot': [], 'link': [], emotion = {id: i.id, 'pos_emot': [], 'nig_emot': [], 'link': [],
'author': session.query(User).filter(User.id == i.author).first()} 'author': session.query(User).filter(User.id == i.author).first(), 'like': None, 'is_like': 0}
if i.pos_emot: if i.pos_emot:
emotion['pos_emot'] = i.pos_emot.split() emotion['pos_emot'] = i.pos_emot.split()
else: else:
@ -116,6 +109,11 @@ def publications():
emotion['link'] = i.link.split() emotion['link'] = i.link.split()
else: else:
emotion['link'] = None emotion['link'] = None
like = session.query(Like).filter(Like.post == i.id).all()
if like:
emotion['like'] = len(like)
if session.query(Like).filter(Like.post == i.id, Like.user == current_user.id).first():
emotion['is_like'] = 1
emotion_fresh.append(emotion) emotion_fresh.append(emotion)
return render_template('publications.html', fresh_post=fresh_posts, emotion_fresh=emotion_fresh, title='moona') return render_template('publications.html', fresh_post=fresh_posts, emotion_fresh=emotion_fresh, title='moona')
@ -287,7 +285,7 @@ def diary():
pub_post = pub_post[::-1] pub_post = pub_post[::-1]
emotion_pub = [] emotion_pub = []
for i in pub_post: for i in pub_post:
emotion = {id: i.id, 'pos_emot': [], 'nig_emot': [], 'link': [], 'like': None, 'dislike': None} emotion = {id: i.id, 'pos_emot': [], 'nig_emot': [], 'link': [], 'like': None, 'is_like': 0}
if i.pos_emot: if i.pos_emot:
emotion['pos_emot'] = i.pos_emot.split() emotion['pos_emot'] = i.pos_emot.split()
else: else:
@ -302,10 +300,9 @@ def diary():
emotion['link'] = None emotion['link'] = None
like = db_sess.query(Like).filter(Like.post == i.id).all() like = db_sess.query(Like).filter(Like.post == i.id).all()
if like: if like:
emotion['like'] = like emotion['like'] = len(like)
dislike = db_sess.query(Like).filter(Like.post == i.id).all() if db_sess.query(Like).filter(Like.post == i.id, Like.user == current_user.id).first():
if like: emotion['is_like'] = 1
emotion['dislike'] = dislike
emotion_pub.append(emotion) emotion_pub.append(emotion)
lis_emotion = [] lis_emotion = []
for i in posts: for i in posts:

View File

@ -30,8 +30,7 @@ summary {
-webkit-border-radius: 22px; -webkit-border-radius: 22px;
-moz-border-radius: 22px; -moz-border-radius: 22px;
border-radius: 22px; border-radius: 22px;
border:2px; border:2px solid #ffffff;
solid #ffffff;
background-color:#1daff0; background-color:#1daff0;
top: 98px; top: 98px;
} }
@ -131,20 +130,3 @@ width: 50%;
margin-left: auto; margin-left: auto;
margin-right: auto; margin-right: auto;
} }
.like_block, .dislike_block {
border-radius: 22px;
width: 40px;
height: 40px;
}
.div_like {
background-color: #95e9ff;
width: 50px;
height: 50px;
border-radius: 20px;
}
.div_dislike {
background-color: #df3c43;
width: 50px;
height: 50px;
border-radius: 20px;
}

Binary file not shown.

Before

Width:  |  Height:  |  Size: 101 KiB

After

Width:  |  Height:  |  Size: 584 KiB

BIN
static/img/like_add.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 411 KiB

View File

@ -78,27 +78,19 @@
приватная приватная
</div> </div>
{% endif %} {% endif %}
<form> <div class="like">
<table class="like">
<th class="like_block">
<div class="div_like">
<a type="button" <a type="button"
href="/new_like/{{current_user.id}}/{{item.id}}/diary"> href="/new_like/{{current_user.id}}/{{item.id}}/diary">
<img src="../static/img/like.png" width="50" height="50" {% if emotion_pub[loop.index0]['is_like'] %}
style="margin-left: auto;margin-right: auto;"> <img src="../static/img/like_add.png" width="117" height="100">
</a></div> {% else %}
</th> <img src="../static/img/like.png" width="94" height="80">
<th class="dislike_block"> {% endif %}
<div class="div_dislike">
<a type="button" class="dislike"
href="/new_dislike/{{current_user.id}}/{{item.id}}/diary">
<img src="../static/img/dislike.png" width="50" height="50"
style="margin-left: auto;margin-right: auto;">
</a> </a>
{% if emotion_pub[loop.index0]['like'] != None %}
<p style="color:#ffffff">{{emotion_pub[loop.index0]['like']}}</p>
{% endif %}
</div> </div>
</th>
</table>
</form>
<div class="my_author"><img src="../{{ current_user.photo }}" width="40" height="40" <div class="my_author"><img src="../{{ current_user.photo }}" width="40" height="40"
style="border-radius: 22px"> style="border-radius: 22px">
<strong style="color: #ffffff">{{ current_user.name }}</strong> <strong style="color: #ffffff">{{ current_user.name }}</strong>
@ -198,9 +190,8 @@
приватная приватная
</div> </div>
{% endif %} {% endif %}
<div class="my_author" style="style=position:absolute; width:148px; height:44px; left:255px; -webkit-border-radius: <div class="my_author"><img src="../{{ current_user.photo }}" width="40" height="40"
22px;-moz-border-radius: 22px;border-radius: 22px; border:2px solid #FFFFFF; background-color:#1daff0; top: style="border-radius: 22px">
98px;"><img src="../{{ current_user.photo }}" width="40" height="40" style="border-radius: 22px">
<strong style="color: #ffffff">{{ current_user.name }}</strong> <strong style="color: #ffffff">{{ current_user.name }}</strong>
</div> </div>
<strong style="color:#ffffff">{{item.date}}</strong> <strong style="color:#ffffff">{{item.date}}</strong>

View File

@ -56,6 +56,19 @@
</div> </div>
<p></p> <p></p>
{% endif %} {% endif %}
<div class="like">
<a type="button"
href="/new_like/{{current_user.id}}/{{item.id}}/publications">
{% if emotion_fresh[loop.index0]['is_like'] %}
<img src="../static/img/like_add.png" width="117" height="100">
{% else %}
<img src="../static/img/like.png" width="94" height="80">
{% endif %}
</a>
{% if emotion_fresh[loop.index0]['like'] != None %}
<p style="color:#ffffff">{{emotion_fresh[loop.index0]['like']}}</p>
{% endif %}
</div>
{% if emotion_fresh[loop.index0]['link'] != None %} {% if emotion_fresh[loop.index0]['link'] != None %}
<details> <details>
<summary class="emot_block"> <summary class="emot_block">
@ -73,7 +86,8 @@
{% endif %} {% endif %}
<div class="author" style="style=position:absolute; width:148px; height:44px; left:255px; -webkit-border-radius: <div class="author" style="style=position:absolute; width:148px; height:44px; left:255px; -webkit-border-radius:
22px;-moz-border-radius: 22px;border-radius: 22px; border:2px solid #FFFFFF; background-color:#1daff0; top: 22px;-moz-border-radius: 22px;border-radius: 22px; border:2px solid #FFFFFF; background-color:#1daff0; top:
98px;"><img src="../{{ emotion_fresh[loop.index0]['author'].photo }}" width="40" height="40" style="border-radius: 22px"> 98px;"><img src="../{{ emotion_fresh[loop.index0]['author'].photo }}" width="40" height="40"
style="border-radius: 22px">
<strong style="color: #ffffff">{{ emotion_fresh[loop.index0]['author'].name }}</strong> <strong style="color: #ffffff">{{ emotion_fresh[loop.index0]['author'].name }}</strong>
</div> </div>
<strong style="color:#ffffff">{{item.date}}</strong> <strong style="color:#ffffff">{{item.date}}</strong>