Beispiel #1
0
def investing(request):
  # Handle file upload
  file_form = FileForm(request.POST)
  form = DocumentForm(request.POST, request.FILES)
  
  
  if request.method == 'POST' and "uploadFile" in request.POST:
    # Upload
    if form.is_valid():
      newdoc = Document(docfile = request.FILES['docfile'])
      newdoc.save()
  
      # Redirect to the document list after POST
      return HttpResponseRedirect(reverse('dataloader.views.investing'))
  elif request.method == 'POST' and "invest" in request.POST:  
    # Make Investments
    if file_form.is_valid():
      file_name = file_form.cleaned_data['file_input']
      print "### FILE FOR INVESTMENTS: " + file_name
      file_path = settings.MEDIA_ROOT + "/" + file_name
      print file_path
      
      cr = csv.reader(open(file_path))    
      # Starting from second row
      
      for row in cr:
        if row[0] == 'fund' or row[0] == 'individual':
          investment_manager.fund(row)
        elif row[0] == 'buy':
          investment_manager.buy(row)
        elif row[0] == 'sell':
          investment_manager.sell(row)
        elif row[0] == 'sellbuy':
          investment_manager.sellbuy(row)
                        
      
      
      return HttpResponseRedirect(reverse('dataloader.views.investing'))

  elif request.method == 'POST' and "clear" in request.POST:   
    # Clear Investments
    print "### This should clear everything"
    Activity.objects.all().delete()
    StakeHold.objects.all().delete()
    Port_Indi.objects.all().delete()
    print "### OK check porfolio page"
    
    
  else:
      form = DocumentForm() # A empty, unbound form
      file_form = FileForm()

  # Load documents for the list page
  documents = Document.objects.all()

  return render_to_response(
       'nav/investing.html',
       {'documents': documents, 'form': form, 'file_form':file_form},
       context_instance=RequestContext(request)
   )
Beispiel #2
0
def upload_attachment(request, projectID):

    if 'file' not in request.FILES:
        raise IOError("Missing file")
    if request.FILES['file'].size > 10 * 1024 * 1024:
        raise IOError("File too large")

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

    if (form.is_valid()):

        #set up parameters for database insert
        fileObj = request.FILES['file']
        projID = project_api.get_project(projectID)
        fname = fileObj.name
        fileUUID = str(uuid.uuid4())

        #rename file object to have UUID as name to avoid conflicts when retrieving files
        fileObj.name = fileUUID

        f = ProjectFile(file=fileObj,
                        project=projID,
                        name=fname,
                        uuid=fileUUID)
        f.save()
    return redirect(request.POST['referer'])
def upload_attachment(request, projectID):
    
    if 'file' not in request.FILES:
        raise IOError("Missing file")
    if request.FILES['file'].size > 10*1024*1024:
        raise IOError("File too large")

    form = FileForm(request.POST, request.FILES)
    
    if(form.is_valid()):
        
        #set up parameters for database insert
        fileObj = request.FILES['file']
        projID = project_api.get_project(projectID)
        fname = fileObj.name
        fileUUID = str(uuid.uuid4())
        
        #rename file object to have UUID as name to avoid conflicts when retrieving files
        fileObj.name=fileUUID
        
        f = ProjectFile(file=fileObj,
                        project=projID,
                        name=fname,
                        uuid=fileUUID)
        f.save()
    return redirect(request.POST['referer'])
Beispiel #4
0
def upload(req):
    up_files = 'yes'
    if req.method == "POST":
        ff = FileForm(req.POST, req.FILES)
        folder_name = req.session.get('folder_name')
        if ff.is_valid():
            #获取表单数据
            b = ' Byte'
            file_name = ff.cleaned_data['file_name']
            file_loc = ff.cleaned_data['file_loc']
            file_handel = req.FILES['file_loc']
            file_size = str(file_handel.size) + b
            file_type_split1 = str(file_handel.name)
            file_type_split2 = file_type_split1.split('.')
            file_type = file_type_split2[1]
            #写入数据库
            upload_time = timezone.now()
            f = Folder.objects.get(folder_name=folder_name)
            f.modify_time = upload_time
            f.save()
            f.file_set.create(file_name=file_name, file_loc=file_loc, file_size=file_size, file_type=file_type, upload_time=upload_time)
            return HttpResponseRedirect('/online/index/')
    else:
        ff = FileForm()

    return render(req, 'create_folder_new.html', {'ff': ff, 'up_files': up_files})
