Exemplo n.º 1
0
def upload(request):
	try:
		uid=request.COOKIES['me_uid']
	except KeyError:
		return HttpResponseRedirect('http://www.me.uestc.edu.cn/stu/index.php/Login/')
	auth= UserAuth(uid).is_auth()	
	if not auth:
		return HttpResponseRedirect('http://www.me.uestc.edu.cn/stu/index.php/Login/')
	if request.method == "POST":
		form = UploadFileForm(request.POST,request.FILES)
		typ=request.FILES['files'].name.split('.')[1]
		if not (typ=='xls' or typ=='xlsx'):
			return  HttpResponse("<h1>上传失败 只能上传excel文件</h1></br><a href='upload'>返回重新上传</a>")
		if form.is_valid():
			files = open('static/excel.xls','w+')
			for chunk in request.FILES['files'].chunks():
				files.write(chunk)
			files.close()
			up_suc=handle_upload_file()
			if up_suc :
				q=u"上传失败,请以学号为第一列!"
			else:
				q=u"上传成功!"
			return render_to_response('success.html',{'q':q})
		
	else :
		form = UploadFileForm()
	return render_to_response('upload.html', {'form': form})
Exemplo n.º 2
0
def uploadFile(request, course_id):
    if request.method == 'POST':
        c = Course.objects.get(pk=course_id)
        upFile = UploadFileForm(request.POST, request.FILES)
        data = upFile.data
        if upFile.is_valid():
            data = upFile.cleaned_data
            newFile = Resource(name = data['name'], description = data['description'], type = 'File',
                                 docfile = request.FILES['docfile'], sourceLink = '', course = c)
            newFile.save()
            return HttpResponseRedirect('/course/%d'%c.id)
        else:
            resources = Resource.objects.all()
            context = RequestContext(request,
                {
                    'c' : c,
                    'resources' : resources,
                    'upFileForm' : UploadFileForm(
                        initial={'name':data['name'], 'description':data['description']}
                    ),
                    'upLinkForm' : UploadSourceLinkForm(),
                    'errorFile' : 'Please fill all information'
                }
            )

            return render_to_response('course.html',context)
    else:
        return viewCourse(request,course_id)
Exemplo n.º 3
0
def upload(user):
    """Upload a file from a client machine."""
    file = UploadFileForm()
    if file.validate_on_submit():
        f = file.file_selector.data
        filename = secure_filename(f.filename)

        mrn = file.mrn.data

        if filename[-4:] != '.wav':
            flash('File type must be .wav')
        elif len(mrn) != 7 or not mrn.isnumeric():
            flash('MRN must be a 7 digit number')
        else:
            file_dir_path = os.path.join(application.instance_path, 'files')
            file_path = os.path.join(file_dir_path, filename)
            f.save(file_path)

            # Add this to the queue table
            current_id = User.query.filter_by(username=user).first().id
            transcription_id = str(uuid.uuid4())
            now_utc = pytz.utc.localize(datetime.utcnow())
            now_pst = now_utc - timedelta(hours=7)
            upload_row = Queue(id=current_id,
                               mrn=mrn,
                               transcription_id=transcription_id,
                               timestamp=now_pst,
                               filename=filename)
            db.session.add(upload_row)
            db.session.commit()

            return redirect(url_for('queue', user=user))
    return render_template('upload.html', form=file)
Exemplo n.º 4
0
def upload_file():
    form = UploadFileForm()
    if form.validate_on_submit():
        card = form.card.data
        year = form.year.data
        month = form.month.data
        owner = form.owner.data
        file = form.file.data
        if not file or file.filename == '':
            return make_response("no file, error! ")
        if file and allowed_file(file.filename):
            filename = "_".join([card, year, month, owner, 'Transactions.csv'])
            path = os.path.join(app.config['BASE_FOLDER'], 'app', 'uploads',
                                filename)
            try:
                file.save(path)
                parseAndSaveTransctions(card, owner, path)
            except ValueError as ve:
                return make_response(
                    "File parsing error, please check your uploading file type or card type: "
                    + str(ve))
            except Exception as e:
                return make_response("Uploading filed failed, as: " + str(e))
            return redirect(url_for('list_records'))
    return render_template('home/uploadfile.html', form=form)
