def survey_answer(request, lang=0): ''' function to save user response and return survey results ''' response = '' user_answer = request.POST.get('answer', '') if user_answer: r = Response(answer_id=user_answer) r.save() values = [] total = 0 user_selected_votes = 0 survey_answers = Answer.objects.filter(question=r.answer.question) for row in survey_answers: votes = Response.objects.filter(answer=row).count() total += votes if str(row.id) == str(user_answer): user_selected_votes = votes values.append({ 'value': row.value, 'votes': votes, }) percentage = user_selected_votes * 100 / total response = { 'status': True, 'percentage': percentage, 'survey': values, 'answer': r.answer.value } else: response = {'status': False} return HttpResponse(json.dumps(response), content_type="application/json")
def create_text_response(self, question, respondant, when, text): text_response = Response(question=question, respondant=respondant, ts=when, answer_raw=json.dumps({'text': text})) text_response.save() return text_response
def create_grid_response(self, question, respondant, when, prices): grid_response = Response(question=question, respondant=respondant, ts=when, answer_raw=self.cost_grid.format(*prices)) grid_response.save() return grid_response
def survey_answer(request, lang=0): ''' function to save user response and return survey results ''' response = '' user_answer = request.POST.get('answer', '') if user_answer: r = Response(answer_id=user_answer) r.save() values = [] total = 0 user_selected_votes = 0 survey_answers = Answer.objects.filter(question=r.answer.question) for row in survey_answers: votes = Response.objects.filter(answer=row).count() total += votes if str(row.id) == str(user_answer): user_selected_votes = votes values.append({ 'value': row.value, 'votes': votes, }) percentage = user_selected_votes * 100 / total response = {'status': True, 'percentage': percentage, 'survey': values, 'answer': r.answer.value} else: response = {'status': False} return HttpResponse(json.dumps(response), content_type="application/json")
def create_multi_select_response(self, question, respondant, when, answers): raw_answer = [{'text': a, 'label': slugify(unicode(a)), 'checked': True, 'isGroupName': False} for a in answers] ms_response = Response(question=question, respondant=respondant, ts=when, answer_raw=json.dumps(raw_answer)) ms_response.save() return ms_response
def setUp(self): self.survey = Survey.objects.get(slug='reef-fish-market-survey') self.question = self.survey.questions.get(slug='survey-site') self.respondant_user = User.objects.get(username='******') self.ts = datetime.datetime(2013, 11, 30).replace(tzinfo=utc) self.respondant = Respondant(survey=self.survey, ts=self.ts, surveyor=self.respondant_user) response = Response(question=self.question, respondant=self.respondant) response.answer_raw = json.dumps({'text': 'Fishing Village'}) self.respondant.save() response.save()
def create_multi_select_response(self, question, respondant, when, answers): raw_answer = [{ 'text': a, 'label': slugify(unicode(a)), 'checked': True, 'isGroupName': False } for a in answers] ms_response = Response(question=question, respondant=respondant, ts=when, answer_raw=json.dumps(raw_answer)) ms_response.save() return ms_response
def user_votting(): poll_id = None try: poll_id = request.get_json()['id'] poll_id = int(poll_id) except: return json.dumps({'status':'unsuccess','message':"Bad socket message"}) finally: if poll_id is None: return json.dumps({'status':'unsuccess','message':"Bad socket message"}) poll = Poll.query.filter_by(id = poll_id).first() category_id = poll.category_id responses = Response.query.filter_by(user_id=current_user.id) for response in responses: if response.category_id is category_id: return json.dumps({'status':'unsuccess','message':"You have voted"}) response = Response(user_id=current_user.id, category_id=category_id,pool_id=poll_id) db.session.add(response) rank = poll.rank poll.rank = rank + 1 db.session.commit() return json.dumps({'status':'OK','message':"Vote success"})
def post(self): # read args = parser.parse_args() json_string = args['json'] # validate try: o = json.loads(json_string) except ValueError: return "JSON string could not be parsed", 400 else: json_string = json.dumps(o) # store response = Response(json_string) db.session.add(response) db.session.commit() return json_string, 201
def setUp(self): self.Sv = Survey() self.Sv.name = "Plantas" self.Sv.description = "ya jale todo" self.Sv.save() self.Qs = Question() self.Qs.question_text = "¿es GG?" self.Qs.survey = self.Sv self.Rs = Response() self.name = "Carlos" self.survey = self.Sv self.BRs = BaseResponse() self.BRs.response = self.Rs self.BRs.question = self.Qs self.BRs.response_text = "Al año con fuerza"
def create_respondant(self, when, market, prices): respondant = Respondant(survey=self.survey, ts=when, surveyor=self.user) response_a = Response(question=self.question_a, respondant=respondant, ts=when) response_a.answer_raw = json.dumps({'text': market}) response_b = Response(question=self.question_b, respondant=respondant, ts=when) response_b.answer_raw = self.cost_grid.format(*prices) respondant.save() response_a.save() response_b.save() respondant.responses.add(response_a) respondant.responses.add(response_b) respondant.save() return respondant
def handle(self, *args, **options): respondants = Respondant.objects.filter(test_data=True) Response.objects.filter(respondant__in=respondants).delete() respondants.delete() survey = Survey.objects.get(slug='reef-fish-market-survey') market_question = Question.objects.get( slug='survey-site', survey=survey) volume_question = Question.objects.get( slug='total-weight', survey=survey) origin_question = Question.objects.get( slug='province-purchased-caught', survey=survey) cost_question = Question.objects.get(slug='cost', survey=survey) date_question = Question.objects.get( slug='survey-date', survey=survey) users = [] for i in range(7): try: user = User.objects.get(username='******'.format(i)) users.append(user) except User.DoesNotExist: users.append(User.objects.create_user(username='******'.format(i), first_name='user', last_name=i, password='******')) for i in range(100): date = datetime.datetime.utcnow() date = datetime.date(year=date.year, month=date.month, day=date.day) + datetime.timedelta(-randint(0, 365)) respondant = Respondant(survey=survey, test_data=True, ts=date, surveyor=choice(users)) market_response = Response( question=market_question, respondant=respondant) volume_response = Response( question=volume_question, respondant=respondant, ts=date) origin_response = Response( question=origin_question, respondant=respondant) date_response = Response( question=date_question, respondant=respondant) cost_response = Response( question=cost_question, respondant=respondant) volume_response.answer_raw = simplejson.dumps(randint(1, 1000)) date_response.answer_raw = simplejson.dumps(date.strftime('%d/%m/%Y')) market_response.answer_raw = simplejson.dumps( {'text': centers[randint(0, len(centers) - 1)]}) origin_response.answer_raw = simplejson.dumps( {'text': provinces[randint(0, len(provinces) - 1)]}) cost_response.answer_raw = cost_grid % (randint(1, 20), randint(1, 20), randint( 1, 20), randint(1, 20), randint(1, 20), randint(1, 20), randint(1, 20)) print date respondant.save() market_response.save() volume_response.save() origin_response.save() date_response.save() cost_response.save() volume_response.ts = date volume_response.save() respondant.response_set.add(market_response) respondant.response_set.add(volume_response) respondant.response_set.add(origin_response) respondant.response_set.add(date_response) respondant.response_set.add(cost_response) respondant.save()
def add_survey(survey_file_path, survey_anime_file_path, survey_late_adds_file_path, year, quarter, is_preseason): survey_queryset = Survey.objects.filter(year=year, season=quarter, is_preseason=is_preseason) if len(survey_queryset) > 0: survey = survey_queryset[0] print('Found pre-existing survey: "%s"' % str(survey)) answer = input('Delete this survey and continue? (Y/N)').lower() while answer != 'y' and answer != 'n': answer = input('Delete this survey and continue? (Y/N)').lower() if answer == 'n': return deletion_count, deletion_dict = Response.objects.filter(survey=survey).delete() print('Deleted %i responses: %s' % (deletion_count, str(deletion_dict))) else: survey = Survey( year=year, season=quarter, is_preseason=is_preseason, is_ongoing=False, ) survey.save() print('Created a new survey: "%s"' % survey) # +------------------+ # | GET SURVEY ANIME | # +------------------+ print('Reading survey anime') fa = open(survey_anime_file_path, 'r', encoding='utf8') fa.readline() anime_series_map = {} special_anime_map = {} for line in fa: split = line.split('\t') series_str = split[0].strip() if series_str and not series_str.isspace(): anime_series = find_accompanying_anime(series_str.split(' | '), True) anime_series_map[series_str] = anime_series special_str = split[1].strip() if special_str and not special_str.isspace(): special_anime = find_accompanying_anime(special_str.split(' | '), False) special_anime_map[special_str] = special_anime # +---------------+ # | GET LATE ADDS | # +---------------+ if survey_late_adds_file_path: print('Reading late adds') fl = open(survey_late_adds_file_path, 'r', encoding='utf8') fl.readline() for line in fl: split = line.split('\t') anime_str_maybe = split[0].strip() add_response_count_str_maybe = split[1].strip() search_int = re.search(r'\d+', add_response_count_str_maybe) if search_int: add_response_count = int(search_int.group()) if anime_str_maybe in anime_series_map.keys(): anime = anime_series_map[anime_str_maybe] elif anime_str_maybe in special_anime_map.keys(): anime = special_anime_map[anime_str_maybe] else: continue if not anime: continue sar = SurveyAdditionRemoval( anime=anime, is_addition=True, response_count=add_response_count, survey=survey, ) sar.save() print('Found late add: "%s" added at %i responses' % (str(anime), add_response_count)) # +---------------------+ # | READ SURVEY RESULTS | # +---------------------+ print('Reading survey results') f = open(survey_file_path, 'r', encoding='utf8') headers = f.readline().split('\t') animeresponse_list = [] line_ctr = 2 for line in f: split = line.split('\t') # +-----------------+ # | CREATE RESPONSE | # +-----------------+ timestamp_str = split[0] date_split = timestamp_str.split(' ')[0].split('/') time_split = timestamp_str.split(' ')[1].split(':') timestamp = datetime( year=int(date_split[2]), month=int(date_split[0]), day=int(date_split[1]), hour=int(time_split[0]), minute=int(time_split[1]), second=int(time_split[2]), ) age_str = split[1] age = float(age_str) if age_str != '' else None gender_str = split[2] if gender_str == 'Male': gender = Response.Gender.MALE elif gender_str == 'Female': gender = Response.Gender.FEMALE elif gender_str == 'Other': gender = Response.Gender.OTHER else: gender = None response = Response( timestamp=timestamp, survey=survey, age=age, gender=gender ) response.save() # +-------------------------+ # | GENERATE ANIMERESPONSES | # +-------------------------+ animeresponse_map = { anime: AnimeResponse(anime=anime, response=response, watching=False, underwatched=False) for anime in list(anime_series_map.values()) + list(special_anime_map.values()) if anime is not None } # Get watching anime watching_anime_str = split[3] watching_anime_list = parse_anime_strlist(watching_anime_str, anime_series_map) for anime in watching_anime_list: animeresponse_map[anime].watching = True # Get underwatched anime if post-season if not survey.is_preseason: underwatched_anime_str = split[4] underwatched_anime_list = parse_anime_strlist(underwatched_anime_str, anime_series_map) for anime in underwatched_anime_list: animeresponse_map[anime].underwatched = True header_idx = 4 if survey.is_preseason else 5 # Get anime scores while headers[header_idx].startswith('How good '): header = headers[header_idx].strip() start = header.index('[') end = header.rindex(']') anime_str = header[start+1:end].strip() anime = anime_series_map[anime_str] anime_score_str = split[header_idx].strip() if anime and anime_score_str and not anime_score_str.isspace() and anime_score_str != 'N/A': anime_score = int(anime_score_str[0]) animeresponse_map[anime].score = anime_score header_idx += 1 # Get surprises/disappointments while headers[header_idx].startswith('What are your '): header = headers[header_idx].strip() start = header.index('[') end = header.rindex(']') anime_str = header[start+1:end].strip() anime = anime_series_map[anime_str] expectations_str = split[header_idx].strip() if anime and expectations_str and not expectations_str.isspace() and expectations_str != 'N/A': expectations = AnimeResponse.Expectations.SURPRISE if expectations_str == 'Surprise' else AnimeResponse.Expectations.DISAPPOINTMENT animeresponse_map[anime].expectations = expectations header_idx += 1 # Get watching special anime watching_special_anime_str = split[header_idx] watching_special_anime_list = parse_anime_strlist(watching_special_anime_str, special_anime_map) for anime in watching_special_anime_list: animeresponse_map[anime].watching = True header_idx += 1 # Get special anime scores while header_idx < len(headers) and headers[header_idx].startswith('How good '): header = headers[header_idx].strip() start = header.index('[') end = header.rindex(']') anime_str = header[start+1:end].strip() anime = special_anime_map[anime_str] anime_score_str = split[header_idx].strip() if anime and anime_score_str and not anime_score_str.isspace() and anime_score_str != 'N/A': anime_score = int(anime_score_str[0]) animeresponse_map[anime].score = anime_score header_idx += 1 # Filter and save AnimeResponses for animeresponse in animeresponse_map.values(): if survey.is_preseason: keep_animeresponse = animeresponse.watching == True or animeresponse.score is not None else: keep_animeresponse = animeresponse.watching == True if keep_animeresponse: animeresponse_list.append(animeresponse) # Bulk create for performance (SQLite only allows max 999 variables per query, >800 to be safe) if len(animeresponse_list) > 800: AnimeResponse.objects.bulk_create(animeresponse_list) animeresponse_list.clear() print('Parsed response %i: "%s: %s %s, %s watching, %s special watching"' % (line_ctr, str(timestamp), str(age) if age is not None else '----', str(gender) if gender is not None else '-', str(len(watching_anime_list)), str(len(watching_special_anime_list)))) line_ctr += 1 #print('Watching anime: %s' % str('\n'.join([str(anime) for anime in watching_anime_list]))) # answer = input('Continue? (Y/N) ') # if answer.lower() == 'n': # break AnimeResponse.objects.bulk_create(animeresponse_list) animeresponse_list.clear()
def handle(self, *args, **options): respondants = Respondant.objects.filter(test_data=True) Response.objects.filter(respondant__in=respondants).delete() respondants.delete() survey = Survey.objects.get(slug='reef-fish-market-survey') market_question = Question.objects.get(slug='survey-site', survey=survey) volume_question = Question.objects.get(slug='total-weight', survey=survey) origin_question = Question.objects.get( slug='province-purchased-caught', survey=survey) cost_question = Question.objects.get(slug='cost', survey=survey) date_question = Question.objects.get(slug='survey-date', survey=survey) users = [] for i in range(7): try: user = User.objects.get(username='******'.format(i)) users.append(user) except User.DoesNotExist: users.append( User.objects.create_user(username='******'.format(i), first_name='user', last_name=i, password='******')) for i in range(100): date = datetime.datetime.utcnow() date = datetime.date( year=date.year, month=date.month, day=date.day) + datetime.timedelta(-randint(0, 365)) respondant = Respondant(survey=survey, test_data=True, ts=date, surveyor=choice(users)) market_response = Response(question=market_question, respondant=respondant) volume_response = Response(question=volume_question, respondant=respondant, ts=date) origin_response = Response(question=origin_question, respondant=respondant) date_response = Response(question=date_question, respondant=respondant) cost_response = Response(question=cost_question, respondant=respondant) volume_response.answer_raw = simplejson.dumps(randint(1, 1000)) date_response.answer_raw = simplejson.dumps( date.strftime('%d/%m/%Y')) market_response.answer_raw = simplejson.dumps( {'text': centers[randint(0, len(centers) - 1)]}) origin_response.answer_raw = simplejson.dumps( {'text': provinces[randint(0, len(provinces) - 1)]}) cost_response.answer_raw = cost_grid % (randint( 1, 20), randint(1, 20), randint(1, 20), randint( 1, 20), randint(1, 20), randint(1, 20), randint(1, 20)) print date respondant.save() market_response.save() volume_response.save() origin_response.save() date_response.save() cost_response.save() volume_response.ts = date volume_response.save() respondant.response_set.add(market_response) respondant.response_set.add(volume_response) respondant.response_set.add(origin_response) respondant.response_set.add(date_response) respondant.response_set.add(cost_response) respondant.save()