def add_view(request, model="photos"): if not request.user.is_authenticated(): return redirect('/accounts/login') if model == 'photos': form = PhotoForm(request.user, request.POST, request.FILES) elif model == 'album': form = AlbumForm(request.user, request.POST, request.FILES) else: raise ValueError("Expected 'photos' or 'album'. got " + model) if request.method == "POST": if form.is_valid(): the_model = form.save() if model == "photos": faces = get_faces(str(the_model.image)) for f in faces: the_model.faces.add(f) return render(request, 'photo.html', context={ 'photo': the_model }) elif model == "album": return render(request, 'album.html', context={ 'album': the_model }) return render(request, 'add.html', context={ 'form': form, 'model': model })
def edit_view(request, model="photos", model_id=0): if not request.user.is_authenticated(): return redirect('/accounts/login') if model == "photos": the_model = Photos.objects.get(pk=model_id) form = PhotoForm(request.user, request.POST, instance=the_model) elif model == "album": the_model = Album.objects.get(pk=model_id) form = AlbumForm(request.user, request.POST, instance=the_model) else: raise ValueError("Expected 'photos' or 'album'/ got " + model) if request.user != the_model.user: return render(request, 'error.html', context={ "error_type": "403 forbidden" }) if request.method == "POST": print "here" if form.is_valid(): print "validated" if model == "photos": the_model = form.save() faces = get_faces(str(the_model.image)) the_model.faces.clear() for f in faces: the_model.faces.add(f) return redirect('photo_page', photo_id=the_model.id) elif model == "album": the_model = form.save() return redirect('album_page', album_id=the_model.id) return render(request, 'edit.html', context={ 'form': form, 'item': the_model })