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)
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')
def add_photo_to_categorie(modeladmin, request, queryset): count = 0 for photo in queryset: if not Photo.objects.filter(image=photo,categorie=categorie).exists(): Photo(image=photo,categorie=categorie).save() count +=1 if count==1: message_bit = "1 photo was" else: message_bit = "{} photos were".format(count) modeladmin.message_user( request, "{} added to Categorie {}".format(message_bit,categorie) )