Exemplo n.º 1
0
def upload(request):
    # Handle file upload
    if request.method == 'POST':
        form = MealForm(request.POST, request.FILES)
        if form.is_valid():
                        
            this_meal = Meal(title        = form.cleaned_data['title'],
                        image        = request.FILES['image'],
                        slug         = random_id(),
                        instructions = form.cleaned_data['instructions'],
                        time_period  = form.cleaned_data['time_period'],
                        serves       = form.cleaned_data['serves'],
                        uploader     = 'C',
                        )

            this_meal.save()

            
            all_ingredients = str(form.cleaned_data['ingredients']).split('\n')

            for line in all_ingredients:
                if len(line) > 2: # if line is not empty and later than or equal to a SPACE in ASCII
                    ingredient = Ingredient(name = line, meal = this_meal)
                    ingredient.save()
    

            # Redirect to the document list after POST
            return HttpResponseRedirect(reverse('meal.views.upload'))
    else:
        form = MealForm() # A empty, unbound form

    # Render list page with the documents and the form
    return render_to_response('upload.html', {'form': form}, context_instance=RequestContext(request))
Exemplo n.º 2
0
def upload(request):
    # Handle file upload
    if request.method == 'POST':
        form = MealForm(request.POST, request.FILES)
        if form.is_valid():
 
            this_meal = Meal(title        = form.cleaned_data['title'],
                             image       = request.FILES['image'],
                             #image        = request.FILES['image'].resize(250, Image.ANTIALIAS),
                             slug         = random_id(8),
                             instructions = form.cleaned_data['instructions'],
                             time_period  = form.cleaned_data['time_period'],
                             serves       = form.cleaned_data['serves'],
                             uploader     = 'C',
                             )

                ######################################################################################
                        #views.py

            try:
                im = handle_uploaded_image(this_meal.image)
                this_meal.image.save(im[0],im[1])
            except KeyError:
                this_meal.save()
            ######################################################################################
            # save the meal then save the image
            this_meal.save()
            #this_meal.image.save(compress(this_meal.image), "JPEG")

            all_ingredients = str(form.cleaned_data['ingredients']).split('\n')

            for line in all_ingredients:
                if len(line) > 2: # if line is not empty and later than or equal to a SPACE in ASCII
                    ingredient = Ingredient(name = line, meal = this_meal)
                    ingredient.save()
    

            # Redirect to the document list after POST
            return HttpResponseRedirect(reverse('meal.views.the_feed'))
    else:
        form = MealForm() # A empty, unbound form

    # Render list page with the documents and the form
    return render_to_response('upload.html', {'form': form}, context_instance=RequestContext(request))