Beispiel #5
0
def file_edit(request, file_id, response_format='html'):
    "File edit page"

    file = get_object_or_404(File, pk=file_id)
    if not request.user.profile.has_permission(file, mode='w'):
        return user_denied(request, message="You don't have access to this File")

    if request.POST:
        if 'cancel' not in request.POST:
            form = FileForm(
                request.user.profile, None, request.POST, request.FILES, instance=file)
            if form.is_valid():
                file = form.save()
                return HttpResponseRedirect(reverse('documents_file_view', args=[file.id]))
        else:
            return HttpResponseRedirect(reverse('documents_file_view', args=[file.id]))

    else:
        form = FileForm(request.user.profile, None, instance=file)

    context = _get_default_context(request)
    context.update({'form': form,
                    'file': file})

    return render_to_response('documents/file_edit', context,
                              context_instance=RequestContext(request),
                              response_format=response_format)
Beispiel #6
0
def import_bookmark(request, template_name='bookmark/import_bookmark.html'):
    file_form = FileForm(request.POST or None, request.FILES or None)
    if request.method == 'POST' and file_form.is_valid():
        file = request.FILES['file']
        tags_input = file_form.cleaned_data['tags']
        public = file_form.cleaned_data['public']
        try:
            bookmarks = parse_firefox_bookmark(file)
            for bk in bookmarks:
                try:
                    bookmark = Bookmark.objects.create(url=bk['url'],
                                                       title=bk['title'],
                                                       owner=request.user,
                                                       public=public)
                    if tags_input:
                        tags = tags_input
                    else:
                        tags = parse_tags(bk['tags'])
                    for tag in tags:
                        bookmark.tags.add(tag)
                    # Update the screen shot
                    update_bk_screen_shot_async(bookmark)
                except Exception:
                    pass
            return HttpResponseRedirect(reverse("my_bookmark"))
        except Exception, e:
            file_form._errors[NON_FIELD_ERRORS] = ErrorList([e.message])
Beispiel #7
0
def upload(req):
    up_files = 'yes'
    if req.method == "POST":
        ff = FileForm(req.POST, req.FILES)
        folder_name = req.session.get('folder_name')
        if ff.is_valid():
            #获取表单数据
            b = ' Byte'
            file_name = ff.cleaned_data['file_name']
            file_loc = ff.cleaned_data['file_loc']
            file_handel = req.FILES['file_loc']
            file_size = str(file_handel.size) + b
            file_type_split1 = str(file_handel.name)
            file_type_split2 = file_type_split1.split('.')
            file_type = file_type_split2[1]
            #写入数据库
            upload_time = timezone.now()
            f = Folder.objects.get(folder_name=folder_name)
            f.modify_time = upload_time
            f.save()
            f.file_set.create(file_name=file_name,
                              file_loc=file_loc,
                              file_size=file_size,
                              file_type=file_type,
                              upload_time=upload_time)
            return HttpResponseRedirect('/online/index/')
    else:
        ff = FileForm()

    return render(req, 'create_folder_new.html', {
        'ff': ff,
        'up_files': up_files
    })
Beispiel #8
0
def file_upload_typed(request, folder_id=None, response_format='html'):
    "File upload to preselected folder"

    folder = None
    if folder_id:
        folder = get_object_or_404(Folder, pk=folder_id)
        if not request.user.profile.has_permission(folder, mode='x'):
            folder = None

    if request.POST:
        if 'cancel' not in request.POST:
            form = FileForm(
                request.user.profile, folder_id, request.POST, request.FILES)
            if form.is_valid():
                file = form.save()
                file.set_user_from_request(request)
                return HttpResponseRedirect(reverse('documents_file_view', args=[file.id]))
        else:
            return HttpResponseRedirect(reverse('document_index'))
    else:
        form = FileForm(request.user.profile, folder_id)

    context = _get_default_context(request)
    context.update({'form': form,
                    'folder': folder})

    return render_to_response('documents/file_upload_typed', context,
                              context_instance=RequestContext(request),
                              response_format=response_format)
Beispiel #9
0
def upload(request):
    # Handle file upload
    if request.method == 'POST':
        form = FileForm(request.POST, request.FILES)
        if form.is_valid():
            logger.debug('File with type ' +
                         request.FILES['docfile'].content_type + ' uploaded.')
            if request.FILES[
                    'docfile'].content_type == u'application/vnd.oasis.opendocument.text':
                file_name = saveFile(request.FILES['docfile'])
                logger.info('Converting file ' + file_name)
                content = subprocess.check_output([
                    'OdtConverter/odt2html', '-x', 'OdtConverter/odt2html.xsl',
                    'EditorApp/static/media/' + file_name
                ])
                logger.info('Deleting file ' + file_name)
                os.remove('EditorApp/static/media/' + file_name)
                logger.info('Saving imported document ' + file_name)
                create_document(None, file_name, content)
            elif request.FILES['docfile'].content_type[:5] == u'image':
                saveFile(request.FILES['docfile'])
            else:
                form = FileForm()
                form.error = 'Invalid type of document.'
                logger.error('Wrong type of document.')
                return render(request, 'upload.html', {'form': form})
            # Redirect to the document list after POST
            return HttpResponseRedirect('/')
    else:
        form = FileForm()  # A empty, unbound form
    return render(request, 'upload.html', {'form': form})