Exemplo n.º 5
0
def upload(request):
    try:
        uid = request.COOKIES['me_uid']
    except KeyError:
        return HttpResponseRedirect(
            'http://www.me.uestc.edu.cn/stu/index.php/Login/')
    auth = UserAuth(uid).is_auth()
    if not auth:
        return HttpResponseRedirect(
            'http://www.me.uestc.edu.cn/stu/index.php/Login/')
    if request.method == "POST":
        form = UploadFileForm(request.POST, request.FILES)
        typ = request.FILES['files'].name.split('.')[1]
        if not (typ == 'xls' or typ == 'xlsx'):
            return HttpResponse(
                "<h1>上传失败 只能上传excel文件</h1></br><a href='upload'>返回重新上传</a>")
        if form.is_valid():
            files = open('static/excel.xls', 'w+')
            for chunk in request.FILES['files'].chunks():
                files.write(chunk)
            files.close()
            up_suc = handle_upload_file()
            if up_suc:
                q = u"上传失败,请以学号为第一列!"
            else:
                q = u"上传成功!"
            return render_to_response('success.html', {'q': q})

    else:
        form = UploadFileForm()
    return render_to_response('upload.html', {'form': form})
Exemplo n.º 6
0
def upload_file():
    form = UploadFileForm()
    if form.validate_on_submit():
        app.logger.info('import file')
        data = fJson.load(form.selected_file.data)
        #app.logger.info(data[0]['tags'][0])
        flash('Import success.')
    return render_template('upload.html', form=form)
Exemplo n.º 7
0
def upload_file(request):
    if request.method == 'POST':
        form = UploadFileForm(request.POST, request.FILES) # 注意获取数据的方式
        if form.is_valid():
            handle_uploaded_file(request.FILES['file'])
            return HttpResponseRedirect('/success/url/')
    else:
        form = UploadFileForm()
    return render(request, 'upload.html', {'form': form})
Exemplo n.º 8
0
def upload_file(request):
    if request.method == 'POST':
        form = UploadFileForm(request.POST, request.FILES)
        #print (form)
        if form.is_valid():
            print("Hai")
            pred_data = handle_uploaded_file(request.FILES['file'])
            return JsonResponse(pred_data, safe=False)

    return JsonResponse(['Error'], safe=False)
Exemplo n.º 9
0
def upload(request):
    form = UploadFileForm()

    if request.method == 'POST':
        form = UploadFileForm(request.POST, request.FILES)
        if form.is_valid():
            p = Post()
            p.description = request.POST['description']
            p.image = request.FILES['file']
            p.user = request.user
            p.save()
            return redirect('/')

    return render(request, 'upload.html', {'form': form})
Exemplo n.º 10
0
def show_model(request):
    """
    Handle Request POST with uploded file
    :param request: Request object.
    :return:
    """
    if request.method == 'POST':
        upload_file_form = UploadFileForm(request.POST, request.FILES)
        if upload_file_form.is_valid():
            diagram_name_unique_id = handle_uploaded_file(
                request.FILES['diagram_file'])
            request.session['diagram_name'] = diagram_name_unique_id
            return HttpResponseRedirect('/open_external_diagram')
    else:
        return HttpResponseRedirect('/')
Exemplo n.º 11
0
    def index(self, request):
        pesan = ""
        #assert isinstance(request, HttpRequest)

        if request.method == "POST":
            pesan = "simpan|false"
            fileForm = UploadFileForm(request.POST, request.FILES)

            if fileForm.is_valid():
                imagesave = ModelTraining()
                imagesave.datatraining = fileForm.cleaned_data['datatraining']
                imagesave.save()
                pesan = "simpan|true"

        else:
            file = ModelTraining()

        return self.tampilHalaman(request, pesan)
Exemplo n.º 12
0
def newSheetPage(request):
	""" NewSheet view to show the UploadSheetForm """
	# TODO: Create an option to upload a new version of a file

	student = getStudent(request.user)

	if request.method == "POST":
		sheetForm = UploadSheetForm(student=student, data=request.POST)
		fileForm = UploadFileForm(data=request.POST, files=request.FILES.getlist('file'))

		if fileForm.is_valid() and sheetForm.is_valid():
			files = request.FILES.getlist('file')
			sheet = sheetForm.save(commit=False)

			if splitext(files[0].name)[1] in ('.jpeg', '.jpg', '.png'):
				sheet.thumbnail = files[0]
				print('THUMBNAILED')
			else:
				sheet.thumbnail = None

			sheet.uploadedBy = student
			sheet.save()

			for file in files:
				uploadedFile = UploadedFile()
				uploadedFile.file = file
				uploadedFile.extension = splitext(file.name)[1]
				uploadedFile.contentType = file.content_type
				uploadedFile.relatedSheet = sheet
				uploadedFile.save()

			localVarsJSON = json.dumps({'success': 'true',})
			messages.add_message(request, messages.SUCCESS, 'Votre fiche a bien été envoyée !')

			return HttpResponse(localVarsJSON, content_type='application/json')
		else:
			errors = []

			if not fileForm.is_valid(): errors.append(fileForm.errors)
			if not sheetForm.is_valid(): errors.append(sheetForm.errors)

			return HttpResponse(json.dumps(errors), content_type='application/json')
	else:
		raise Http404('Hey :/ I wasn\'t expecting you here !')
