Skip to content

Commit

Permalink
Merge Develop, Resolve
Browse files Browse the repository at this point in the history
  • Loading branch information
samkitshah18 committed Apr 28, 2024
2 parents 25c1a2f + 0aa112e commit 5177edb
Show file tree
Hide file tree
Showing 30 changed files with 1,191 additions and 300 deletions.
45 changes: 44 additions & 1 deletion Community/forms.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from typing import Any
from django import forms
from TutorRegister.models import Post, Reply
from TutorRegister.presets import EXPERTISE_CHOICES
Expand Down Expand Up @@ -51,4 +52,46 @@ class Meta:
widgets = {"content": forms.Textarea(attrs={"class": "form-control"})}


# class SearchFilterForm(forms.Form):
class SearchFilterForm(forms.Form):
search = forms.CharField(
required=False,
max_length=320,
widget=forms.TextInput(
attrs={"placeholder": "Search posts...", "class": "form-control"}
),
)
label = forms.ChoiceField(
required=False,
choices=[("", "None"), ("resource", "Resource"), ("question", "Question")],
label="Label",
widget=forms.Select(attrs={"class": "form-control"}),
)
topic = forms.ChoiceField(
required=False,
choices=[("", "None")] + EXPERTISE_CHOICES,
label="Topic",
widget=forms.Select(attrs={"class": "form-control"}),
)
sort = forms.ChoiceField(
required=False,
choices=[
("", "None"),
("highest_rating", "Highest Rating"),
("most_viewed", "Most Viewed"),
("earliest_post", "Earliest Post"),
],
label="Sort",
widget=forms.Select(attrs={"class": "form-control"}),
)

def clean(self):
cleaned_data = super().clean()

for key in cleaned_data:
value = cleaned_data[key]
if isinstance(value, str):
cleaned_data[key] = value.strip()
if not value:
cleaned_data[key] = None

return cleaned_data
2 changes: 1 addition & 1 deletion Community/templates/create_post.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{% extends 'Dashboard/base_student.html' %}
{% extends baseTemplate %}

{% block title %}Create New Post{% endblock %}

Expand Down
137 changes: 137 additions & 0 deletions Community/templates/edit_post.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
{% extends baseTemplate %}

{% block title %}Edit Post{% endblock %}

