Exemple #1
0
def add_new_action(request, experience_id):
    """
    Handle the file upload for a new action
    :param request:
    :param exp_id: The experience id linked to the action
    :return:
    """
    user = get_object_or_404(User, id=request.user.id)
    exp = get_object_or_404(Experience, id=experience_id)
    action = Action(experience=exp, date_performed=datetime.now().strftime("%Y-%m-%d %H:%M"))
    if request.method == "POST":
        image_form = UploadImageForm(request.POST, request.FILES)
        comment_form = UploadCommentForm(request.POST)
        if not image_form.is_valid() and not comment_form.is_valid():
            return render(request, 'webinator/error.html', {'error_id': "2"})
        if image_form.is_valid():
            image = image_form.save()
            action.photo = image
        if comment_form.is_valid():
            comment = comment_form.save()
            action.comment = comment

        action.save()
        #return HttpResponseRedirect(reverse('action_list', args=[experience_id]))
        return HttpResponseRedirect(reverse('index'))

    else:
        return render(request, 'webinator/new_action.html', {
            'image_form': UploadImageForm(),
            'comment_form': UploadCommentForm()
        }
        )
Exemple #2
0
def save_experience_data(experience, my_experience, user, user_created):
    """
    Save all the actions into the database. Return a json and a status code
    :rtype : application/json, int
    :param experience: JSON that contains all the actions
    :param my_experience: Experience's instance
    :param user: User's instance
    :param user_created: True/False. is CustomUser instance created now?
    """
    for exp in experience:
        action = Action()
        action.date_performed = exp['date']
        action.experience = my_experience
        if exp.get('type', '') == 'scan' and exp.get('id'):
            try:
                item = Item.objects.get(id=exp['id'])
            except Item.DoesNotExist:
                my_experience.delete()
                if user_created:
                    user.delete()
                return {
                           "status": "error",
                           "error": "Item does not exist"
                }, 404
            scan = Scan.objects.create(content=item)
            action.scan = scan
        if not exp.get('photo', '') == '':
            name, content = save_image(exp['photo'])
            photo = Photo()
            photo.content.save(name, content)
            action.photo = photo
        if not exp.get('text', '') == '':
            comment = Comment.objects.create(content=exp['text'])
            action.comment = comment
        action.save()
    return {
               "status": "saved",
               "error": ""
    }, 200