Beispiel #10
0
def upload_file(request, upload_path=None, upload_link=None):
    form = FileForm(request.POST, request.FILES)
    if form.is_valid():
        uploaded_file = form.cleaned_data['file']
        path = os.path.join(upload_path or UPLOAD_PATH, uploaded_file.name)
        image_path = default_storage.save(path, uploaded_file)
        image_url = default_storage.url(image_path)
        if upload_link:
            return HttpResponse(image_url)
        return HttpResponse(json.dumps({'filelink': image_url, 'filename': uploaded_file.name}))
    return HttpResponseForbidden()
Beispiel #11
0
def update_log(request):
    if request.method == "POST":
        form = FileForm(request.POST, request.FILES)
        if form.is_valid():
            cd = form.cleaned_data
            handle_uploaded_file(cd)
            return HttpResponse("Ok: File %s update success." % cd['name'])
        else:
            return HttpResponseBadRequest("<h1>Need more arguments</h1>")
    else:
        return HttpResponseBadRequest("<h1>Wrong request type</h1>")
Beispiel #12
0
def upload_file(request, upload_path=None, upload_link=None):
    form = FileForm(request.POST, request.FILES)
    if form.is_valid():
        uploaded_file = form.cleaned_data['file']
        path = os.path.join(upload_path or UPLOAD_PATH, uploaded_file.name)
        image_path = default_storage.save(path, uploaded_file)
        image_url = default_storage.url(image_path)
        if upload_link:
            return HttpResponse(image_url)
        return HttpResponse(json.dumps({'filelink': image_url, 'filename': uploaded_file.name}))
    return HttpResponseForbidden()
Beispiel #13
0
def upload_attachment(request, projectID):

    if 'file' not in request.FILES:
        raise IOError("Missing file")
    if request.FILES['file'].size > 1100000:
        raise IOError("File too large")
        
    form = FileForm(request.POST, request.FILES)
    if(form.is_valid()):
        file = request.FILES['file']
        f = ProjectFile(project = project_api.get_project(projectID), file = file, name = file.name)
        f.save()
    return redirect(request.POST['referer'])
Beispiel #14
0
def upload_attachment(request, projectID):

    if 'file' not in request.FILES:
        raise IOError("Missing file")
    if request.FILES['file'].size > 1100000:
        raise IOError("File too large")
        
    form = FileForm(request.POST, request.FILES)
    if(form.is_valid()):
        file = request.FILES['file']
        f = ProjectFile(project = project_api.get_project(projectID), file = file, name = file.name)
        f.save()
    return redirect(request.POST['referer'])
def add_containt(request):
    if request.user.is_authenticated():
        args = {}
        if request.POST:
            try:
                request.POST['file_field']
                file_form = FileForm(request.POST, request.FILES)
                if file_form.is_valid():
                    file_name = request.FILES['file']
                    media_containt = Media_containt(file=file_name)
                    media_containt.save()
                    pdf_file_name = str(file_name).replace(' ', '_')
                    image_file_name = str(file_name).replace(' ',
                                                             '_').split('.')
                    image_file_name[-1] = 'png'
                    image_file_name = '.'.join(image_file_name)
                    cmd = 'gs -o ' + media_files_url + image_file_name + ' -sDEVICE=pngalpha -dLastPage=1 ' + media_files_url + pdf_file_name
                    print cmd
                    subprocess.call(cmd, shell=True)
                    print 'save and redirect'
                    return HttpResponseRedirect('/elab/add/containt/')
                else:
                    containt_form = ContaintForm()
                    args['containt_form'] = containt_form
                    args['file_form'] = file_form
                    print file_form.errors
                    return render_to_response(
                        'html_template/admin_template/admin_pannel.html', args)

            except Exception, e:
                print "Exception-->", e
                containt_form = ContaintForm(request.POST)
                if containt_form.is_valid():
                    containt_form.save()
                    return HttpResponseRedirect('/elab/add/containt/')
                else:
                    file_form = FileForm()
                    args['containt_form'] = containt_form
                    args['file_form'] = file_form
                    print containt_form.errors
                    return render_to_response(
                        'html_template/admin_template/admin_pannel.html', args)

        else:
            containt_form = ContaintForm()
            file_form = FileForm()
            args['containt_form'] = containt_form
            args['file_form'] = file_form
        return render_to_response(
            'html_template/admin_template/admin_pannel.html', args)
