Update app.py
Browse files
app.py
CHANGED
|
@@ -545,6 +545,45 @@ def increment_view(post_id):
|
|
| 545 |
return '', 204 # Empty response with code 204 (No Content)
|
| 546 |
return 'Post not found', 404
|
| 547 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 548 |
# Feed
|
| 549 |
@app.route('/', methods=['GET', 'POST'])
|
| 550 |
def feed():
|
|
@@ -553,7 +592,7 @@ def feed():
|
|
| 553 |
is_authenticated = 'username' in session
|
| 554 |
username = session.get('username', None)
|
| 555 |
|
| 556 |
-
search_query = request.form.get('search', '').strip().lower() if request.method == 'POST' else request.args.get('search', '').strip().lower()
|
| 557 |
if search_query:
|
| 558 |
posts = [post for post in posts if search_query in post['title'].lower() or search_query in post['description'].lower()]
|
| 559 |
|
|
@@ -605,10 +644,10 @@ def feed():
|
|
| 605 |
.search-btn:hover {
|
| 606 |
background: #e65b5b;
|
| 607 |
}
|
| 608 |
-
.post-
|
| 609 |
-
display:
|
| 610 |
-
|
| 611 |
-
gap:
|
| 612 |
}
|
| 613 |
.post-item {
|
| 614 |
background: var(--card-bg-light);
|
|
@@ -616,9 +655,9 @@ def feed():
|
|
| 616 |
overflow: hidden;
|
| 617 |
box-shadow: var(--shadow);
|
| 618 |
transition: var(--transition);
|
| 619 |
-
text-decoration: none;
|
| 620 |
color: var(--text-light);
|
| 621 |
backdrop-filter: blur(10px);
|
|
|
|
| 622 |
}
|
| 623 |
body.dark .post-item {
|
| 624 |
background: var(--card-bg-dark);
|
|
@@ -630,42 +669,36 @@ def feed():
|
|
| 630 |
}
|
| 631 |
.post-preview {
|
| 632 |
width: 100%;
|
| 633 |
-
height:
|
| 634 |
-
object-fit:
|
| 635 |
-
border-
|
|
|
|
| 636 |
transition: var(--transition);
|
| 637 |
}
|
| 638 |
-
body.dark .post-preview {
|
| 639 |
-
border-bottom: 1px solid rgba(255, 255, 255, 0.1);
|
| 640 |
-
}
|
| 641 |
.post-item:hover .post-preview {
|
| 642 |
filter: brightness(1.1);
|
| 643 |
}
|
| 644 |
.post-content {
|
| 645 |
-
padding: 20px;
|
| 646 |
}
|
| 647 |
.post-item h2 {
|
| 648 |
-
font-size: 1.
|
| 649 |
font-weight: 600;
|
| 650 |
-
margin-bottom:
|
| 651 |
background: var(--gradient);
|
| 652 |
-webkit-background-clip: text;
|
| 653 |
color: transparent;
|
| 654 |
}
|
| 655 |
.post-item p {
|
| 656 |
-
font-size: 1em;
|
| 657 |
-
margin-bottom:
|
| 658 |
-
display: -webkit-box;
|
| 659 |
-
-webkit-line-clamp: 2;
|
| 660 |
-
-webkit-box-orient: vertical;
|
| 661 |
-
overflow: hidden;
|
| 662 |
-
text-overflow: ellipsis;
|
| 663 |
}
|
| 664 |
.post-meta {
|
| 665 |
display: flex;
|
| 666 |
justify-content: space-between;
|
| 667 |
-
font-size: 0.
|
| 668 |
color: rgba(0, 0, 0, 0.7);
|
|
|
|
| 669 |
}
|
| 670 |
body.dark .post-meta {
|
| 671 |
color: rgba(255, 255, 255, 0.7);
|
|
@@ -680,13 +713,46 @@ def feed():
|
|
| 680 |
}
|
| 681 |
.stats {
|
| 682 |
display: flex;
|
| 683 |
-
gap:
|
| 684 |
}
|
| 685 |
.stats span {
|
| 686 |
display: flex;
|
| 687 |
align-items: center;
|
| 688 |
gap: 5px;
|
| 689 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 690 |
</style>
|
| 691 |
</head>
|
| 692 |
<body>
|
|
@@ -701,29 +767,44 @@ def feed():
|
|
| 701 |
<button type="submit" class="search-btn">Search</button>
|
| 702 |
</form>
|
| 703 |
</div>
|
| 704 |
-
<div class="post-
|
| 705 |
{% for post in posts %}
|
| 706 |
-
<
|
| 707 |
-
{
|
| 708 |
-
|
| 709 |
-
<
|
| 710 |
-
|
| 711 |
-
|
| 712 |
-
|
| 713 |
-
|
| 714 |
-
|
| 715 |
-
<
|
| 716 |
-
|
| 717 |
-
|
| 718 |
-
<
|
| 719 |
-
|
| 720 |
-
<
|
| 721 |
-
|
| 722 |
-
|
|
|
|
|
|
|
| 723 |
</div>
|
| 724 |
</div>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 725 |
</div>
|
| 726 |
-
|
|
|
|
| 727 |
{% endfor %}
|
| 728 |
</div>
|
| 729 |
</div>
|
|
@@ -1834,32 +1915,36 @@ def chat():
|
|
| 1834 |
<img id="modalImage" src="">
|
| 1835 |
</div>
|
| 1836 |
<script>
|
| 1837 |
-
|
| 1838 |
-
|
| 1839 |
-
|
| 1840 |
-
|
| 1841 |
-
|
| 1842 |
-
|
| 1843 |
-
}
|
| 1844 |
-
function openModal(src) {
|
| 1845 |
-
const modal = document.getElementById('imageModal');
|
| 1846 |
-
const modalImg = document.getElementById('modalImage');
|
| 1847 |
-
modal.style.display = 'flex';
|
| 1848 |
-
modalImg.src = src;
|
| 1849 |
-
}
|
| 1850 |
-
function closeModal(event) {
|
| 1851 |
-
if (event.target.tagName !== 'IMG') {
|
| 1852 |
-
document.getElementById('imageModal').style.display = 'none';
|
| 1853 |
}
|
| 1854 |
-
|
| 1855 |
-
|
| 1856 |
-
|
| 1857 |
-
|
| 1858 |
-
|
| 1859 |
-
|
| 1860 |
-
|
| 1861 |
-
|
| 1862 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1863 |
'''
|
| 1864 |
user_posts = [p for p in data['posts'] if p['uploader'] == username] if is_authenticated else []
|
| 1865 |
return render_template_string(html,
|
|
|
|
| 545 |
return '', 204 # Empty response with code 204 (No Content)
|
| 546 |
return 'Post not found', 404
|
| 547 |
|
| 548 |
+
# Like post
|
| 549 |
+
@app.route('/like_post/<post_id>', methods=['POST'])
|
| 550 |
+
def like_post(post_id):
|
| 551 |
+
if 'username' not in session:
|
| 552 |
+
return 'Login required', 401
|
| 553 |
+
|
| 554 |
+
data = load_data()
|
| 555 |
+
post = next((p for p in data['posts'] if p['id'] == post_id), None)
|
| 556 |
+
if post:
|
| 557 |
+
username = session['username']
|
| 558 |
+
if username not in post.get('likes', []):
|
| 559 |
+
post['likes'] = post.get('likes', []) + [username]
|
| 560 |
+
else:
|
| 561 |
+
post['likes'] = [user for user in post.get('likes', []) if user != username]
|
| 562 |
+
save_data(data)
|
| 563 |
+
return '', 204
|
| 564 |
+
return 'Post not found', 404
|
| 565 |
+
|
| 566 |
+
# Comment on post from feed
|
| 567 |
+
@app.route('/comment_post/<post_id>', methods=['POST'])
|
| 568 |
+
def comment_post(post_id):
|
| 569 |
+
if 'username' not in session:
|
| 570 |
+
return 'Login required', 401
|
| 571 |
+
|
| 572 |
+
data = load_data()
|
| 573 |
+
post = next((p for p in data['posts'] if p['id'] == post_id), None)
|
| 574 |
+
if post:
|
| 575 |
+
comment_text = request.form.get('comment')
|
| 576 |
+
if comment_text:
|
| 577 |
+
username = session['username']
|
| 578 |
+
post['comments'] = post.get('comments', []) + [{
|
| 579 |
+
'user': username,
|
| 580 |
+
'text': comment_text,
|
| 581 |
+
'date': datetime.now().strftime('%Y-%m-%d %H:%M:%S')
|
| 582 |
+
}]
|
| 583 |
+
save_data(data)
|
| 584 |
+
return redirect(url_for('feed'))
|
| 585 |
+
return 'Post not found', 404
|
| 586 |
+
|
| 587 |
# Feed
|
| 588 |
@app.route('/', methods=['GET', 'POST'])
|
| 589 |
def feed():
|
|
|
|
| 592 |
is_authenticated = 'username' in session
|
| 593 |
username = session.get('username', None)
|
| 594 |
|
| 595 |
+
search_query = request.form.get('search', '').strip().lower() if request.method == 'POST' and 'search' in request.form else request.args.get('search', '').strip().lower()
|
| 596 |
if search_query:
|
| 597 |
posts = [post for post in posts if search_query in post['title'].lower() or search_query in post['description'].lower()]
|
| 598 |
|
|
|
|
| 644 |
.search-btn:hover {
|
| 645 |
background: #e65b5b;
|
| 646 |
}
|
| 647 |
+
.post-list {
|
| 648 |
+
display: flex;
|
| 649 |
+
flex-direction: column;
|
| 650 |
+
gap: 40px;
|
| 651 |
}
|
| 652 |
.post-item {
|
| 653 |
background: var(--card-bg-light);
|
|
|
|
| 655 |
overflow: hidden;
|
| 656 |
box-shadow: var(--shadow);
|
| 657 |
transition: var(--transition);
|
|
|
|
| 658 |
color: var(--text-light);
|
| 659 |
backdrop-filter: blur(10px);
|
| 660 |
+
padding: 20px;
|
| 661 |
}
|
| 662 |
body.dark .post-item {
|
| 663 |
background: var(--card-bg-dark);
|
|
|
|
| 669 |
}
|
| 670 |
.post-preview {
|
| 671 |
width: 100%;
|
| 672 |
+
max-height: 400px;
|
| 673 |
+
object-fit: contain;
|
| 674 |
+
border-radius: 15px;
|
| 675 |
+
margin-bottom: 20px;
|
| 676 |
transition: var(--transition);
|
| 677 |
}
|
|
|
|
|
|
|
|
|
|
| 678 |
.post-item:hover .post-preview {
|
| 679 |
filter: brightness(1.1);
|
| 680 |
}
|
| 681 |
.post-content {
|
| 682 |
+
padding: 0 20px 20px;
|
| 683 |
}
|
| 684 |
.post-item h2 {
|
| 685 |
+
font-size: 1.8em;
|
| 686 |
font-weight: 600;
|
| 687 |
+
margin-bottom: 15px;
|
| 688 |
background: var(--gradient);
|
| 689 |
-webkit-background-clip: text;
|
| 690 |
color: transparent;
|
| 691 |
}
|
| 692 |
.post-item p {
|
| 693 |
+
font-size: 1.1em;
|
| 694 |
+
margin-bottom: 20px;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 695 |
}
|
| 696 |
.post-meta {
|
| 697 |
display: flex;
|
| 698 |
justify-content: space-between;
|
| 699 |
+
font-size: 0.95em;
|
| 700 |
color: rgba(0, 0, 0, 0.7);
|
| 701 |
+
margin-bottom: 15px;
|
| 702 |
}
|
| 703 |
body.dark .post-meta {
|
| 704 |
color: rgba(255, 255, 255, 0.7);
|
|
|
|
| 713 |
}
|
| 714 |
.stats {
|
| 715 |
display: flex;
|
| 716 |
+
gap: 20px;
|
| 717 |
}
|
| 718 |
.stats span {
|
| 719 |
display: flex;
|
| 720 |
align-items: center;
|
| 721 |
gap: 5px;
|
| 722 |
}
|
| 723 |
+
.interaction-bar {
|
| 724 |
+
display: flex;
|
| 725 |
+
gap: 15px;
|
| 726 |
+
align-items: center;
|
| 727 |
+
padding-top: 15px;
|
| 728 |
+
border-top: 1px solid rgba(0, 0, 0, 0.1);
|
| 729 |
+
}
|
| 730 |
+
body.dark .interaction-bar {
|
| 731 |
+
border-top: 1px solid rgba(255, 255, 255, 0.1);
|
| 732 |
+
}
|
| 733 |
+
.like-btn {
|
| 734 |
+
padding: 8px 16px;
|
| 735 |
+
font-size: 0.95em;
|
| 736 |
+
background: var(--secondary);
|
| 737 |
+
}
|
| 738 |
+
.like-btn.liked {
|
| 739 |
+
background: #45b7b0;
|
| 740 |
+
}
|
| 741 |
+
.comment-form {
|
| 742 |
+
flex: 1;
|
| 743 |
+
display: flex;
|
| 744 |
+
gap: 10px;
|
| 745 |
+
}
|
| 746 |
+
.comment-form input {
|
| 747 |
+
flex: 1;
|
| 748 |
+
margin: 0;
|
| 749 |
+
padding: 8px;
|
| 750 |
+
font-size: 0.95em;
|
| 751 |
+
}
|
| 752 |
+
.comment-btn {
|
| 753 |
+
padding: 8px 16px;
|
| 754 |
+
font-size: 0.95em;
|
| 755 |
+
}
|
| 756 |
</style>
|
| 757 |
</head>
|
| 758 |
<body>
|
|
|
|
| 767 |
<button type="submit" class="search-btn">Search</button>
|
| 768 |
</form>
|
| 769 |
</div>
|
| 770 |
+
<div class="post-list">
|
| 771 |
{% for post in posts %}
|
| 772 |
+
<div class="post-item">
|
| 773 |
+
<a href="{{ url_for('post_page', post_id=post['id']) }}" style="text-decoration: none; color: inherit;">
|
| 774 |
+
{% if post['type'] == 'video' %}
|
| 775 |
+
<video class="post-preview" preload="metadata" muted>
|
| 776 |
+
<source src="https://huggingface.co/datasets/{{ repo_id }}/resolve/main/{{ post['type'] }}s/{{ post['filename'] }}" type="video/mp4">
|
| 777 |
+
</video>
|
| 778 |
+
{% else %}
|
| 779 |
+
<img class="post-preview" src="https://huggingface.co/datasets/{{ repo_id }}/resolve/main/{{ post['type'] }}s/{{ post['filename'] }}" alt="{{ post['title'] }}" onclick="openModal(this.src, event, '{{ post['id'] }}')">
|
| 780 |
+
{% endif %}
|
| 781 |
+
<div class="post-content">
|
| 782 |
+
<h2>{{ post['title'] }}</h2>
|
| 783 |
+
<p>{{ post['description'] }}</p>
|
| 784 |
+
<div class="post-meta">
|
| 785 |
+
<a href="{{ url_for('user_profile', username=post['uploader']) }}" class="username-link">{{ post['uploader'] }}</a>
|
| 786 |
+
<div class="stats">
|
| 787 |
+
<span>👁️ {{ post['views'] }}</span>
|
| 788 |
+
<span>❤️ {{ post['likes']|length }}</span>
|
| 789 |
+
<span>💬 {{ post.get('comments', [])|length }}</span>
|
| 790 |
+
</div>
|
| 791 |
</div>
|
| 792 |
</div>
|
| 793 |
+
</a>
|
| 794 |
+
{% if is_authenticated %}
|
| 795 |
+
<div class="interaction-bar">
|
| 796 |
+
<form method="POST" action="{{ url_for('like_post', post_id=post['id']) }}">
|
| 797 |
+
<button type="submit" class="btn like-btn {% if username in post['likes'] %}liked{% endif %}">
|
| 798 |
+
{% if username in post['likes'] %}Unlike{% else %}Like{% endif %}
|
| 799 |
+
</button>
|
| 800 |
+
</form>
|
| 801 |
+
<form method="POST" action="{{ url_for('comment_post', post_id=post['id']) }}" class="comment-form">
|
| 802 |
+
<input type="text" name="comment" placeholder="Add a comment" required>
|
| 803 |
+
<button type="submit" class="btn comment-btn">Comment</button>
|
| 804 |
+
</form>
|
| 805 |
</div>
|
| 806 |
+
{% endif %}
|
| 807 |
+
</div>
|
| 808 |
{% endfor %}
|
| 809 |
</div>
|
| 810 |
</div>
|
|
|
|
| 1915 |
<img id="modalImage" src="">
|
| 1916 |
</div>
|
| 1917 |
<script>
|
| 1918 |
+
<div class="modal" id="imageModal" onclick="closeModal(event)">
|
| 1919 |
+
<img id="modalImage" src="">
|
| 1920 |
+
</div>
|
| 1921 |
+
<script>
|
| 1922 |
+
function toggleSidebar() {
|
| 1923 |
+
document.getElementById('sidebar').classList.toggle('active');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1924 |
}
|
| 1925 |
+
function toggleTheme() {
|
| 1926 |
+
document.body.classList.toggle('dark');
|
| 1927 |
+
localStorage.setItem('theme', document.body.classList.contains('dark') ? 'dark' : 'light');
|
| 1928 |
+
}
|
| 1929 |
+
function openModal(src) {
|
| 1930 |
+
const modal = document.getElementById('imageModal');
|
| 1931 |
+
const modalImg = document.getElementById('modalImage');
|
| 1932 |
+
modal.style.display = 'flex';
|
| 1933 |
+
modalImg.src = src;
|
| 1934 |
+
}
|
| 1935 |
+
function closeModal(event) {
|
| 1936 |
+
if (event.target.tagName !== 'IMG') {
|
| 1937 |
+
document.getElementById('imageModal').style.display = 'none';
|
| 1938 |
+
}
|
| 1939 |
+
}
|
| 1940 |
+
window.onload = () => {
|
| 1941 |
+
if (localStorage.getItem('theme') === 'dark') document.body.classList.add('dark');
|
| 1942 |
+
const chatBox = document.querySelector('.chat-box');
|
| 1943 |
+
chatBox.scrollTop = chatBox.scrollHeight;
|
| 1944 |
+
};
|
| 1945 |
+
</script>
|
| 1946 |
+
</body>
|
| 1947 |
+
</html>
|
| 1948 |
'''
|
| 1949 |
user_posts = [p for p in data['posts'] if p['uploader'] == username] if is_authenticated else []
|
| 1950 |
return render_template_string(html,
|