Skip to content

Commit

Permalink
Add some type hinting
Browse files Browse the repository at this point in the history
  • Loading branch information
quique committed Sep 9, 2022
1 parent 75f90fc commit f3b75d1
Show file tree
Hide file tree
Showing 9 changed files with 130 additions and 83 deletions.
1 change: 1 addition & 0 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pre-commit = "*"
pylint = "*"
rope = "*"
types-bleach = "*"
types-requests = "*"

[packages]
bleach = {extras = ["css"], version = "*"}
Expand Down
19 changes: 17 additions & 2 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion accounts/models.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Standard library
from __future__ import annotations
import json

# Third-party
Expand Down Expand Up @@ -180,7 +181,7 @@ def get_num_equipos(self, anyo):
return num_equipos

@classmethod
def crear_usuario(cls, request, nip):
def crear_usuario(cls, request, nip: str) -> CustomUser:
"""Crea un registro de usuario con el NIP indicado y los datos de Gestión Identidades."""

usuario = cls.objects.create_user(username=nip)
Expand Down
8 changes: 5 additions & 3 deletions accounts/pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@
from requests import Session
from requests.auth import HTTPBasicAuth
from requests.exceptions import ConnectionError as RequestConnectionError
from social_core.strategy import BaseStrategy

# Django
from django.contrib import messages
from django.core.validators import ValidationError, validate_email
from django.core.exceptions import ValidationError
from django.core.validators import validate_email


def get_identidad(strategy, response, user, *args, **kwargs):
def get_identidad(strategy: BaseStrategy, response, user, *args, **kwargs) -> None:
"""Actualiza el usuario con los datos obtenidos de Gestión de Identidades."""

wsdl = get_config('WSDL_IDENTIDAD')
Expand Down Expand Up @@ -74,7 +76,7 @@ def get_identidad(strategy, response, user, *args, **kwargs):
strategy.storage.user.changed(user)


def is_email_valid(email):
def is_email_valid(email: str) -> bool:
"""Validate email address"""
try:
validate_email(email)
Expand Down
18 changes: 11 additions & 7 deletions indo/models.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# Standard library
from __future__ import annotations
import datetime

from typing import Optional

# Django
from django.core.validators import FileExtensionValidator
from django.db import connection, models
from django.urls import reverse
Expand Down Expand Up @@ -107,10 +108,13 @@ def __str__(self):
return str(f'{self.id}-{self.id + 1}')

@classmethod
def get_ultima(cls):
def get_ultima(cls) -> Convocatoria:
"""Devuelve la última convocatoria."""

return Convocatoria.objects.order_by('-id').first()
ultima_convocatoria = Convocatoria.objects.order_by('-id').first()
if not ultima_convocatoria:
raise Convocatoria.DoesNotExist
return ultima_convocatoria


class Criterio(models.Model):
Expand Down Expand Up @@ -661,7 +665,7 @@ def get_absolute_url(self):
def en_borrador(self):
return self.estado == 'BORRADOR'

def get_pp_coordinador_or_none(self, tipo) -> Optional[ParticipanteProyecto]:
def get_pp_coordinador_or_none(self, tipo) -> ParticipanteProyecto | None:
"""Busca el coordinador o coordinador_2 del proyecto"""
try:
return self.participantes.get(tipo_participacion_id=tipo)
Expand Down Expand Up @@ -799,7 +803,7 @@ def get_up_gastos(cls, anyo):
datos_proyectos.insert(0, cabeceras)
return datos_proyectos

def get_unidad_planificacion(self) -> Optional[str]:
def get_unidad_planificacion(self) -> str | None:
"""Devuelve el ID de la Unidad de Planificación del proyecto
PIEC, PIPOUZ, PIET: UP del centro del proyecto
Expand Down Expand Up @@ -833,7 +837,7 @@ def get_coordinadores(self):
coordinadores = [self.coordinador, self.coordinador_2]
return list(filter(None, coordinadores))

def get_dict_valoraciones(self, user_id):
def get_dict_valoraciones(self, user_id: int) -> dict[int, Valoracion]:
"""Devuelve diccionario criterio_id => valoración con las valoraciones de un evaluador."""
return {
valoracion.criterio_id: valoracion
Expand Down
11 changes: 9 additions & 2 deletions indo/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
# Third-party
from django_tables2 import SingleTableView

from .models import Evento, Registro
# Django
from django.http import HttpRequest

# Local Django
from .models import Evento, Proyecto, Registro


class PagedFilteredTableView(SingleTableView):
Expand Down Expand Up @@ -33,7 +38,9 @@ def get_client_ip(request):
return request.META.get('REMOTE_ADDR')


def registrar_evento(request, nombre_evento, descripcion, proyecto):
def registrar_evento(
request: HttpRequest, nombre_evento: str, descripcion: str, proyecto: Proyecto
) -> None:
evento = Evento.objects.get(nombre=nombre_evento)
ip_address = get_client_ip(request)

Expand Down
Loading

0 comments on commit f3b75d1

Please sign in to comment.