diff --git a/PlanktonDetector/Community/froms.py b/PlanktonDetector/Community/froms.py new file mode 100644 index 0000000..5cc041d --- /dev/null +++ b/PlanktonDetector/Community/froms.py @@ -0,0 +1,18 @@ +from django import forms +from .models import Post, Comment + + +class PostForm(forms.ModelForm): + class Meta: + model = Post + fields = ["title", "content"] + + +class CommentForm(forms.ModelForm): + class Meta: + model = Comment + fields = ["content"] + labels = { + "content": "Comment", + } + widgets = {"content": forms.Textarea(attrs={"rows": 3})} diff --git a/PlanktonDetector/Community/migrations/0003_comment_date_added.py b/PlanktonDetector/Community/migrations/0003_comment_date_added.py new file mode 100644 index 0000000..46f0226 --- /dev/null +++ b/PlanktonDetector/Community/migrations/0003_comment_date_added.py @@ -0,0 +1,21 @@ +# Generated by Django 4.2.7 on 2024-01-05 19:44 + +from django.db import migrations, models +import django.utils.timezone + + +class Migration(migrations.Migration): + dependencies = [ + ("Community", "0002_post_date_pub"), + ] + + operations = [ + migrations.AddField( + model_name="comment", + name="date_added", + field=models.DateField( + auto_now_add=True, default=django.utils.timezone.now + ), + preserve_default=False, + ), + ] diff --git a/PlanktonDetector/Community/models.py b/PlanktonDetector/Community/models.py index aed7013..fa94610 100644 --- a/PlanktonDetector/Community/models.py +++ b/PlanktonDetector/Community/models.py @@ -10,8 +10,13 @@ class Post(models.Model): author = models.ForeignKey(User, on_delete=models.CASCADE, null=False) date_pub = models.DateField(auto_now_add=True) + @property + def sorted_comments(self): + return self.comment_set.all().order_by("-date_added") + class Comment(models.Model): author = models.ForeignKey(User, on_delete=models.CASCADE, null=False) content = models.TextField() post = models.ForeignKey(Post, on_delete=models.CASCADE) + date_added = models.DateField(auto_now_add=True) diff --git a/PlanktonDetector/Community/urls.py b/PlanktonDetector/Community/urls.py index c31225f..c95a7df 100644 --- a/PlanktonDetector/Community/urls.py +++ b/PlanktonDetector/Community/urls.py @@ -6,4 +6,5 @@ from . import views urlpatterns = [ path("posts/", views.ListPosts.as_view(), name="posts"), path("posts/", views.PostDetails.as_view(), name="post-details"), + path("posts/create/", views.AddPost.as_view(), name="add_post"), ] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) diff --git a/PlanktonDetector/Community/views.py b/PlanktonDetector/Community/views.py index 99ce625..2bfd4a6 100644 --- a/PlanktonDetector/Community/views.py +++ b/PlanktonDetector/Community/views.py @@ -1,6 +1,12 @@ -from django.shortcuts import render +from typing import Any +from django.db.models.query import QuerySet +from django.http import HttpResponse +from django.shortcuts import redirect, render from django.views.generic import ListView, DetailView +from django.views.generic.edit import FormView, FormMixin from .models import Post, Comment +from .froms import PostForm, CommentForm + # Create your views here. @@ -8,8 +14,44 @@ from .models import Post, Comment class ListPosts(ListView): model = Post template_name = "list_posts.html" + paginate_by = 3 + + def get_queryset(self) -> QuerySet[Any]: + queryset = Post.objects.all().order_by("-date_pub") + return queryset -class PostDetails(DetailView): +class PostDetails(DetailView, FormMixin): model = Post template_name = "post_details.html" + form_class = CommentForm + + # def get_queryset(self) -> QuerySet[Any]: + # queryset = Post.objects.get(pk=self.request.GET.get("pk")) + + def post(self, request, *args, **kwargs): + self.object = self.get_object() + form = self.get_form() + if form.is_valid(): + return self.form_valid(form) + else: + return self.form_invalid(form) + + def form_valid(self, form) -> HttpResponse: + post = self.get_object() + new_comment = form.save(commit=False) + new_comment.author = self.request.user + new_comment.post = post + new_comment.save() + return redirect("post-details", pk=post.id) + + +class AddPost(FormView): + template_name = "add_post.html" + form_class = PostForm + + def form_valid(self, form) -> HttpResponse: + new_post = form.save(commit=False) + new_post.author = self.request.user + new_post.save() + return redirect("post-details", pk=new_post.id) diff --git a/PlanktonDetector/DetectionApp/urls.py b/PlanktonDetector/DetectionApp/urls.py index eca106e..b69b15c 100644 --- a/PlanktonDetector/DetectionApp/urls.py +++ b/PlanktonDetector/DetectionApp/urls.py @@ -10,5 +10,5 @@ urlpatterns = [ path( "detection/", views.DetectionDetails.as_view(), name="detection-details" ), - path("export_pred/", views.download_pred_res, name="download_predicitons"), + path("export_pred/", views.download_pred_res, name="download_predicitions"), ] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) diff --git a/PlanktonDetector/UserManagement/views.py b/PlanktonDetector/UserManagement/views.py index 94df782..04e1091 100644 --- a/PlanktonDetector/UserManagement/views.py +++ b/PlanktonDetector/UserManagement/views.py @@ -1,8 +1,10 @@ from django.shortcuts import render, redirect from django.contrib.auth.forms import AuthenticationForm, UserCreationForm from django.contrib.auth import authenticate, login, logout +from django.urls import reverse_lazy from django.views import View + class LoginView(View): form_class = AuthenticationForm template_name = "login_signup.html" @@ -12,6 +14,7 @@ class LoginView(View): return render(request, self.template_name, {"form": form}) def post(self, request, *args, **kwargs): + redirection_path = request.GET.get("next") form = self.form_class(data=request.POST) if form.is_valid(): user = authenticate( @@ -20,7 +23,10 @@ class LoginView(View): ) if user is not None: login(request, user) - return redirect("detect") + if redirection_path is not None: + return redirect(redirection_path) + else: + return redirect("detect/") return render(request, self.template_name, {"form": form}) @@ -37,10 +43,14 @@ class SignupView(View): if form.is_valid(): user = form.save() login(request, user) - return redirect("detect") + return redirect("detect/") return render(request, self.template_name, {"form": form, "signup": True}) def logout_view(request): + redirection_path = request.GET.get("next") logout(request) - return redirect("detect") + if redirection_path is not None: + return redirect(redirection_path) + else: + return redirect("detect/") diff --git a/PlanktonDetector/db.sqlite3 b/PlanktonDetector/db.sqlite3 index 2778999..30fa830 100644 Binary files a/PlanktonDetector/db.sqlite3 and b/PlanktonDetector/db.sqlite3 differ diff --git a/PlanktonDetector/static/Community/css/add_post.css b/PlanktonDetector/static/Community/css/add_post.css new file mode 100644 index 0000000..e69de29 diff --git a/PlanktonDetector/static/Community/css/list_posts.css b/PlanktonDetector/static/Community/css/list_posts.css index 17adc82..274e4e6 100644 --- a/PlanktonDetector/static/Community/css/list_posts.css +++ b/PlanktonDetector/static/Community/css/list_posts.css @@ -27,6 +27,7 @@ ul{ align-items: flex-start; justify-content: space-between; margin-bottom: 25px; + box-shadow: 0 2px lightgray; } .date_author{ @@ -35,4 +36,16 @@ ul{ flex-direction: row; align-items: flex-start; justify-content: space-between; +} + +#add_post{ + text-align: center; + padding: 1px 6px 1px 6px; + width: 10%; + background-color: chartreuse; + border-radius: 25px; + border: 2px black; + box-shadow: 0 0 0 2px black; + font-size: 20px; + font-family: Arial; } \ No newline at end of file diff --git a/PlanktonDetector/static/Community/css/post_details.css b/PlanktonDetector/static/Community/css/post_details.css index f2d3589..3a95ff6 100644 --- a/PlanktonDetector/static/Community/css/post_details.css +++ b/PlanktonDetector/static/Community/css/post_details.css @@ -77,9 +77,46 @@ body, html{ margin-top:10px; } -#comment-author{ +#add_comment{ display: flex; - width: 100%; - justify-content: flex-end; + flex-direction: row; + width: 55%; + justify-content: space-evenly; + align-items: center; + margin-top:30px; } +#comment_form{ + width: 100%; + display: flex; + flex-direction: row; + justify-content: space-between; + align-items: center; +} + +#comment_form textarea{ + height: auto; +} + +#comment_form label{ + font-size: 20px; +} + +.date_author{ + display: flex; + width: 100%; + flex-direction: row; + align-items: flex-start; + justify-content: space-between; +} + +#submit{ + background-color: chartreuse; + border-radius: 25px; + border: 2px solid black; + font-size: 20px; + text-decoration: none; + color:black; + font-family: Arial; + text-align: center; +} \ No newline at end of file diff --git a/PlanktonDetector/static/DetectionApp/css/results.css b/PlanktonDetector/static/DetectionApp/css/results.css index 0c466e9..6511018 100644 --- a/PlanktonDetector/static/DetectionApp/css/results.css +++ b/PlanktonDetector/static/DetectionApp/css/results.css @@ -29,17 +29,6 @@ form{ margin: 10px 0px 15px 0px; } -#submit{ - background-color: chartreuse; - margin-top: auto; - height: 50px; - width: 80%; - border-radius: 25px; - border: 2px white; - box-shadow: 0 0 0 4px white; - font-size: 25px; -} - .upload_field { margin: auto; display: flex; @@ -91,7 +80,26 @@ form{ height: 50px; width: 60%; border-radius: 25px; - border: 2px white; - box-shadow: 0 0 0 4px white; + border: 2px solid black; font-size: 25px; + text-decoration: none; + color:black; + font-family: Arial; + text-align: center; + line-height:45px; +} + +#submit{ + background-color: chartreuse; + margin-top: auto; + height: 50px; + width: 80%; + border-radius: 25px; + border: 2px solid white; + font-size: 25px; + text-decoration: none; + color:black; + font-family: Arial; + text-align: center; + line-height:45px; } \ No newline at end of file diff --git a/PlanktonDetector/static/DetectionApp/css/upload.css b/PlanktonDetector/static/DetectionApp/css/upload.css index 35e4672..55ef641 100644 --- a/PlanktonDetector/static/DetectionApp/css/upload.css +++ b/PlanktonDetector/static/DetectionApp/css/upload.css @@ -34,9 +34,13 @@ form{ height: 50px; width: 80%; border-radius: 25px; - border: 2px white; - box-shadow: 0 0 0 4px white; + border: 2px solid white; font-size: 25px; + text-decoration: none; + color:black; + font-family: Arial; + text-align: center; + line-height:45px; } .upload_field { diff --git a/PlanktonDetector/templates/add_post.html b/PlanktonDetector/templates/add_post.html new file mode 100644 index 0000000..69e7ac2 --- /dev/null +++ b/PlanktonDetector/templates/add_post.html @@ -0,0 +1,12 @@ +{%extends 'base.html'%} +{%load static%} +{%block extracss%} + +{%endblock extracss%} +{%block content%} +
+ {% csrf_token %} + {{ form.as_p }} + +
+{%endblock content%} \ No newline at end of file diff --git a/PlanktonDetector/templates/base.html b/PlanktonDetector/templates/base.html index a5764e0..42ece1c 100644 --- a/PlanktonDetector/templates/base.html +++ b/PlanktonDetector/templates/base.html @@ -18,9 +18,9 @@ Predict {% if user.is_authenticated %} History - Logout {{user}} + Logout {{user}} {%else%} - Login + Login {%endif%} {%block content%}{%endblock content%} diff --git a/PlanktonDetector/templates/list_posts.html b/PlanktonDetector/templates/list_posts.html index 5ca2d15..62187b8 100644 --- a/PlanktonDetector/templates/list_posts.html +++ b/PlanktonDetector/templates/list_posts.html @@ -6,7 +6,10 @@ {%block content%}