{% block content %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Edit Post</title>
<!-- Bootstrap CSS -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet">
<style>
.btn-purple {
color: #800080;
background-color: transparent;
border-color: #800080;
}
.btn-purple.clicked {
color: #fff;
background-color: #800080;
border-color: #800080;
}
.btn-purple:hover {
color: #fff;
background-color: #800080;
border-color: #800080;
}
.card-header {
background: none; /* Remove background */
border-bottom: none; /* Remove border */
}
.btn-purple:not(.clicked):hover {
background-color: #800080;
border-color: #800080;
color: white;
}
</style>
</head>
<body>
<div class="container-fluid">
<div class="row justify-content-center">
<div class="col-lg-8">
<div class="card mt-5 rounded-4 shadow">
<div class="card-header">
<h5 class="card-title" style="margin-top: 20px; margin-left: 20px;"><strong>Edit Post</strong></h5>
</div>
<div class="card-body" style="margin-left: 20px; margin-right: 20px;">
<form method="post" enctype="multipart/form-data">
{% csrf_token %}
{% if form.errors %}
<div class="alert alert-danger" role="alert">
Please correct the errors below:
<ul>
{% for error in form.non_field_errors %}
<li>{{ error }}</li>
{% endfor %}
</ul>
</div>
{% endif %}
<div class="mb-3">
<label class="form-label">Label:</label>
<div class="btn-group" role="group" aria-label="Post Type">
{% for value, label in form.label.field.choices %}
<input type="radio" class="btn-check" name="label" id="label_{{ forloop.counter }}" value="{{ value }}" autocomplete="off" {% if value == form.label.value %} checked {% endif %}>
<label class="btn btn-purple" style="margin-right: 5px;" onclick="selectLabel(this)" for="label_{{ forloop.counter }}">{{ label }}</label>
{% endfor %}
</div>
</div>
<div class="mb-3">
<label for="title" class="form-label">Topic:</label>
{{ form.topics }}
{% for error in form.topics.errors %}
<div class="text-danger">{{ error }}</div>
{% endfor %}
</div>
<div class="mb-3">
<label for="title" class="form-label">Title</label>
{{ form.title }}
{% for error in form.title.errors %}
<div class="text-danger">{{ error }}</div>
{% endfor %}
</div>
<div class="mb-3">
<label for="content" class="form-label">Content</label>
{{ form.content }}
{% for error in form.content.errors %}
<div class="text-danger">{{ error }}</div>
{% endfor %}
</div>
<div class="mb-3">
<label for="attachment" class="form-label">Attach File (optional)</label>
<input class="form-control" type="file" id="attachment" name="attachment">
{% for error in form.attachment.errors %}
<div class="text-danger">{{ error }}</div>
{% endfor %}
</div>
<div class="d-flex justify-content-center">
<button type="submit" class="btn btn-primary btn-purple" style="background-color: #600E90; color: white; border-color: #600E90; margin-bottom: 20px; margin-left: 20px">Save</button>
</div>
</form>
</div>
</div>
</div>
</div>
</div>

<!-- Bootstrap Bundle with Popper -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/js/bootstrap.bundle.min.js"></script>
<script>
document.addEventListener("DOMContentLoaded", function() {
// Get the selected label value
var selectedLabel = "{{ form.label.value }}";

// Find the corresponding button and apply the 'clicked' class
document.querySelector(`input[name="label"][value="${selectedLabel}"]`).nextElementSibling.classList.add('clicked');
document.querySelector(`input[name="label"][value="${selectedLabel}"]`).nextElementSibling.style.backgroundColor = '#600E90';
document.querySelector(`input[name="label"][value="${selectedLabel}"]`).nextElementSibling.style.color = 'white';
});

function selectLabel(button) {
// Remove 'clicked' class from all buttons
document.querySelectorAll('.btn-purple').forEach(btn => {
btn.classList.remove('clicked');
btn.style.backgroundColor = "white";
btn.style.color='#600E90';
});
// Add 'clicked' class to selected button
button.classList.add('clicked');
button.style.backgroundColor='#600E90';
button.style.color = 'white';
}
</script>
</body>
</html>
{% endblock %}
108 changes: 91 additions & 17 deletions Community/templates/post_detail.html
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
{% extends baseTemplate %}

{% load custom_tags %}

{% block title %}Post Detail{% endblock %}

{% block extra_css %}
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/lightbox2/2.11.4/css/lightbox.css" integrity="sha512-Woz+DqWYJ51bpVk5Fv0yES/edIMXjj3Ynda+KWTIkGoynAMHrqTcDUQltbipuiaD5ymEo9520lyoVOo9jCQOCA==" crossorigin="anonymous" referrerpolicy="no-referrer" />
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css">
<link rel='stylesheet' href='https://cdn-uicons.flaticon.com/2.3.0/uicons-solid-straight/css/uicons-solid-straight.css'>
<link rel='stylesheet' href='https://cdn-uicons.flaticon.com/2.3.0/uicons-solid-rounded/css/uicons-solid-rounded.css'>

<script src="https://kit.fontawesome.com/000661c1ee.js" crossorigin="anonymous"></script>
<style>
/* Define your custom styles here */
.post-container {
Expand Down Expand Up @@ -96,6 +99,14 @@
font-size: 0.8em; /* Make the date smaller */
color: #777777; /* Set color to a muted gray */
}
.function-link {
position: absolute;
right: 30px;
bottom: 150px;
}
.label-topic-group a {
text-decoration: none;
}
</style>
{% endblock %}

Expand All @@ -104,7 +115,9 @@
<div class="row align-items-center mb-3">
<div class="col">
<h1 class="mb-0 fs-4">
<a href="{% url 'Community:all_posts' %}">&lt; All posts</a>
<a href="{% url 'Community:all_posts' %}" style="text-decoration: none;">
<i class="fi fi-sr-hand-back-point-left"></i> All posts
</a>
</h1>
</div>
<div class="col text-end">
Expand Down Expand Up @@ -136,26 +149,43 @@ <h1 class="mb-0 fs-4">
</div>
</div>
</div>
<div class="col text-end">
<button class="btn btn-primary btn-sm" style="margin-top:10px;color:black;background-color:#F3E4F7;border-color:#F3E4F7">{{ post.label|capfirst }}</button>
<button class="btn btn-primary btn-sm" style="margin-top:10px;margin-left:10px;color:white;background-color:#AC65BF;border-color:#AC65BF">{{ post.topics|capfirst }}</button>
<div class="col text-end label-topic-group">
<a href="{% url 'Community:all_posts' %}?label={{ post.label }}">
<button class="btn btn-primary btn-sm" style="margin-top:10px;color:black;background-color:#F3E4F7;border-color:#F3E4F7">{{ post.label|capfirst }}</button>
</a>
<a href="{% url 'Community:all_posts' %}?topic={{ post.topics }}">
<button class="btn btn-primary btn-sm" style="margin-top:10px;margin-left:10px;color:white;background-color:#AC65BF;border-color:#AC65BF">{{ topic_dict|get_topic:post.topics }}</button>
</a>
</div>
</div>
<div class="post-title fs-4" style="margin-left: 30px;">{{ post.title }}</div>
<div class="post-title fs-4" style="margin-left: 30px;">{{ post.title }}</div>
{% if post.attachment %}
<a href="{{ post.attachment.url }}" data-lightbox="post-image" data-title="Post Attachment" style="margin-left: 30px;">
<img src="{{ post.attachment.url }}" alt="attachment" style="width: 200px; height: auto;">
</a>
<div>
<a href="{{ post.attachment.url }}" data-lightbox="post-image" data-title="Post Attachment" style="margin-left: 30px;">
<img src="{{ post.attachment.url }}" alt="attachment" style="width: 40em; height: auto;">
</a>
</div>
{% endif %}
<div style="margin-left: 30px;margin-bottom:10px">{{ post.content }}</div>
<div class="vote-button-group" style="margin-left: 30px;margin-bottom:10px">
<button class="btn btn-outline-success btn-sm" onclick="vote({{ post.id }}, 'upvote')"><i class="fa-solid fa-arrow-up"></i></button>
<span id="rating-{{ post.id }}" style="margin: 0px 5px;">{{ post.get_rating }}</span>
<button class="btn btn-outline-danger btn-sm" onclick="vote({{ post.id }}, 'downvote')"><i class="fa-solid fa-arrow-down"></i></button>
<div style="margin-left: 30px; padding-right:50px; margin-bottom:10px">{{ post.content|linebreaks }}</div>
<div class="vote-button-group" style="margin-left: 30px; margin-bottom:10px">
<button class="btn btn-sm {% if post.user_vote == 1 %}btn-success{% else %}btn-outline-success{% endif %}" id="upvote-btn-{{ post.id }}" onclick="vote({{ post.id }}, 'upvote')"><i class="fi fi-ss-up"></i></button>
<span id="upvotes-{{ post.id }}" style="margin: 0px 5px;">{{ post.upvotes_count }}</span>
<button class="btn btn-sm {% if post.user_vote == -1 %}btn-danger{% else %}btn-outline-danger{% endif %}" id="downvote-btn-{{ post.id }}" onclick="vote({{ post.id }}, 'downvote')"><i class="fi fi-ss-down"></i></i></button>
<span id="downvotes-{{ post.id }}" style="margin: 0px 5px;">{{ post.downvotes_count }}</span>
</div>
<div class="small-date" style="margin-left: 30px;margin-bottom:10px">Posted on {{ post.post_date }}</div> <!-- Make the post date smaller -->
<div class="function-link">
{% if request.user == post.user %}
<a href="{% url 'Community:edit' post_id=post.id %}">Edit Post</a>
<span class="delete-link" onclick="confirmDelete({{ post.id }})" style="color: red; cursor: pointer; margin-left: 10px;">Delete</span>
{% endif %}
</div>
<div class="small-date" style="margin-left: 30px;margin-bottom:10px">Replied on {{ post.post_date }}</div> <!-- Make the post date smaller -->
<div class="comments-count" style="margin-left: 30px;">
{{ num_r }} Comments
{% if num_r == 0 or num_r == 1 %}
{{ num_r }} Comment
{% else %}
{{ num_r }} Comments
{% endif %}
</div>
<!-- New container for replies -->
<div class="reply-container">
Expand Down Expand Up @@ -229,7 +259,25 @@ <h1 class="mb-0 fs-4">
if (data.error) {
alert(data.error);
} else {
document.getElementById(`rating-${postId}`).innerText = data.rating;
document.getElementById(`upvotes-${postId}`).innerText = data.upvotes_count;
document.getElementById(`downvotes-${postId}`).innerText = data.downvotes_count;

// Handle button states
const upvoteBtn = document.getElementById(`upvote-btn-${postId}`);
const downvoteBtn = document.getElementById(`downvote-btn-${postId}`);

// Reset classes first
upvoteBtn.className = "btn btn-outline-success btn-sm";
downvoteBtn.className = "btn btn-outline-danger btn-sm";

// Add active class based on the vote type and new value
if (data.user_vote == 1) {
upvoteBtn.classList.add('btn-success');
upvoteBtn.classList.remove('btn-outline-success');
} else if (data.user_vote == -1) {
downvoteBtn.classList.add('btn-danger');
downvoteBtn.classList.remove('btn-outline-danger');
}
}
})
.catch(error => console.error('Error:', error));
Expand All @@ -249,6 +297,32 @@ <h1 class="mb-0 fs-4">
}
return cookieValue;
}

function confirmDelete(postId) {
if (confirm("Are you sure you want to delete this post?")) {
// If confirmed, send the delete request
fetch(`/community/${postId}/delete`, {
method: 'POST',
headers: {
'X-CSRFToken': '{{ csrf_token }}',
'Content-Type': 'application/json'
}
})
.then(response => response.json())
.then(data => {
// Redirect to the current page after deletion or handle success as needed
if (data.success) {
window.location.reload(); // Reload the page
} else {
alert("Failed to delete post.");
}
})
.catch(error => {
console.error('Error:', error);
alert("Failed to delete post.");
});
}
}
</script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/lightbox2/2.11.4/js/lightbox.min.js" integrity="sha512-Ixzuzfxv1EqafeQlTCufWfaC6ful6WFqIz4G+dWvK0beHw0NVJwvCKSgafpy5gwNqKmgUfIBraVwkKI+Cz0SEQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
Expand Down

0 comments on commit 5177edb

Please sign in to comment.