Beispiel #16
0
def upload_file(request, upload_path=None, upload_link=None):
    form = FileForm(request.POST, request.FILES)
    if form.is_valid():
        uploaded_file = form.cleaned_data['file']
        file_name, extension = os.path.splitext(uploaded_file.name)
        timestr = time.strftime(u"%Y%m%d-%H%M%S")
        full_name = u'{0}{1}'.format(file_name+timestr, extension)
        path = os.path.join(upload_path or UPLOAD_PATH, full_name)
        image_path = default_storage.save(path, uploaded_file)
        image_url = default_storage.url(image_path)
        if upload_link:
            return HttpResponse(image_url)
        return HttpResponse(json.dumps({'filelink': image_url, 'filename': uploaded_file.name}))
    return HttpResponseForbidden()
Beispiel #17
0
def create(request):
    # return render(request, 'files/create.html', {"foo": "bar"})
    print 'entrou no create'

    if request.method == 'POST':
        form = FileForm(request.POST, request.FILES)
        print 'request is post!'
        print form.errors
        if form.is_valid():
            print 'form is valid!'
            # print 'analysis name should be ', form.cleaned_data['name']

            # file.name = form.cleaned_data['name']

            file = File.objects.create(user=request.user)

            file.file = request.FILES.get('file')
            file.name = str(os.path.splitext(file.file.name)[0])
            file.user = request.user
            file.save()

            f = file.file
            #fix permissions
            #os.chmod("%s/genomes/%s/" % (settings.BASE_DIR, fastafile.user), 0777)

            # os.chmod("%s/uploads/%s/%s" % (settings.BASE_DIR, fastafile.user, fastafile.id), 0777)

            #Align.delay(analysis.id)

            data = {
                'files': [{
                    'deleteType': 'DELETE',
                    'name': file.name,
                    'url': '',
                    'thumbnailUrl': '',
                    'type': 'image/png',
                    'deleteUrl': '',
                    'size': f.size
                }]
            }
            response = JSONResponse(data, mimetype=response_mimetype(request))
            response['Content-Disposition'] = 'inline; filename=files.json'
            return response
    else:

        form = FileForm()

    return render(request, 'files/create.html', {'form': form})
def add_containt(request):
	if request.user.is_authenticated():
		args = {}
		if request.POST:
			try:
				request.POST['file_field']
				file_form = FileForm(request.POST, request.FILES)
				if file_form.is_valid():
					file_name = request.FILES['file']
					media_containt = Media_containt(file=file_name)
					media_containt.save()
					pdf_file_name = str(file_name).replace(' ', '_')
					image_file_name = str(file_name).replace(' ', '_').split('.')
					image_file_name[-1] = 'png'
					image_file_name = '.'.join(image_file_name)
					cmd = 'gs -o '+media_files_url+image_file_name+' -sDEVICE=pngalpha -dLastPage=1 '+media_files_url+pdf_file_name
					print cmd
					subprocess.call(cmd, shell=True)
					print 'save and redirect'
					return HttpResponseRedirect('/elab/add/containt/')
				else:
					containt_form = ContaintForm()
					args['containt_form'] = containt_form
					args['file_form'] = file_form
					print file_form.errors
					return render_to_response('html_template/admin_template/admin_pannel.html', args)

			except Exception, e:
				print "Exception-->", e
				containt_form = ContaintForm(request.POST)
				if containt_form.is_valid():
					containt_form.save()
					return HttpResponseRedirect('/elab/add/containt/')
				else:
					file_form = FileForm()
					args['containt_form'] = containt_form
					args['file_form'] = file_form
					print containt_form.errors
					return render_to_response('html_template/admin_template/admin_pannel.html', args)

		else:
			containt_form = ContaintForm()
			file_form = FileForm()
			args['containt_form'] = containt_form
			args['file_form'] = file_form
		return render_to_response('html_template/admin_template/admin_pannel.html', args)
Beispiel #19
0
def attach_file(request,content_type_id,pk):
    ct = ContentType.objects.get(id=content_type_id)
    klass = ct.model_class()
    obj = klass.objects.get(pk=pk)
    if request.method == 'GET':
        form = FileForm()
    elif request.method == 'POST':
        form = FileForm(request.POST,request.FILES)
        if form.is_valid():
            obj = form.save(commit=False)
            obj.uploaded_by=request.user
            obj.object_id = pk
            obj.content_type = ct
            obj.name = obj.file.name
            obj.save()
            return Response(FileSerializer(obj).data)
        else:
            return Response({'errors':form.errors},status=404)