Posts

- {% for post in object_list %} +{% if user.is_authenticated %} +Add new post +{%endif%} + {% for post in page_obj %}

{{ post.title }}

@@ -17,5 +20,22 @@
{% endfor %} +
{%endblock content%} \ No newline at end of file diff --git a/PlanktonDetector/templates/post_details.html b/PlanktonDetector/templates/post_details.html index 8d4451a..e1a02af 100644 --- a/PlanktonDetector/templates/post_details.html +++ b/PlanktonDetector/templates/post_details.html @@ -14,12 +14,22 @@

{{object.author}}

+
+
+ {%csrf_token%} + {{form}} + +
+

Comments:

- {%for comments in object.comment_set.all%} + {%for comment in object.sorted_comments%}
-

{{comments.content}}

-

{{comments.author}}

+

{{comment.content}}

+
+

{{comment.date_added}}

+

{{comment.author}}

+
{%endfor%}
diff --git a/PlanktonDetector/templates/results.html b/PlanktonDetector/templates/results.html index 658bc77..994c90d 100644 --- a/PlanktonDetector/templates/results.html +++ b/PlanktonDetector/templates/results.html @@ -9,7 +9,7 @@
{% if not user.is_authenticated %}

Please login to see results

- + Login {% else %}

Results:

{%for img in img.images.all %} @@ -20,7 +20,7 @@
{%endfor%} {%endfor%} - + Submit again {%endif%}
@@ -29,7 +29,7 @@ upload_image {%endfor%} - + Export predictions