Exemplo n.º 13
0
def upload_file():
    # os.listdir('/Volumes') lists drives on MacOS
    # desktop = os.path.join(os.path.join(os.environ['USERPROFILE']), 'Desktop')  #     This is the way to properly get to desktop
    form = UploadFileForm()
    if form.is_submitted():
        # print(form.data)
        if form.path.data == '':
            name_tuple = save_file(file=form.uploaded_file.data)
            file_obj = UploadedFile(path='/static/uploads/',
                                    drive='app.root_path',
                                    filename=name_tuple[0],
                                    uuid_name=name_tuple[1])
            db.session.add(file_obj)
            db.session.commit()

            flash('File has been uploaded successfully.', 'success')
            return redirect(url_for('view_files'))
        # file_object = UploadedFile(path=)
    return render_template('file_uploads.html', form=form, active='upload')
Exemplo n.º 14
0
def upload(request):
    if request.user.is_superuser:
        context_dict = {}
        if request.method == 'POST' and request.FILES['file']:
            try:
                form = UploadFileForm(request.POST, request.FILES)
                if form.is_valid():

                    def choice_func(row):
                        if (str(row[0]).lower().startswith("inc")):
                            row[0] = "income"
                        else:
                            row[0] = "expense"
                        try:
                            info = Info.objects.get(name=str(row[1]),
                                                    date=str(row[3]))
                            if (info.amount == row[2]):
                                return None
                            else:
                                info.delete()
                            return (row)
                        except:
                            return row

                    request.FILES['file'].save_to_database(
                        model=Info,
                        initializer=choice_func,
                        mapdict=['ttype', 'name', 'amount', 'date'])
                    done = 1
                    update()
                    form = UploadFileForm()
                    context_dict["form"] = form
                    context_dict["success"] = "Succesfully Uploaded!!"
                    return render(request, 'app/upload.html', context_dict)
            except Exception as e:
                print(e)
                form = UploadFileForm()
                context_dict["form"] = form
                context_dict["error"] = e
                return render(request, 'app/upload.html', context_dict)
        else:
            form = UploadFileForm()
            context_dict["form"] = form
            context_dict["error"] = "No file uploaded"
            return render(request, 'app/upload.html', context_dict)
    else:
        return HttpResponseRedirect("/app/login/")
Exemplo n.º 15
0
def upload():
    """Upload a file from a client machine."""
    file = UploadFileForm()
    if file.validate_on_submit():
        f = file.file_selector.data
        filename = secure_filename(f.filename)
        file_dir_path = os.path.join(application.instance_path, 'files')
        file_path = os.path.join(file_dir_path, filename)
        # Save file to file_path (instance/ + 'files' + filename)
        f.save(file_path)

        file_dir_path = os.path.join(application.instance_path, 'files')
        file_path = os.path.join(file_dir_path, filename)

        # Convert audio file to text (String)
        r = sr.Recognizer()
        harvard = sr.AudioFile(file_path)
        with harvard as source:
            audio = r.record(source)
        talk_to_text = r.recognize_google(audio)

        # pipe results from talk to text to nlp model
        example_result = prepare_note(spacy_model, talk_to_text)
        """Display the model results."""
        proper_title_keys = [k.title() for k in list(example_result.keys())]

        session['example_result'] = example_result
        session['proper_title_keys'] = proper_title_keys

        # delete the file
        if os.path.exists(file_path):
            os.remove(file_path)
        else:
            print("The file does not exist.")

        return redirect(url_for('results', filename=filename))
    return render_template('upload.html', form=file)
