From ef12610d6cfa9102b49d7001270f6550d2f1dca1 Mon Sep 17 00:00:00 2001 From: Farid Chowdhury Date: Wed, 28 Sep 2022 07:27:56 +0600 Subject: [PATCH] updated form validation in homepage, added optimization in db query, added customization in django admin panel --- album/admin.py | 20 ++++++ album/migrations/0003_album_target.py | 19 ++++++ album/models.py | 5 ++ album/templates/album/homepage.html | 90 ++++++++++++++++++++------ album/views.py | 19 ++++-- db.sqlite3 | Bin 159744 -> 159744 bytes templates/base.html | 7 +- 7 files changed, 133 insertions(+), 27 deletions(-) create mode 100644 album/migrations/0003_album_target.py diff --git a/album/admin.py b/album/admin.py index 0c7ec8c..8a1ed73 100644 --- a/album/admin.py +++ b/album/admin.py @@ -7,4 +7,24 @@ from .models import Album class AlbumAdmin(admin.ModelAdmin): list_display = ['id', 'image', 'is_verified'] + fieldsets = ( + ('General', { + 'fields': ('object_type', 'description', 'image'), + }), + ('User Info', { + 'fields': ( + ('surname', 'first_name'), + ('birth_date','postal_code'), + ('address','town'), + ('telephone','email'), + ), + }), + ('Others', { + 'fields': ( + ('target', 'is_agreed_terms_and_cond'), + 'is_verified' + ), + }) + ) + admin.site.register(Album, AlbumAdmin) diff --git a/album/migrations/0003_album_target.py b/album/migrations/0003_album_target.py new file mode 100644 index 0000000..b9eff76 --- /dev/null +++ b/album/migrations/0003_album_target.py @@ -0,0 +1,19 @@ +# Generated by Django 3.2 on 2022-09-27 22:51 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('album', '0002_auto_20220926_2135'), + ] + + operations = [ + migrations.AddField( + model_name='album', + name='target', + field=models.CharField(choices=[('DONATE_TO_MUSEUM', 'Donate To Museum'), ('MAKE_IT_AVAILABLE_SOLELY', 'Make It Available Solely')], default='', max_length=100), + preserve_default=False, + ), + ] diff --git a/album/models.py b/album/models.py index 2e5cf8d..b0ee4ce 100644 --- a/album/models.py +++ b/album/models.py @@ -12,6 +12,10 @@ class Album(models.Model): MOUNTAIN_STORY = 'MOUNTAIN_STORY' SPECIAL_KNOWLEDGE = 'SPECIAL_KNOWLEDGE' + class TARGET_CHOICES(models.TextChoices): + DONATE_TO_MUSEUM = 'DONATE_TO_MUSEUM' + MAKE_IT_AVAILABLE_SOLELY = 'MAKE_IT_AVAILABLE_SOLELY' + object_type = models.CharField(max_length=40, choices=OBJECT_TYPE_CHOICES.choices) description = models.TextField() image = models.ImageField() @@ -23,6 +27,7 @@ class Album(models.Model): town = models.CharField(max_length=20) telephone = models.CharField(max_length=20) email = models.EmailField() + target = models.CharField(max_length=100, choices=TARGET_CHOICES.choices) is_agreed_terms_and_cond = models.BooleanField() is_verified = models.BooleanField(default=False, blank=True) diff --git a/album/templates/album/homepage.html b/album/templates/album/homepage.html index 7693a1d..34b2500 100644 --- a/album/templates/album/homepage.html +++ b/album/templates/album/homepage.html @@ -2,6 +2,21 @@ {% load humanize %} + +{% block header %} + + +{% endblock header %} + + {% block content %}
@@ -67,7 +82,7 @@ {% endfor %} {% endif %} -
+
+ + diff --git a/album/views.py b/album/views.py index 700c301..ea48444 100644 --- a/album/views.py +++ b/album/views.py @@ -1,12 +1,14 @@ from django.contrib import messages from django.contrib.auth.mixins import LoginRequiredMixin +from django.core import exceptions from django.core.exceptions import PermissionDenied -from django.http import HttpResponse, HttpResponseRedirect +from django.http import (HttpResponse, HttpResponseBadRequest, + HttpResponseRedirect) from django.shortcuts import render from django.views.generic import CreateView, TemplateView -from .models import Album from .forms import AlbumForm +from .models import Album from .serializers import AlbumCreateUpdateSerializer # Create your views here. @@ -20,15 +22,24 @@ class HomePageView(TemplateView): # NOTE: This will be used to update the active window in navbar context.update({ 'contact_us_create': 'active', - 'albums': Album.objects.all() + 'albums': Album.objects.filter(is_verified=True).only('id', 'image', 'is_verified') }) return context def post(self, request, *args, **kwargs) -> HttpResponse: + print(request.POST.get('surname')) form = AlbumForm(request.POST, request.FILES) + # print(form.__dict__) if form.is_valid(): - print(form) + # print('opno') + instance = form.save() + messages.success(self.request, 'Image uploaded successfully.') + + return HttpResponseRedirect('/') + else: + return HttpResponseBadRequest() + # raise exceptions.ValidationError(form.errors) return data = {'image': request.FILES.get('image')} diff --git a/db.sqlite3 b/db.sqlite3 index 634e0b60b057e693521bde2520c8e5b75ef86186..68327a53cf1c46e5f3108f6b22abb515eeab422c 100644 GIT binary patch delta 1437 zcmZXU&u`jh9L81lo{@ z_EL&Eb!t_$n{vo)+M&l?&<;%6ZMR)^$YsZMhiQMpq+Z(ByaD3G@V+wN=k@!3-sgSr z(|7FCckK6b{1Qb`mvQ?XHyXFEez>a8OPxD!eIueLbu=*}F`iVX?eWB;;us}HbbI&i z;T`}$v3hsbfxW+d?*IXWB#@9$d;y_k6eb~*Riq*f{nGp-6qpH&EYf6rulFAP90}3l zvy;eoClR`P5@F<-1r9IoWuH^NH@?vHZ__6CFZam%pEo!4qvwB*$v*G2@3XCT%Il}h zNnM`{3L%k=1m=ZV2_=xz0Ej78vtl1e)rM65#;$GGG`ms0Z||pCdvKSV-mtW5q>1zgp z0IXE%&2r^%|1m^L;qh+e-a$H_(~E{x%v;$~!6;?hSEl@wg7+ZeJqYalEH8*lhqawr zy;-xg>~3RcOLu`|QClmxA8jugAo)S$o$_=~Rmq843<8P{ujWy#&$-JI5 zaz%XasHDR`vih1~nMF&#rJEUjIb&Fbe8$LZv_JC@Tp=j5!U7u>@Fn$mZSoR=6!SMXT4X<@zu>FH63|dkka#xC zpXIz+iq%(}U!3Q}VbziV%1}cw{OJdR#CV{@qj*Kf#8&U{2g4u%aaoLwbRiRid&h7A z_|hZ+o(+m2Ve+zLEY~--dI+{2*~@O=mh<;gaD)>wR3S{boIRtUXo8^iQE?K;QM=4t z$(Y$?Gk48eH8WZA0O{M)6MkwDM--oVcovfd1l}QQIT>{$of1J!))c5k&v}ShiIM;a zPu<6XinET(HxkG5M7?x6LwCmgRG6L8buP$p96KN2j$dywU-Ngz{FI16hvoO1Oo4we zN}L^*H*YgGUl}3W8kYaO&1~=$2AA2hlf*4tuG4-By5|1LV(xV56!XD%^x^NZOOtGx z@|C84=U#J$_mww4^(&hm|EJUbW1JcNtJ7X*{LB;lR>Rk~50@sy#YOr>__$VO=IBuO ZWtEW__Ugi?83O}@IuNS@F(VL%Pt-AHG~1Z4gr8ZAt9&xMfbPadXRgiVQmUc? zBFxH+i8)E7x$$60Y`k?0{1f=q z`S$Vo^1kA&+t`@JE0Dz|$e_vT=*X+c%gf6tFFL)`mnlXdkyVgEoC8Vpn=ezaKt78g zgE6vblpj-~KsvJ^gEF${89$~-fpjLID6+X`{y@<@MnMKcWVH+YnG&RUf!^fcOkv;; z(^b diff --git a/templates/base.html b/templates/base.html index f0b2797..0f838ca 100644 --- a/templates/base.html +++ b/templates/base.html @@ -22,9 +22,9 @@ - {% block head %} + {% block header %} - {% endblock head %} + {% endblock header %} @@ -44,9 +44,6 @@ - - -