Ejemplo n.º 1
0
def upload_image(request, gallery_id):
    """
    Allows a user to upload an image.
    """
    username = request.subdomain
    # Ensure that we cannot upload photos to another's portfolio:
    if username != request.user.username or not request.user.is_authenticated():
        raise Http404
    profile = request.user.get_profile()
    customer = request.user.customer
    error = None
    if request.method == 'POST':
        photo_form = UploadPhotoForm(request.POST, request.FILES)
        item_form = UploadItemForm(request.POST)
        if photo_form.is_valid() and item_form.is_valid():
            image=request.FILES['image']
            if image.size > (customer.max_file_size * 1024 * 1024):
                error = 'Image too large!  Images must be less than ' + str(customer.max_file_size) +  ' MB.'
            else:
                gallery = get_object_or_404(Gallery, pk=gallery_id)
                item = Item(
                    gallery=gallery,
                    caption=item_form.cleaned_data['caption'],
                    is_photo=True,
                    )
                item.save()
                photo = Photo(
                    item=item,
                    image=image,
                    )
                try:
                    photo.save()
                except Exception as e:
                    item.delete()
                    error = 'Cannot upload this image!  Supported file types: JPEG, PNG, BMP.'
                else:
                    # Update the photo count on the user
                    profile.photo_count += 1
                    profile.save()
                    # Update the photo count on the gallery
                    gallery.count += 1
                    # Set this item as the thumbnail if there isn't one already
                    if not gallery.thumbnail_item:
                        gallery.thumbnail_item = item
                    gallery.save()
                    return HttpResponseRedirect('/gallery/' + gallery_id + '/')
    else:
        item_form = UploadItemForm()
        photo_form = UploadPhotoForm()
    variables = RequestContext(request,
                               {'item_form': item_form,
                                'photo_form': photo_form,
                                'video_form': None,
                                'gallery_id': gallery_id,
                                'username': username,
                                'error': error,
                                'customer': customer,
                                'profile': profile})
    return render_to_response('portfolios/upload.html', variables)
Ejemplo n.º 2
0
def upload_video(request, gallery_id):
    """
    Allows a user to upload a new video.
    """
    username = request.subdomain
    # Ensure that we cannot upload photos to another's portfolio:
    if username != request.user.username or not request.user.is_authenticated():
        raise Http404
    profile = request.user.get_profile()
    customer = request.user.customer
    if request.method == 'POST':
        video_form = UploadVideoForm(request.POST)
        if video_form.is_valid():
            gallery = get_object_or_404(Gallery, pk=gallery_id)
            item = Item(
                gallery=gallery,
                caption=video_form.cleaned_data['caption'],
                is_photo=False
                )
            item.save()
            video = Video(
                item=item,
                url=video_form.cleaned_data['url']
                )
            video.save()
            # Update the upload count on the user
            profile.photo_count += 1
            profile.save()
            # Update the count on the gallery
            gallery.count += 1
            # Set this item as the thumbnail if there isn't one already
            if not gallery.thumbnail_item:
                gallery.thumbnail_item = item
            gallery.save()
            return HttpResponseRedirect('/gallery/' + gallery_id + '/')
    else:
        video_form = UploadVideoForm()
    variables = RequestContext(request,
                               {'item_form': None,
                                'photo_form': None,
                                'video_form': video_form,
                                'gallery_id': gallery_id,
                                'username': username,
                                'error': None,
                                'customer': customer,
                                'profile': profile})
    return render_to_response('portfolios/upload.html', variables)
Ejemplo n.º 3
0
def upload_multiple_images(request, gallery_pk):
    """
    Handles multiple image uploads.
    """
    username = request.subdomain
    if username != request.user.username or not request.user.is_authenticated():
        raise Http404
    user = get_object_or_404(User, username=username)
    gallery = get_object_or_404(Gallery, pk=gallery_pk)
    profile = user.get_profile()
    customer = user.customer
    results = {'success': False,}
    if profile.photo_count >= customer.account_limit:
        results = {'success': False,
                   'reason': 'Upload Limit',}
    elif request.method == 'POST':
        if request.FILES:
            image = request.FILES['file']
            if image.size < (customer.max_file_size * 1024 * 1024):
                item = Item(
                    gallery=gallery,
                    is_photo=True,
                    )
                item.save()
                photo = Photo(
                    item=item,
                    image=image,
                    )
                try:
                    photo.save()
                except Exception as e:
                    item.delete()
                else:
                    profile.photo_count = F('photo_count') + 1
                    profile.save()
                    gallery.count = F('count') + 1
                    # Set this item as the thumbnail if there isn't one already
                    if not gallery.thumbnail_item:
                        gallery.thumbnail_item = item
                    gallery.save()
                    results = {'success': True,
                               'name': image.name,
                               'size': image.size,
                               'url': photo.image.url,}
    return HttpResponse(json.dumps(results), mimetype='application/json')