Ejemplo n.º 1
0
def upload_file(request):
    """ upload a file. This function does something weird: it gets the session id from the
    POST variables. This is weird but... as far as we know it's not too bad as we only need
    the user login """

    logger.info("start uploading file")
    engine = __import__(settings.SESSION_ENGINE, {}, {}, [''])  # get the current session engine
    session_data = engine.SessionStore(request.POST.get('sessionid', ''))
    try:
        user_id = session_data['_auth_user_id']
        logger.info("\tuser id %s", str(user_id))
    except KeyError:
        logger.warning("failed to get user id from session")
        return HttpResponseBadRequest("You're not logged in. Log in and try again.")
    try:
        request.user = User.objects.get(id=user_id)
        logger.info("\tfound user: %s", request.user.username)
    except User.DoesNotExist:
        logger.warning("user with this id does not exist")
        return HttpResponseBadRequest("user with this ID does not exist.")

    if request.method == 'POST':
        form = UploadFileForm(request.POST, request.FILES)
        if form.is_valid():
            logger.info("\tform data is valid")
            if handle_uploaded_file(user_id, request.FILES["file"]):
                return HttpResponse("File uploaded OK")
            else:
                return HttpResponseServerError("Error in file upload")
        else:
            logger.warning("form data is invalid: %s", str(form.errors))
            return HttpResponseBadRequest("Form is not valid.")
    else:
        logger.warning("no data in post")
        return HttpResponseBadRequest("No POST data in request")
Ejemplo n.º 2
0
def upload(request, no_flash = False):
    form = UploadFileForm()
    success = False
    error = False
    if no_flash:
        if request.method == 'POST':
            form = UploadFileForm(request.POST, request.FILES)
            if form.is_valid():
                if handle_uploaded_file(request.user.id, request.FILES["file"]):
                    uploaded_file=request.FILES["file"]
                    success = True
                else:
                    error = True
    return render_to_response('accounts/upload.html', locals(), context_instance=RequestContext(request))
Ejemplo n.º 3
0
def loadfile(request):
    if request.method == 'POST':
        form = UploadFileForm(request.POST, request.FILES)
        if form.is_valid():
            adminuser = User.objects.get(id=1)
            newuser = UserProfile()
            newuser.avatar = form.cleaned_data['userpic']
            newuser.user_id = adminuser.id
            newuser.accepted_eula = True
            newuser.favorite_animal = '*****@*****.**'
            newuser.save()
            return HttpResponse('ok!')
        else:
            return HttpResponse('parameters is wronngs!')
Ejemplo n.º 4
0
def upload(request, no_flash=False):
    form = UploadFileForm()
    success = False
    error = False
    uploaded_file = None
    if no_flash:
        if request.method == "POST":
            form = UploadFileForm(request.POST, request.FILES)
            if form.is_valid():
                if handle_uploaded_file(request.user.id, request.FILES["file"]):
                    uploaded_file = request.FILES["file"]
                    success = True
                else:
                    error = True
    tvars = {"form": form, "uploaded_file": uploaded_file, "success": success, "error": error, "no_flash": no_flash}
    return render(request, "accounts/upload.html", tvars)
Ejemplo n.º 5
0
def upload(request, no_flash=False):
    form = UploadFileForm()
    success = False
    error = False
    uploaded_file = None
    if no_flash:
        if request.method == 'POST':
            form = UploadFileForm(request.POST, request.FILES)
            if form.is_valid():
                if handle_uploaded_file(request.user.id, request.FILES["file"]):
                    uploaded_file = request.FILES["file"]
                    success = True
                else:
                    error = True
    tvars = {
        'form': form,
        'uploaded_file': uploaded_file,
        'success': success,
        'error': error,
        'no_flash': no_flash,
    }
    return render(request, 'accounts/upload.html', tvars)
Ejemplo n.º 6
0
def upload(request, no_flash=False):
    form = UploadFileForm()
    successes = 0
    errors = []
    uploaded_file = None
    if no_flash:
        if request.method == 'POST':
            form = UploadFileForm(request.POST, request.FILES)
            if form.is_valid():
                submitted_files = request.FILES.getlist('files')
                for file_ in submitted_files:
                    if handle_uploaded_file(request.user.id, file_):
                        uploaded_file = file_
                        successes += 1
                    else:
                        errors.append(file_)
    tvars = {
        'form': form,
        'uploaded_file': uploaded_file,
        'successes': successes,
        'errors': errors,
        'no_flash': no_flash,
    }
    return render(request, 'accounts/upload.html', tvars)
Ejemplo n.º 7
0
def upload(request, no_flash=False):
    form = UploadFileForm()
    successes = 0
    errors = []
    uploaded_file = None
    if no_flash:
        if request.method == 'POST':
            form = UploadFileForm(request.POST, request.FILES)
            if form.is_valid():
                submitted_files = request.FILES.getlist('files')
                for file_ in submitted_files:
                    if handle_uploaded_file(request.user.id, file_):
                        uploaded_file = file_
                        successes += 1
                    else:
                        errors.append(file_)
    tvars = {
        'form': form,
        'uploaded_file': uploaded_file,
        'successes': successes,
        'errors': errors,
        'no_flash': no_flash,
    }
    return render(request, 'accounts/upload.html', tvars)
Ejemplo n.º 8
0
def edit_profile(request, username):
    '''
    Allow user to edit their details as well as upload a new profile image
    '''
    username = User.objects.get(username=request.user.username)
    if request.method == 'POST':
        edit_form = UserChangeForm(request.POST, instance=request.user)
        upload_img_form = UploadFileForm(request.POST,
                                         request.FILES,
                                         instance=request.user.userprofile)
        if edit_form.is_valid() and upload_img_form.is_valid():
            upload_img_form.save()
            edit_form.save()
            messages.success(
                request, 'Your profile has been successfully \
                              updated!')
            return redirect('profile', username)
    else:
        edit_form = UserChangeForm(instance=request.user)
        upload_img_form = UploadFileForm(instance=request.user.userprofile)

    context = {'edit_form': edit_form, 'upload_img_form': upload_img_form}

    return render(request, 'edit_profile.html', context)
Ejemplo n.º 9
0
def sentiment(request,primary_id):
	if request.method == 'POST':
		form = UploadFileForm(request.POST or None, request.FILES or None)
		get_id = primary_id
		if form.is_valid():
			content = request.FILES['file']
			if content.multiple_chunks():
				print("Uploaded file is too big (%.2f MB)" % (content.size/(1000*1000),))
			
			file_data = content.read()
			instance = form.save(commit=False)
			instance.user_id = get_id
			instance.content = file_data
			instance.save()
			return redirect('/accounts')
	else:
		form = UploadFileForm()
		context = {
			'upload_form':form
		}
		return render(request,'accounts/sentiment_form.html',context)
Ejemplo n.º 10
0
def loadfileHander(request):
    template = loader.get_template('accounts/file.html')
    content = {'form': UploadFileForm()}
    return HttpResponse(template.render(content, request))