Beispiel #20
0
def batch_import_bulletin_by_excel(request):
    if request.method == 'POST':
        form = FileForm(request.POST, request.FILES)
        if form.is_valid():
            if request.FILES['filefield'].name.split('.')[-1] == 'xlsx':
                try:
                    utils.handle_uploaded_file(request, f=request.FILES['filefield'])
                except ObjectDoesNotExist, e:
                    error_message = '科室和医生组合不存在,请检查所上传表格内容后重新上传'
                    return render(request, 'publisher/file.html',
                                  {'form': form, 'error_message': error_message,
                                   'loginname': request.session['loginname']})
                return render(request, 'publisher/file.html',
                              {'form': form, 'error_message': '上传成功!',
                               'loginname': request.session['loginname']})
            else:
                error_message = '文件格式错误,请上传Excel文件(.xlsl)'
                form = FileForm()
                return render(request, 'publisher/file.html', {'form': form, 'error_message': error_message, 'loginname': request.session['loginname']})
Beispiel #21
0
def upload_gl(request):
    if request.method != "POST":
        forecast_obj = Forecast.objects.get(id=request.GET.get('forecast'))
        context = {'file_category': 'GL Projections'}
        context['file_type'] = '.json'
        context['obj_label'] = forecast_obj.label
        form = FileForm()
        context['form'] = form

        return render_to_response('forecasts/upload_gl.html', RequestContext(request, context))
    else:
        form = FileForm(request.POST, request.FILES)
        if form.is_valid():
            upload = request.FILES.values()[0]
            file_name = upload._name
            data = json.loads(upload.file.read())
            forecast_obj = Forecast.objects.get(id=request.GET.get('forecast'))
            forecast_obj.projections = data
            forecast_obj.save()
            return HttpResponseRedirect('/forecasts/forecast/%s' % forecast_obj.id)
Beispiel #22
0
def batch_import_bulletin_by_excel(request):
    if request.method == 'POST':
        form = FileForm(request.POST, request.FILES)
        if form.is_valid():
            if request.FILES['filefield'].name.split('.')[-1] == 'xlsx':
                utils.handle_uploaded_file(request,
                                           f=request.FILES['filefield'])
                return render(request, 'publisher/file.html', {
                    'form': form,
                    'error_message': '上传成功!'
                })
            else:
                error_message = '文件格式错误,请上传Excel文件(.xlsl)'
                form = FileForm()
                return render(request, 'publisher/file.html', {
                    'form': form,
                    'error_message': error_message
                })
    else:
        form = FileForm()
        return render(request, 'publisher/file.html', {'form': form})
Beispiel #23
0
def upload_file(request, **config):
    file_type = config['file_type']

    context = {'file_type': file_type,}     
    form = FileForm(request.POST, request.FILES)

    if form.is_valid():
        upload = request.FILES.values()[0]
        file_name = upload._name
        file_name_with_timestamp = save_file(upload)
        company = utils.get_company(request)

        model = config.pop('model')
        unique = config.pop('unique')
        name_cleaner_path = config.pop('name_cleaner')
        value_cleaner_path = config.pop('value_cleaner')
        exclude_path = config.pop('exclude')
        post_process_path = config.pop('post_process')



        #result is zero if it was able to start the background task successfully.
        result, out, err = process_incoming_file(model, unique, name_cleaner, value_cleaner, exclude, post_process,
                                                 file_type=config['file_type'],
                                                 file_name=file_name_with_timestamp,
                                                 company=company,
                                                 task_title="%s data for %s" % (file_type, company),
                                                 task_success_url=reverse('upload_complete'),
                                                 )

        if result != 0:
            context.update({'file_name': file_name, 'success': False, 'out': out, 'err': err})
            messages.error(request, 'Could not process the file provided, please see below')
            return render(request, 'uploaded.html', context)
        else:
            return HttpResponseRedirect(reverse('task_result', kwargs={'pid':out,}))
    else:
        context.update({'file_name': file_name, 'success': False, 'out': out, 'err': err})
        messages.error(request, 'Could not process the file provided, please see below')
        return render(request, 'uploaded.html', context)
