From be6e2dad68bbb17825e79128d41e828e51714c41 Mon Sep 17 00:00:00 2001 From: Dmitriy Lyalyuev Date: Mon, 24 Oct 2022 16:31:34 +0300 Subject: [PATCH] feat: archive for URLs --- later42/migrations/0002_url_archived.py | 18 +++++++++++++++ later42/models/urls.py | 1 + later42/templates/archive.html | 30 +++++++++++++++++++++++++ later42/templates/base.html | 1 + later42/templates/index.html | 2 +- later42/urls.py | 6 ++++- later42/views/api.py | 5 ++++- later42/views/index.py | 24 ++++++++++++++++---- 8 files changed, 80 insertions(+), 7 deletions(-) create mode 100644 later42/migrations/0002_url_archived.py create mode 100644 later42/templates/archive.html diff --git a/later42/migrations/0002_url_archived.py b/later42/migrations/0002_url_archived.py new file mode 100644 index 0000000..0da94ff --- /dev/null +++ b/later42/migrations/0002_url_archived.py @@ -0,0 +1,18 @@ +# Generated by Django 4.1.2 on 2022-10-24 13:04 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('later42', '0001_initial'), + ] + + operations = [ + migrations.AddField( + model_name='url', + name='archived', + field=models.BooleanField(default=False), + ), + ] diff --git a/later42/models/urls.py b/later42/models/urls.py index 3bd1453..b7498c1 100644 --- a/later42/models/urls.py +++ b/later42/models/urls.py @@ -7,3 +7,4 @@ class URL(models.Model): user = models.ForeignKey(User, on_delete=models.CASCADE) url = models.CharField(max_length=2000) title = models.CharField(max_length=2000) + archived = models.BooleanField(default=False) diff --git a/later42/templates/archive.html b/later42/templates/archive.html new file mode 100644 index 0000000..d8a7166 --- /dev/null +++ b/later42/templates/archive.html @@ -0,0 +1,30 @@ +{% extends 'base.html' %} + +{% block content %} +{% if urls|length > 0 %} +{% for url in urls %} + +
+
+ +
+ +
+
+ +
+ +
+{% endfor %} +{% else %} +
+

У вас нет ссылок в архиве

+
+{% endif %} +{% endblock %} \ No newline at end of file diff --git a/later42/templates/base.html b/later42/templates/base.html index addc6bf..b9fb243 100644 --- a/later42/templates/base.html +++ b/later42/templates/base.html @@ -31,6 +31,7 @@ {% if user.is_authenticated %} + {% else %} diff --git a/later42/templates/index.html b/later42/templates/index.html index 6bc4afd..2b17fa4 100644 --- a/later42/templates/index.html +++ b/later42/templates/index.html @@ -10,7 +10,7 @@

{{ url.title }}

- +

diff --git a/later42/urls.py b/later42/urls.py index 229dc35..2d757fd 100644 --- a/later42/urls.py +++ b/later42/urls.py @@ -42,7 +42,8 @@ router.register(r'users', UserViewSet) urlpatterns = [ path('admin/', admin.site.urls), path('signup/', signup.register, name='signup'), - path("accounts/login/", LoginView.as_view(authentication_form=CustomLoginForm), name="login"), + path("accounts/login/", + LoginView.as_view(authentication_form=CustomLoginForm), name="login"), path("accounts/", include("django.contrib.auth.urls")), path('profile/', profile.get, name='profile'), path('api/url/', api.URL.as_view(), name='urls'), @@ -50,4 +51,7 @@ urlpatterns = [ path('api_token/', api_token.create, name='api_token'), path('', index.get, name='index'), path('about/', about.get, name='about'), + path('archive/', index.archive, name='archive'), + path('archive/', index.archive, name='archive_url'), + ] diff --git a/later42/views/api.py b/later42/views/api.py index 0dc937b..c1e3309 100644 --- a/later42/views/api.py +++ b/later42/views/api.py @@ -31,7 +31,10 @@ class URL(APIView): def delete(self, request, format=None): id = request.GET.get('id') if id: - URLModel.objects.filter(id=id).delete() + url = URLModel.objects.filter(id=id).first() + if url: + url.archived = True + url.save() return Response({'status': 'success'}) else: return Response({'status': 'error'}) diff --git a/later42/views/index.py b/later42/views/index.py index 3e93684..cd26d01 100644 --- a/later42/views/index.py +++ b/later42/views/index.py @@ -1,3 +1,4 @@ +from multiprocessing import context from django.contrib.auth.decorators import login_required from django.shortcuts import render, redirect from later42.models.urls import URL @@ -5,14 +6,29 @@ from later42.models.urls import URL def get(request): try: - urls = URL.objects.filter(user=request.user).order_by('-id') - context = {'urls': urls} + urls = URL.objects.filter( + user=request.user, archived=False).order_by('-id') except: - context = {'urls': []} + urls = [] + context = {'urls': urls} return render(request, 'index.html', context) +@login_required +def archive(request, url_id=None): + if url_id: + URL.objects.filter(id=url_id, user=request.user).update(archived=True) + return redirect('index') + try: + urls = URL.objects.filter( + user=request.user, archived=True).order_by('-id') + except: + urls = [] + context = {'urls': urls} + return render(request, 'archive.html', context) + + @login_required def delete(request, url_id): URL.objects.filter(id=url_id, user=request.user).delete() - return redirect('index') + return redirect('archive')