def post(self, project_id): data = json.loads(self.request.body) project = Project.get(project_id) participant = Participant(name=data.get('name'), note=data.get('note'), project=project, json=DEFAULT_JSON) participant.put() self.response.write(json.dumps(participant.to_dict()))
def post(self, project_id): data = json.loads(self.request.body) project = Project.get(project_id) participant = Participant( name=data.get('name'), note=data.get('note'), project=project, json=DEFAULT_JSON) participant.put() self.response.write(json.dumps(participant.to_dict()))
def get(self, project_id, participant_id=None): if participant_id: participant = Participant.get(participant_id) if participant.deleted_at is not None: self.error(404) self.response.write(json.dumps(participant.to_dict())) else: project = Project.get(project_id) participants = Participant.all()\ .filter('project =', project)\ .order('created_at') self.response.write(json.dumps([p.to_dict() for p in participants]))
def init_db_with_participants(): """ Initialises a local test db and removes it after testing is finished. """ db.drop_all() db.create_all() User.create_user("username", "password") Consent.create_consent("John", "Doe", "Signature") Participant.create_participant(1, "Ionut", "Cons", 15, Gender.Jongen.value, [Ethnicity.Nederlands.value], "", Version.A) yield db db.session.close() db.drop_all()
def put(self, project_id, participant_id): data = json.loads(self.request.body) participant = Participant.get(participant_id) participant.name = data.get('name') participant.note = data.get('note') participant.put() self.response.write(json.dumps(participant.to_dict()))
def login_view(request): context = { 'loginpage': settings.LOGIN_URL, 'error_message': None, 'app_version': settings.APP_VERSION, 'data': {} } next_url = request.GET.get('next', '/panel/') if request.user.is_authenticated: return redirect(next_url) if request.method == 'POST': username = request.POST.get('username') password = request.POST.get('password') user = authenticate(username=username, password=password) if user is not None: if user.is_active and Participant.exists(user): login(request, user) return redirect(next_url) else: context['error_message'] = 'Пользователь не активен' else: context['error_message'] = 'Неправильный логин или пароль' # Сохранить для удобства context['data']['username'] = username return render(request, 'panel/login.html', context)
def test_stats_gender(client, init_db_with_participants): """ Tests the gender distribution from /stats endpoint :param client: Testing client for flask :param init_db_with_participants: Initialize database :return: Nothing """ login_response = client.post("/login", data=dict(username='******', password='******')) assert login_response.status_code == 200 token = login_response.get_json()['access_token'] # create a new participant with no gender Participant.create_participant(1, "New", "Participant", 15, quiz_version=Version.A) response = client.get("/stats", headers={'Authorization': 'Bearer ' + token}) assert response.status_code == 200 assert response.get_json()['gender_distribution'][-1]['gender'] == "Not known" assert response.get_json()['gender_distribution'][-1]['number'] == 1
def post(self, project_id): data = json.loads(self.request.body) project = Project.get(project_id) participants = Participant.all().filter('project =', project) grid = merge_grids(participants) questionnaire = [node['text'] for node in grid['nodes']] analysis = Analysis( name=data.get('name'), grid=json.dumps(grid), questionnaire=json.dumps(questionnaire), project=project) analysis.put() self.response.write(json.dumps(analysis.to_dict()))
def index_view(request): name_surname = None try: name_surname = Participant.get_name_surname(request.user) except Participant.DoesNotExist: logout_view(request) context = { # 'app_version': settings.APP_VERSION 'app_version': misc.random_string(), 'name_surname': name_surname } return render(request, 'panel/index.html', context)
def signup_view(request): context = { 'app_version': settings.APP_VERSION, 'error_message': None, 'data': {}, 'created': False } next_url = request.GET.get('next', '/panel/') if request.user.is_authenticated: return redirect(next_url) if request.method == 'POST': name = request.POST.get('name') surname = request.POST.get('surname') email = request.POST.get('email').lower() password = request.POST.get('password') password2 = request.POST.get('password2') # Сохранить для удобства context['data']['name'] = name context['data']['surname'] = surname context['data']['email'] = email if password != password2: context['error_message'] = 'Пароли не совпадают' return render(request, 'panel/signup.html', context) if Participant.email_exists(email): context['error_message'] = 'Пользователь с данным email уже существует' return render(request, 'panel/signup.html', context) user = User.objects.create_user(username=email, password=password) Participant.objects.create( user=user, name=name, surname=surname, email=email) login(request, user) return redirect(next_url) return render(request, 'panel/signup.html', context)
def post(self): """ On a post request on the /form endpoint we add the consent form to the database :return: If the request is valid, a 200 status code, otherwise a 400 code """ validators = { 'parent': valid.validate_person_data, 'children': valid.validate_children_data, 'signature': valid.validate_signature, 'email': valid.validate_email } data = valid.validate(valid.read_form_data(request), validators) if not data: return ANSWERS[400], 400 parent = data['parent'] signature = data['signature'] if not current_app.config['TESTING']: upload_result = upload(signature, folder="signatures") signature = upload_result["secure_url"] cons = Consent.create_consent(parent_first_name=parent['firstName'], parent_last_name=parent['lastName'], signature=signature, email=data['email']) for child in data['children']: participant = Participant(first_name=child['firstName'], last_name=child['lastName'], consent_id=cons.id) add_to_db(participant) obj = { "firstName": participant.first_name, "lastName": participant.last_name, "id": participant.id } red.rpush("queue", str(obj)) socketio.emit("free-laptops") return ANSWERS[200], 200
def get(self, project_id): project = Project.get(project_id) participants = Participant.all().filter('project =', project) self.response.write(json.dumps(merge_grids(participants)))
def delete(self, project_id, participant_id): participant = Participant.get(participant_id) participant.remove()
def put(self, project_id, participant_id): participant = Participant.get(participant_id) participant.json = self.request.body.decode('utf-8') participant.put() self.response.write(json.dumps(participant.to_dict()))
def get(self, project_id, participant_id): participant = Participant.get(participant_id) result = json.loads(participant.json) result['projectKey'] = project_id result['participantKey'] = participant_id self.response.write(json.dumps(result))