def save_information(request: WSGIRequest) -> Poll: try: category = request.POST['category'] except MultiValueDictKeyError: return None try: poll_id = int(request.POST['pollId']) poll = Poll.objects.get(id=poll_id) except (MultiValueDictKeyError, ObjectDoesNotExist, ValueError): poll = Poll() if category == "editor": poll = _create_or_change_poll(request, poll) if poll.new_template is None: try: template_id = int(request.POST['templateId']) template = TemplatesPoll.objects.get(id=template_id) except (ObjectDoesNotExist, ValueError, MultiValueDictKeyError): pass else: poll.new_template = template poll.save() version = _create_new_questions_or_change(request, poll) poll.questions.all().exclude(version=version).delete() return poll elif category == "preview": return poll return None
def poll_create(request): if auth.get_user(request).is_anonymous: return redirect('/') profile = get_user_profile(request) new_poll = Poll() new_poll.initiator = profile new_poll.save() result = get_render_poll(request, new_poll) return result
def _create_unique_key(poll: Poll): poll_id_changed = poll.pk % 1000 + 1000 initiator_id_changed = poll.initiator.id % 1000 + 1000 target_id_changed = poll.initiator.id % 1000 + 1000 date = datetime.today() date_changed_str = '{}{}{}{}{}{}{}'.format(date.day, date.month, date.year, date.hour, date.minute, date.second, date.microsecond) key = '{}{}{}{}'.format(poll_id_changed, initiator_id_changed, target_id_changed, date_changed_str) poll.key = key poll.save()
def poll_preview(request: WSGIRequest) -> JsonResponse: try: poll_id = int(request.POST['pollId']) poll = Poll.objects.get(id=poll_id) poll = _create_or_change_poll(request, poll) except (MultiValueDictKeyError, ObjectDoesNotExist, ValueError): poll = _create_or_change_poll(request, Poll()) version = _create_new_questions_or_change(request, poll) if poll.new_template is None: try: template_id = int(request.POST['templateId']) template = TemplatesPoll.objects.get(id=template_id) except (ObjectDoesNotExist, ValueError, MultiValueDictKeyError): pass else: poll.new_template = template poll.save() poll.questions.all().exclude(version=version).delete() created_poll = _build_poll(poll) if poll.target is not None: created_poll['target'] = { 'name': poll.target.name, 'surname': poll.target.surname, 'patronymic': poll.target.patronymic } content = SimpleTemplateResponse('main/poll/taking_poll_preview.html', { 'poll': created_poll }).rendered_content return JsonResponse({'content': content, 'pollId': poll.pk}, status=200)
def _create_or_change_poll(request: WSGIRequest, poll: Poll) -> Poll: data = request.POST data_key = 'template[{}]' poll.name_poll = data[data_key.format('name')] poll.description = data[data_key.format('description')] poll.color = data.get(data_key.format('color'), None) poll.creation_date = datetime.today() profile = get_user_profile(request) if not SurveyWizard.objects.filter(profile=profile).exists(): poll.target = profile poll.initiator = profile poll.save() return poll
def poll_create_from_team(request, team_id): if auth.get_user(request).is_anonymous: return redirect('/') team = Group.objects.filter(id=team_id).first() if team is None: return render(request, 'main/errors/global_error.html', {'global_error': '404'}) profile = get_user_profile(request) if not team.profile_set.filter(id=profile.pk).exists() and \ not SurveyWizard.objects.filter(profile=profile).exists(): return render(request, 'main/errors/global_error.html', {'global_error': '403'}) new_poll = Poll() new_poll.initiator = profile new_poll.team = team new_poll.save() return get_render_poll(request, new_poll)
def create_new_poll_from_company(request, company_id): if auth.get_user(request).is_anonymous: return redirect('/') company = Company.objects.filter(id=company_id).first() if company is None: return render(request, 'main/errors/global_error.html', {'global_error': '404'}) profile = get_user_profile(request) if not company.profile_set.filter(id=profile.pk).exists(): return render(request, 'main/errors/global_error.html', {'global_error': '403'}) new_poll = Poll() new_poll.initiator = profile new_poll.company = company new_poll.save() return get_render_poll(request, new_poll)
def add_poll(channel: str, question: str, options: List[str]) -> Poll: poll = Poll(channel=channel, question=question, options=options) poll.save() return poll