Exemplo n.º 16
0
def event_list(request):
    #verify user id
    try:
        if request.user.venue is None:
            return redirect('/login/?next=%s' % request.path)
    except ObjectDoesNotExist:
        return redirect('/login/?next=%s' % request.path)

    events = Event.objects.filter(venue__id=request.user.venue.id).filter(
        start__gte=timezone.now()).all()
    uploadForm = UploadFileForm(initial={'venue': request.user.venue.id})
    return render(request, 'app/admin/event_list.html', {
        'events': events,
        'year': datetime.now().year,
        'uploadForm': uploadForm
    })
Exemplo n.º 17
0
def upload_events(request):
    file = request.FILES['file']
    fs = FileSystemStorage()
    venue = request.POST.get('venue')
    #save the file to disk for processing
    filename = fs.save(venue + '/' + file.name, file)
    #BASE_DIR = os.path.dirname(os.path.abspath(__file__))
    media_folder = settings.MEDIA_ROOT
    wb = load_workbook(media_folder + '/' + filename,
                       read_only=True)  #open the file and the events worksheet
    ws = wb['Events']
    event_type_to_index = dict()
    for i, j in enumerate(
            Event.EVENT_TYPES
    ):  #load event_type_to_index to get the numeric value of the Event Type
        event_type_to_index.update({j[1]: i})
    for row in ws.iter_rows(row_offset=1):  #process the events
        event = Event(title=row[0].value,
                      event_type=(event_type_to_index[row[1].value] + 1),
                      description=row[2].value,
                      confirmed=(1 if row[3].value == 'Y' else 0),
                      start=localize(row[4].value),
                      venue_id=venue,
                      created=timezone.now())
        if row[5].value is not None and row[
                7] is not None:  #only save customer info for Tentative bookings - this may change
            customer = Customer(name=row[5].value,
                                email=row[6].value,
                                phone=row[7].value)
            customer.save()
            event.customer = customer
        #only add an event if it doesn't already exist
        event_validate = Event.objects.filter(title=row[0].value).filter(
            venue_id=venue).filter(start__date=row[4].value.date()).first()
        if event_validate is None:
            event.save()
    events = Event.objects.filter(venue__id=venue).filter(
        start__gte=timezone.now()).all()
    uploadForm = UploadFileForm(initial={'venue': venue})
    return render(request, 'app/admin/event_list.html', {
        'events': events,
        'year': datetime.now().year,
        'uploadForm': uploadForm
    })
Exemplo n.º 18
0
def file_upload(request):
    # print('-------------------------------')
    # print(request)
    # print(request.POST)
    # print(request.FILES)
    # print('-------------------------------')
    if (request.method == 'POST'):
        form = UploadFileForm(request.POST, request.FILES)
        if (form.is_valid()):
            form.save()
            return redirect('file_upload')
        else:
            print('[INFO] UploadFileForm is invalid')
            upload_file_form = UploadFileForm()
    else:
        upload_file_form = UploadFileForm()
    
    uploaded_files = UploadFiles.objects.all()
    context = {
        'upload_file_form': upload_file_form,
        'uploaded_files': uploaded_files,
    }
    return render(request, "app/file_upload.html", context)
