def signup(request): if request.method == 'GET': context = {"captcha": not settings.DEBUG} return render(request, 'accounts/signup.html', Context(context)) username = request.POST['username'] password = request.POST['password'] password_repeat = request.POST['password_repeat'] email = request.POST['email'] if not settings.DEBUG: if 'captcha' in request.POST: captha_response = request.POST['captcha'] else: captha_response = "" if not captha_response.lower() == 'additive': return general_views.message( request, "You haven't passed Turing test.") if len(username) < 3: return general_views.message( request, "Username must be at least 3 character long.") if password != password_repeat: return general_views.message( request, ("Your passwords don't match." "<br><a href='/accounts/signup'>Try again</a>")) if registration_closed(): return general_views.message( request, ("Registration is disabled on this server.")) try: user = User.objects.create_user(username, email, password) # Granting permissions if settings.DEBUG: user.user_permissions.add( Permission.objects.get(codename='add_dataset')) user.user_permissions.add( Permission.objects.get(codename='add_model')) user.save() except BaseException: HttpResponse("Fail.") return general_views.message( request, ("Registration complete." "<br><a href='/accounts/login'>To login page</a>."))
def accept_exam(request): def hash_md5(s): import hashlib hasher = hashlib.md5() hasher.update(s.encode('utf-8')) return hasher.hexdigest() assessor = User.objects.get(username=request.GET["username"]) problem = AssessmentProblem(id=int(request.GET["problem_id"])) try: ver = ExamVerification.objects.get( problem=problem, assessor=request.user) except BaseException: return HttpResponseNotFound("This exam wasn't requested by system.") received_hash = request.GET["hash"] correct_hash = hash_md5( assessor.username + str(problem.id) + settings.EXAM_KEY) if correct_hash != received_hash: return HttpResponseForbidden("Invalid verification hash.") ver.passed = True ver.save() return general_views.message( request, ( "Тестирование успешно пройдено.<br>" "Чтобы приступить к оценивванию, щёлкните " "<a href='/assessment/get_task?problem_id=%d'>здесь</a>.") % (problem.id))
def arrange_topics(request): model = ArtmModel.objects.get(id=request.GET['model']) if request.user != model.author: return HttpResponseForbidden("You are not the author") if model.status != 0: return general_views.message(request, "Model is locked.") model.creation_time = datetime.now() model.status = 11 model.save() model.prepare_log() mode = request.GET['mode'] try: metric = request.GET['metric'] except BaseException: metric = "default" if settings.THREADING: t = Thread(target=ArtmModel.arrange_topics, args=( model, mode, metric, ), daemon=True) t.start() else: model.arrange_topics(mode, metric) return redirect("/model?model=" + str(model.id))
def delete_all_models(request): dataset = Dataset.objects.filter(text_id=request.GET['dataset'])[0] if request.user != dataset.owner: return HttpResponseForbidden("You are not the owner of the dataset.") if 'sure' in request.GET and request.GET['sure'] == 'yes': ArtmModel.objects.filter(dataset=dataset).delete() return general_views.message( request, "All models were deleted. <a href ='/dataset?dataset=" + dataset.text_id + "'>Return to dataset</a>.") else: return general_views.message( request, "Are you sure that you want delete ALL models for dataset " + str(dataset) + " permanently?<br>" + "<a href = '/models/delete_all_models?dataset=" + dataset.text_id + "&sure=yes'>Yes</a><br>" + "<a href = '/dataset?dataset=" + dataset.text_id + "'>No</a>")
def reset_visuals(request): model = ArtmModel.objects.get(id=request.GET['model']) if request.user != model.author: return HttpResponseForbidden("You are not the author") model.reset_visuals() return general_views.message( request, "Resetted. <a href ='/model?model=" + str(model.id) + "'> <br>Return to model</a>.")
def example(request, vis_name): models = ArtmModel.objects.filter(dataset__is_public=True, dataset__time_provided=True, layers_count__gte=2) if len(models) > 0: return redirect("/visual/global?type=%s&model=%d" % (vis_name, models[0].id)) else: return general_views.message( request, "No appropriate model to illustrate this visualization.")
def get_task(request): problem = AssessmentProblem.objects.get(id=request.GET["problem_id"]) if problem.can_assess(request.user): if problem.exam_needed: try: ver = ExamVerification.objects.get( problem=problem, assessor=request.user) except BaseException: ver = ExamVerification() ver.problem = problem ver.assessor = request.user ver.passed = False ver.save() if not ver.passed: import urllib exam_url = "%s/start_exam?%s" % ( problem.exam_host, urllib.parse.urlencode({ "username": request.user.username, "problem_id": problem.id, "host": request.META['HTTP_HOST'] }) ) return general_views.message( request, ("Вам нужно пройти тестирование, " "прежде чем приступать к оцениванию.<br>" "Чтобы приступить к тестированию, " "щёлкните <a href=%s>здесь</a>.") % exam_url) task = problem.create_task(request) if not task: return general_views.message( request, "Seems like everything is assessed!") return redirect("/assessment/task?task_id=" + str(task.id)) else: return HttpResponseForbidden(( "You are not allowed to make assessions " "in this assessment problem."))
def delete_model(request): model = ArtmModel.objects.get(id=request.GET['model']) if request.user != model.author: return HttpResponseForbidden( ("You are not the author! " "(<a href='/admin/models/artmmodel/%d/change/'>" "Delete as admin</a>)") % model.id) dataset_name = model.dataset.text_id if request.user != model.author: return HttpResponseForbidden("You are not the author of the model.") if 'sure' in request.GET and request.GET['sure'] == 'yes': ArtmModel.objects.filter(id=request.GET['model']).delete() return general_views.message( request, "Model was deleted. <a href ='/dataset?dataset=" + dataset_name + "'> Return to dataset</a>.") else: return general_views.message( request, "Are you sure that you want delete model " + str(model) + " permanently?<br>" + "<a href = '/models/delete_model?model=" + str(model.id) + "&sure=yes'>Yes</a><br>" + "<a href = '/dataset?dataset=" + dataset_name + "'>No</a>")
def login_view(request): if request.method == 'GET': return render(request, 'accounts/login.html') username = request.POST['username'] password = request.POST['password'] user = authenticate(username=username, password=password) if user is not None: if user.is_active: login(request, user) try: return redirect(request.GET["next"]) except BaseException: return redirect("/") else: return general_views.message( request, ("Disabled account. " "<a href='/accounts/login'>Try again</a>.")) else: return general_views.message( request, ("Invalid login. " "<a href='/accounts/login'>Try again</a>."))
def reload_model(request): try: model = ArtmModel.objects.get(id=request.GET['model']) except BaseException: model = ArtmModel.objects.get(id=request.GET['id']) if request.user != model.author: return HttpResponseForbidden("You are not the author") if model.status == 1: return general_views.message(request, "Model is locked.") model.creation_time = datetime.now() model.status = 1 model.save() model.prepare_log() t = Thread(target=ArtmModel.reload_untrusted, args=(model, ), daemon=True) t.start() return redirect("/model?model=" + str(model.id))
def task(request): if request.method == 'POST': task = AssessmentTask.objects.get(id=request.POST["task_id"]) if "finished" in request.POST: task.finalize(request.POST) task.completion_time = datetime.now() task.status = 2 task.save() if ("continue" in request.POST and request.POST["continue"] == "true"): return redirect( "/assessment/get_task?problem_id=%d" % task.problem.id) else: return general_views.message( request, "Thank you! Assess one more task?<br>" + "<a href='/assessment/get_task?problem_id=" + str( task.problem.id) + "'>Yes</a>.<br>" + "<a href='/dataset?dataset=" + task.problem.dataset.text_id + "'>No</a>.<br>") else: task.alter(request.POST) return redirect("/assessment/task?task_id=" + str(task.id)) try: task = AssessmentTask.objects.get(id=request.GET["task_id"]) except BaseException: return HttpResponseForbidden("Task doen't exist or was timed out.") if task.assessor != request.user: return HttpResponseForbidden("This is not your task.") if task.status == 2: return HttpResponseForbidden("Task is already completed.") return render( request, os.path.join( "assessment", task.problem.type, "assessor.html"), Context( task.get_view_context()))
def show_research(request, research_id): research = Research.objects.get(id=research_id) if research.is_private and research.researcher != request.user: return HttpResponseForbidden( "You are not authorized to see this report.") if research.status == 3: error_message = research.error_message.replace("\n", "<br>") return general_views.message( request, ("Error during research.<br>%s<br>" "<a href='/research/rerun?id=%d'>Rerun</a>") % (error_message, research.id) ) with open(research.get_report_file(), "r", encoding="utf-8") as f: response = HttpResponse(f.read(), content_type='text/html') ''' if research.status == 1: response['Refresh'] = "10" ''' return response
def visual_model(request): model = ArtmModel.objects.get(id=request.GET['model']) if model.status != 0: if model.status == 1 or model.status == 11: return general_views.wait(request, model.read_log(), model.creation_time) if model.status == 2: error_message = model.error_message.replace('\n', "<br>") return general_views.message( request, ("Model is bad. Error occured.<br>%s<br>" "<a href='/models/delete_model?model=%d'>" "Delete this model</a><br>" "<a href='/models/reload_model?model=%d'>" "Reload this model</a><br>") % (error_message, model.id, model.id)) if model.status == 3: return redirect('/models/settings?model_id=%d' % model.id) if 'matrices' in request.GET: try: head = int(request.GET['matrices']) except BaseException: head = 10 import pandas as pd ret = "" ret += "MATRIX PHI<br>" ret += pd.read_pickle(os.path.join(model.get_folder(), "phi"))[0:head].to_html() + "<br>" phi = model.get_phi() for i in range(head): for j in range(phi.shape[1]): ret += ("%.05f " % phi[i][j]) ret += "<br>" ret += "<br><br><br>MATRIX THETA<br>" ret += pd.read_pickle(os.path.join(model.get_folder(), "theta"))[0:head].to_html() theta = model.get_theta() for i in range(min(theta.shape[0], head)): for j in range(min(theta.shape[1], head)): ret += ("%.02e " % theta[i][j]) ret += "<br>" return HttpResponse(ret) topics_count = model.topics_count.split() topics = Topic.objects.filter(model=model) topics_layers = [{ "i": i + 1, "topics_count": topics_count[i + 1], "topics": topics.filter(layer=i + 1).order_by("spectrum_index") } for i in range(0, model.layers_count)] template = loader.get_template('models/model.html') context = Context({'model': model, 'topics_layers': topics_layers}) from algo.arranging.metrics import metrics_list context["metrics"] = metrics_list return render(request, 'models/model.html', context)
def clear(request): GlobalVisualization.objects.all().delete() Polygon.objects.all().delete() return general_views.message(request, "Cleared.")
def visual_global(request): model = ArtmModel.objects.get(id=request.GET['model']) dataset = model.dataset visual_name = request.GET['type'] if model is None: return general_views.message( request, "You have to create model first.<br>" + "<a href='/models/create?dataset=" + dataset.text_id + "'>Create model</a><br>" + "<a href='/dataset?dataset=" + dataset.text_id + "'>Return to dataset</a><br>") if 'try' in request.GET and request.GET['try'] == 'again': GlobalVisualization.objects.filter(model=model, name=visual_name).delete() return redirect("/visual/global?type=" + visual_name + "&model=" + str(model.id)) try: visualization = GlobalVisualization.objects.filter(model=model, name=visual_name)[0] except BaseException: visualization = GlobalVisualization() visualization.name = visual_name visualization.model = model visualization.status = 0 visualization.save() if settings.THREADING: t = Thread(target=GlobalVisualization.render_untrusted, args=(visualization, ), daemon=True) t.start() else: # print("RENDER") visualization.render() if visualization.status == 0: return general_views.wait(request, "Pending...", visualization.start_time, period="2") elif visualization.status == 2: return general_views.message( request, "Error during rendering.<br>" + visualization.error_message.replace('\n', "<br>") + "<br><br><a href='/visual/global?type=" + visual_name + "&model=" + str(model.id) + "&try=again'>Try again</a>") data_file_name = os.path.join(model.get_visual_folder(), visual_name + ".txt") with open(data_file_name, "r", encoding='utf-8') as f: data = f.read() context = Context({ 'dataset': dataset, 'model': model, 'data': data, 'no_footer': True }) return render(request, "visual/" + visual_name.split('_')[0] + ".html", context)