-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
435 additions
and
108 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
Empty file.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# coding=utf-8 | ||
from django import forms | ||
from backend_candidate.models import Candidacy | ||
from merepresenta.models import Candidate | ||
|
||
|
||
class CPFAndDdnForm(forms.Form): | ||
cpf = forms.CharField(required=True) | ||
nascimento = forms.DateField(required=True, input_formats=['%d/%m/%Y','%d/%m/%y', '%d-%m-%Y', '%d-%m-%y',]) | ||
|
||
def __init__(self, *args, **kwargs): | ||
self.user = kwargs.pop('user') | ||
super(CPFAndDdnForm, self).__init__(*args, **kwargs) | ||
|
||
def clean(self): | ||
cpf = self.cleaned_data['cpf'] | ||
ddn = self.cleaned_data.get('nascimento', None) | ||
if ddn is None: | ||
raise forms.ValidationError(u'Não encontramos o candidato') | ||
try: | ||
self.candidate = Candidate.objects.get(cpf=cpf, data_de_nascimento=ddn) | ||
except Candidate.DoesNotExist: | ||
raise forms.ValidationError(u'Não encontramos o candidato') | ||
return self.cleaned_data | ||
|
||
def save(self): | ||
return Candidacy.objects.create(user=self.user, candidate=self.candidate) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
from social_django.strategy import DjangoStrategy | ||
from django.shortcuts import resolve_url | ||
from django.contrib.auth import authenticate | ||
|
||
|
||
class CandidateStrategy(DjangoStrategy): | ||
|
||
def get_setting(self, name): | ||
if name in ['NEW_USER_REDIRECT_URL', 'LOGIN_REDIRECT_URL', 'SOCIAL_AUTH_FACEBOOK_LOGIN_REDIRECT_URL']: | ||
return resolve_url('cpf_and_date') | ||
return super(CandidateStrategy, self).get_setting(name) | ||
|
||
def create_user(self, *args, **kwargs): | ||
user = super(CandidateStrategy, self).create_user(*args, **kwargs) | ||
return user | ||
|
||
def authenticate(self, backend, *args, **kwargs): | ||
kwargs['strategy'] = self | ||
kwargs['storage'] = self.storage | ||
kwargs['backend'] = backend | ||
if 'request' in kwargs: | ||
kwargs.pop('request') | ||
return authenticate(*args, **kwargs) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
|
||
from django.conf.urls import url, include | ||
from .views import (LoginView, | ||
CPFAndDDNSelectView, | ||
complete, | ||
auth | ||
) | ||
|
||
|
||
urlpatterns = [ | ||
url(r'^login/?$', | ||
LoginView.as_view(), | ||
name='candidate_login'), | ||
|
||
url(r'^cpf_e_ddn/?$', | ||
CPFAndDDNSelectView.as_view(), | ||
name='cpf_and_date'), | ||
url(r'^complete/(?P<backend>[^/]+)$', | ||
complete, | ||
name='candidate_social_complete' | ||
), | ||
url(r'^login/(?P<backend>[^/]+)/?$', auth, | ||
name='candidato_social_begin'), | ||
|
||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
from django.views.generic.edit import FormView | ||
from django.contrib.auth.mixins import LoginRequiredMixin | ||
from django.core.urlresolvers import reverse_lazy | ||
from django.http import HttpResponseNotFound, HttpResponseRedirect | ||
from backend_candidate.models import Candidacy | ||
from .forms import CPFAndDdnForm | ||
from social_core.actions import do_complete, do_auth | ||
from social_django.views import _do_login | ||
from django.views.decorators.cache import never_cache | ||
from django.views.decorators.csrf import csrf_exempt | ||
from social_django.utils import psa, STORAGE, get_strategy | ||
from django.views.generic.base import TemplateView | ||
from backend_candidate.models import is_candidate | ||
|
||
|
||
|
||
class LoginView(TemplateView): | ||
template_name="candidatos/login.html" | ||
|
||
def dispatch(self, *args, **kwargs): | ||
if self.request.user.is_authenticated: | ||
url = reverse_lazy('cpf_and_date') | ||
return HttpResponseRedirect(url) | ||
return super(LoginView, self).dispatch(*args, **kwargs) | ||
|
||
|
||
class CPFAndDDNSelectView(LoginRequiredMixin, FormView): | ||
form_class = CPFAndDdnForm | ||
template_name = 'candidatos/cpf_and_ddn.html' | ||
# login_url = reverse_lazy('') | ||
def dispatch(self, *args, **kwargs): | ||
if not self.request.user.is_authenticated: | ||
return HttpResponseRedirect(reverse_lazy('index')) | ||
if self.request.user.is_staff: | ||
return HttpResponseNotFound() | ||
try: | ||
c = Candidacy.objects.get(user=self.request.user) | ||
election = c.candidate.election | ||
candidate = c.candidate | ||
url = reverse_lazy('backend_candidate:complete_profile', kwargs={'slug': election.slug, 'candidate_slug': candidate.slug}) | ||
return HttpResponseRedirect(url) | ||
except Candidacy.DoesNotExist: | ||
pass | ||
|
||
return super(CPFAndDDNSelectView, self).dispatch(*args, **kwargs) | ||
|
||
def get_form_kwargs(self): | ||
original = super(CPFAndDDNSelectView, self).get_form_kwargs() | ||
original['user'] = self.request.user | ||
return original | ||
|
||
def form_valid(self, form): | ||
form.save() | ||
return super(CPFAndDDNSelectView, self).form_valid(form) | ||
|
||
|
||
def get_success_url(self): | ||
c = Candidacy.objects.get(user=self.request.user) | ||
election = c.candidate.election | ||
candidate = c.candidate | ||
url = reverse_lazy('backend_candidate:complete_profile', kwargs={'slug': election.slug, 'candidate_slug': candidate.slug}) | ||
return url | ||
|
||
|
||
|
||
def load_strategy(request=None): | ||
return get_strategy('merepresenta.candidatos.strategy.CandidateStrategy', STORAGE, request) | ||
|
||
@never_cache | ||
@csrf_exempt | ||
@psa(load_strategy=load_strategy, redirect_uri='candidate_social_complete') | ||
def complete(request, backend, *args, **kwargs): | ||
return do_complete(request.backend, _do_login, request.user, | ||
redirect_value='next', request=request, | ||
*args, **kwargs) | ||
|
||
@never_cache | ||
@psa('candidate_social_complete') | ||
def auth(request, backend): | ||
return do_auth(request.backend) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# -*- coding: utf-8 -*- | ||
# Generated by Django 1.11.9 on 2018-08-01 15:51 | ||
from __future__ import unicode_literals | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('merepresenta', '0013_candidate_data_de_nascimento'), | ||
] | ||
|
||
operations = [ | ||
migrations.AlterField( | ||
model_name='candidate', | ||
name='cpf', | ||
field=models.CharField(max_length=1024, unique=True), | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
{% extends "base.html" %} | ||
{% load staticfiles %} | ||
{% load i18n %} | ||
|
||
{% block title %}- Portada{% endblock title %} | ||
|
||
{% block content %} | ||
<div class="content"> | ||
{% if form.errors %} | ||
{{form.errors}} | ||
{% endif %} | ||
<form method='POST' action="{% url 'cpf_and_date' %}"> | ||
{% csrf_token %} | ||
<div class="form-group"> | ||
<label for="cpf-id">Informe seu CPF</label> | ||
<input id='cpf-id' class="form-control" name='cpf' aria-describedby="cpfhelp" placeholder="CPF" required="True"> | ||
<small id="cpfhelp" class="form-text text-muted"></small> | ||
</div> | ||
<div class="form-group"> | ||
<label for="nascimento-id">Sua data de nascimento</label> | ||
<input id='nascimento-id' class="form-control" name='nascimento' required="True" aria-describedby="helpNascimento" placeholder="Data de nascimento"> | ||
<small id="helpNascimento" class="form-text text-muted"></small> | ||
</div> | ||
<button type="submit" class="btn btn-primary">Submit</button> | ||
</form> | ||
</div> | ||
<script type="text/javascript"> | ||
$(function(){ | ||
var currentDate = new Date(new Date().getTime() + 24 * 60 * 60 * 1000); | ||
$("#nascimento-id").datepicker({ | ||
format: 'dd/mm/yyyy' | ||
}); | ||
}); | ||
|
||
</script> | ||
{% endblock content %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{% extends "base.html" %} | ||
{% load staticfiles %} | ||
{% load i18n %} | ||
|
||
{% block title %}- Portada{% endblock title %} | ||
|
||
{% block content %} | ||
<div class="container"> | ||
<a class="btn btn-block btn-facebook" href="{% url 'candidato_social_begin' 'facebook' %}"> | ||
<i class="fa fa-facebook"></i> {% trans 'Entra con Facebook' %} | ||
</a> | ||
</div> | ||
{% endblock content %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.