Skip to content

Commit

Permalink
{Modified} project
Browse files Browse the repository at this point in the history
  • Loading branch information
DaniDiazTech committed May 17, 2021
1 parent 4750107 commit 769bc34
Show file tree
Hide file tree
Showing 5 changed files with 194 additions and 2 deletions.
14 changes: 14 additions & 0 deletions config/config/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',

# 3rd party apps
'taggit',

# Custom apps
'photoapp',
]

MIDDLEWARE = [
Expand Down Expand Up @@ -114,6 +120,14 @@
USE_TZ = True


# Django taggit

TAGGIT_CASE_INSENSITIVE = True

# Django Authentication
LOGIN_URL = 'users:login'
LOGIN_REDIRECT_URL = 'photo:list'

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.2/howto/static-files/

Expand Down
22 changes: 22 additions & 0 deletions config/photoapp/forms.py
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')

31 changes: 31 additions & 0 deletions config/photoapp/migrations/0001_initial.py
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')),
],
),
]
28 changes: 28 additions & 0 deletions config/photoapp/urls.py
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'),
]
101 changes: 99 additions & 2 deletions config/photoapp/views.py
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')

0 comments on commit 769bc34

Please sign in to comment.