Skip to content

Commit

Permalink
Merge pull request EGCETSII#27 from Decide-Part-Penyagolosa-1/jesgaml…
Browse files Browse the repository at this point in the history
…ar-census

feature,fix: redireccion y acceso a votaciones ; EGCETSII#26
  • Loading branch information
josgrarom authored Jan 5, 2022
2 parents cb2869f + d8cea0f commit 7b5cb80
Show file tree
Hide file tree
Showing 6 changed files with 136 additions and 7 deletions.
115 changes: 115 additions & 0 deletions decide/authentication/templates/redirection.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@


{% extends "base.html" %}
{% load i18n static %}

{% block extrahead %}
<link type="text/css" rel="stylesheet"
href="https://unpkg.com/bootstrap/dist/css/bootstrap.min.css" />
<link type="text/css" rel="stylesheet"
href="https://unpkg.com/bootstrap-vue@latest/dist/bootstrap-vue.css" />
{% endblock %}

{% block content %}
<h1>Redirigiendo GitHub a la votación {{voting_id}}</h1>
<div id="app-auth">
<!-- Navbar -->
<b-navbar type="dark" variant="secondary">
<b-navbar-brand tag="h1">Decide</b-navbar-brand>
<b-navbar-nav class="ml-auto" v-if="!signup">
<b-nav-item href="#" v-on:click="decideLogout">
{% trans "logout" %}
</b-nav-item>
</b-navbar-nav>
</b-navbar>

<b-alert :variant="alertLvl" dismissible v-model="alertShow">
[[ alertMsg ]]
</b-alert>

</div>


{% endblock %}

{% block extrabody %}

<!-- Vuejs -->
<script src="https://unpkg.com/vue"></script>
<script src="https://unpkg.com/babel-polyfill@latest/dist/polyfill.min.js"></script>
<script src="https://unpkg.com/bootstrap-vue@latest/dist/bootstrap-vue.js"></script>

<script>
var app = new Vue({
delimiters: ['[[', ']]'],
el: '#app-auth',
data: {
selected: "",
signup: true,
alertShow: false,
alertMsg: "",
alertLvl: "info",
token: "{{token}}",
user: null
},
beforeMount() {
this.init();
},
methods: {
init() {
document.cookie = 'decide='+this.token+'; path=/';
this.getUser();
window.location.replace('{{callback}}');
},
postData(url, data) {
// Default options are marked with *
var fdata = {
body: JSON.stringify(data),
headers: {
'content-type': 'application/json',
},
method: 'POST',
};

if (this.token) {
fdata.headers['Authorization'] = 'Token ' + this.token;
}

return fetch(url, fdata)
.then(response => {
if (response.status === 200) {
return response.json();
} else {
return Promise.reject(response.statusText);
}
});
},
getUser(evt) {
var data = {token: this.token};
this.postData("{% url "gateway" "authentication" "/getuser/" %}", data)
.then(data => {
this.user = data;
this.signup = false;
}).catch(error => {
this.showAlert("danger", '{% trans "Error: " %}' + error);
});
},
decideLogout(evt) {
evt.preventDefault();
var data = {token: this.token};
this.postData("{% url "gateway" "authentication" "/logout/" %}", data);
this.token = null;
this.user = null;
document.cookie = 'decide=; path=/';
this.signup = true;
},
showAlert(lvl, msg) {
this.alertLvl = lvl;
this.alertMsg = msg;
this.alertShow = true;
}
},
})
</script>
</body>
{% endblock %}
3 changes: 2 additions & 1 deletion decide/authentication/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from django.views.generic import TemplateView
from django.contrib import admin
from django.contrib.auth.views import LogoutView
from .views import GetUserView, LogoutView2, RegisterView
from .views import GetUserView, LogoutView2, RegisterView, redirection

urlpatterns = [
path('login/', obtain_auth_token),
Expand All @@ -13,4 +13,5 @@
path('signin/',TemplateView.as_view(template_name="login.html")),
path('accounts/',include('allauth.urls')),
path('logoutG', LogoutView.as_view(), name="logoutG"),
path('redirection', redirection),
]
16 changes: 15 additions & 1 deletion decide/authentication/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from rest_framework.authtoken.models import Token
from django.contrib.auth.models import User
from django.db import IntegrityError
from django.shortcuts import get_object_or_404
from django.shortcuts import get_object_or_404, render
from django.core.exceptions import ObjectDoesNotExist

from .serializers import UserSerializer
Expand Down Expand Up @@ -53,3 +53,17 @@ def post(self, request):
except IntegrityError:
return Response({}, status=HTTP_400_BAD_REQUEST)
return Response({'user_pk': user.pk, 'token': token.key}, HTTP_201_CREATED)

def redirection(request):

user = request.user
session_token = Token.objects.get_or_create(user=user)[0]
host = request.get_host()
scheme = request.is_secure() and "https" or "http"
base_url = scheme+'://'+request.get_host()
context = {
"token":session_token.key,
"callback":base_url+'/booth/voting',
"host":host,
}
return render(request, 'redirection.html',context)
1 change: 0 additions & 1 deletion decide/booth/templates/booth/booth.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
href="https://unpkg.com/bootstrap/dist/css/bootstrap.min.css" />
<link type="text/css" rel="stylesheet"
href="https://unpkg.com/bootstrap-vue@latest/dist/bootstrap-vue.css" />
<link type="text/css" rel="stylesheet" href="{% static "booth/style.css" %}" />
{% endblock %}

{% block content %}
Expand Down
2 changes: 1 addition & 1 deletion decide/booth/templates/booth/votings.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
{% load i18n static %}

{% block content %}
<link rel="stylesheet" href="/static/css/style.css">

<table>
<tr>
<th>Name</th>
Expand Down
6 changes: 3 additions & 3 deletions decide/decide/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,10 +215,10 @@
'allauth.account.auth_backends.AuthenticationBackend'
)

SITE_ID = 2
SITE_ID = 3

LOGIN_REDIRECT_URL = '/'
LOGOUT_REDIRECT_URL = '/'
LOGIN_REDIRECT_URL = '/authentication/redirection'
LOGOUT_REDIRECT_URL = '/authentication/signin'


NOSE_ARGS = [
Expand Down

0 comments on commit 7b5cb80

Please sign in to comment.