def upload_doc(): if 'doc' not in request.files: # flash('no doc part in request') session.close() print('no doc part in request') return redirect(request.url) photo = request.files['doc'] obj = upload_file(photo, 'DOC') if obj['status'] == 'BAD REQUEST': print(obj['message']) session.close() return obj if obj['status'] == 'OK': print(obj['message']) filename = obj['filename'] info = UploadedFile(filename=filename, file_type='DOC') session.add(info) try: session.commit() except: sesssion.rollback() flash(config.UNEXPECTED_ERROR) session.close() return redirect('/dashboard') session.close() return { 'status':'ERROR', 'message':'UNPREDICTED ERROR OCCURRED' }
def handle_uploaded_file(f, user): username = user.username os.system('mkdir %(user)s'%{'user':username}) destination = open('%(user)s/%(name)s'%{'user':username, 'name':f.name},'wb+') for chunk in f.chunks(): destination.write(chunk) destination.close() for old in UploadedFile.objects.filter(name = f.name): old.delete() uFile = UploadedFile(name = f.name, username = username, local_path = '%(user)s/%(name)s'%{'user':username, 'name':f.name}) uFile.save() #prof = Profile.objects.get(user = user) acc = Account.objects.get(user = user) acc.files.add(uFile) acc.save() return uFile
def upload_image(): if 'photo' not in request.files: # flash('no photo part in request') print('no photo part in request') session.close() return redirect(request.url) photo = request.files['photo'] obj = upload_file(photo, 'IMG') if obj['status'] == 'BAD REQUEST': print(obj['message']) return obj if obj['status'] == 'OK': print(obj['message']) filename = obj['filename'] info = UploadedFile(filename=filename, file_type='IMAGE') session.add(info) session.commit() session.close() return redirect('/dashboard') # return { # 'status':'OK', # 'message':'SUCCESSFULLY ADDED IMAGE' # }, 200 session.close() return { 'status':'ERROR', 'message':'UNPREDICTED ERROR OCCURRED' }
def test_transmute(self): doctype = DocumentType.objects.create(name='transmuted') doc = Document( document_type = doctype, ) doc.save() uf = UploadedFile.transmute_document( doc, uploader=self.test_user, ) uf.save() self.assertEqual(uf.document_ptr_id, doc.id) self.assertEqual(uf.document_ptr, doc) self.assertEqual(uf.document_type, doc.document_type) self.assertEqual(doc.uploadedfile, uf) self.assertRaises(ValueError, UploadedFile.transmute_document, doc, uploader=self.test_user)
def upload_doc(): if request.method == 'POST': if 'doc' not in request.files: # flash('no doc part in request') print('no doc part in request') return redirect(request.url) photo = request.files['doc'] obj = upload_file(photo, 'DOC') if obj['status'] == 'BAD REQUEST': print(obj['message']) return obj if obj['status'] == 'OK': print(obj['message']) filename = obj['filename'] info = UploadedFile(filename=filename, event_id=request.data['event_id']) session.add(info) session.commit() return {'status': 'OK', 'message': 'SUCCESSFULLY ADDED DOC'}, 200 return {'status': 'ERROR', 'message': 'UNPREDICTED ERROR OCCURRED'} else: return render_template('upload-file.html', filetype='doc', url=request.url)
def test(self): filename = 'up-' + rand_string() contents = 'upladded contestsn' a = UploadedFile( uploader = self.test_user, ) a.uploaded_file.save(filename, ContentFile(contents)) a.clean() a.save() # TODO self.assertEqual(a.url,?) # TODO self.assertEqual(a.path,?) self.assertEqual(a.specific_instance(), a) self.assertEqual(a.document_ptr.__class__, Document) self.assertNotEqual(a.document_ptr.__class__, UploadedFile) self.assertEqual(a.document_ptr.specific_instance(), a) self.assertEqual(a.document_ptr.specific_instance().__class__, UploadedFile)
def add_document(request, documentable_id): obj = Documentable.objects.get(id=documentable_id).specific_instance() # even though only one subclass of Document is listed here, this view is # written with adding new ones in mind form_classes = { 'document': NewDocumentForm, 'uploaded_file': NewUploadedFileForm, } form_kwargs = {} for name in form_classes.keys(): form_kwargs[name] = { 'prefix': name, } forms = {} if request.method == 'POST': # we have to instantiate the 'document' form in order to know which other # forms to bind form_kwargs['document']['data'] = request.POST forms['document'] = form_classes['document'](**form_kwargs['document']) # we only want to bind the forms the user acutally filled in if forms['document'].is_valid(): is_upload = forms['document'].cleaned_data['is_uploadedfile'] if is_upload: form_kwargs['uploaded_file']['data'] = request.POST form_kwargs['uploaded_file']['files'] = request.FILES # now instantiate the forms besides the 'document' form for name in filter(lambda fn: fn != 'document', form_classes.keys()): forms[name] = form_classes[name](**form_kwargs[name]) if forms['document'].is_valid() and (not is_upload or (is_upload and forms['uploaded_file'].is_valid())): document = forms['document'].save(commit=False) document.attached_to = obj document.save() forms['document'].save_m2m() if is_upload: uploaded_file = UploadedFile.transmute_document(document, **forms['uploaded_file'].cleaned_data) uploaded_file.uploader = request.user uploaded_file.clean() uploaded_file.save() return redirect(obj) else: # request.method != 'POST' for name in form_classes.keys(): forms[name] = form_classes[name](**form_kwargs[name]) # the default template uses the radiohider script with forms['models'] # select widget. TODO currently no way to pass in a custom Media to go with # a custom template. template_media = Media( js= (settings.JQUERY_FILE, 'checkboxhider.js'), ) media = reduce( lambda m, f: m + f.media, forms.values(), template_media) return render_to_response( 'documents/add_attachment.html', { 'object': obj, 'forms': forms, 'media': media, }, context_instance= RequestContext(request), )