Exemplo n.º 19
0
def admin_problem_file(problem_id):
    #if it is not an admin user or the user is not logged does not display this page
    if not current_user.is_authenticated or not current_user.admin:
        return render_template('404.html')
    if not ProblemInformation.query.filter_by(code=problem_id).first():
        return redirect(url_for('admin'))

    form = UploadFileForm(prefix='form')
    form1 = DeleteFileForm(prefix='form1')
    form11 = SetDescriptionForm(prefix='form11')
    form2 = SetTestCaseForm(prefix='form2')
    form3 = DeleteTestCaseForm(prefix='form3')

    if form.submit.data and form.validate_on_submit():
        filename = os.path.join(app.config['PROBLEMS_DIR'], problem_id,
                                form.file_name.data)
        form.file_sent.data.save(filename)
        f = ProblemFile.query.filter_by(problem_code=problem_id,
                                        file_name=form.file_name.data).first()
        if f:
            f.visible = form.visible.data
            db.session.commit()
            flash("File overwritten.")
        else:
            file = ProblemFile(problem_id, form.file_name.data,
                               form.visible.data)
            db.session.add(file)
            db.session.commit()
            flash("File uploaded.")
    elif form1.submit.data and form1.validate_on_submit():
        file = ProblemFile.query.filter_by(id=form1.file_id.data,
                                           problem_code=problem_id).first()
        if file:
            path = os.path.join(app.config['PROBLEMS_DIR'], problem_id,
                                file.file_name)
            db.session.delete(file)
            db.session.commit()
            os.unlink(path)
            flash('File deleted.')
        else:
            flash('File not found.')
    elif form11.submit.data and form11.validate_on_submit():
        if form11.remove.data:
            p = ProblemInformation.query.filter_by(code=problem_id).first()
            p.description_file = None
            db.session.commit()
            flash('Description file removed')
        else:
            exists = ProblemFile.query.filter_by(
                problem_code=problem_id,
                id=form11.description_file.data).first()
            if not exists:
                flash('Description file not in problem!')
            else:
                p = ProblemInformation.query.filter_by(code=problem_id).first()
                p.description_file = form11.description_file.data
                db.session.commit()
                flash('Description file set.')
    elif form2.submit.data and form2.validate_on_submit():
        in_file = ProblemFile.query.filter_by(
            problem_code=problem_id, id=form2.input_file.data).first()
        res_file = ProblemFile.query.filter_by(problem_code=problem_id,
                                               id=form2.res_file.data).first()
        exists = ProblemTestCaseInformation.query.filter_by(
            problem_code=problem_id, test_case=form2.test_case.data).first()
        if not in_file:
            flash('The input file is not in this problem.')
        if not res_file:
            flash('The response file is not in this problem.')
        elif not exists:
            n = ProblemTestCaseInformation(problem_id, form2.test_case.data,
                                           form2.input_file.data,
                                           form2.res_file.data,
                                           form2.is_open.data)
            db.session.add(n)
            db.session.commit()
            flash('Test case added.')
        else:
            exists.input_file = form2.input_file.data
            exists.res_file = form2.res_file.data
            exists.is_open_case = form2.is_open.data
            db.session.commit()
            flash('Test case was overwritten.')
    elif form3.submit.data and form3.validate_on_submit():
        exists = ProblemTestCaseInformation.query.filter_by(
            problem_code=problem_id, test_case=form3.test_case.data).first()
        if exists:
            db.session.delete(exists)
            db.session.commit()
            flash('Test case removed.')
        else:
            flash('Test case was not removed because it does not exist.')
    return render_template(
        'admin_problem_file.html',
        title='Manage problem - files',
        form=form,
        form1=form1,
        form11=form11,
        form2=form2,
        form3=form3,
        problem=ProblemInformation.query.filter_by(code=problem_id).first(),
        files=ProblemFile.query.filter_by(problem_code=problem_id).all(),
        testcases=ProblemTestCaseInformation.query.filter_by(
            problem_code=problem_id).all())
Exemplo n.º 20
0
def uploadRegistered(request):
    """Upload and save the zip"""
    if request.user.is_authenticated():
        user = request.user.username
    else:
        return HttpResponseRedirect('/')

    if request.method == 'POST':
        form = UploadFileForm(request.POST)
        # Analyze the scratch project and save in our server files
        fileName = handle_uploaded_file(request.FILES['zipFile'])
        # Analize project and to save in database the metrics
        d = analyzeProject(request, fileName)
        fupdate = datetime.now()
        # Get the short name
        shortName = fileName.split('/')[-1]
        # Get the dashboard of user
        myDashboard = Dashboard.objects.get(user=user)
        # Save the project
        newProject = Project(name=shortName,
                             version=1,
                             score=0,
                             path=fileName,
                             fupdate=fupdate,
                             dashboard=myDashboard)
        newProject.save()
        # Save the metrics
        dmaster = d["mastery"]
        newMastery = Mastery(myproject=newProject,
                             abstraction=dmaster["Abstraction"],
                             paralel=dmaster["Parallelization"],
                             logic=dmaster["Logic"],
                             synchronization=dmaster["Synchronization"],
                             flowcontrol=dmaster["FlowControl"],
                             interactivity=dmaster["UserInteractivity"],
                             representation=dmaster["DataRepresentation"],
                             TotalPoints=dmaster["TotalPoints"])
        newMastery.save()
        newProject.score = dmaster[
            "Total{% if forloop.counter0|divisibleby:1 %}<tr>{% endif %}Points"]
        if newProject.score > 15:
            newProject.level = "advanced"
        elif newProject.score > 7:
            newProject.level = "developing"
        else:
            newProject.level = "beginner"
        newProject.save()

        for charx, dmetrics in d["attribute"].items():
            if charx != 'stage':
                newAttribute = Attribute(myproject=newProject,
                                         character=charx,
                                         orientation=dmetrics["orientation"],
                                         position=dmetrics["position"],
                                         costume=dmetrics["costume"],
                                         visibility=dmetrics["visibility"],
                                         size=dmetrics["size"])
            newAttribute.save()

        iterator = 0
        for deadx in d["dead"]:
            if (iterator % 2) == 0:
                newDead = Dead(myproject=newProject, character=deadx, blocks=0)
            else:
                newDead.blocks = deadx
            newDead.save()
            iterator += 1

        newDuplicate = Duplicate(myproject=newProject,
                                 numduplicates=d["duplicate"][0])
        newDuplicate.save()
        for charx in d["sprite"]:
            newSprite = Sprite(myproject=newProject, character=charx)
            newSprite.save()
        return HttpResponseRedirect('/myprojects')
