SHOW:
|
|
- or go back to the newest paste.
| 1 | def add_ostrov(request): | |
| 2 | """ | |
| 3 | The View to add a Ostrov | |
| 4 | """ | |
| 5 | ImageFormSet = modelformset_factory(model=Image, extra=3, can_delete=True, exclude=('ostrov'))
| |
| 6 | if request.method == 'POST': | |
| 7 | form = OstrovForm(request.POST, request.FILES) | |
| 8 | image_formset = ImageFormSet(request.POST, request.FILES) | |
| 9 | if form.is_valid() and image_formset.is_valid(): | |
| 10 | ostrov = form.save(commit=False) | |
| 11 | ostrov.plaintiff = request.user | |
| 12 | ostrov.status = 'new' | |
| 13 | images = image_formset.save(commit = False) | |
| 14 | for image in images: | |
| 15 | image.ostrov = ostrov | |
| 16 | image.save() | |
| 17 | return redirect('ostrov', ostrov.pk)
| |
| 18 | else: | |
| 19 | form = OstrovForm() | |
| 20 | image_formset = ImageFormSet() | |
| 21 | ||
| 22 | template = 'ostrovs/add_ostrov.html' | |
| 23 | ||
| 24 | context = {
| |
| 25 | 'form': form, | |
| 26 | 'image_formset': image_formset, | |
| 27 | } | |
| 28 | return render_to_response(template, context, context_instance = RequestContext(request)) | |
| 29 | ||
| 30 | def change_ostrov(request, pk): | |
| 31 | if not request.user.is_authenticated(): | |
| 32 | return redirect('/login/?next=%s'%request.META['PATH_INFO'])
| |
| 33 | ImageFormSet = modelformset_factory(model=Image, extra=2, can_delete=True, exclude=('ostrov'))
| |
| 34 | try: | |
| 35 | - | ostrov = Ostrov.ostrovs.get(pk = pk, plaintiff = request.user) |
| 35 | + | ostrov = Ostrov.ostrovs.get(pk = pk, user = request.user) |
| 36 | ostrov_images = ostrov.image_set.all() | |
| 37 | except Ostrov.DoesNotExist: | |
| 38 | raise Http404 | |
| 39 | if request.method == 'POST': | |
| 40 | form = OstrovForm(request.POST, instance=ostrov) | |
| 41 | image_formset = ImageFormSet(request.POST, queryset=ostrov_images) | |
| 42 | if form.is_valid() and image_formset.is_valid(): | |
| 43 | ostrov = form.save(commit=False) | |
| 44 | ostrov.user = request.user | |
| 45 | ostrov.save() | |
| 46 | image_formset.save() | |
| 47 | else: | |
| 48 | form = OstrovForm(instance=ostrov) | |
| 49 | image_formset = ImageFormSet(queryset=ostrov_images) | |
| 50 | ||
| 51 | template = 'ostrovs/add_ostrov.html' | |
| 52 | context = {
| |
| 53 | 'form': form, | |
| 54 | 'image_formset': image_formset, | |
| 55 | } | |
| 56 | return render_to_response(template, context, context_instance = RequestContext(request)) |