Skip to content

Commit

Permalink
Merge pull request #19 from Fer-Bar/feature/auth-methods
Browse files Browse the repository at this point in the history
Add authentication app for login and logout functionality; update templates
  • Loading branch information
Fer-Bar authored May 26, 2024
2 parents 469e0be + 6bb6049 commit 419f7b7
Show file tree
Hide file tree
Showing 17 changed files with 136 additions and 31 deletions.
6 changes: 5 additions & 1 deletion albercan_backend/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@
"django.contrib.staticfiles",
"people",
"pet",
"structure"
"structure",
"authentication"
]

MIDDLEWARE = [
Expand Down Expand Up @@ -143,3 +144,6 @@
# https://docs.djangoproject.com/en/5.0/ref/settings/#default-auto-field

DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"

LOGIN_REDIRECT_URL = "/index"
LOGOUT_REDIRECT_URL = "/auth/login"
9 changes: 9 additions & 0 deletions albercan_backend/static/css/navbar.css
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,13 @@ header .navbar .dropdown-menu .dropdown-item.active:hover {
header .navbar .dropdown-menu .dropdown-item.active {
background: #bc5148;
color: #fff;
}

#logout-form {
display: inline;
}
#logout-form button {
background: none;
border: none;
cursor: pointer;
}
1 change: 1 addition & 0 deletions albercan_backend/templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
<!-- FontAwesome -->
<script src="https://kit.fontawesome.com/31b739dec4.js" crossorigin="anonymous"></script>
<link rel="stylesheet" href={% static "css/base.css" %}>
<link rel="stylesheet" href={% static "css/navbar.css" %}>
<!-- Additional Styling -->
{% block styles %}{% endblock %}
</head>
Expand Down
8 changes: 7 additions & 1 deletion albercan_backend/templates/navbar.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<!-- navbar.html -->
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<div class="container">
<a class="navbar-brand" href="#">AlberCan</a>
<a class="navbar-brand" href="{% url 'pet:main_page' %}">AlberCan</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse"
data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent"
aria-expanded="false" aria-label="Toggle navigation">
Expand Down Expand Up @@ -30,6 +30,12 @@
<li class="nav-item">
<a class="nav-link" href="#">Contáctanos</a>
</li>
<li class="nav-item">
<form id="logout-form" method="post" action="{% url 'authentication:logout' %}">
{% csrf_token %}
<button type="submit" class="nav-link">Cerrar Sesión</button>
</form>
</li>
</ul>
</div>
</div>
Expand Down
4 changes: 3 additions & 1 deletion albercan_backend/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
from django.urls import include, path

urlpatterns = [
path('', include('pet.urls')),
path('', include('pet.urls', namespace='pet')),
path('', include('people.urls', namespace='people')),
path('auth/', include('authentication.urls', namespace='authentication')),
path("admin/", admin.site.urls),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
File renamed without changes.
6 changes: 6 additions & 0 deletions authentication/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.apps import AppConfig


class AuthenticationConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'authentication'
5 changes: 5 additions & 0 deletions authentication/forms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from django.contrib.auth.forms import AuthenticationForm


class UserLoginForm(AuthenticationForm):
pass
Empty file.
30 changes: 30 additions & 0 deletions authentication/templates/authentication/login_page.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{% extends "base.html" %}
{% load static %}

{% block title %}
Página de Inicio de Sesión
{% endblock %}

{% block styles %}
{% endblock %}

{% block content %}
{% if messages %}
<ul class="messages">
{% for message in messages %}
<li{% if message.tags %} class="{{ message.tags }}"{% endif %}>{{ message }}</li>
{% endfor %}
</ul>
{% endif %}

<form action="" method="post">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Submit">
<p><a href="#">Don't have an account?</a></p>
</form>

{% endblock %}

{% block scripts %}
{% endblock %}
20 changes: 20 additions & 0 deletions authentication/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from django.contrib.auth.views import LoginView, LogoutView
from django.urls import path

from authentication.forms import UserLoginForm

app_name = "authentication"
urlpatterns = [
path(
"login/",
LoginView.as_view(
template_name="authentication/login_page.html", form_class=UserLoginForm
),
name="login",
),
path(
"logout/",
LogoutView.as_view(),
name="logout",
),
]
23 changes: 0 additions & 23 deletions people/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,29 +154,6 @@ def picture_url(self):
if self.picture and hasattr(self.picture, "url"):
return self.picture.url

def create_user(self):
if self.user_id is not None:
return
self.user, _ = User.objects.get_or_create(
username=self.personal_email,
defaults={
"email": self.personal_email,
"first_name": self.first_name,
"last_name": self.last_name,
"is_staff": True
}
)

def deactivate_user(self):
if not self.user_id:
return
self.user.is_staff = False
self.user.is_active = False
self.user.save()

def save(self, *args, **kwargs):
super().save(*args, **kwargs)

def __str__(self):
return f"{self.last_name}, {self.first_name}"

Expand Down
24 changes: 24 additions & 0 deletions people/templates/people/members.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{% extends "base.html" %}
{% load static %}

{% block title %}
Nuestros Miembros
{% endblock %}

{% block styles %}
{% endblock %}

{% block content %}

<ul>
{% for person in people %}
<li>{{ person }}</li>
{% endfor %}
</ul>



{% endblock %}

{% block scripts %}
{% endblock %}
8 changes: 8 additions & 0 deletions people/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from django.urls import path

from people.views import PersonListView

app_name = "people"
urlpatterns = [
path("members/", PersonListView.as_view(), name="member-list"),
]
12 changes: 12 additions & 0 deletions people/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from django.utils.decorators import method_decorator
from django.views.decorators.http import require_http_methods
from django.views.generic import ListView

from people.models import Person


@method_decorator(require_http_methods(["GET"]), name='dispatch')
class PersonListView(ListView):
queryset = Person.objects.filter(user__is_staff=True)
context_object_name = "people"
template_name = "people/members.html"
10 changes: 5 additions & 5 deletions pet/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,25 +24,25 @@ def setUp(self):
)

def test_main_page_status_code(self):
response = self.client.get(reverse("main_page"))
response = self.client.get(reverse("pet:main_page"))
self.assertEqual(response.status_code, 200)

def test_main_page_template_used(self):
response = self.client.get(reverse("main_page"))
response = self.client.get(reverse("pet:main_page"))
self.assertTemplateUsed(response, "pet/main_page.html")

def test_main_page_contains_correct_html(self):
response = self.client.get(reverse("main_page"))
response = self.client.get(reverse("pet:main_page"))
self.assertContains(response, "¡Haz un cambio hoy!")
self.assertContains(response, "Pet1")
self.assertContains(response, "Pet2")

def test_main_page_does_not_contain_incorrect_html(self):
response = self.client.get(reverse("main_page"))
response = self.client.get(reverse("pet:main_page"))
self.assertNotContains(response, "Hello, world!")

def test_main_page_context(self):
response = self.client.get(reverse("main_page"))
response = self.client.get(reverse("pet:main_page"))
self.assertTrue("pets" in response.context)
self.assertTrue("headlines" in response.context)
self.assertEqual(len(response.context["pets"]), 2)
Expand Down
1 change: 1 addition & 0 deletions pet/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from pet.views import main_page

app_name = 'pet'
urlpatterns = [
path("index/", main_page, name="main_page"),
]

0 comments on commit 419f7b7

Please sign in to comment.