-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4750107
commit 769bc34
Showing
5 changed files
with
194 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
'''Photo app Forms''' | ||
|
||
from django import forms | ||
from django.forms import fields | ||
|
||
from .models import Photo | ||
|
||
class CreatePhotoForm(forms.ModelForm): | ||
|
||
class Meta: | ||
model = Photo | ||
|
||
fields = ('title', 'description', 'image', 'tags') | ||
|
||
|
||
class UpdatePhotoForm(forms.ModelForm): | ||
|
||
class Meta: | ||
model = Photo | ||
|
||
fields = ('title', 'description') | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# Generated by Django 3.2.3 on 2021-05-16 21:29 | ||
|
||
from django.conf import settings | ||
from django.db import migrations, models | ||
import django.db.models.deletion | ||
import taggit.managers | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
initial = True | ||
|
||
dependencies = [ | ||
migrations.swappable_dependency(settings.AUTH_USER_MODEL), | ||
('taggit', '0003_taggeditem_add_unique_index'), | ||
] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name='Photo', | ||
fields=[ | ||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), | ||
('title', models.CharField(max_length=45)), | ||
('description', models.CharField(max_length=250)), | ||
('created', models.DateTimeField(auto_now_add=True)), | ||
('image', models.ImageField(upload_to='photos/')), | ||
('submitter', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)), | ||
('tags', taggit.managers.TaggableManager(help_text='A comma-separated list of tags.', through='taggit.TaggedItem', to='taggit.Tag', verbose_name='Tags')), | ||
], | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
'''Photoapp URL patterns''' | ||
|
||
from django.urls import path | ||
|
||
from .views import ( | ||
PhotoListView, | ||
PhotoTagListView, | ||
PhotoDetailView, | ||
PhotoCreateView, | ||
PhotoUpdateView, | ||
PhotoDeleteView | ||
) | ||
|
||
appname = 'photo' | ||
|
||
urlpatterns = [ | ||
path('', PhotoListView.as_view(), name='list'), | ||
|
||
path('tag/<slug:tag>/', PhotoTagListView.as_view(), name='tag'), | ||
|
||
path('photo/<int:pk>/', PhotoDetailView.as_view(), name='detail'), | ||
|
||
path('photo/create/', PhotoCreateView.as_view(), name='create'), | ||
|
||
path('photo/<int:pk>/update/', PhotoUpdateView.as_view(), name='update'), | ||
|
||
path('photo/<int:pk>/delete/', PhotoDeleteView.as_view(), name='delete'), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,100 @@ | ||
from django.shortcuts import render | ||
'''Photo app generic views''' | ||
|
||
# Create your views here. | ||
from django.shortcuts import get_object_or_404 | ||
|
||
from django.http import HttpResponseNotAllowed | ||
|
||
from django.urls.resolvers import get_resolver | ||
|
||
from django.views.generic import ListView, DetailView, CreateView, UpdateView, DeleteView | ||
|
||
from django.contrib.auth.mixins import LoginRequiredMixin, UserPassesTestMixin | ||
|
||
from django.urls import reverse_lazy | ||
|
||
from .models import Photo | ||
|
||
from .forms import CreatePhotoForm, UpdatePhotoForm | ||
|
||
class PhotoListView(ListView): | ||
|
||
model = Photo | ||
|
||
template_name = 'photoapp/list.html' | ||
|
||
context_object_name = 'photos' | ||
|
||
|
||
class PhotoTagListView(ListView): | ||
|
||
model = Photo | ||
|
||
template_name = 'photoapp/taglist.html' | ||
|
||
context_object_name = 'photos' | ||
|
||
|
||
# Custom function | ||
def get_tag(self): | ||
return self.kwargs.get('tag') | ||
|
||
def get_queryset(self): | ||
return Photo.objects.filter(tag__slug_in=[self.get_tag()]) | ||
|
||
def get_context_data(self, **kwargs): | ||
context = super().get_context_data(**kwargs) | ||
context["tag"] = self.get_tag() | ||
return context | ||
|
||
|
||
class PhotoDetailView(DetailView): | ||
|
||
model = Photo | ||
|
||
context_object_name = 'photo' | ||
|
||
template_name = 'photoapp/detail.html' | ||
|
||
|
||
class PhotoCreateView(LoginRequiredMixin, CreateView): | ||
|
||
model = Photo | ||
|
||
form = CreatePhotoForm | ||
|
||
success_url = reverse_lazy('photo:list') | ||
|
||
def form_valid(self, form): | ||
|
||
form.user = self.request.user | ||
form.save() | ||
# Without this next line the tags won't be saved. | ||
form.save_m2m() | ||
return super().form_valid(form) | ||
|
||
class UserIsSubmitter(UserPassesTestMixin): | ||
|
||
# Custom method | ||
def get_photo(self): | ||
return get_object_or_404(Photo, pk=self.kwargs.get('pk')) | ||
|
||
def test_func(self): | ||
|
||
if self.request.user.is_authenticated(): | ||
return self.request.user is self.get_photo().submitter | ||
else: | ||
raise HttpResponseNotAllowed('Sorry you are not allowed here') | ||
|
||
class PhotoUpdateView(UserIsSubmitter, UpdateView): | ||
|
||
model = Photo | ||
|
||
form = UpdatePhotoForm | ||
|
||
success_url = reverse_lazy('photo:list') | ||
|
||
class PhotoDeleteView(UserIsSubmitter, DeleteView): | ||
|
||
model = Photo | ||
|
||
success_url = reverse_lazy('photo:list') |