Esempio n. 1
0
    def test_form_can_be_validated(self):
        form = FileForm(data={
            'name': 'Demo Name',
            'desc': 'This is a demo description',
        },
                        files={'file': get_test_file()})

        self.assertTrue(form.is_valid())
Esempio n. 2
0
def create_file(request):
    '''
    :param request:
    :return:
    '''
    form = FileForm(request.POST or None, request.FILES or None)
    if form.is_valid():
        collection_name = create_collection(request, form)
        instance = form.save(commit=False)
        instance.collection = collection_name
        instance.save()
        return redirect(reverse('files:list'))
    else:
        return render(request, 'files/create.html', {'form': form})
Esempio n. 3
0
    def test_form_can_save_data_in_db(self):
        user_obj = get_user_obj_by_name('Zlatan Ibrahimovic')
        form = FileForm(data={
            'name': 'Demo Name',
            'desc': 'This is a demo description',
        },
                        files={'file': get_test_file()})
        if form.is_valid():
            instance = form.save(user_obj)

        file_obj = File.objects.first()
        self.assertEqual(file_obj.name, instance.name)
        self.assertEqual(file_obj.desc, instance.desc)
        self.assertEqual(file_obj.user.username, instance.user.username)
        self.assertEqual(file_obj.file, instance.file)
Esempio n. 4
0
def swfupload(request):
    """
    Handles swfupload.
    Saves file in session.
    File is coupled with object on post_save signal.
    """
    import re
    from django.contrib.contenttypes.models import ContentType

    if request.method == "POST":

        form = FileForm(request.POST, request.FILES, user=request.user)

        if not form.is_valid():
            return HttpResponseServerError(
                str(form._errors), mimetype="text/plain")

        app_label = request.POST['storme_app_label']
        model = unicode(request.POST['storme_model']).lower()
        object_id = int(request.POST['storme_instance_id'])

        if object_id == 'undefined':
            object_id = 0

        try:
            file = form.save(commit=False)
            file.name = re.sub(r'[^a-zA-Z0-9._]+', '-', file.file.name)
            file.object_id = object_id
            file.content_type = ContentType.objects.get(app_label=app_label, model=model)
            file.owner = request.user
            file.creator = request.user
            file.is_public = True
            file.allow_anonymous_view = True
            file.save()
        except Exception, e:
            print e

        d = {
            "id" : file.id,
            "name" : file.name,
            "url" : file.file.url,
        }

        return HttpResponse(json.dumps([d]), mimetype="text/plain")
Esempio n. 5
0
def update_file(request, id):
    '''
    :param request:
    :return:
    '''
    instance = get_object_or_404(File, id=id)
    form = FileForm(request.POST or None,
                    request.FILES or None,
                    instance=instance)
    if form.is_valid():
        collection_name = update_collection(request,
                                            form,
                                            pre_instance=instance)
        instance = form.save(commit=False)
        instance.collection = collection_name
        instance.save()
        return redirect(reverse('files:list'))
    else:
        return render(request, 'files/update.html', {'form': form})
Esempio n. 6
0
def list_file(request):
    '''
    :param request:
    :return:a response
    '''

    report_form = ReportForm(request.POST or None)
    file_form = FileForm(request.POST or None, request.FILES or None)

    if file_form.is_valid():
        collection_name = create_collection(request, file_form)
        instance = file_form.save(commit=False)
        instance.collection = collection_name
        instance.save()
        collection_obj = get_collection(request, collection_name)
        field_list, _ = get_fields_and_data_from_collection(collection_obj)
        field_instance_list = []
        for turn in field_list:
            field_instance_list.append(
                Field.objects.create(name=turn[0], type=turn[1],
                                     file=instance))
        print(Field.objects.all())
        return redirect(reverse('files:list'))

    elif report_form.is_valid():
        instance = report_form.save()
        narrative_instance = Narrative.objects.create(report=instance)
        narrative_instance.segments.create(index=0)
        print(instance.name, instance.file)
        return redirect(reverse('reports:editor', kwargs={'id': instance.id}))

    else:
        file_qs = File.objects.all()
        collection_list = []
        for turn in file_qs:
            collection_list.append(get_summary_of_collection(request, turn))
        context = {
            'collection_list': collection_list,
            'file_form': file_form,
            'report_form': report_form,
        }
        return render(request, 'files/list.html', context)
Esempio n. 7
0
    def test_form_can_update_file_info(self):
        user_obj = get_user_obj_by_name('Zlatan Ibrahimovic')
        file_obj = File.objects.create(
            name='test_bank_revenue.csv',
            desc='Data of test banks revenue',
            file=get_test_file(),
            user=user_obj,
        )
        form = FileForm(instance=file_obj,
                        data={
                            'name': 'Demo Name',
                            'desc': 'This is a demo description',
                        },
                        files={'file': get_test_file()})
        if form.is_valid():
            instance = form.save(user_obj)

        self.assertEqual(File.objects.count(), 1)
        file_obj = File.objects.first()
        self.assertEqual(file_obj.name, instance.name)
        self.assertEqual(file_obj.desc, instance.desc)
        self.assertEqual(file_obj.file, instance.file)
Esempio n. 8
0
def add(request, app_label, model_name, id):
    ''' Tag a file to another model object '''

    try:
        ct = ContentType.objects.get(\
                app_label = app_label,
                model = model_name)
        obj = ct.get_object_for_this_type( id=id )

    except:
        return HttpResponseNotFound()

    if request.method == 'POST':
        #request.POST.update( { 'owner':request.user.id, 'object_id':id,
        #        'content_type':ct.id, 'content_obj': obj, } )
        form = FileForm(request.POST, request.FILES)

        if form.is_valid():
            ev = form.save(commit=False)
            ev.owner = request.user
            ev.object_id = obj.id
            ev.content_type = ct
            ev.save()

            try:
                return HttpResponseRedirect(obj.get_absolute_url())

            except:
                return HttpResponseRedirect(reverse('files_all'))

    else:
        form = FileForm()

    context = { 'form':form, 'object':obj, 'content_type':ct, }
    context.update(locals())

    return render_to_response('files/files_add.html', context,\
            context_instance = RequestContext(request))
def file_upload(request):
    context = {}
    form = FileForm()
    if request.method == "POST":
        form = FileForm(request.POST or None, files=request.FILES)
        if form.is_valid():
            file = form.save(commit=False)
            file.save()

            messages.success(request, "Successfully uploaded file.")
            return redirect('files:file_upload')
    context['form'] = form
    return render(request, 'files/upload.html', context)
Esempio n. 10
0
def NewFile(request):
    if request.user.is_authenticated:
        if request.method == "POST":
            fform = FileForm(request.POST , request.FILES)
            if fform.is_valid():
                ff = fform.save(commit=False)
                ff.owner = request.user
                ff.name  = request.FILES['filedata'].name
                ff.size  = request.FILES['filedata'].size
                with open(UPLOAD_DIR+ff.name.encode("hex") , "wb+") as f:
                    for chunk in request.FILES['filedata'].chunks():
                        f.write(chunk)
                ff.save()
                return HttpResponseRedirect('/files')
            else:
                return render(request , 'new_file.html' , {'form' : fform})
        else:
            fform = FileForm()
            return render(request , 'new_file.html' , {'form':fform})
    return HttpResponseRedirect('/files')