Exemple #1
0
def photoset_edit(request,
                  id,
                  form_class=PhotoSetEditForm,
                  template_name="photos/photo-set/edit.html"):
    from tendenci.core.perms.object_perms import ObjectPermission
    photo_set = get_object_or_404(PhotoSet, id=id)

    # if no permission; permission exception
    if not has_perm(request.user, 'photos.change_photoset', photo_set):
        raise Http403

    if request.method == "POST":
        if request.POST["action"] == "edit":
            form = form_class(request.POST,
                              instance=photo_set,
                              user=request.user)
            if form.is_valid():
                photo_set = form.save(commit=False)

                # update all permissions and save the model
                photo_set = update_perms_and_save(request, form, photo_set)

                # copy all privacy settings from photo set to photos
                Image.objects.filter(photoset=photo_set).update(
                    **get_privacy_settings(photo_set))

                # photo set group permissions
                group_perms = photo_set.perms.filter(
                    group__isnull=False).values_list('group', 'codename')
                group_perms = tuple([(unicode(g), c.split('_')[0])
                                     for g, c in group_perms])

                photos = Image.objects.filter(photoset=photo_set)
                for photo in photos:
                    ObjectPermission.objects.remove_all(photo)
                    ObjectPermission.objects.assign_group(group_perms, photo)

                messages.add_message(request, messages.SUCCESS,
                                     _("Successfully updated photo set! "))

                return HttpResponseRedirect(
                    reverse('photoset_details', args=[photo_set.id]))
    else:
        form = form_class(instance=photo_set, user=request.user)

    return render_to_response(template_name, {
        'photo_set': photo_set,
        "photoset_form": form,
    },
                              context_instance=RequestContext(request))
Exemple #2
0
def photoset_edit(request, id, form_class=PhotoSetEditForm, template_name="photos/photo-set/edit.html"):
    from tendenci.core.perms.object_perms import ObjectPermission
    photo_set = get_object_or_404(PhotoSet, id=id)

    # if no permission; permission exception
    if not has_perm(request.user,'photos.change_photoset',photo_set):
        raise Http403

    if request.method == "POST":
        if request.POST["action"] == "edit":
            form = form_class(request.POST, instance=photo_set, user=request.user)
            if form.is_valid():
                photo_set = form.save(commit=False)

                # update all permissions and save the model
                photo_set = update_perms_and_save(request, form, photo_set)

                # copy all privacy settings from photo set to photos
                Image.objects.filter(photoset=photo_set).update(**get_privacy_settings(photo_set))

                # photo set group permissions
                group_perms = photo_set.perms.filter(group__isnull=False).values_list('group','codename')
                group_perms = tuple([(unicode(g), c.split('_')[0]) for g, c in group_perms ])

                photos = Image.objects.filter(photoset=photo_set)
                for photo in photos:
                    ObjectPermission.objects.remove_all(photo)
                    ObjectPermission.objects.assign_group(group_perms, photo)

                messages.add_message(request, messages.INFO, _("Successfully updated photo set! "))
                EventLog.objects.log(**{
                    'event_id': 991200,
                    'event_data': '%s (%d) edited by %s' % (photo_set._meta.object_name, photo_set.pk, request.user),
                    'description': '%s edited' % photo_set._meta.object_name,
                    'user': request.user,
                    'request': request,
                    'instance': photo_set,
                })

                return HttpResponseRedirect(reverse('photoset_details', args=[photo_set.id]))
    else:
        form = form_class(instance=photo_set, user=request.user)

    return render_to_response(template_name, {
        'photo_set': photo_set,
        "photoset_form": form,
    }, context_instance=RequestContext(request))
Exemple #3
0
def photos_batch_add(request, photoset_id=0):
    """
    params: request, photoset_id
    returns: HttpResponse

    on flash request:
        photoset_id is passed via request.POST
        and received as type unicode; i convert to type integer
    on http request:
        photoset_id is passed via url
    """
    import uuid
    from tendenci.core.perms.object_perms import ObjectPermission

    # photoset permission required to add photos
    if not has_perm(request.user, 'photos.add_photoset'):
        raise Http403

    if request.method == 'POST':
        for field_name in request.FILES:
            uploaded_file = request.FILES[field_name]

            # use file to create title; remove extension
            filename, extension = os.path.splitext(uploaded_file.name)
            request.POST.update({'title': filename, })

            # clean filename; alphanumeric with dashes
            filename = re.sub(r'[^a-zA-Z0-9._]+', '-', filename)

            # truncate; make unique; append extension
            request.FILES[field_name].name = \
                filename[:70] + '-' + unicode(uuid.uuid1())[:5] + extension

            # photoset_id set in swfupload
            photoset_id = int(request.POST["photoset_id"])

            request.POST.update({
                'owner': request.user.id,
                'owner_username': unicode(request.user),
                'creator_username': unicode(request.user),
                'status': True,
                'status_detail': 'active',
            })

            photo_form = PhotoUploadForm(request.POST, request.FILES, user=request.user)

            if photo_form.is_valid():
                # save photo
                photo = photo_form.save(commit=False)
                photo.creator = request.user
                photo.member = request.user
                photo.safetylevel = 3
                photo.allow_anonymous_view = True
                photo.position = 0

                # update all permissions and save the model
                photo = update_perms_and_save(request, photo_form, photo)

                EventLog.objects.log(**{
                    'event_id': 990100,
                    'event_data': '%s (%d) added by %s' % (photo._meta.object_name, photo.pk, request.user),
                    'description': '%s added' % photo._meta.object_name,
                    'user': request.user,
                    'request': request,
                    'instance': photo,
                })

                # add to photo set if photo set is specified
                if photoset_id:
                    photo_set = get_object_or_404(PhotoSet, id=photoset_id)
                    photo_set.image_set.add(photo)

                privacy = get_privacy_settings(photo_set)

                # photo privacy = album privacy
                for k, v in privacy.items():
                    setattr(photo, k, v)

                photo.save()

                # photo group perms = album group perms
                group_perms = photo_set.perms.filter(group__isnull=False).values_list('group', 'codename')
                group_perms = tuple([(unicode(g), c.split('_')[0]) for g, c in group_perms])
                ObjectPermission.objects.assign_group(group_perms, photo)

                # serialize queryset
                data = serializers.serialize("json", Image.objects.filter(id=photo.id))

                cache_image = Popen(["python", "manage.py", "precache_photo", str(photo.pk)])

                # returning a response of "ok" (flash likes this)
                # response is for flash, not humans
                return HttpResponse(data, mimetype="text/plain")
            else:
                return HttpResponse("photo is not valid", mimetype="text/plain")

    else:
        if not photoset_id:
            HttpResponseRedirect(reverse('photoset_latest'))
        photo_set = get_object_or_404(PhotoSet, id=photoset_id)
        # current limit for photo set images is hard coded to 50
        image_slot_left = 150 - photo_set.image_set.count()

        # show the upload UI
        return render_to_response('photos/batch-add.html', {
            "photoset_id": photoset_id,
            "photo_set": photo_set,
            "csrf_token": csrf_get_token(request),
            "image_slot_left": image_slot_left,
             },
            context_instance=RequestContext(request))
