示例#1
0
def edit_gallery(request, slug):
    gallery = get_object_or_404(MediaGallery, pk=slug)
    gallery_url = gallery.get_absolute_url()
    password = getattr(gallery.password, 'password', '')
    form = GalleryForm(request.POST or None, edit=True, instance=gallery, initial={'password': password})
    if form.is_valid():
        instance = form.save(commit=False)
        pwd = instance.password
        # Makes sure to only update the password if its required and
        # sets it to "Active".
        if instance.lock and pwd is not None:
            pwd.password = form.cleaned_data['password']
            pwd.active = True
            pwd.save()
        # Check if lock is true and if there's not a password associated with this gallery.
        # if so then create it.
        elif instance.lock and not pwd:
            new = create_pwd(path=gallery_url, password=form.cleaned_data['password'])
            instance.password = new
        # Mark the password object associated with this gallery as "Inactive".
        elif not instance.lock and pwd:
            pwd.active = False
            pwd.save()
        instance.save()
        messages.success(request, 'Gallery has been edited succesfully.')
    return render_to_response('media-gallery/edit.html', 
        {'form': form, 'gallery': gallery}, 
        context_instance=RequestContext(request))
示例#2
0
def create_gallery(request):
    form = GalleryForm(request.POST or None)
    if form.is_valid():
        instance = form.save(commit=False)
        instance.save()
        signals.media_gallery_created.send(sender=None, gallery=instance)
        messages.success(request, 'Your gallery has been succesfully created. Now add images to it :)')
        return HttpResponseRedirect(reverse('gallery-upload-images', args=[instance.pk]))
    return render_to_response('media-gallery/add.html', {'form': form}, context_instance=RequestContext(request))