Beispiel #24
0
def create(request):
    # return render(request, 'files/create.html', {"foo": "bar"})
    print 'entrou no create'

    if request.method == 'POST':
        form = FileForm(request.POST, request.FILES)
        print 'request is post!'
        print form.errors
        if form.is_valid():
            print 'form is valid!'
            # print 'analysis name should be ', form.cleaned_data['name']

            # file.name = form.cleaned_data['name']

            file = File.objects.create(user=request.user)

            file.file= request.FILES.get('file')
            file.name= str(os.path.splitext(file.file.name)[0])
            file.user = request.user
            file.save()
            
            
            f = file.file
            #fix permissions
            #os.chmod("%s/genomes/%s/" % (settings.BASE_DIR, fastafile.user), 0777)
            
            # os.chmod("%s/uploads/%s/%s" % (settings.BASE_DIR, fastafile.user, fastafile.id), 0777)

            #Align.delay(analysis.id)

            data = {'files': [{'deleteType': 'DELETE', 'name': file.name, 'url': '', 'thumbnailUrl': '', 'type': 'image/png', 'deleteUrl': '', 'size': f.size}]}
            response = JSONResponse(data, mimetype=response_mimetype(request))
            response['Content-Disposition'] = 'inline; filename=files.json'
            return response
    else:
        
        form = FileForm()

    return render(request, 'files/create.html', {'form':form})
Beispiel #25
0
def upload_gl(request):
    if request.method != "POST":
        forecast_obj = Forecast.objects.get(id=request.GET.get('forecast'))
        context = {'file_category': 'GL Projections'}
        context['file_type'] = '.json'
        context['obj_label'] = forecast_obj.label
        form = FileForm()
        context['form'] = form

        return render_to_response('forecasts/upload_gl.html',
                                  RequestContext(request, context))
    else:
        form = FileForm(request.POST, request.FILES)
        if form.is_valid():
            upload = request.FILES.values()[0]
            file_name = upload._name
            data = json.loads(upload.file.read())
            forecast_obj = Forecast.objects.get(id=request.GET.get('forecast'))
            forecast_obj.projections = data
            forecast_obj.save()
            return HttpResponseRedirect('/forecasts/forecast/%s' %
                                        forecast_obj.id)
Beispiel #26
0
def file_upload(request, response_format='html'):
    "New file form"

    if request.POST:
        if 'cancel' not in request.POST:
            file = File()
            form = FileForm(
                request.user.profile, None, request.POST, request.FILES, instance=file)
            if form.is_valid():
                file = form.save()
                file.set_user_from_request(request)
                return HttpResponseRedirect(reverse('documents_file_view', args=[file.id]))
        else:
            return HttpResponseRedirect(reverse('document_index'))
    else:
        form = FileForm(request.user.profile, None)

    context = _get_default_context(request)
    context.update({'form': form})

    return render_to_response('documents/file_upload', context,
                              context_instance=RequestContext(request),
                              response_format=response_format)
Beispiel #27
0
def import_bookmark(request, template_name='bookmark/import_bookmark.html'):
    file_form = FileForm(request.POST or None, request.FILES or None)
    if request.method == 'POST' and file_form.is_valid():
        file = request.FILES['file']
        tags_input = file_form.cleaned_data['tags']
        public =file_form.cleaned_data['public']
        try:
            bookmarks = parse_firefox_bookmark(file)
            for bk in bookmarks:
                try:
                    bookmark = Bookmark.objects.create(url=bk['url'], title=bk['title'], owner=request.user, public=public)
                    if tags_input:
                        tags = tags_input
                    else:
                        tags=parse_tags(bk['tags'])
                    for tag in tags:
                        bookmark.tags.add(tag)
                    # Update the screen shot
                    update_bk_screen_shot_async(bookmark)
                except Exception:
                    pass
            return HttpResponseRedirect(reverse("my_bookmark"))
        except Exception, e:
            file_form._errors[NON_FIELD_ERRORS] = ErrorList([e.message])
Beispiel #28
0
def upload_file(request, **config):
    file_type = config['file_type']

    context = {
        'file_type': file_type,
    }
    form = FileForm(request.POST, request.FILES)

    if form.is_valid():
        upload = request.FILES.values()[0]
        file_name = upload._name
        file_name_with_timestamp = save_file(upload)
        company = utils.get_company(request)

        model = config.pop('model')
        unique = config.pop('unique')
        name_cleaner = config.pop('name_cleaner')
        value_cleaner = config.pop('value_cleaner')
        exclude = config.pop('exclude')
        post_process = config.pop('post_process')

        #result is zero if it was able to start the background task successfully.
        result, out, err = process_incoming_file(
            model,
            unique,
            name_cleaner,
            value_cleaner,
            exclude,
            post_process,
            file_type=config['file_type'],
            file_name=file_name_with_timestamp,
            company=company,
            task_title="%s data for %s" % (file_type, company),
            task_success_url=reverse('upload_complete'),
        )

        if result != 0:
            context.update({
                'file_name': file_name,
                'success': False,
                'out': out,
                'err': err
            })
            messages.error(
                request,
                'Could not process the file provided, please see below')
            return render_to_response('uploaded.html',
                                      context,
                                      context_instance=RequestContext(request))
        else:
            return HttpResponseRedirect(
                reverse('task_result', kwargs={
                    'pid': out,
                }))
    else:
        context.update({
            'file_name': file_name,
            'success': False,
            'out': out,
            'err': err
        })
        messages.error(
            request, 'Could not process the file provided, please see below')
        return render_to_response('uploaded.html',
                                  context,
                                  context_instance=RequestContext(request))