Exemple #4
0
def photos_batch_add(request, photoset_id=0):
    """
    params: request, photoset_id
    returns: HttpResponse

    on flash request:
        photoset_id is passed via request.POST
        and received as type unicode; i convert to type integer
    on http request:
        photoset_id is passed via url
    """
    import uuid
    from django.db.models import Max
    from tendenci.core.perms.object_perms import ObjectPermission

    # photoset permission required to add photos
    if not has_perm(request.user, 'photos.add_photoset'):
        raise Http403

    if request.method == 'POST':
        for field_name in request.FILES:
            uploaded_file = request.FILES[field_name]

            # use file to create title; remove extension
            filename, extension = os.path.splitext(uploaded_file.name)
            request.POST.update({
                'title': filename,
            })

            # clean filename; alphanumeric with dashes
            filename = re.sub(r'[^a-zA-Z0-9._]+', '-', filename)

            # truncate; make unique; append extension
            request.FILES[field_name].name = \
                filename[:70] + '-' + unicode(uuid.uuid1())[:5] + extension

            # photoset_id set in swfupload
            photoset_id = int(request.POST["photoset_id"])

            request.POST.update({
                'owner': request.user.id,
                'owner_username': unicode(request.user),
                'creator_username': unicode(request.user),
                'status': True,
                'status_detail': 'active',
            })

            photo_form = PhotoUploadForm(request.POST,
                                         request.FILES,
                                         user=request.user)

            if photo_form.is_valid():
                # save photo
                photo = photo_form.save(commit=False)
                photo.creator = request.user
                photo.member = request.user
                photo.safetylevel = 3
                photo.allow_anonymous_view = True

                position_max = Image.objects.filter(
                    photoset=photoset_id).aggregate(
                        Max('position'))['position__max'] or 0

                photo.position = position_max + 1

                # update all permissions and save the model
                photo = update_perms_and_save(request, photo_form, photo)

                EventLog.objects.log(
                    **{
                        'event_id':
                        990100,
                        'event_data':
                        '%s (%d) added by %s' %
                        (photo._meta.object_name, photo.pk, request.user),
                        'description':
                        '%s added' % photo._meta.object_name,
                        'user':
                        request.user,
                        'request':
                        request,
                        'instance':
                        photo,
                    })

                # add to photo set if photo set is specified
                if photoset_id:
                    photo_set = get_object_or_404(PhotoSet, id=photoset_id)
                    photo_set.image_set.add(photo)

                privacy = get_privacy_settings(photo_set)

                # photo privacy = album privacy
                for k, v in privacy.items():
                    setattr(photo, k, v)

                photo.save()

                # photo group perms = album group perms
                group_perms = photo_set.perms.filter(
                    group__isnull=False).values_list('group', 'codename')
                group_perms = tuple([(unicode(g), c.split('_')[0])
                                     for g, c in group_perms])
                ObjectPermission.objects.assign_group(group_perms, photo)

                # serialize queryset
                data = serializers.serialize("json",
                                             Image.objects.filter(id=photo.id))

                cache_image = Popen(
                    ["python", "manage.py", "precache_photo",
                     str(photo.pk)])

                # returning a response of "ok" (flash likes this)
                # response is for flash, not humans
                return HttpResponse(data, mimetype="text/plain")
            else:
                return HttpResponse("photo is not valid",
                                    mimetype="text/plain")

    else:
        if not photoset_id:
            HttpResponseRedirect(reverse('photoset_latest'))
        photo_set = get_object_or_404(PhotoSet, id=photoset_id)
        # current limit for photo set images is hard coded to 50
        image_slot_left = 150 - photo_set.image_set.count()

        # show the upload UI
        return render_to_response('photos/batch-add.html', {
            "photoset_id": photoset_id,
            "photo_set": photo_set,
            "csrf_token": csrf_get_token(request),
            "image_slot_left": image_slot_left,
        },
                                  context_instance=RequestContext(request))