Skip to content

Commit

Permalink
Convert one letters-views to CBV - see #113
Browse files Browse the repository at this point in the history
  • Loading branch information
ad-m committed Jul 26, 2015
1 parent bdf71fc commit 90a8d24
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 45 deletions.
6 changes: 1 addition & 5 deletions poradnia/letters/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from django.utils.translation import ugettext_lazy as _
import autocomplete_light
from cases.models import Case
from utilities.forms import PartialMixin, SingleButtonMixin, FormsetHelper, GIODOMixin
from utilities.forms import PartialMixin, SingleButtonMixin, GIODOMixin
from .models import Letter, Attachment


Expand All @@ -23,7 +23,6 @@ def validate(self, value):


class NewCaseForm(SingleButtonMixin, PartialMixin, GIODOMixin, autocomplete_light.ModelForm):
form_helper_cls = FormsetHelper
attachment_cls = Attachment
attachment_rel_field = 'letter'
attachment_file_field = 'attachment'
Expand Down Expand Up @@ -111,7 +110,6 @@ class Meta:


class AddLetterForm(SingleButtonMixin, PartialMixin, ModelForm):
form_helper_cls = FormsetHelper

def __init__(self, *args, **kwargs):
self.user = kwargs.pop('user')
Expand Down Expand Up @@ -171,8 +169,6 @@ class Meta:


class LetterForm(SingleButtonMixin, PartialMixin, ModelForm): # eg. edit form
form_helper_cls = FormsetHelper

def __init__(self, *args, **kwargs):
self.user = kwargs.pop('user')
super(LetterForm, self).__init__(*args, **kwargs)
Expand Down
2 changes: 1 addition & 1 deletion poradnia/letters/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
url(r'^$', views.NewCaseCreateView.as_view(), name='add'),
url(r'^sprawa-(?P<case_pk>\d+)/$', views.add, name="add"),
url(r'^(?P<pk>\d+)/wyslij/$', views.send, name="send"),
url(r'^(?P<pk>\d+)/edytuj/$', views.edit, name="edit"),
url(r'^(?P<pk>\d+)/edytuj/$', views.LetterUpdateView.as_view(), name="edit"),
url(r'^(?P<pk>\d+)/$', views.send, name="detail"),
)
4 changes: 2 additions & 2 deletions poradnia/letters/views/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
from .fbv import add, send, edit, detail # noqa
from .cbv import NewCaseCreateView # noqa
from .fbv import add, send, detail # noqa
from .cbv import NewCaseCreateView, LetterUpdateView # noqa
36 changes: 35 additions & 1 deletion poradnia/letters/views/cbv.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
from django.contrib import messages
from django.http import HttpResponseRedirect
from braces.views import UserFormKwargsMixin, SetHeadlineMixin
from django.views.generic import UpdateView
from utilities.views import FormSetMixin
from ..forms import NewCaseForm, AttachmentForm
from ..forms import NewCaseForm, AttachmentForm, LetterForm
from ..models import Letter, Attachment
from .fbv import REGISTRATION_TEXT

Expand All @@ -27,3 +28,36 @@ def formset_valid(self, formset, *args, **kwargs):
if self.request.user.is_anonymous():
messages.success(self.request, _(REGISTRATION_TEXT) % {'user': self.object.created_by})
return HttpResponseRedirect(self.object.case.get_absolute_url())


class LetterUpdateView(SetHeadlineMixin, FormSetMixin, UserFormKwargsMixin, UpdateView):
model = Letter
form_class = LetterForm
headline = _('Edit')
template_name = 'letters/form_edit.html'
inline_model = Attachment
inline_form_cls = AttachmentForm

def get_context_data(self, **kwargs):
context = super(LetterUpdateView, self).get_context_data(**kwargs)
context['case'] = self.object.case
return context

def get_object(self):
obj = super(LetterUpdateView, self).get_object()
if obj.created_by_id == self.request.user.pk:
obj.case.perm_check(self.request.user, 'can_change_own_record')
else:
obj.case.perm_check(self.request.user, 'can_change_all_record')
return obj

def get_formset_valid_message(self):
return ("Letter %(object)s updated!") % {'object': self.object}

def get_success_url(self):
return self.object.case.get_absolute_url()

def formset_valid(self, form, formset):
resp = super(LetterUpdateView, self).formset_valid(form, formset)
self.object.send_notification(actor=self.request.user, verb='updated')
return resp
37 changes: 1 addition & 36 deletions poradnia/letters/views/fbv.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from django.utils.translation import ugettext as _
from ..helpers import AttachmentFormSet
# from crispy_forms.helper import FormHelper
from ..forms import AddLetterForm, LetterForm, SendLetterForm
from ..forms import AddLetterForm, SendLetterForm
from ..models import Letter


Expand Down Expand Up @@ -74,40 +74,5 @@ def send(request, pk):
return render(request, 'letters/form_send.html', context)


@login_required
def edit(request, pk):
context = {}
letter = get_object_or_404(Letter, pk=pk)
context['object'] = letter

case = letter.case
context['case'] = case

if letter.created_by == request.user:
case.perm_check(request.user, 'can_change_own_record')
else:
case.perm_check(request.user, 'can_change_all_record')

formset = None
if request.method == 'POST':
form = LetterForm(request.POST, user=request.user, instance=letter)
if form.is_valid():
obj = form.save(commit=False)
formset = AttachmentFormSet(request.POST, request.FILES, instance=obj)
if formset.is_valid():
obj.save()
formset.save()
messages.success(request,
_("Letter %(object)s updated!") % {'object': obj, })
obj.send_notification(actor=request.user, verb='updated')
return HttpResponseRedirect(obj.case.get_absolute_url())
else:
form = LetterForm(user=request.user, instance=letter)
context['form'] = form
context['formset'] = formset or AttachmentFormSet(instance=letter)
context['headline'] = _('Edit')
return render(request, 'letters/form_edit.html', context)


def detail(request, pk):
pass

0 comments on commit 90a8d24

Please sign in to comment.