Beispiel #29
0
def file_form(request):
    # if not request.user.is_staff or not request.user.is_superuser:
    #     raise Http404;
    if request.POST:
        form = FileForm(request.POST, request.FILES)
        if form.is_valid():
            instance = form.save(commit=False)
            instance.user = request.user
            instance.save()

            # read CSV file and define parameters
            input_file_name = re.search(r'/(media/.*)', instance.file.url)
            fileID = instance.id
            input_file = vcf.Reader(open(input_file_name.group(1), "r"))
            # output_somatic_file = open('media/somatic_mutation-'+str(fileID)+'.tsv', 'wb')
            # output_germline_file = open('media/germline_mutation-'+str(fileID)+'.tsv', 'wb')
            logs = open('media/logs-'+str(fileID)+'.txt', 'w')
            somatic_mutation_array = []
            germline_mutation_array = []

            # Read vcf file and create seperate arrays for somatic and germline mutation
            for record in input_file:
                chr = re.search(r'chr(.*)', str(record.CHROM))
                start_pos = str(record.POS)
                if record.REF == '-':
                    end_pos = record.POS + 1
                else:
                    end_pos = start_pos
                ref = str(record.REF)
                alt = re.search(r'\[(.*)\]', str(record.ALT))
                oncoTator = 'http://www.broadinstitute.org/oncotator/mutation/' + chr.group(
                    1) + '_' + start_pos + '_' + end_pos + '_' + ref + '_' + alt.group(1)
                try:
                    oncoTator_response = requests.get(oncoTator)
                except ConnectionError as e:
                    print e
                    logs.write(str(e))
                    oncoTator_response = "No response"
                if oncoTator_response != "No response" and oncoTator_response.status_code != 500:
                    mutation = json.loads(oncoTator_response.text)
                    if "ERROR" in mutation:
                        logs.write("OncoTator is giving error for url:" + oncoTator + " Error message: " + str(oncoTator_response.text))
                    else:

                        if mutation["CGC_Cancer Germline Mut"] == "yes":
                            germline_mutation_array.append(mutation)

                        if mutation["CGC_Cancer Somatic Mut"] == "yes":
                            somatic_mutation_array.append(mutation)
            logs.close()

            # write values from arrays to tsv files
            # fieldnames = []
            # for key in somatic_mutation_array[0]:
            #     fieldnames.append(str(key))

            # tsv_somatic_file = csv.DictWriter(output_somatic_file, fieldnames=fieldnames, delimiter='\t')
            # tsv_somatic_file.writeheader()
            for mutation in somatic_mutation_array:
                # tsv_somatic_file.writerow(mutation)
                ssnp = somatic_snp.objects.create(study_name=instance, gene=mutation["gene"], chromosome=mutation["ESP_Chromosome"], start=mutation["start"], end=mutation["end"], ref_allele=mutation["ref_allele"], alt_allele=mutation["alt_allele"], variant_class=mutation["variant_classification"],variant_type=mutation["variant_type"], ensemblID=mutation["HGNC_Ensembl Gene ID"])
            # output_somatic_file.close()

            # tsv_germline_file = csv.DictWriter(output_germline_file, fieldnames=fieldnames, delimiter='\t')
            # tsv_germline_file.writeheader()
            for mutation in germline_mutation_array:
                # tsv_germline_file.writerow(mutation)
                gsnp = germline_snp.objects.create(study_name=instance, gene=mutation["gene"], chromosome=mutation["ESP_Chromosome"], start=mutation["start"], end=mutation["end"], ref_allele=mutation["ref_allele"], alt_allele=mutation["alt_allele"], variant_class=mutation["variant_classification"],variant_type=mutation["variant_type"], ensemblID=mutation["HGNC_Ensembl Gene ID"])
            # output_germline_file.close()

            # fileread = open('media/germline_mutation-'+str(fileID)+'.tsv', 'r')
            # TSVfile = File(fileread)
            # instance.germline_result.save('media/germline_mutation-'+str(fileID)+'.tsv', TSVfile, save=True)
            #
            # fileread = open('media/somatic_mutation-' + str(fileID) + '.tsv', 'r')
            # TSVfile = File(fileread)
            # instance.somatic_result.save('media/somtic_mutation-' + str(fileID) + '.tsv', TSVfile, save=True)

            return HttpResponseRedirect('/result/')
    else:
        form = FileForm()
    return render(request, 'oncotator/snp_tools.html', {'form': form})