Exemplo n.º 21
0
def upload_file(request):
    data_name = 'Загрузка информации в БД'
    if Group.objects.filter(name='Authors').count() == 0:
        Group.objects.create(name='Admins')
        Group.objects.create(name='Editors')
        Group.objects.create(name='Authors')
        Group.objects.create(name='Guests')
        return redirect('articles')
    else:
        usergroup = get_usergroup(request)
        if usergroup == 'Admins':
            if request.method == 'POST':
                form = UploadFileForm(request.POST, request.FILES)
                if form.is_valid():
                    # так производится создание объектов модели FileModel и запись их в базу данных
                    p1 = FileModel.objects.create(file=request.FILES['file'])
                    data_way1 = 'app/static/'
                    data_way2 = str(request.FILES['file'])
                    data_way = data_way1 + data_way2
                    excel_data_file = xlrd.open_workbook(data_way)
                    sheet_3 = excel_data_file.sheet_by_index(3)  #Users, Авторы
                    sheet_4 = excel_data_file.sheet_by_index(
                        4)  #Usrrs, Редакторы
                    row_number = sheet_3.nrows  # количество строк
                    if row_number > 0:  # Проверка на пустоту файла
                        for row in range(1, row_number):
                            if User.objects.filter(
                                    username=str(sheet_3.row(row)[0]).replace(
                                        "text:", "").replace("'",
                                                             "")).count() == 0:
                                user = User.objects.create_user(
                                    username=str(sheet_3.row(row)[0]).replace(
                                        "text:", "").replace("'", ""),
                                    email=str(sheet_3.row(row)[7]).replace(
                                        "text:", "").replace("'", ""),
                                    password=str(sheet_3.row(row)[0]).replace(
                                        "text:", "").replace("'", ""))
                                group = Group.objects.get(name='Authors')
                                group.user_set.add(user)
                                permission = Permission.objects.get(
                                    name='Can add Article')
                                user.user_permissions.add(permission)
                                permission = Permission.objects.get(
                                    name='Can change Article')
                                user.user_permissions.add(permission)
                                permission = Permission.objects.get(
                                    name='Can delete Article')
                                user.user_permissions.add(permission)
                                AuthorModel.objects.create(
                                    login=user,
                                    index="",
                                    name=str(sheet_3.row(row)[1]).replace(
                                        "text:", "").replace("'", ""),
                                    birth_date=str(
                                        sheet_3.row(row)[2]).replace(
                                            "text:",
                                            "").replace("'",
                                                        "").replace(".", ""),
                                    degree=str(sheet_3.row(row)[3]).replace(
                                        "text:", "").replace("'", ""),
                                    title=str(sheet_3.row(row)[4]).replace(
                                        "text:", "").replace("'", ""),
                                    work=str(sheet_3.row(row)[5]).replace(
                                        "text:", "").replace("'", ""),
                                    position=str(sheet_3.row(row)[6]).replace(
                                        "text:", "").replace("'", ""),
                                    email=str(sheet_3.row(row)[7]).replace(
                                        "text:", "").replace("'", ""),
                                    education=str(sheet_3.row(row)[8]).replace(
                                        "text:", "").replace("'", ""))
                                author = AuthorModel.objects.get(index="")
                                author.index = str(author.id)
                                author.save()
                    row_number = sheet_4.nrows  # количество строк
                    if row_number > 0:  # Проверка на пустоту файла
                        for row in range(1, row_number):
                            if User.objects.filter(
                                    username=str(sheet_4.row(row)[0]).replace(
                                        "text:", "").replace("'",
                                                             "")).count() == 0:
                                user = User.objects.create_user(
                                    username=str(sheet_4.row(row)[0]).replace(
                                        "text:", "").replace("'", ""),
                                    email=str(sheet_4.row(row)[8]).replace(
                                        "text:", "").replace("'", ""),
                                    password=str(sheet_4.row(row)[0]).replace(
                                        "text:", "").replace("'", ""))
                                group = Group.objects.get(name='Editors')
                                group.user_set.add(user)
                                editor = EditorModel.objects.create(
                                    login=user,
                                    name_magazine=str(
                                        sheet_4.row(row)[3]).replace(
                                            "text:", "").replace("'", ""),
                                    name=str(sheet_4.row(row)[1]).replace(
                                        "text:", "").replace("'", ""),
                                    birth_date=str(
                                        sheet_4.row(row)[2]).replace(
                                            "text:",
                                            "").replace("'",
                                                        "").replace(".", ""),
                                    degree=str(sheet_4.row(row)[4]).replace(
                                        "text:", "").replace("'", ""),
                                    title=str(sheet_4.row(row)[5]).replace(
                                        "text:", "").replace("'", ""),
                                    work=str(sheet_4.row(row)[6]).replace(
                                        "text:", "").replace("'", ""),
                                    position=str(sheet_4.row(row)[7]).replace(
                                        "text:", "").replace("'", ""),
                                    email=str(sheet_4.row(row)[8]).replace(
                                        "text:", "").replace("'", ""),
                                    education=str(sheet_4.row(row)[9]).replace(
                                        "text:", "").replace("'", ""))
                    p1.delete()
                    data_name = "Загрузка завершена"
                    return render_to_response('upload.html',
                                              {'data_name': data_name})
                else:
                    return render_to_response(
                        'upload.html', {
                            'form': form,
                            'data_name': "Ошибка ввода данных"
                        })
            else:
                form = UploadFileForm()
            return render_to_response('upload.html', {
                'form': form,
                'data_name': data_name
            })
        else:
            return redirect('articles')
