2013-02-25 04:08:35 +01:00
|
|
|
from django.contrib.auth import REDIRECT_FIELD_NAME
|
|
|
|
|
from django.contrib.auth.decorators import login_required
|
2012-04-26 03:44:16 +00:00
|
|
|
from django.core.urlresolvers import reverse
|
2012-05-01 05:44:50 +00:00
|
|
|
from django.contrib import messages
|
2013-02-25 04:08:35 +01:00
|
|
|
from django.utils.decorators import method_decorator
|
2013-02-24 16:56:03 +01:00
|
|
|
from django.utils.functional import lazy
|
2013-02-25 04:08:35 +01:00
|
|
|
from django.views.generic import (
|
|
|
|
|
TemplateView, CreateView)
|
2012-04-26 03:44:16 +00:00
|
|
|
|
2013-02-25 04:08:35 +01:00
|
|
|
from django_images.models import Image
|
|
|
|
|
|
|
|
|
|
from .forms import ImageForm
|
2012-04-26 03:44:16 +00:00
|
|
|
|
|
|
|
|
|
2013-02-24 18:18:22 +01:00
|
|
|
reverse_lazy = lambda name=None, *args: lazy(reverse, str)(name, args=args)
|
2012-04-26 03:44:16 +00:00
|
|
|
|
|
|
|
|
|
2013-02-25 04:08:35 +01:00
|
|
|
class LoginRequiredMixin(object):
|
|
|
|
|
"""
|
|
|
|
|
A login required mixin for use with class based views. This Class is a light wrapper around the
|
|
|
|
|
`login_required` decorator and hence function parameters are just attributes defined on the class.
|
|
|
|
|
|
|
|
|
|
Due to parent class order traversal this mixin must be added as the left most
|
|
|
|
|
mixin of a view.
|
|
|
|
|
|
|
|
|
|
The mixin has exactly the same flow as `login_required` decorator:
|
|
|
|
|
|
|
|
|
|
If the user isn't logged in, redirect to settings.LOGIN_URL, passing the current
|
|
|
|
|
absolute path in the query string. Example: /accounts/login/?next=/polls/3/.
|
|
|
|
|
|
|
|
|
|
If the user is logged in, execute the view normally. The view code is free to
|
|
|
|
|
assume the user is logged in.
|
2013-02-24 16:56:03 +01:00
|
|
|
|
2013-02-25 04:08:35 +01:00
|
|
|
**Class Settings**
|
|
|
|
|
`redirect_field_name - defaults to "next"
|
|
|
|
|
`login_url` - the login url of your site
|
2013-02-24 16:56:03 +01:00
|
|
|
|
2013-02-25 04:08:35 +01:00
|
|
|
"""
|
|
|
|
|
redirect_field_name = REDIRECT_FIELD_NAME
|
|
|
|
|
login_url = None
|
|
|
|
|
|
|
|
|
|
@method_decorator(login_required(redirect_field_name=redirect_field_name, login_url=login_url))
|
|
|
|
|
def dispatch(self, request, *args, **kwargs):
|
|
|
|
|
return super(LoginRequiredMixin, self).dispatch(request, *args, **kwargs)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class UploadImage(LoginRequiredMixin, CreateView):
|
|
|
|
|
template_name = 'pins/pin_form.html'
|
|
|
|
|
model = Image
|
|
|
|
|
form_class = ImageForm
|
2013-02-24 16:56:03 +01:00
|
|
|
|
|
|
|
|
def form_valid(self, form):
|
|
|
|
|
messages.success(self.request, 'New pin successfully added.')
|
2013-02-25 04:08:35 +01:00
|
|
|
return super(UploadImage, self).form_valid(form)
|
2013-02-24 16:56:03 +01:00
|
|
|
|
|
|
|
|
def form_invalid(self, form):
|
|
|
|
|
messages.error(self.request, 'Pin did not pass validation!')
|
2013-02-25 04:08:35 +01:00
|
|
|
return super(UploadImage, self).form_invalid(form)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class RecentPins(TemplateView):
|
|
|
|
|
template_name = 'pins/recent_pins.html'
|