Beispiel #30
0
def file_form(request):
    # if not request.user.is_staff or not request.user.is_superuser:
    #     raise Http404;
    if request.POST:
        form = FileForm(request.POST, request.FILES)
        if form.is_valid():
            instance = form.save(commit=False)
            instance.user = request.user
            instance.save()

            # read CSV file and define parameters
            input_file_name = re.search(r'/(media/.*)', instance.file.url)
            fileID = instance.id
            input_file = vcf.Reader(open(input_file_name.group(1), "r"))
            # output_somatic_file = open('media/somatic_mutation-'+str(fileID)+'.tsv', 'wb')
            # output_germline_file = open('media/germline_mutation-'+str(fileID)+'.tsv', 'wb')
            logs = open('media/logs-' + str(fileID) + '.txt', 'w')
            somatic_mutation_array = []
            germline_mutation_array = []

            # Read vcf file and create seperate arrays for somatic and germline mutation
            for record in input_file:
                chr = re.search(r'chr(.*)', str(record.CHROM))
                start_pos = str(record.POS)
                if record.REF == '-':
                    end_pos = record.POS + 1
                else:
                    end_pos = start_pos
                ref = str(record.REF)
                alt = re.search(r'\[(.*)\]', str(record.ALT))
                oncoTator = 'http://www.broadinstitute.org/oncotator/mutation/' + chr.group(
                    1
                ) + '_' + start_pos + '_' + end_pos + '_' + ref + '_' + alt.group(
                    1)
                try:
                    oncoTator_response = requests.get(oncoTator)
                except ConnectionError as e:
                    print e
                    logs.write(str(e))
                    oncoTator_response = "No response"
                if oncoTator_response != "No response" and oncoTator_response.status_code != 500:
                    mutation = json.loads(oncoTator_response.text)
                    if "ERROR" in mutation:
                        logs.write("OncoTator is giving error for url:" +
                                   oncoTator + " Error message: " +
                                   str(oncoTator_response.text))
                    else:

                        if mutation["CGC_Cancer Germline Mut"] == "yes":
                            germline_mutation_array.append(mutation)

                        if mutation["CGC_Cancer Somatic Mut"] == "yes":
                            somatic_mutation_array.append(mutation)
            logs.close()

            # write values from arrays to tsv files
            # fieldnames = []
            # for key in somatic_mutation_array[0]:
            #     fieldnames.append(str(key))

            # tsv_somatic_file = csv.DictWriter(output_somatic_file, fieldnames=fieldnames, delimiter='\t')
            # tsv_somatic_file.writeheader()
            for mutation in somatic_mutation_array:
                # tsv_somatic_file.writerow(mutation)
                ssnp = somatic_snp.objects.create(
                    study_name=instance,
                    gene=mutation["gene"],
                    chromosome=mutation["ESP_Chromosome"],
                    start=mutation["start"],
                    end=mutation["end"],
                    ref_allele=mutation["ref_allele"],
                    alt_allele=mutation["alt_allele"],
                    variant_class=mutation["variant_classification"],
                    variant_type=mutation["variant_type"],
                    ensemblID=mutation["HGNC_Ensembl Gene ID"])
            # output_somatic_file.close()

            # tsv_germline_file = csv.DictWriter(output_germline_file, fieldnames=fieldnames, delimiter='\t')
            # tsv_germline_file.writeheader()
            for mutation in germline_mutation_array:
                # tsv_germline_file.writerow(mutation)
                gsnp = germline_snp.objects.create(
                    study_name=instance,
                    gene=mutation["gene"],
                    chromosome=mutation["ESP_Chromosome"],
                    start=mutation["start"],
                    end=mutation["end"],
                    ref_allele=mutation["ref_allele"],
                    alt_allele=mutation["alt_allele"],
                    variant_class=mutation["variant_classification"],
                    variant_type=mutation["variant_type"],
                    ensemblID=mutation["HGNC_Ensembl Gene ID"])
            # output_germline_file.close()

            # fileread = open('media/germline_mutation-'+str(fileID)+'.tsv', 'r')
            # TSVfile = File(fileread)
            # instance.germline_result.save('media/germline_mutation-'+str(fileID)+'.tsv', TSVfile, save=True)
            #
            # fileread = open('media/somatic_mutation-' + str(fileID) + '.tsv', 'r')
            # TSVfile = File(fileread)
            # instance.somatic_result.save('media/somtic_mutation-' + str(fileID) + '.tsv', TSVfile, save=True)

            return HttpResponseRedirect('/result/')
    else:
        form = FileForm()
    return render(request, 'oncotator/snp_tools.html', {'form': form})