Exemplo n.º 22
0
def index():
    global selected_dataset, last_dataset
    # ^ it's bad decision...

    if selected_dataset is None:
        selected_dataset = request.args.get("dataset")
        if selected_dataset is not None:
            last_dataset = selected_dataset
            return redirect(url_for(".index"))

    header = None
    if session.get("current_data") is not None:
        header = get_header(session["current_data"])

    simplegraph_form = SimpleGraphForm(header)
    anomalies_form = AnomaliesForm(algorithms)
    dataoverview_form = DataOverviewForm()
    upload_form = UploadFileForm()

    kwargs = dict(
        upload_form=upload_form,
        simplegraph_form=simplegraph_form,
        anomalies_form=anomalies_form,
        dataoverview_form=dataoverview_form,
        header=header,
    )

    def _get_axes(axes_form):
        """
        Return selected columns to draw plots with.
        :return: (axis_x, axis_y)

        """
        return (
            dict(axes_form.axis_x.choices)[axes_form.axis_x.data],
            dict(axes_form.axis_y.choices)[axes_form.axis_y.data],
        )

    def _get_algorithm():
        return dict(anomalies_form.selected_algorithm.choices)[
            anomalies_form.selected_algorithm.data]

    def _filesize(path):
        fs = os.path.getsize(path)
        m = "B"
        if fs > 1025:
            fs /= 1024
            m = "KB"
        if fs > 1025:
            fs /= 1024
            m = "MB"
        if fs > 1025:
            fs /= 1024
            m = "GB"
        fs = round(fs)
        res = "{} {}".format(str(fs), m)
        return res

    # Display content of dataset.
    if upload_form.validate_on_submit() or selected_dataset is not None:
        if selected_dataset is not None:
            uploaded_file = os.path.join("uploads", selected_dataset)
        else:
            uploaded_file = upload_form.uploaded_file.data

        session["current_data"] = get_json_data(uploaded_file)

        header = get_header(session["current_data"])
        values = get_values(session["current_data"])

        simplegraph_form.change_choices(header)

        if selected_dataset is not None:
            session["current_file_id"] = save_uploaded_file(uploaded_file,
                                                            skip=True)
            kwargs.update(dataset=selected_dataset)
            selected_dataset = None
        else:
            session["current_file_id"] = save_uploaded_file(uploaded_file)
            kwargs.update(dataset=upload_form.uploaded_file.data.filename)
        last_dataset = kwargs["dataset"]

        mat = db_queries.get_dataframe(session["current_file_id"])
        y = mat["outlier"].values
        X = mat.drop("outlier", axis=1).values

        n_dim = X.shape[1] - 1
        n_obj = len(y)
        n_out = np.count_nonzero(y)
        percentage = round(100 * n_out / n_obj, 2)

        kwargs.update(
            simplegraph_form=simplegraph_form,
            anomalies_form=anomalies_form,
            header=header,
            data=values,
            n_dim=n_dim,
            n_obj=n_obj,
            n_out=n_out,
            percentage=percentage,
        )

        return render_template("showcsv.html", **kwargs)

    # Simple plots.
    if simplegraph_form.submit_graph.data:
        try:
            axis_x, axis_y = _get_axes(simplegraph_form)
        except:
            axis_x, axis_y = None, None

        if "current_file_id" not in session:
            uploaded_file = os.path.join("uploads", last_dataset)
            session["current_file_id"] = save_uploaded_file(uploaded_file,
                                                            skip=True)

            session["current_data"] = get_json_data(uploaded_file)
            header = get_header(session["current_data"])
            simplegraph_form.change_choices(header)
            kwargs.update(simplegraph_form=simplegraph_form)

        filename = plot_df.naive_plot_df(session["current_file_id"], axis_x,
                                         axis_y)
        kwargs.update(filename=filename,
                      axis_x=axis_x,
                      axis_y=axis_y,
                      dataset=last_dataset)

        return render_template("showgraph.html", **kwargs)

    # Anomaly Detection.
    if anomalies_form.submit_anomalies.data:
        # axis_x, axis_y = _get_axes(simplegraph_form)
        selected_algortihm = _get_algorithm()

        if "current_file_id" not in session:
            uploaded_file = os.path.join("uploads", last_dataset)
            session["current_file_id"] = save_uploaded_file(uploaded_file,
                                                            skip=True)

            session["current_data"] = get_json_data(uploaded_file)
            header = get_header(session["current_data"])
            simplegraph_form.change_choices(header)
            kwargs.update(simplegraph_form=simplegraph_form)

        filename_analyze_selected_algorithm = plot_anomalies.analyze_selected_algorithm(
            session["current_file_id"], last_dataset, selected_algortihm)
        # filename_simple_plot = plot_anomalies.simple_plot(
        #     session["current_file_id"], axis_x, axis_y
        # )
        # filename_simple_anomalies = plot_anomalies.simple_anomalies(
        #     session["current_file_id"], axis_x, axis_y
        # )
        # filename_data_overview = plot_anomalies.data_overview(
        #     session["current_file_id"], dataset_title=last_dataset
        # )

        kwargs.update(
            algorithms=algorithms,
            # axis_x=axis_x,
            # axis_y=axis_y,
            # filename_simple_plot=filename_simple_plot,
            # filename_simple_anomalies=filename_simple_anomalies,
            # filename_data_overview=filename_data_overview,
            filename_analyze_selected_algorithm=
            filename_analyze_selected_algorithm,
            dataset=last_dataset,
        )

        return render_template("showanomalies.html", **kwargs)

    # Data Overview.
    if dataoverview_form.validate_on_submit():
        if "current_file_id" not in session:
            uploaded_file = os.path.join("uploads", last_dataset)
            session["current_file_id"] = save_uploaded_file(uploaded_file,
                                                            skip=True)

            session["current_data"] = get_json_data(uploaded_file)
            header = get_header(session["current_data"])
            simplegraph_form.change_choices(header)
            kwargs.update(simplegraph_form=simplegraph_form)

        filename_data_overview = plot_anomalies.data_overview(
            session["current_file_id"], dataset_title=last_dataset)

        kwargs.update(filename_data_overview=filename_data_overview,
                      dataset=last_dataset)

        return render_template("showdataoverview.html", **kwargs)

    datasets = sorted(os.listdir("uploads/"), key=str.lower)
    datasets = list(filter(lambda x: x.startswith(".") is False, datasets))

    datasets = list(
        map(lambda x: (x, _filesize(os.path.join("uploads", x))), datasets))
    kwargs.update(datasets=datasets)

    return render_template("showdatasets.html", **kwargs)