def statistics(request): issues = Issue.objects.all() stances = [] issue_votes = [] stance_votes = [] max_cols = 1 for issue in issues: stances.append(issue.stances) issue_votes.append(len(MessageTag.objects.filter(issue = issue))) stance_votes.append([len(MessageTag.objects.filter(stance = stance)) for stance in issue.stances]) max_cols = max(max_cols, len(issue.stances)) stance_percents = [] for i_vote, s_votes in zip(issue_votes, stance_votes): if not i_vote: stance_percents.append([0.0] * len(s_votes)) else: stance_percents.append([s_vote * 100.0 / i_vote for s_vote in s_votes]) context = utils.nav_context(request) stances_and_percents = [list(zip(stance, stance_pct)) for stance, stance_pct in zip(stances, stance_percents)] context["issues_and_stances"] = list(zip(issues, stances_and_percents)) context["max_cols"] = max_cols return HttpResponse(render(request, "messages/statistics.html", context = context))
def politicians(request): context = utils.nav_context(request) context["connected_politicians"] = request.user.politicians_filtered context["requesting_politicians"] = request.user.politician_filter_requests return HttpResponse( render(request, "users/politicians.html", context=context))
def new(request): context = utils.nav_context(request) if request.method == "POST": message = request.POST["message"] politician_id = request.POST["politician"] is_public = "is_public" in request.POST context["message"] = message.strip() context["politician_id"] = politician_id context["is_public"] = is_public if len(message) < 30: context["error_messages"] = "Your message must be at least 30 characters long." elif len(message) > 65535: context["error_messages"] = "Your message must be less than 65,535 characters long." else: message = Message(text = message, sender = request.user, recipient = get_user_model().objects.get(pk = politician_id), is_public = is_public) try: message.save() except ValidationError as ex: context["error_messages"] = str(ex.message) else: return HttpResponseRedirect("/") context["politicians"] = list(get_user_model().objects.filter(user_type = "politician")) return HttpResponse(render(request, "messages/new.html", context = context))
def signup(request): if request.user.is_authenticated: return HttpResponseRedirect(reverse("auth:index")) context = utils.nav_context(request) if request.method == "POST": username = request.POST["username"] firstname = request.POST["firstname"] lastname = request.POST["lastname"] email = request.POST["email"] filterer = "filterer" in request.POST context["username"] = username context["firstname"] = firstname context["lastname"] = lastname context["email"] = email context["filterer"] = filterer if not username.strip(): context["error_messages"] = "You have to type a username." elif len(username) > 30: context[ "error_messages"] = "Sorry, but your username can't be more than 30 characters." else: same_username = get_user_model().objects.filter(username=username) if len(list(same_username)): context[ "error_messages"] = "Sorry, somebody else has the same username." else: if not firstname.strip(): context[ "error_messages"] = "You have to type a first name." elif not lastname.strip(): context["error_messages"] = "You have to type a last name." else: if not email.strip(): context[ "error_messages"] = "You have to type an email address." else: if request.POST["password"] != request.POST[ "password2"]: context[ "error_messages"] = "Sorry, those passwords don't match." elif not request.POST["password"].strip(): context[ "error_messages"] = "You need to enter a password" else: user = get_user_model().objects.create_user( first_name=firstname, last_name=lastname, username=username, email=email, password=request.POST["password"], user_type="filterer" if filterer else "person") user.save() return HttpResponseRedirect(reverse("auth:index")) return HttpResponse( render(request, "registration/signup.html", context=context))
def filterers(request): context = utils.nav_context(request) context["connected_filterers"] = request.user.connected_filterers context["requested_filterers"] = request.user.requested_filterers context["available_filterers"] = list( get_user_model().objects.filter(user_type="filterer")) for filterer in itertools.chain(context["connected_filterers"], context["requested_filterers"]): if filterer in context["available_filterers"]: context["available_filterers"].remove(filterer) return HttpResponse( render(request, "users/filterers.html", context=context))
def filter(request, politician_id): if request.method == "POST": message_id = request.POST["message_id"] issue_id = request.POST["issue_id"] stance_id = request.POST["stance_id"] message = Message.objects.get(pk = message_id) issue = Issue.objects.get(pk = issue_id) stance = Stance.objects.get(pk = stance_id) tag = MessageTag(filterer = request.user, message = message, issue = issue, stance = stance) tag.save() context = utils.nav_context(request) politician = get_user_model().objects.get(pk = politician_id) messages = Message.objects.filter(recipient = politician) context["messages"] = [message for message in messages if message.tag is None] context["politician"] = politician issues = Issue.objects.all() stances = {issue.id: [[stance.id, stance.name] for stance in issue.stances] for issue in issues} context["issues"] = issues context["json_issues_stances"] = json.dumps(stances) return HttpResponse(render(request, "messages/list_to_filter.html", context = context))
def login(request): return auth_views.login(request, "registration/login.html", extra_context=utils.nav_context(request))
def index(request): return HttpResponse( render(request, "index.html", context=utils.nav_context(request)))
def show(request, user_id): context = utils.nav_context(request) context["user"] = get_user_model().objects.get(pk=user_id) return HttpResponse(render(request, "users/show.html", context=context))
def sent_by_me(request): context = utils.nav_context(request) messages = Message.objects.filter(sender = request.user) context["messages"] = messages return HttpResponse(render(request, "messages/list_mine.html", context = context))
def list_public(request): context = utils.nav_context(request) context["messages"] = Message.objects.filter(is_public = True) return HttpResponse(render(request, "messages/public.html", context = context))