diff --git a/.dockerignore b/.dockerignore index efc2616a..c238afc6 100644 --- a/.dockerignore +++ b/.dockerignore @@ -1,3 +1,5 @@ __pycache__ media db.sqlite3 +.tox +.coverage \ No newline at end of file diff --git a/.env_example b/.env_example index 91db0e31..eef94aac 100644 --- a/.env_example +++ b/.env_example @@ -1,6 +1,6 @@ DJANGO_APP_STAGE=prod # Only used in dev mode, change to "postgresql" if you want to use PostgreSQL in dev -DJANGO_DEV_STORE_METHOD=sqllite +DJANGO_DEV_STORE_METHOD=sqlite DJANGO_DB_HOST=localhost DJANGO_DB_NAME=note_db DJANGO_DB_USER=note @@ -10,9 +10,15 @@ DJANGO_SECRET_KEY=CHANGE_ME DJANGO_SETTINGS_MODULE=note_kfet.settings CONTACT_EMAIL=tresorerie.bde@localhost NOTE_URL=localhost +DOMAIN=localhost + # Config for mails. Only used in production NOTE_MAIL=notekfet@localhost EMAIL_HOST=smtp.localhost -EMAIL_PORT=465 +EMAIL_PORT=25 EMAIL_USER=notekfet@localhost EMAIL_PASSWORD=CHANGE_ME + +# Wiki configuration +WIKI_USER=NoteKfet2020 +WIKI_PASSWORD= diff --git a/.gitignore b/.gitignore index 2372bf46..94433be1 100644 --- a/.gitignore +++ b/.gitignore @@ -39,7 +39,9 @@ secrets.py .env map.json *.log -media/ +backups/ +/static/ +/media/ # Virtualenv env/ diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 152da589..07fcd529 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -1,30 +1,59 @@ -image: python:3.8 - stages: + - build_docker_image - test - quality-assurance -before_script: - - pip install tox - -py36-django22: - image: python:3.6 - stage: test - script: tox -e py36-django22 +docker: + stage: build_docker_image + image: + name: gcr.io/kaniko-project/executor:debug + entrypoint: [""] + script: + - mkdir -p /kaniko/.docker + - echo "{\"auths\":{\"https://index.docker.io/v1/\":{\"auth\":\"$CI_BUILD_TOKEN\"}}}" > /kaniko/.docker/config.json + - /kaniko/executor --cache=true --context $CI_PROJECT_DIR --dockerfile $CI_PROJECT_DIR/Dockerfile --destination $CI_REGISTRY_IMAGE:latest --destination $CI_REGISTRY_IMAGE:debian + only: + - master + - beta + cache: + key: one-key-to-rule-them-all + paths: + - /cache/ +# Debian Buster py37-django22: - image: python:3.7 stage: test + image: + name: $CI_REGISTRY_IMAGE:debian + entrypoint: [""] + before_script: + - apt-get update && apt-get install -y tox script: tox -e py37-django22 +# Ubuntu 20.04 py38-django22: - image: python:3.8 stage: test + image: ubuntu:20.04 + before_script: + # Fix tzdata prompt + - ln -sf /usr/share/zoneinfo/Europe/Paris /etc/localtime && echo Europe/Paris > /etc/timezone + - > + apt-get update && + apt-get install -y python3-django python3-django-crispy-forms + python3-django-extensions python3-django-filters python3-django-polymorphic + python3-djangorestframework python3-django-cas-server python3-psycopg2 python3-pil + python3-babel python3-lockfile python3-pip python3-phonenumbers ipython3 + gettext libjs-bootstrap4 fonts-font-awesome tox && + rm -rf /var/lib/apt/lists/* script: tox -e py38-django22 linters: - image: python:3.8 stage: quality-assurance + image: + name: $CI_REGISTRY_IMAGE:debian + entrypoint: [""] + before_script: + - apt-get update && apt-get install -y tox script: tox -e linters # Be nice to new contributors, but please use `tox` diff --git a/Dockerfile b/Dockerfile index 60acc6e3..8377912e 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,27 +1,28 @@ -FROM python:3-alpine +FROM debian:buster-backports +# Force the stdout and stderr streams to be unbuffered ENV PYTHONUNBUFFERED 1 -# Install LaTeX requirements -RUN apk add --no-cache gettext texlive texmf-dist-latexextra texmf-dist-fontsextra nginx gcc libc-dev libffi-dev postgresql-dev libxml2-dev libxslt-dev jpeg-dev +# Install Django, external apps, LaTeX and dependencies +RUN apt-get update && \ + apt-get install --no-install-recommends -t buster-backports -y \ + python3-django python3-django-crispy-forms \ + python3-django-extensions python3-django-filters python3-django-polymorphic \ + python3-djangorestframework python3-django-cas-server python3-psycopg2 python3-pil \ + python3-babel python3-lockfile python3-pip python3-phonenumbers ipython3 \ + python3-bs4 python3-setuptools \ + uwsgi uwsgi-plugin-python3 \ + texlive-latex-extra texlive-lang-french lmodern texlive-fonts-recommended \ + gettext libjs-bootstrap4 fonts-font-awesome && \ + rm -rf /var/lib/apt/lists/* -RUN apk add --no-cache bash +# Instal PyPI requirements +COPY requirements.txt /var/www/note_kfet/ +RUN pip3 install -r /var/www/note_kfet/requirements.txt --no-cache-dir -RUN mkdir /code -WORKDIR /code -COPY requirements /code/requirements -RUN pip install gunicorn ptpython --no-cache-dir -RUN pip install -r requirements/base.txt -r requirements/cas.txt -r requirements/production.txt --no-cache-dir +# Copy code +WORKDIR /var/www/note_kfet +COPY . /var/www/note_kfet/ -COPY . /code/ - -# Configure nginx -RUN mkdir /run/nginx -RUN ln -sf /dev/stdout /var/log/nginx/access.log && ln -sf /dev/stderr /var/log/nginx/error.log -RUN ln -sf /code/nginx_note.conf_docker /etc/nginx/conf.d/nginx_note.conf -RUN rm /etc/nginx/conf.d/default.conf - -ENTRYPOINT ["/code/entrypoint.sh"] -EXPOSE 80 - -CMD ["./manage.py", "shell_plus", "--ptpython"] +EXPOSE 8080 +ENTRYPOINT ["/var/www/note_kfet/entrypoint.sh"] diff --git a/README.md b/README.md index c04c7321..ee843d08 100644 --- a/README.md +++ b/README.md @@ -1,96 +1,107 @@ # NoteKfet 2020 [![License: GPL v3](https://img.shields.io/badge/License-GPL%20v3-blue.svg)](https://www.gnu.org/licenses/gpl-3.0.txt) -[![pipeline status](https://gitlab.crans.org/bde/nk20/badges/master/pipeline.svg)](https://gitlab.crans.org/bde/nk20/nk20/commits/master) +[![pipeline status](https://gitlab.crans.org/bde/nk20/badges/master/pipeline.svg)](https://gitlab.crans.org/bde/nk20/commits/master) [![coverage report](https://gitlab.crans.org/bde/nk20/badges/master/coverage.svg)](https://gitlab.crans.org/bde/nk20/commits/master) ## Installation sur un serveur -On supposera pour la suite que vous utilisez Debian/Ubuntu sur un serveur tout nu ou bien configuré. +On supposera pour la suite que vous utilisez une installation de Debian Buster ou Ubuntu 20.04 fraîche ou bien configuré. -1. Paquets nécessaires +Pour aller vite vous pouvez lancer le Playbook Ansible fournit dans ce dépôt en l'adaptant. +Sinon vous pouvez suivre les étapes ici. - $ sudo apt install nginx python3 python3-pip python3-dev uwsgi - $ sudo apt install uwsgi-plugin-python3 python3-venv git acl +### Installation avec Debian/Ubuntu - La génération des factures de l'application trésorerie nécessite une installation de LaTeX suffisante : +0. **Activer Debian Backports.** En effet Django 2.2 LTS n'est que disponible dans les backports. - $ sudo apt install texlive-latex-extra texlive-fonts-extra texlive-lang-french + ```bash + $ echo "deb http://deb.debian.org/debian buster-backports main" | sudo tee /etc/apt/sources.list.d/deb_debian_org_debian.list + ``` -2. Clonage du dépot +1. **Installation des dépendances APT.** + On tire les dépendances le plus possible à partir des dépôts de Debian. + On a besoin d'un environnement LaTeX pour générer les factures. - on se met au bon endroit : + ```bash + $ sudo apt update + $ sudo apt install -t buster-backports -y python3-django python3-django-crispy-forms \ + python3-django-extensions python3-django-filters python3-django-polymorphic \ + python3-djangorestframework python3-django-cas-server python3-psycopg2 python3-pil \ + python3-babel python3-lockfile python3-pip python3-phonenumbers ipython3 \ + uwsgi uwsgi-plugin-python3 \ + texlive-latex-extra texlive-fonts-extra texlive-lang-french \ + gettext libjs-bootstrap4 fonts-font-awesome \ + nginx python3-venv git acl + ``` - $ cd /var/www/ - $ mkdir note_kfet - $ sudo chown www-data:www-data note_kfet - $ sudo usermod -a -G www-data $USER - $ sudo chmod g+ws note_kfet - $ sudo setfacl -d -m "g::rwx" note_kfet - $ cd note_kfet - $ git clone git@gitlab.crans.org:bde/nk20.git . -3. Environment Virtuel +2. **Clonage du dépot** dans `/var/www/note_kfet`, - À la racine du projet: + ```bash + $ sudo mkdir -p /var/www/note_kfet && cd /var/www/note_kfet + $ sudo chown www-data:www-data . + $ sudo chmod g+rwx . + $ sudo -u www-data git clone https://gitlab.crans.org/bde/nk20.git + ``` - $ python3 -m venv env - $ source env/bin/activate - (env)$ pip3 install -r requirements/base.txt - (env)$ pip3 install -r requirements/prod.txt # uniquement en prod, nécessite un base postgres - (env)$ deactivate +3. **Création d'un environment de travail Python décorrélé du système.** -4. uwsgi et Nginx + ```bash + $ python3 -m venv env --system-site-packages + $ source env/bin/activate # entrer dans l'environnement + (env)$ pip3 install -r requirements.txt + (env)$ deactivate # sortir de l'environnement + ``` - Un exemple de conf est disponible : +4. **Pour configurer UWSGI et NGINX**, des exemples de conf sont disponibles. + **_Modifier le fichier pour être en accord avec le reste de votre config_** - $ cp nginx_note.conf_example nginx_note.conf + ```bash + $ cp nginx_note.conf_example nginx_note.conf + $ sudo ln -sf /var/www/note_kfet/nginx_note.conf /etc/nginx/sites-enabled/ + ``` - ***Modifier le fichier pour être en accord avec le reste de votre config*** + Si l'on a un emperor (plusieurs instance uwsgi): - On utilise uwsgi et Nginx pour gérer le coté serveur : + ```bash + $ sudo ln -sf /var/www/note_kfet/uwsgi_note.ini /etc/uwsgi/sites/ + ``` - $ sudo ln -sf /var/www/note_kfet/nginx_note.conf /etc/nginx/sites-enabled/ + Sinon si on est dans le cas habituel : - Si l'on a un emperor (plusieurs instance uwsgi): - - $ sudo ln -sf /var/www/note_kfet/uwsgi_note.ini /etc/uwsgi/sites/ - - Sinon: - - $ sudo ln -sf /var/www/note_kfet/uwsgi_note.ini /etc/uwsgi/apps-enabled/ - + ```bash + $ sudo ln -sf /var/www/note_kfet/uwsgi_note.ini /etc/uwsgi/apps-enabled/ + ``` + Le touch-reload est activé par défault, pour redémarrer la note il suffit donc de faire `touch uwsgi_note.ini`. -5. Base de données +5. **Base de données.** En production on utilise PostgreSQL. + + $ sudo apt-get install postgresql postgresql-contrib - En prod on utilise postgresql. - - $ sudo apt-get install postgresql postgresql-contrib libpq-dev - (env)$ pip3 install psycopg2 - La config de la base de donnée se fait comme suit: - + a. On se connecte au shell de psql - + $ sudo su - postgres $ psql - + b. On sécurise l'utilisateur postgres - + postgres=# \password Enter new password: - + Conservez ce mot de passe de la meme manière que tous les autres. - + c. On créer la basse de donnée, et l'utilisateur associé - + postgres=# CREATE USER note WITH PASSWORD 'un_mot_de_passe_sur'; CREATE ROLE postgres=# CREATE DATABASE note_db OWNER note; CREATE DATABASE Si tout va bien : - + postgres=#\list List of databases Name | Owner | Encoding | Collate | Ctype | Access privileges @@ -100,14 +111,14 @@ On supposera pour la suite que vous utilisez Debian/Ubuntu sur un serveur tout n template0 | postgres | UTF8 | fr_FR.UTF-8 | fr_FR.UTF-8 | =c/postgres+postgres=CTc/postgres template1 | postgres | UTF8 | fr_FR.UTF-8 | fr_FR.UTF-8 | =c/postgres +postgres=CTc/postgres (4 rows) - -6. Variable d'environnement et Migrations - + +6. Variable d'environnement et Migrations + On copie le fichier `.env_example` vers le fichier `.env` à la racine du projet et on renseigne des secrets et des paramètres : - + DJANGO_APP_STAGE=dev # ou "prod" - DJANGO_DEV_STORE_METHOD=sqllite # ou "postgres" + DJANGO_DEV_STORE_METHOD=sqlite # ou "postgres" DJANGO_DB_HOST=localhost DJANGO_DB_NAME=note_db DJANGO_DB_USER=note @@ -115,15 +126,17 @@ On supposera pour la suite que vous utilisez Debian/Ubuntu sur un serveur tout n DJANGO_DB_PORT= DJANGO_SECRET_KEY=CHANGE_ME DJANGO_SETTINGS_MODULE="note_kfet.settings + NOTE_URL=localhost # URL où accéder à la note DOMAIN=localhost # note.example.com CONTACT_EMAIL=tresorerie.bde@localhost - NOTE_URL=localhost # URL où accéder à la note # Le reste n'est utile qu'en production, pour configurer l'envoi des mails NOTE_MAIL=notekfet@localhost EMAIL_HOST=smtp.localhost - EMAIL_PORT=465 + EMAIL_PORT=25 EMAIL_USER=notekfet@localhost EMAIL_PASSWORD=CHANGE_ME + WIKI_USER=NoteKfet2020 + WIKI_PASSWORD=CHANGE_ME Ensuite on (re)bascule dans l'environement virtuel et on lance les migrations @@ -133,68 +146,80 @@ On supposera pour la suite que vous utilisez Debian/Ubuntu sur un serveur tout n (env)$ ./manage.py makemigrations (env)$ ./manage.py migrate -7. Enjoy +7. *Enjoy \o/* - -## Installer avec Docker +### Installation avec Docker Il est possible de travailler sur une instance Docker. -1. Cloner le dépôt là où vous voulez : - - $ git clone git@gitlab.crans.org:bde/nk20.git +Pour construire l'image Docker `nk20`, -2. Copiez le fichier `.env_example` à la racine du projet vers le fichier `.env`, -et mettez à jour vos variables d'environnement +``` +git clone https://gitlab.crans.org/bde/nk20/ && cd nk20 +docker build . -t nk20 +``` -3. Dans le fichier `docker_compose.yml`, qu'on suppose déjà configuré, - ajouter les lignes suivantes, en les adaptant à la configuration voulue : +Ensuite pour lancer la note Kfet en tant que vous (option `-u`), +l'exposer sur son port 80 (option `-p`) et monter le code en écriture (option `-v`), - nk20: - build: /chemin/vers/nk20 - volumes: - - /chemin/vers/nk20:/code/ - env_file: /chemin/vers/nk20/.env - restart: always - labels: - - traefik.domain=ndd.example.com - - traefik.frontend.rule=Host:ndd.example.com - - traefik.port=8000 +``` +docker run -it --rm -u $(id -u):$(id -g) -v "$(pwd):/var/www/note_kfet/" -p 80:8080 nk20 +``` -3. Enjoy : +Si vous souhaitez lancer une commande spéciale, vous pouvez l'ajouter à la fin, par exemple, - $ docker-compose up -d nk20 +``` +docker run -it --rm -u $(id -u):$(id -g) -v "$(pwd):/var/www/note_kfet/" -p 80:8080 nk20 python3 ./manage.py createsuperuser +``` -## Installer un serveur de développement +#### Avec Docker Compose + +On vous conseilles de faire un fichier d'environnement `.env` en prenant exemple sur `.env_example`. + +Pour par exemple utiliser le Docker de la note Kfet avec Traefik pour réaliser le HTTPS, + +```YAML +nk20: + build: /chemin/vers/le/code/nk20 + volumes: + - /chemin/vers/le/code/nk20:/var/www/note_kfet/ + env_file: /chemin/vers/le/code/nk20/.env + restart: always + labels: + - "traefik.http.routers.nk20.rule=Host(`ndd.example.com`)" + - "traefik.http.services.nk20.loadbalancer.server.port=8080" +``` + +### Lancer un serveur de développement Avec `./manage.py runserver` il est très rapide de mettre en place un serveur de développement par exemple sur son ordinateur. -1. Cloner le dépôt là où vous voulez : +1. Cloner le dépôt là où vous voulez : $ git clone git@gitlab.crans.org:bde/nk20.git && cd nk20 -2. Créer un environnement Python isolé - pour ne pas interférer avec les versions de paquets systèmes : +2. Créer un environnement Python isolé + pour ne pas interférer avec les versions de paquets systèmes : - $ python3 -m venv venv - $ source venv/bin/activate - (env)$ pip install -r requirements/base.txt + $ python3 -m venv venv + $ source venv/bin/activate + (env)$ pip install -r requirements.txt -3. Copier le fichier `.env_example` vers `.env` à la racine du projet et mettre à jour -ce qu'il faut +3. Copier le fichier `.env_example` vers `.env` à la racine du projet et mettre à jour + ce qu'il faut -4. Migrations et chargement des données initiales : +4. Migrations et chargement des données initiales : (env)$ ./manage.py makemigrations (env)$ ./manage.py migrate (env)$ ./manage.py loaddata initial -5. Créer un super-utilisateur : +5. Créer un super-utilisateur : (env)$ ./manage.py createsuperuser -6. Enjoy : +6. Enjoy : (env)$ ./manage.py runserver 0.0.0.0:8000 @@ -202,11 +227,27 @@ En mettant `0.0.0.0:8000` après `runserver`, vous rendez votre instance Django accessible depuis l'ensemble de votre réseau, pratique pour tester le rendu de la note sur un téléphone ! -## Cahier des Charges - -Il est disponible [ici](https://wiki.crans.org/NoteKfet/NoteKfet2018/CdC). - ## Documentation -La documentation est générée par django et son module admindocs. +Le cahier des charges initial est disponible [sur le Wiki Crans](https://wiki.crans.org/NoteKfet/NoteKfet2018/CdC). + +La documentation des classes et fonctions est directement dans le code et est explorable à partir de la partie documentation de l'interface d'administration de Django. **Commentez votre code !** + +La documentation plus haut niveau sur le développement est disponible sur [le Wiki associé au dépôt Git](https://gitlab.crans.org/bde/nk20/-/wikis/home). + +## FAQ + +### Regénérer les fichiers de traduction + +Pour regénérer les traductions vous pouvez vous placer à la racine du projet et lancer le script `makemessages`. Il faut penser à ignorer les dossiers ne contenant pas notre code, dont le virtualenv. + +```bash +django-admin makemessages -i env +``` + +Une fois les fichiers édités, vous pouvez compiler les nouvelles traductions avec + +```bash +django-admin compilemessages +``` diff --git a/ansible/base.yml b/ansible/base.yml index 6e455581..56ba83d9 100755 --- a/ansible/base.yml +++ b/ansible/base.yml @@ -6,6 +6,8 @@ - name: DB_PASSWORD prompt: "Password of the database" private: yes + vars: + mirror: deb.debian.org roles: - 1-apt-basic - 2-nk20 diff --git a/ansible/roles/1-apt-basic/tasks/main.yml b/ansible/roles/1-apt-basic/tasks/main.yml index eba6e5c3..f0ac56b2 100644 --- a/ansible/roles/1-apt-basic/tasks/main.yml +++ b/ansible/roles/1-apt-basic/tasks/main.yml @@ -1,21 +1,47 @@ --- -- name: Install basic APT packages +- name: Add buster-backports to apt sources + apt_repository: + repo: deb http://{{ mirror }}/debian buster-backports main + state: present + +- name: Install note_kfet APT dependencies apt: update_cache: true + default_release: buster-backports name: - - nginx - - python3 - - python3-pip - - python3-dev - - uwsgi - - uwsgi-plugin-python3 - - python3-venv - - git - - acl + # Common tools - gettext - - texlive-latex-extra + - git + - ipython3 + + # Front-end dependencies + - fonts-font-awesome + - libjs-bootstrap4 + + # Python dependencies + - python3-babel + - python3-django + - python3-django-cas-server + - python3-django-crispy-forms + - python3-django-extensions + - python3-django-filters + - python3-django-polymorphic + - python3-djangorestframework + - python3-lockfile + - python3-phonenumbers + - python3-pil + - python3-pip + - python3-psycopg2 + - python3-venv + + # LaTeX (PDF generation) - texlive-fonts-extra - texlive-lang-french + - texlive-latex-extra + + # WSGI server + - uwsgi + - uwsgi-plugin-python3 register: pkg_result retries: 3 until: pkg_result is succeeded diff --git a/ansible/roles/2-nk20/tasks/main.yml b/ansible/roles/2-nk20/tasks/main.yml index 2aca0698..37d29819 100644 --- a/ansible/roles/2-nk20/tasks/main.yml +++ b/ansible/roles/2-nk20/tasks/main.yml @@ -11,7 +11,7 @@ git: repo: https://gitlab.crans.org/bde/nk20.git dest: /var/www/note_kfet - version: master + version: beta force: true - name: Use default env vars (should be updated!) @@ -28,3 +28,10 @@ recurse: yes owner: www-data group: www-data + +- name: Setup cron jobs + template: + src: note.cron.j2 + dest: /etc/cron.d/note + owner: root + group: root diff --git a/ansible/roles/2-nk20/templates/note.cron.j2 b/ansible/roles/2-nk20/templates/note.cron.j2 new file mode 100644 index 00000000..17d65279 --- /dev/null +++ b/ansible/roles/2-nk20/templates/note.cron.j2 @@ -0,0 +1,22 @@ +# {{ ansible_managed }} +# Les cronjobs dont a besoin la Note Kfet + +# m h dom mon dow user command +# Envoyer les mails en attente + * * * * * root cd /var/www/note_kfet && env/bin/python manage.py send_mail >> /var/www/note_kfet/cron_mail.log + * * * * * root cd /var/www/note_kfet && env/bin/python manage.py retry_deferred >> /var/www/note_kfet/cron_mail_deferred.log + 00 0 * * * root cd /var/www/note_kfet && env/bin/python manage.py purge_mail_log 7 >> /var/www/note_kfet/cron_mail_purge.log +# Faire une sauvegarde de la base de données + 00 2 * * * root cd /var/www/note_kfet && apps/scripts/shell/backup_db +# Vérifier la cohérence de la base et mailer en cas de problème + 00 4 * * * root cd /var/www/note_kfet && env/bin/python manage.py check_consistency --sum-all --check-all --mail +# Mettre à jour le wiki (modification sans (dé)validation, activités passées) +#30 5 * * * root cd /var/www/note_kfet && env/bin/python manage.py refresh_activities --raw --comment refresh +# Spammer les gens en négatif + 00 5 * * 2 root cd /var/www/note_kfet && env/bin/python manage.py send_mail_to_negative_balances --spam +# Envoyer le rapport mensuel aux trésoriers et respos info + 00 8 6 * * root cd /var/www/note_kfet && env/bin/python manage.py send_mail_to_negative_balances --report +# Envoyer les rapports aux gens + 55 6 * * * root cd /var/www/note_kfet && env/bin/python manage.py send_reports +# Envoyer les rapports aux gens + 00 9 * * * root cd /var/www/note_kfet && env/bin/python manage.py refresh_highlighted_buttons diff --git a/ansible/roles/3-pip/tasks/main.yml b/ansible/roles/3-pip/tasks/main.yml index cbc3e902..4fd954ab 100644 --- a/ansible/roles/3-pip/tasks/main.yml +++ b/ansible/roles/3-pip/tasks/main.yml @@ -1,14 +1,8 @@ --- - name: Install PIP basic dependencies pip: - requirements: /var/www/note_kfet/requirements/base.txt - virtualenv: /var/www/note_kfet/env - virtualenv_command: /usr/bin/python3 -m venv - become_user: www-data - -- name: Install PIP production dependencies - pip: - requirements: /var/www/note_kfet/requirements/production.txt + requirements: /var/www/note_kfet/requirements.txt virtualenv: /var/www/note_kfet/env virtualenv_command: /usr/bin/python3 -m venv + virtualenv_site_packages: true become_user: www-data diff --git a/ansible/roles/4-nginx/tasks/main.yml b/ansible/roles/4-nginx/tasks/main.yml index 32fa651a..431e470b 100644 --- a/ansible/roles/4-nginx/tasks/main.yml +++ b/ansible/roles/4-nginx/tasks/main.yml @@ -1,4 +1,11 @@ --- +- name: Install NGINX + apt: + name: nginx + register: pkg_result + retries: 3 + until: pkg_result is succeeded + - name: Copy conf of Nginx template: src: "nginx_note.conf" diff --git a/ansible/roles/4-nginx/templates/nginx_note.conf b/ansible/roles/4-nginx/templates/nginx_note.conf index 9be2d980..b195e739 100644 --- a/ansible/roles/4-nginx/templates/nginx_note.conf +++ b/ansible/roles/4-nginx/templates/nginx_note.conf @@ -53,7 +53,7 @@ server { # Finally, send all non-media requests to the Django server. location / { uwsgi_pass note; - include /var/www/note_kfet/uwsgi_params; # the uwsgi_params file you installed + include /etc/nginx/uwsgi_params; } ssl_certificate /etc/letsencrypt/live/nk20-beta.crans.org/fullchain.pem; diff --git a/apps/activity/admin.py b/apps/activity/admin.py index 27cabd4e..257705eb 100644 --- a/apps/activity/admin.py +++ b/apps/activity/admin.py @@ -2,9 +2,10 @@ # SPDX-License-Identifier: GPL-3.0-or-later from django.contrib import admin - from note_kfet.admin import admin_site -from .models import Activity, ActivityType, Guest, Entry + +from .forms import GuestForm +from .models import Activity, ActivityType, Entry, Guest @admin.register(Activity, site=admin_site) @@ -35,6 +36,7 @@ class GuestAdmin(admin.ModelAdmin): Admin customisation for Guest """ list_display = ('last_name', 'first_name', 'activity', 'inviter') + form = GuestForm @admin.register(Entry, site=admin_site) diff --git a/apps/activity/api/serializers.py b/apps/activity/api/serializers.py index 2f257de0..d259324d 100644 --- a/apps/activity/api/serializers.py +++ b/apps/activity/api/serializers.py @@ -3,7 +3,7 @@ from rest_framework import serializers -from ..models import ActivityType, Activity, Guest, Entry, GuestTransaction +from ..models import Activity, ActivityType, Entry, Guest, GuestTransaction class ActivityTypeSerializer(serializers.ModelSerializer): diff --git a/apps/activity/api/urls.py b/apps/activity/api/urls.py index 3a2495fb..be769932 100644 --- a/apps/activity/api/urls.py +++ b/apps/activity/api/urls.py @@ -1,7 +1,7 @@ # Copyright (C) 2018-2020 by BDE ENS Paris-Saclay # SPDX-License-Identifier: GPL-3.0-or-later -from .views import ActivityTypeViewSet, ActivityViewSet, GuestViewSet, EntryViewSet +from .views import ActivityTypeViewSet, ActivityViewSet, EntryViewSet, GuestViewSet def register_activity_urls(router, path): diff --git a/apps/activity/api/views.py b/apps/activity/api/views.py index 764f2ac3..8d555b3b 100644 --- a/apps/activity/api/views.py +++ b/apps/activity/api/views.py @@ -1,12 +1,12 @@ # Copyright (C) 2018-2020 by BDE ENS Paris-Saclay # SPDX-License-Identifier: GPL-3.0-or-later +from api.viewsets import ReadProtectedModelViewSet from django_filters.rest_framework import DjangoFilterBackend from rest_framework.filters import SearchFilter -from api.viewsets import ReadProtectedModelViewSet -from .serializers import ActivityTypeSerializer, ActivitySerializer, GuestSerializer, EntrySerializer -from ..models import ActivityType, Activity, Guest, Entry +from .serializers import ActivitySerializer, ActivityTypeSerializer, EntrySerializer, GuestSerializer +from ..models import Activity, ActivityType, Entry, Guest class ActivityTypeViewSet(ReadProtectedModelViewSet): diff --git a/apps/activity/fixtures/initial.json b/apps/activity/fixtures/initial.json index 1856bce4..63c5009e 100644 --- a/apps/activity/fixtures/initial.json +++ b/apps/activity/fixtures/initial.json @@ -1,20 +1,32 @@ [ - { - "model": "activity.activitytype", - "pk": 1, - "fields": { - "name": "Pot", - "can_invite": true, - "guest_entry_fee": 500 + { + "model": "activity.activitytype", + "pk": 1, + "fields": { + "name": "Pot", + "manage_entries": true, + "can_invite": true, + "guest_entry_fee": 500 + } + }, + { + "model": "activity.activitytype", + "pk": 2, + "fields": { + "name": "Soir\u00e9e de club", + "manage_entries": false, + "can_invite": false, + "guest_entry_fee": 0 + } + }, + { + "model": "activity.activitytype", + "pk": 3, + "fields": { + "name": "Autre", + "manage_entries": false, + "can_invite": false, + "guest_entry_fee": 0 + } } - }, - { - "model": "activity.activitytype", - "pk": 2, - "fields": { - "name": "Soir\u00e9e de club", - "can_invite": false, - "guest_entry_fee": 0 - } - } -] \ No newline at end of file +] diff --git a/apps/activity/forms.py b/apps/activity/forms.py index dced014a..cf9bc3fc 100644 --- a/apps/activity/forms.py +++ b/apps/activity/forms.py @@ -1,18 +1,40 @@ # Copyright (C) 2018-2020 by BDE ENS Paris-Saclay # SPDX-License-Identifier: GPL-3.0-or-later -from datetime import timedelta, datetime + +from datetime import timedelta +from random import shuffle from django import forms from django.contrib.contenttypes.models import ContentType +from django.utils import timezone from django.utils.translation import gettext_lazy as _ from member.models import Club -from note.models import NoteUser, Note -from note_kfet.inputs import DateTimePickerInput, Autocomplete +from note.models import Note, NoteUser +from note_kfet.inputs import Autocomplete, DateTimePickerInput +from note_kfet.middlewares import get_current_authenticated_user +from permission.backends import PermissionBackend from .models import Activity, Guest class ActivityForm(forms.ModelForm): + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + # By default, the Kfet club is attended + self.fields["attendees_club"].initial = Club.objects.get(name="Kfet") + self.fields["attendees_club"].widget.attrs["placeholder"] = "Kfet" + clubs = list(Club.objects.filter(PermissionBackend + .filter_queryset(get_current_authenticated_user(), Club, "view")).all()) + shuffle(clubs) + self.fields["organizer"].widget.attrs["placeholder"] = ", ".join(club.name for club in clubs[:4]) + ", ..." + + def clean_date_end(self): + date_end = self.cleaned_data["date_end"] + date_start = self.cleaned_data["date_start"] + if date_end < date_start: + self.add_error("date_end", _("The end date must be after the start date.")) + return date_end + class Meta: model = Activity exclude = ('creater', 'valid', 'open', ) @@ -39,9 +61,18 @@ class ActivityForm(forms.ModelForm): class GuestForm(forms.ModelForm): def clean(self): + """ + Someone can be invited as a Guest to an Activity if: + - the activity has not already started. + - the activity is validated. + - the Guest has not already been invited more than 5 times. + - the Guest is already invited. + - the inviter already invited 3 peoples. + """ + cleaned_data = super().clean() - if self.activity.date_start > datetime.now(): + if timezone.now() > timezone.localtime(self.activity.date_start): self.add_error("inviter", _("You can't invite someone once the activity is started.")) if not self.activity.valid: @@ -50,20 +81,20 @@ class GuestForm(forms.ModelForm): one_year = timedelta(days=365) qs = Guest.objects.filter( - first_name=cleaned_data["first_name"], - last_name=cleaned_data["last_name"], + first_name__iexact=cleaned_data["first_name"], + last_name__iexact=cleaned_data["last_name"], activity__date_start__gte=self.activity.date_start - one_year, ) - if len(qs) >= 5: + if qs.filter(entry__isnull=False).count() >= 5: self.add_error("last_name", _("This person has been already invited 5 times this year.")) qs = qs.filter(activity=self.activity) if qs.exists(): self.add_error("last_name", _("This person is already invited.")) - qs = Guest.objects.filter(inviter=cleaned_data["inviter"], activity=self.activity) - if len(qs) >= 3: - self.add_error("inviter", _("You can't invite more than 3 people to this activity.")) + if "inviter" in cleaned_data: + if Guest.objects.filter(inviter=cleaned_data["inviter"], activity=self.activity).count() >= 3: + self.add_error("inviter", _("You can't invite more than 3 people to this activity.")) return cleaned_data diff --git a/apps/activity/models.py b/apps/activity/models.py index 45942cc5..131cd725 100644 --- a/apps/activity/models.py +++ b/apps/activity/models.py @@ -1,14 +1,17 @@ # Copyright (C) 2018-2020 by BDE ENS Paris-Saclay # SPDX-License-Identifier: GPL-3.0-or-later -from datetime import timedelta, datetime +from datetime import timedelta +from threading import Thread + +from django.conf import settings from django.contrib.auth.models import User from django.db import models from django.db.models import Q from django.utils import timezone from django.utils.translation import gettext_lazy as _ -from rest_framework.exceptions import ValidationError from note.models import NoteUser, Transaction +from rest_framework.exceptions import ValidationError class ActivityType(models.Model): @@ -24,11 +27,21 @@ class ActivityType(models.Model): verbose_name=_('name'), max_length=255, ) + + manage_entries = models.BooleanField( + verbose_name=_('manage entries'), + help_text=_('Enable the support of entries for this activity.'), + default=False, + ) + can_invite = models.BooleanField( verbose_name=_('can invite'), + default=False, ) + guest_entry_fee = models.PositiveIntegerField( verbose_name=_('guest entry fee'), + default=0, ) class Meta: @@ -54,6 +67,14 @@ class Activity(models.Model): verbose_name=_('description'), ) + location = models.CharField( + verbose_name=_('location'), + max_length=255, + blank=True, + default="", + help_text=_("Place where the activity is organized, eg. Kfet."), + ) + activity_type = models.ForeignKey( ActivityType, on_delete=models.PROTECT, @@ -72,6 +93,7 @@ class Activity(models.Model): on_delete=models.PROTECT, related_name='+', verbose_name=_('organizer'), + help_text=_("Club that organizes the activity. The entry fees will go to this club."), ) attendees_club = models.ForeignKey( @@ -79,6 +101,7 @@ class Activity(models.Model): on_delete=models.PROTECT, related_name='+', verbose_name=_('attendees club'), + help_text=_("Club that is authorized to join the activity. Mostly the Kfet club."), ) date_start = models.DateTimeField( @@ -99,12 +122,29 @@ class Activity(models.Model): verbose_name=_('open'), ) + def save(self, *args, **kwargs): + """ + Update the activity wiki page each time the activity is updated (validation, change description, ...) + """ + if self.date_end < self.date_start: + raise ValidationError(_("The end date must be after the start date.")) + + ret = super().save(*args, **kwargs) + if settings.DEBUG and self.pk and "scripts" in settings.INSTALLED_APPS: + def refresh_activities(): + from scripts.management.commands.refresh_activities import Command as RefreshActivitiesCommand + RefreshActivitiesCommand.refresh_human_readable_wiki_page("Modification de l'activité " + self.name) + RefreshActivitiesCommand.refresh_raw_wiki_page("Modification de l'activité " + self.name) + Thread(daemon=True, target=refresh_activities).start() + return ret + def __str__(self): return self.name class Meta: verbose_name = _("activity") verbose_name_plural = _("activities") + unique_together = ("name", "date_start", "date_end",) class Entry(models.Model): @@ -213,18 +253,18 @@ class Guest(models.Model): one_year = timedelta(days=365) if not force_insert: - if self.activity.date_start > datetime.now(): + if timezone.now() > timezone.localtime(self.activity.date_start): raise ValidationError(_("You can't invite someone once the activity is started.")) if not self.activity.valid: raise ValidationError(_("This activity is not validated yet.")) qs = Guest.objects.filter( - first_name=self.first_name, - last_name=self.last_name, + first_name__iexact=self.first_name, + last_name__iexact=self.last_name, activity__date_start__gte=self.activity.date_start - one_year, ) - if len(qs) >= 5: + if qs.filter(entry__isnull=False).count() >= 5: raise ValidationError(_("This person has been already invited 5 times this year.")) qs = qs.filter(activity=self.activity) @@ -232,7 +272,7 @@ class Guest(models.Model): raise ValidationError(_("This person is already invited.")) qs = Guest.objects.filter(inviter=self.inviter, activity=self.activity) - if len(qs) >= 3: + if qs.count() >= 3: raise ValidationError(_("You can't invite more than 3 people to this activity.")) return super().save(force_insert, force_update, using, update_fields) diff --git a/apps/activity/tables.py b/apps/activity/tables.py index d6e566d3..e1c4817c 100644 --- a/apps/activity/tables.py +++ b/apps/activity/tables.py @@ -1,13 +1,13 @@ # Copyright (C) 2018-2020 by BDE ENS Paris-Saclay # SPDX-License-Identifier: GPL-3.0-or-later - +from django.utils import timezone from django.utils.html import format_html from django.utils.translation import gettext_lazy as _ import django_tables2 as tables from django_tables2 import A from note.templatetags.pretty_money import pretty_money -from .models import Activity, Guest, Entry +from .models import Activity, Entry, Guest class ActivityTable(tables.Table): @@ -20,6 +20,11 @@ class ActivityTable(tables.Table): attrs = { 'class': 'table table-condensed table-striped table-hover' } + row_attrs = { + 'class': lambda record: 'bg-success' if record.open else ('' if record.valid else 'bg-warning'), + 'title': lambda record: _("The activity is currently open.") if record.open else + ('' if record.valid else _("The validation of the activity is pending.")), + } model = Activity template_name = 'django_tables2/bootstrap4.html' fields = ('name', 'activity_type', 'organizer', 'attendees_club', 'date_start', 'date_end', ) @@ -28,22 +33,17 @@ class ActivityTable(tables.Table): class GuestTable(tables.Table): inviter = tables.LinkColumn( 'member:user_detail', - args=[A('inviter.user.pk'), ], + args=[A('inviter__user__pk'), ], ) entry = tables.Column( empty_values=(), - attrs={ - "td": { - "class": lambda record: "" if record.has_entry else "validate btn btn-danger", - "onclick": lambda record: "" if record.has_entry else "remove_guest(" + str(record.pk) + ")" - } - } + verbose_name=_("Remove"), ) class Meta: attrs = { - 'class': 'table table-condensed table-striped table-hover' + 'class': 'table table-condensed table-striped' } model = Guest template_name = 'django_tables2/bootstrap4.html' @@ -52,7 +52,8 @@ class GuestTable(tables.Table): def render_entry(self, record): if record.has_entry: return str(_("Entered on ") + str(_("{:%Y-%m-%d %H:%M:%S}").format(record.entry.time, ))) - return _("remove").capitalize() + return format_html(''.format(id=record.id, delete_trans=_("remove").capitalize())) def get_row_class(record): @@ -66,6 +67,10 @@ def get_row_class(record): qs = Entry.objects.filter(note=record.note, activity=record.activity, guest=None) if qs.exists(): c += " table-success" + elif not record.note.user.memberships.filter(club=record.activity.attendees_club, + date_start__lte=timezone.now(), + date_end__gte=timezone.now()).exists(): + c += " table-info" elif record.note.balance < 0: c += " table-danger" return c diff --git a/apps/activity/templates/activity/activity_detail.html b/apps/activity/templates/activity/activity_detail.html new file mode 100644 index 00000000..c3a40891 --- /dev/null +++ b/apps/activity/templates/activity/activity_detail.html @@ -0,0 +1,77 @@ +{% extends "base.html" %} +{% comment %} +SPDX-License-Identifier: GPL-3.0-or-later +{% endcomment %} +{% load i18n perms %} +{% load render_table from django_tables2 %} + +{% block content %} +

{{ title }}

+{% include "activity/includes/activity_info.html" %} + +{% if guests.data %} +
+

+ {% trans "Guests list" %} +

+
+ {% render_table guests %} +
+
+{% endif %} +{% endblock %} + +{% block extrajavascript %} + +{% endblock %} diff --git a/apps/activity/templates/activity/activity_entry.html b/apps/activity/templates/activity/activity_entry.html new file mode 100644 index 00000000..d59a4c48 --- /dev/null +++ b/apps/activity/templates/activity/activity_entry.html @@ -0,0 +1,166 @@ +{% extends "base.html" %} +{% comment %} +SPDX-License-Identifier: GPL-3.0-or-later +{% endcomment %} +{% load static i18n pretty_money perms %} +{% load render_table from django_tables2 %} + +{% block content %} +

{{ title }}

+
+
+
+ + {% trans "Transfer" %} + + {% if "note.notespecial"|not_empty_model_list %} + + {% trans "Credit" %} + + + {% trans "Debit" %} + + {% endif %} + {% for a in activities_open %} + + {% trans "Entries" %} {{ a.name }} + + {% endfor %} +
+
+
+ +
+ + + + + + + +
+ +
+

{{ entries.count }} + {% if entries.count >= 2 %}{% trans "entries" %}{% else %}{% trans "entry" %}{% endif %}

+ {% render_table table %} +
+{% endblock %} + +{% block extrajavascript %} + +{% endblock %} \ No newline at end of file diff --git a/apps/activity/templates/activity/activity_form.html b/apps/activity/templates/activity/activity_form.html new file mode 100644 index 00000000..c472d3cb --- /dev/null +++ b/apps/activity/templates/activity/activity_form.html @@ -0,0 +1,20 @@ +{% extends "base.html" %} +{% comment %} +SPDX-License-Identifier: GPL-3.0-or-later +{% endcomment %} +{% load i18n crispy_forms_tags %} + +{% block content %} +
+

+ {{ title }} +

+
+
+ {% csrf_token %} + {{ form|crispy }} + +
+
+
+{% endblock %} \ No newline at end of file diff --git a/apps/activity/templates/activity/activity_list.html b/apps/activity/templates/activity/activity_list.html new file mode 100644 index 00000000..3316d698 --- /dev/null +++ b/apps/activity/templates/activity/activity_list.html @@ -0,0 +1,49 @@ +{% extends "base.html" %} +{% comment %} +SPDX-License-Identifier: GPL-3.0-or-later +{% endcomment %} +{% load render_table from django_tables2 %} +{% load i18n %} + +{% block content %} +{% if started_activities %} +
+

+ {% trans "Current activity" %} +

+
+ {% for activity in started_activities %} + {% include "activity/includes/activity_info.html" %} + {% endfor %} +
+
+{% endif %} + +
+

+ {% trans "Upcoming activities" %} +

+ {% if upcoming.data %} + {% render_table upcoming %} + {% else %} +
+
+ {% trans "There is no planned activity." %} +
+
+ {% endif %} + +
+ +
+

+ {% trans "All activities" %} +

+ {% render_table table %} +
+{% endblock %} \ No newline at end of file diff --git a/apps/activity/templates/activity/includes/activity_info.html b/apps/activity/templates/activity/includes/activity_info.html new file mode 100644 index 00000000..a16ad33b --- /dev/null +++ b/apps/activity/templates/activity/includes/activity_info.html @@ -0,0 +1,78 @@ +{% comment %} +SPDX-License-Identifier: GPL-3.0-or-later +{% endcomment %} +{% load i18n perms pretty_money %} +{% url 'activity:activity_detail' activity.pk as activity_detail_url %} + +
+
+

+ {% if request.path_info != activity_detail_url %} + {{ activity.name }} + {% else %} + {{ activity.name }} + {% endif %} +

+
+
+
+
{% trans 'description'|capfirst %}
+
{{ activity.description|linebreaks }}
+ +
{% trans 'type'|capfirst %}
+
{{ activity.activity_type }}
+ +
{% trans 'start date'|capfirst %}
+
{{ activity.date_start }}
+ +
{% trans 'end date'|capfirst %}
+
{{ activity.date_end }}
+ + {% if "activity.change_activity_valid"|has_perm:activity %} +
{% trans 'creater'|capfirst %}
+
{{ activity.creater }}
+ {% endif %} + +
{% trans 'organizer'|capfirst %}
+
{{ activity.organizer }}
+ +
{% trans 'attendees club'|capfirst %}
+
{{ activity.attendees_club }}
+ +
{% trans 'can invite'|capfirst %}
+
{{ activity.activity_type.can_invite|yesno }}
+ + {% if activity.activity_type.can_invite %} +
{% trans 'guest entry fee'|capfirst %}
+
{{ activity.activity_type.guest_entry_fee|pretty_money }}
+ {% endif %} + +
{% trans 'valid'|capfirst %}
+
{{ activity.valid|yesno }}
+ +
{% trans 'opened'|capfirst %}
+
{{ activity.open|yesno }}
+
+
+ + +
\ No newline at end of file diff --git a/apps/activity/views.py b/apps/activity/views.py index cac7f183..fd218db5 100644 --- a/apps/activity/views.py +++ b/apps/activity/views.py @@ -1,30 +1,45 @@ # Copyright (C) 2018-2020 by BDE ENS Paris-Saclay # SPDX-License-Identifier: GPL-3.0-or-later -from datetime import datetime, timezone - from django.conf import settings from django.contrib.auth.mixins import LoginRequiredMixin from django.contrib.contenttypes.models import ContentType +from django.core.exceptions import PermissionDenied from django.db.models import F, Q from django.urls import reverse_lazy -from django.views.generic import CreateView, DetailView, UpdateView, TemplateView +from django.utils import timezone from django.utils.translation import gettext_lazy as _ +from django.views.generic import DetailView, TemplateView, UpdateView from django_tables2.views import SingleTableView -from note.models import NoteUser, Alias, NoteSpecial +from note.models import Alias, NoteSpecial, NoteUser from permission.backends import PermissionBackend -from permission.views import ProtectQuerysetMixin +from permission.views import ProtectQuerysetMixin, ProtectedCreateView from .forms import ActivityForm, GuestForm -from .models import Activity, Guest, Entry -from .tables import ActivityTable, GuestTable, EntryTable +from .models import Activity, Entry, Guest +from .tables import ActivityTable, EntryTable, GuestTable -class ActivityCreateView(ProtectQuerysetMixin, LoginRequiredMixin, CreateView): +class ActivityCreateView(ProtectQuerysetMixin, ProtectedCreateView): + """ + View to create a new Activity + """ model = Activity form_class = ActivityForm extra_context = {"title": _("Create new activity")} + def get_sample_object(self): + return Activity( + name="", + description="", + creater=self.request.user, + activity_type_id=1, + organizer_id=1, + attendees_club_id=1, + date_start=timezone.now(), + date_end=timezone.now(), + ) + def form_valid(self, form): form.instance.creater = self.request.user return super().form_valid(form) @@ -35,6 +50,9 @@ class ActivityCreateView(ProtectQuerysetMixin, LoginRequiredMixin, CreateView): class ActivityListView(ProtectQuerysetMixin, LoginRequiredMixin, SingleTableView): + """ + Displays all Activities, and classify if they are on-going or upcoming ones. + """ model = Activity table_class = ActivityTable ordering = ('-date_start',) @@ -46,16 +64,24 @@ class ActivityListView(ProtectQuerysetMixin, LoginRequiredMixin, SingleTableView def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) - upcoming_activities = Activity.objects.filter(date_end__gt=datetime.now()) + upcoming_activities = Activity.objects.filter(date_end__gt=timezone.now()) context['upcoming'] = ActivityTable( data=upcoming_activities.filter(PermissionBackend.filter_queryset(self.request.user, Activity, "view")), prefix='upcoming-', ) + started_activities = Activity.objects\ + .filter(PermissionBackend.filter_queryset(self.request.user, Activity, "view"))\ + .filter(open=True, valid=True).all() + context["started_activities"] = started_activities + return context class ActivityDetailView(ProtectQuerysetMixin, LoginRequiredMixin, DetailView): + """ + Shows details about one activity. Add guest to context + """ model = Activity context_object_name = "activity" extra_context = {"title": _("Activity detail")} @@ -67,12 +93,15 @@ class ActivityDetailView(ProtectQuerysetMixin, LoginRequiredMixin, DetailView): .filter(PermissionBackend.filter_queryset(self.request.user, Guest, "view"))) context["guests"] = table - context["activity_started"] = datetime.now(timezone.utc) > self.object.date_start + context["activity_started"] = timezone.now() > timezone.localtime(self.object.date_start) return context class ActivityUpdateView(ProtectQuerysetMixin, LoginRequiredMixin, UpdateView): + """ + Updates one Activity + """ model = Activity form_class = ActivityForm extra_context = {"title": _("Update activity")} @@ -81,10 +110,23 @@ class ActivityUpdateView(ProtectQuerysetMixin, LoginRequiredMixin, UpdateView): return reverse_lazy('activity:activity_detail', kwargs={"pk": self.kwargs["pk"]}) -class ActivityInviteView(ProtectQuerysetMixin, LoginRequiredMixin, CreateView): +class ActivityInviteView(ProtectQuerysetMixin, ProtectedCreateView): + """ + Invite a Guest, The rules to invites someone are defined in `forms:activity.GuestForm` + """ model = Guest form_class = GuestForm - template_name = "activity/activity_invite.html" + template_name = "activity/activity_form.html" + + def get_sample_object(self): + """ Creates a standart Guest binds to the Activity""" + activity = Activity.objects.get(pk=self.kwargs["pk"]) + return Guest( + activity=activity, + first_name="", + last_name="", + inviter=self.request.user.note, + ) def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) @@ -96,6 +138,7 @@ class ActivityInviteView(ProtectQuerysetMixin, LoginRequiredMixin, CreateView): form = super().get_form(form_class) form.activity = Activity.objects.filter(PermissionBackend.filter_queryset(self.request.user, Activity, "view"))\ .get(pk=self.kwargs["pk"]) + form.fields["inviter"].initial = self.request.user.note return form def form_valid(self, form): @@ -108,57 +151,115 @@ class ActivityInviteView(ProtectQuerysetMixin, LoginRequiredMixin, CreateView): class ActivityEntryView(LoginRequiredMixin, TemplateView): + """ + Manages entry to an activity + """ template_name = "activity/activity_entry.html" - def get_context_data(self, **kwargs): - context = super().get_context_data(**kwargs) + def dispatch(self, request, *args, **kwargs): + """ + Don't display the entry interface if the user has no right to see it (no right to add an entry for itself), + it is closed or doesn't manage entries. + """ + activity = Activity.objects.get(pk=self.kwargs["pk"]) - activity = Activity.objects.filter(PermissionBackend.filter_queryset(self.request.user, Activity, "view"))\ - .get(pk=self.kwargs["pk"]) - context["activity"] = activity + sample_entry = Entry(activity=activity, note=self.request.user.note) + if not PermissionBackend.check_perm(self.request.user, "activity.add_entry", sample_entry): + raise PermissionDenied(_("You are not allowed to display the entry interface for this activity.")) - matched = [] + if not activity.activity_type.manage_entries: + raise PermissionDenied(_("This activity does not support activity entries.")) - pattern = "^$" - if "search" in self.request.GET: - pattern = self.request.GET["search"] + if not activity.open: + raise PermissionDenied(_("This activity is closed.")) + return super().dispatch(request, *args, **kwargs) - if not pattern: - pattern = "^$" - - if pattern[0] != "^": - pattern = "^" + pattern + def get_invited_guest(self, activity): + """ + Retrieves all Guests to the activity + """ guest_qs = Guest.objects\ .annotate(balance=F("inviter__balance"), note_name=F("inviter__user__username"))\ - .filter(Q(first_name__regex=pattern) | Q(last_name__regex=pattern) - | Q(inviter__alias__name__regex=pattern) - | Q(inviter__alias__normalized_name__regex=Alias.normalize(pattern))) \ + .filter(activity=activity)\ .filter(PermissionBackend.filter_queryset(self.request.user, Guest, "view"))\ - .distinct()[:20] - for guest in guest_qs: - guest.type = "Invité" - matched.append(guest) + .order_by('last_name', 'first_name').distinct() + if "search" in self.request.GET and self.request.GET["search"]: + pattern = self.request.GET["search"] + if pattern[0] != "^": + pattern = "^" + pattern + guest_qs = guest_qs.filter( + Q(first_name__regex=pattern) + | Q(last_name__regex=pattern) + | Q(inviter__alias__name__regex=pattern) + | Q(inviter__alias__normalized_name__regex=Alias.normalize(pattern)) + ) + else: + guest_qs = guest_qs.none() + return guest_qs + + def get_invited_note(self, activity): + """ + Retrieves all Note that can attend the activity, + they need to have an up-to-date membership in the attendees_club. + """ note_qs = Alias.objects.annotate(last_name=F("note__noteuser__user__last_name"), first_name=F("note__noteuser__user__first_name"), username=F("note__noteuser__user__username"), note_name=F("name"), - balance=F("note__balance"))\ - .filter(Q(note__polymorphic_ctype__model="noteuser") - & (Q(note__noteuser__user__first_name__regex=pattern) - | Q(note__noteuser__user__last_name__regex=pattern) - | Q(name__regex=pattern) - | Q(normalized_name__regex=Alias.normalize(pattern)))) \ - .filter(PermissionBackend.filter_queryset(self.request.user, Alias, "view")) - if settings.DATABASES[note_qs.db]["ENGINE"] == 'django.db.backends.postgresql_psycopg2': + balance=F("note__balance")) + + # Keep only users that have a note + note_qs = note_qs.filter(note__noteuser__isnull=False) + + # Keep only members + note_qs = note_qs.filter( + note__noteuser__user__memberships__club=activity.attendees_club, + note__noteuser__user__memberships__date_start__lte=timezone.now(), + note__noteuser__user__memberships__date_end__gte=timezone.now(), + ) + + # Filter with permission backend + note_qs = note_qs.filter(PermissionBackend.filter_queryset(self.request.user, Alias, "view")) + + if "search" in self.request.GET and self.request.GET["search"]: + pattern = self.request.GET["search"] + note_qs = note_qs.filter( + Q(note__noteuser__user__first_name__regex=pattern) + | Q(note__noteuser__user__last_name__regex=pattern) + | Q(name__regex=pattern) + | Q(normalized_name__regex=Alias.normalize(pattern)) + ) + else: + note_qs = note_qs.none() + + if settings.DATABASES[note_qs.db]["ENGINE"] == 'django.db.backends.postgresql': note_qs = note_qs.distinct('note__pk')[:20] else: # SQLite doesn't support distinct fields. For compatibility reason (in dev mode), the note list will only # have distinct aliases rather than distinct notes with a SQLite DB, but it can fill the result page. # In production mode, please use PostgreSQL. note_qs = note_qs.distinct()[:20] - for note in note_qs: + return note_qs + + def get_context_data(self, **kwargs): + """ + Query the list of Guest and Note to the activity and add information to makes entry with JS. + """ + context = super().get_context_data(**kwargs) + + activity = Activity.objects.filter(PermissionBackend.filter_queryset(self.request.user, Activity, "view"))\ + .distinct().get(pk=self.kwargs["pk"]) + context["activity"] = activity + + matched = [] + + for guest in self.get_invited_guest(activity): + guest.type = "Invité" + matched.append(guest) + + for note in self.get_invited_note(activity): note.type = "Adhérent" note.activity = activity matched.append(note) @@ -172,8 +273,11 @@ class ActivityEntryView(LoginRequiredMixin, TemplateView): context["noteuser_ctype"] = ContentType.objects.get_for_model(NoteUser).pk context["notespecial_ctype"] = ContentType.objects.get_for_model(NoteSpecial).pk - context["activities_open"] = Activity.objects.filter(open=True).filter( - PermissionBackend.filter_queryset(self.request.user, Activity, "view")).filter( - PermissionBackend.filter_queryset(self.request.user, Activity, "change")).all() + activities_open = Activity.objects.filter(open=True).filter( + PermissionBackend.filter_queryset(self.request.user, Activity, "view")).distinct().all() + context["activities_open"] = [a for a in activities_open + if PermissionBackend.check_perm(self.request.user, + "activity.add_entry", + Entry(activity=a, note=self.request.user.note,))] return context diff --git a/apps/api/urls.py b/apps/api/urls.py index 03d6bd68..9b4d44de 100644 --- a/apps/api/urls.py +++ b/apps/api/urls.py @@ -1,21 +1,16 @@ # Copyright (C) 2018-2020 by BDE ENS Paris-Saclay # SPDX-License-Identifier: GPL-3.0-or-later +from django.conf import settings from django.conf.urls import url, include from django.contrib.auth.models import User from django.contrib.contenttypes.models import ContentType +from django.db.models import Q from django_filters.rest_framework import DjangoFilterBackend from rest_framework import routers, serializers -from rest_framework.filters import SearchFilter from rest_framework.viewsets import ReadOnlyModelViewSet -from activity.api.urls import register_activity_urls from api.viewsets import ReadProtectedModelViewSet -from member.api.urls import register_members_urls -from note.api.urls import register_note_urls -from treasury.api.urls import register_treasury_urls -from logs.api.urls import register_logs_urls -from permission.api.urls import register_permission_urls -from wei.api.urls import register_wei_urls +from note.models import Alias class UserSerializer(serializers.ModelSerializer): @@ -52,9 +47,47 @@ class UserViewSet(ReadProtectedModelViewSet): """ queryset = User.objects.all() serializer_class = UserSerializer - filter_backends = [DjangoFilterBackend, SearchFilter] + filter_backends = [DjangoFilterBackend] filterset_fields = ['id', 'username', 'first_name', 'last_name', 'email', 'is_superuser', 'is_staff', 'is_active', ] - search_fields = ['$username', '$first_name', '$last_name', ] + + def get_queryset(self): + queryset = super().get_queryset().order_by("username") + + if "search" in self.request.GET: + pattern = self.request.GET["search"] + + # We match first a user by its username, then if an alias is matched without normalization + # And finally if the normalized pattern matches a normalized alias. + queryset = queryset.filter( + username__iregex="^" + pattern + ).union( + queryset.filter( + Q(note__alias__name__iregex="^" + pattern) + & ~Q(username__iregex="^" + pattern) + ), all=True).union( + queryset.filter( + Q(note__alias__normalized_name__iregex="^" + Alias.normalize(pattern)) + & ~Q(note__alias__name__iregex="^" + pattern) + & ~Q(username__iregex="^" + pattern) + ), + all=True).union( + queryset.filter( + Q(note__alias__normalized_name__iregex="^" + pattern.lower()) + & ~Q(note__alias__normalized_name__iregex="^" + Alias.normalize(pattern)) + & ~Q(note__alias__name__iregex="^" + pattern) + & ~Q(username__iregex="^" + pattern) + ), + all=True).union( + queryset.filter( + (Q(last_name__iregex="^" + pattern) | Q(first_name__iregex="^" + pattern)) + & ~Q(note__alias__normalized_name__iregex="^" + pattern.lower()) + & ~Q(note__alias__normalized_name__iregex="^" + Alias.normalize(pattern)) + & ~Q(note__alias__name__iregex="^" + pattern) + & ~Q(username__iregex="^" + pattern) + ), + all=True) + + return queryset # This ViewSet is the only one that is accessible from all authenticated users! @@ -73,13 +106,34 @@ class ContentTypeViewSet(ReadOnlyModelViewSet): router = routers.DefaultRouter() router.register('models', ContentTypeViewSet) router.register('user', UserViewSet) -register_members_urls(router, 'members') -register_activity_urls(router, 'activity') -register_note_urls(router, 'note') -register_treasury_urls(router, 'treasury') -register_permission_urls(router, 'permission') -register_logs_urls(router, 'logs') -register_wei_urls(router, 'wei') + +if "member" in settings.INSTALLED_APPS: + from member.api.urls import register_members_urls + register_members_urls(router, 'members') + +if "member" in settings.INSTALLED_APPS: + from activity.api.urls import register_activity_urls + register_activity_urls(router, 'activity') + +if "note" in settings.INSTALLED_APPS: + from note.api.urls import register_note_urls + register_note_urls(router, 'note') + +if "treasury" in settings.INSTALLED_APPS: + from treasury.api.urls import register_treasury_urls + register_treasury_urls(router, 'treasury') + +if "permission" in settings.INSTALLED_APPS: + from permission.api.urls import register_permission_urls + register_permission_urls(router, 'permission') + +if "logs" in settings.INSTALLED_APPS: + from logs.api.urls import register_logs_urls + register_logs_urls(router, 'logs') + +if "wei" in settings.INSTALLED_APPS: + from wei.api.urls import register_wei_urls + register_wei_urls(router, 'wei') app_name = 'api' diff --git a/apps/api/viewsets.py b/apps/api/viewsets.py index 6e0cb6b8..01fc7998 100644 --- a/apps/api/viewsets.py +++ b/apps/api/viewsets.py @@ -4,7 +4,7 @@ from django.contrib.contenttypes.models import ContentType from permission.backends import PermissionBackend from rest_framework import viewsets -from note_kfet.middlewares import get_current_authenticated_user +from note_kfet.middlewares import get_current_session class ReadProtectedModelViewSet(viewsets.ModelViewSet): @@ -17,8 +17,9 @@ class ReadProtectedModelViewSet(viewsets.ModelViewSet): self.model = ContentType.objects.get_for_model(self.serializer_class.Meta.model).model_class() def get_queryset(self): - user = get_current_authenticated_user() - return self.model.objects.filter(PermissionBackend.filter_queryset(user, self.model, "view")) + user = self.request.user + get_current_session().setdefault("permission_mask", 42) + return self.model.objects.filter(PermissionBackend.filter_queryset(user, self.model, "view")).distinct() class ReadOnlyProtectedModelViewSet(viewsets.ReadOnlyModelViewSet): @@ -31,5 +32,6 @@ class ReadOnlyProtectedModelViewSet(viewsets.ReadOnlyModelViewSet): self.model = ContentType.objects.get_for_model(self.serializer_class.Meta.model).model_class() def get_queryset(self): - user = get_current_authenticated_user() - return self.model.objects.filter(PermissionBackend.filter_queryset(user, self.model, "view")) + user = self.request.user + get_current_session().setdefault("permission_mask", 42) + return self.model.objects.filter(PermissionBackend.filter_queryset(user, self.model, "view")).distinct() diff --git a/apps/logs/api/views.py b/apps/logs/api/views.py index b3b9b166..4160d609 100644 --- a/apps/logs/api/views.py +++ b/apps/logs/api/views.py @@ -19,5 +19,5 @@ class ChangelogViewSet(ReadOnlyProtectedModelViewSet): serializer_class = ChangelogSerializer filter_backends = [DjangoFilterBackend, OrderingFilter] filterset_fields = ['model', 'action', "instance_pk", 'user', 'ip', ] - ordering_fields = ['timestamp', ] - ordering = ['-timestamp', ] + ordering_fields = ['timestamp', 'id', ] + ordering = ['-id', ] diff --git a/apps/logs/signals.py b/apps/logs/signals.py index 68bf95c0..2d443d13 100644 --- a/apps/logs/signals.py +++ b/apps/logs/signals.py @@ -23,6 +23,9 @@ EXCLUDED = [ 'cas_server.userattributes', 'contenttypes.contenttype', 'logs.changelog', # Never remove this line + 'mailer.dontsendentry', + 'mailer.message', + 'mailer.messagelog', 'migrations.migration', 'note.note' # We only store the subclasses 'note.transaction', @@ -78,19 +81,30 @@ def save_object(sender, instance, **kwargs): if instance.last_login != previous.last_login: return - # On crée notre propre sérialiseur JSON pour pouvoir sauvegarder les modèles + changed_fields = '__all__' + if previous: + # On ne garde que les champs modifiés + changed_fields = [] + for field in instance._meta.fields: + if field.name.endswith("_ptr"): + # A field ending with _ptr is a OneToOneRel with a subclass, e.g. NoteClub.note_ptr -> Note + continue + if getattr(instance, field.name) != getattr(previous, field.name): + changed_fields.append(field.name) + + if len(changed_fields) == 0: + # Pas de log s'il n'y a pas de modification + return + + # On crée notre propre sérialiseur JSON pour pouvoir sauvegarder les modèles avec uniquement les champs modifiés class CustomSerializer(ModelSerializer): class Meta: model = instance.__class__ - fields = '__all__' + fields = changed_fields previous_json = JSONRenderer().render(CustomSerializer(previous).data).decode("UTF-8") if previous else None instance_json = JSONRenderer().render(CustomSerializer(instance).data).decode("UTF-8") - if previous_json == instance_json: - # Pas de log s'il n'y a pas de modification - return - Changelog.objects.create(user=user, ip=ip, model=ContentType.objects.get_for_model(instance), diff --git a/apps/member/admin.py b/apps/member/admin.py index bd29557b..4cc2d0bf 100644 --- a/apps/member/admin.py +++ b/apps/member/admin.py @@ -17,6 +17,7 @@ class ProfileInline(admin.StackedInline): Inline user profile in user admin """ model = Profile + form = ProfileForm can_delete = False @@ -25,7 +26,6 @@ class CustomUserAdmin(UserAdmin): inlines = (ProfileInline,) list_display = ('username', 'email', 'first_name', 'last_name', 'is_staff') list_select_related = ('profile',) - form = ProfileForm def get_inline_instances(self, request, obj=None): """ diff --git a/apps/member/forms.py b/apps/member/forms.py index 50fa9c47..a5d571b6 100644 --- a/apps/member/forms.py +++ b/apps/member/forms.py @@ -4,6 +4,8 @@ from django import forms from django.contrib.auth.forms import AuthenticationForm from django.contrib.auth.models import User +from django.forms import CheckboxSelectMultiple +from django.utils import timezone from django.utils.translation import gettext_lazy as _ from note.models import NoteSpecial, Alias from note_kfet.inputs import Autocomplete, AmountInput, DatePickerInput @@ -36,6 +38,20 @@ class ProfileForm(forms.ModelForm): """ A form for the extras field provided by the :model:`member.Profile` model. """ + report_frequency = forms.IntegerField(required=False, initial=0, label=_("Report frequency")) + + last_report = forms.DateTimeField(required=False, disabled=True, label=_("Last report date")) + + def clean_promotion(self): + promotion = self.cleaned_data["promotion"] + if promotion > timezone.now().year: + self.add_error("promotion", _("You can't register to the note if you come from the future.")) + return promotion + + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + self.fields['address'].widget.attrs.update({"placeholder": "4 avenue des Sciences, 91190 GIF-SUR-YVETTE"}) + self.fields['promotion'].widget.attrs.update({"max": timezone.now().year}) def save(self, commit=True): if not self.instance.section or (("department" in self.changed_data @@ -49,6 +65,19 @@ class ProfileForm(forms.ModelForm): exclude = ('user', 'email_confirmed', 'registration_valid', ) +class ImageForm(forms.Form): + """ + Form used for the js interface for profile picture + """ + image = forms.ImageField(required=False, + label=_('select an image'), + help_text=_('Maximal size: 2MB')) + x = forms.FloatField(widget=forms.HiddenInput()) + y = forms.FloatField(widget=forms.HiddenInput()) + width = forms.FloatField(widget=forms.HiddenInput()) + height = forms.FloatField(widget=forms.HiddenInput()) + + class ClubForm(forms.ModelForm): def clean(self): cleaned_data = super().clean() @@ -151,6 +180,7 @@ class MembershipRolesForm(forms.ModelForm): roles = forms.ModelMultipleChoiceField( queryset=Role.objects.filter(weirole=None).all(), label=_("Roles"), + widget=CheckboxSelectMultiple(), ) class Meta: diff --git a/apps/member/models.py b/apps/member/models.py index efd8bf8c..b17f1f09 100644 --- a/apps/member/models.py +++ b/apps/member/models.py @@ -8,11 +8,15 @@ from django.conf import settings from django.contrib.auth.models import User from django.core.exceptions import ValidationError from django.db import models +from django.db.models import Q from django.template import loader from django.urls import reverse, reverse_lazy +from django.utils import timezone from django.utils.encoding import force_bytes from django.utils.http import urlsafe_base64_encode from django.utils.translation import gettext_lazy as _ +from phonenumber_field.modelfields import PhoneNumberField +from permission.models import Role from registration.tokens import email_validation_token from note.models import MembershipTransaction @@ -30,7 +34,7 @@ class Profile(models.Model): on_delete=models.CASCADE, ) - phone_number = models.CharField( + phone_number = PhoneNumberField( verbose_name=_('phone number'), max_length=50, blank=True, @@ -68,7 +72,7 @@ class Profile(models.Model): ] ) - promotion = models.PositiveIntegerField( + promotion = models.PositiveSmallIntegerField( null=True, default=datetime.date.today().year, verbose_name=_("promotion"), @@ -88,6 +92,39 @@ class Profile(models.Model): default=False, ) + ml_events_registration = models.CharField( + blank=True, + null=True, + default=None, + max_length=2, + choices=[ + (None, _("No")), + ('fr', _("Yes (receive them in french)")), + ('en', _("Yes (receive them in english)")), + ], + verbose_name=_("Register on the mailing list to stay informed of the events of the campus (1 mail/week)"), + ) + + ml_sport_registration = models.BooleanField( + default=False, + verbose_name=_("Register on the mailing list to stay informed of the sport events of the campus (1 mail/week)"), + ) + + ml_art_registration = models.BooleanField( + default=False, + verbose_name=_("Register on the mailing list to stay informed of the art events of the campus (1 mail/week)"), + ) + + report_frequency = models.PositiveSmallIntegerField( + verbose_name=_("report frequency (in days)"), + default=0, + ) + + last_report = models.DateTimeField( + verbose_name=_("last report date"), + default=timezone.now, + ) + email_confirmed = models.BooleanField( verbose_name=_("email confirmed"), default=False, @@ -128,18 +165,28 @@ class Profile(models.Model): indexes = [models.Index(fields=['user'])] def get_absolute_url(self): - return reverse('user_detail', args=(self.pk,)) + return reverse('member:user_detail', args=(self.user_id,)) + + def __str__(self): + return str(self.user) def send_email_validation_link(self): - subject = _("Activate your Note Kfet account") - message = loader.render_to_string('registration/mails/email_validation_email.html', + subject = "[Note Kfet] " + str(_("Activate your Note Kfet account")) + message = loader.render_to_string('registration/mails/email_validation_email.txt', { 'user': self.user, 'domain': os.getenv("NOTE_URL", "note.example.com"), 'token': email_validation_token.make_token(self.user), 'uid': urlsafe_base64_encode(force_bytes(self.user.pk)), }) - self.user.email_user(subject, message) + html = loader.render_to_string('registration/mails/email_validation_email.html', + { + 'user': self.user, + 'domain': os.getenv("NOTE_URL", "note.example.com"), + 'token': email_validation_token.make_token(self.user), + 'uid': urlsafe_base64_encode(force_bytes(self.user.pk)), + }) + self.user.email_user(subject, message, html_message=html) class Club(models.Model): @@ -195,17 +242,14 @@ class Club(models.Model): blank=True, null=True, verbose_name=_('membership start'), - help_text=_('How long after January 1st the members can renew ' - 'their membership.'), + help_text=_('Date from which the members can renew their membership.'), ) membership_end = models.DateField( blank=True, null=True, verbose_name=_('membership end'), - help_text=_('How long the membership can last after January 1st ' - 'of the next year after members can renew their ' - 'membership.'), + help_text=_('Maximal date of a membership, after which members must renew it.'), ) def update_membership_dates(self): @@ -294,13 +338,33 @@ class Membership(models.Model): else: return self.date_start.toordinal() <= datetime.datetime.now().toordinal() + def renew(self): + if Membership.objects.filter( + user=self.user, + club=self.club, + date_start__gte=self.club.membership_start, + ).exists(): + # Membership is already renewed + return + new_membership = Membership( + user=self.user, + club=self.club, + date_start=max(self.date_end + datetime.timedelta(days=1), self.club.membership_start), + ) + if hasattr(self, '_force_renew_parent') and self._force_renew_parent: + new_membership._force_renew_parent = True + if hasattr(self, '_soge') and self._soge: + new_membership._soge = True + if hasattr(self, '_force_save') and self._force_save: + new_membership._force_save = True + new_membership.save() + new_membership.roles.set(self.roles.all()) + new_membership.save() + def save(self, *args, **kwargs): """ Calculate fee and end date before saving the membership and creating the transaction if needed. """ - if self.club.parent_club is not None: - if not Membership.objects.filter(user=self.user, club=self.club.parent_club).exists(): - raise ValidationError(_('User is not a member of the parent club') + ' ' + self.club.parent_club.name) if self.pk: for role in self.roles.all(): @@ -320,6 +384,55 @@ class Membership(models.Model): ).exists(): raise ValidationError(_('User is already a member of the club')) + if self.club.parent_club is not None and not self.pk: + # Check that the user is already a member of the parent club if the membership is created + if not Membership.objects.filter( + user=self.user, + club=self.club.parent_club, + date_start__gte=self.club.parent_club.membership_start, + ).exists(): + if hasattr(self, '_force_renew_parent') and self._force_renew_parent: + parent_membership = Membership.objects.filter( + user=self.user, + club=self.club.parent_club, + ).order_by("-date_start") + if parent_membership.exists(): + # Renew the previous membership of the parent club + parent_membership = parent_membership.first() + parent_membership._force_renew_parent = True + if hasattr(self, '_soge'): + parent_membership._soge = True + if hasattr(self, '_force_save'): + parent_membership._force_save = True + parent_membership.renew() + else: + # Create a new membership in the parent club + parent_membership = Membership( + user=self.user, + club=self.club.parent_club, + date_start=self.date_start, + ) + parent_membership._force_renew_parent = True + if hasattr(self, '_soge'): + parent_membership._soge = True + if hasattr(self, '_force_save'): + parent_membership._force_save = True + parent_membership.save() + parent_membership.refresh_from_db() + + if self.club.parent_club.name == "BDE": + parent_membership.roles.set( + Role.objects.filter(Q(name="Adhérent BDE") | Q(name="Membre de club")).all()) + elif self.club.parent_club.name == "Kfet": + parent_membership.roles.set( + Role.objects.filter(Q(name="Adhérent Kfet") | Q(name="Membre de club")).all()) + else: + parent_membership.roles.set(Role.objects.filter(name="Membre de club").all()) + parent_membership.save() + else: + raise ValidationError(_('User is not a member of the parent club') + + ' ' + self.club.parent_club.name) + if self.user.profile.paid: self.fee = self.club.membership_fee_paid else: @@ -353,8 +466,9 @@ class Membership(models.Model): reason="Adhésion " + self.club.name, ) transaction._force_save = True - print(hasattr(self, '_soge')) - if hasattr(self, '_soge') and "treasury" in settings.INSTALLED_APPS: + if hasattr(self, '_soge') and "treasury" in settings.INSTALLED_APPS\ + and (self.club.name == "BDE" or self.club.name == "Kfet" + or ("wei" in settings.INSTALLED_APPS and hasattr(self.club, "weiclub") and self.club.weiclub)): # If the soge pays, then the transaction is unvalidated in a first time, then submitted for control # to treasurers. transaction.valid = False diff --git a/apps/member/static/member/js/alias.js b/apps/member/static/member/js/alias.js new file mode 100644 index 00000000..2d652dde --- /dev/null +++ b/apps/member/static/member/js/alias.js @@ -0,0 +1,43 @@ +/** + * On form submit, create a new alias + */ +function create_alias (e) { + // Do not submit HTML form + e.preventDefault(); + + // Get data and send to API + const formData = new FormData(e.target); + $.post("/api/note/alias/", { + "csrfmiddlewaretoken": formData.get("csrfmiddlewaretoken"), + "name": formData.get("name"), + "note": formData.get("note") + }).done(function () { + // Reload table + $("#alias_table").load(location.pathname + " #alias_table"); + addMsg("Alias ajouté", "success"); + }).fail(function (xhr, _textStatus, _error) { + errMsg(xhr.responseJSON); + }); +} + +/** + * On click of "delete", delete the alias + * @param Integer button_id Alias id to remove + */ +function delete_button (button_id) { + $.ajax({ + url: "/api/note/alias/" + button_id + "/", + method: "DELETE", + headers: { "X-CSRFTOKEN": CSRF_TOKEN } + }).done(function () { + addMsg('Alias supprimé', 'success'); + $("#alias_table").load(location.pathname + " #alias_table"); + }).fail(function (xhr, _textStatus, _error) { + errMsg(xhr.responseJSON); + }); +} + +$(document).ready(function () { + // Attach event + document.getElementById("form_alias").addEventListener("submit", create_alias); +}) \ No newline at end of file diff --git a/apps/member/tables.py b/apps/member/tables.py index 1247da00..8c979f08 100644 --- a/apps/member/tables.py +++ b/apps/member/tables.py @@ -1,6 +1,7 @@ # Copyright (C) 2018-2020 by BDE ENS Paris-Saclay # SPDX-License-Identifier: GPL-3.0-or-later -from datetime import datetime + +from datetime import date import django_tables2 as tables from django.contrib.auth.models import User @@ -38,19 +39,22 @@ class UserTable(tables.Table): """ List all users. """ - section = tables.Column(accessor='profile.section') + alias = tables.Column() - balance = tables.Column(accessor='note.balance', verbose_name=_("Balance")) + section = tables.Column(accessor='profile__section') - def render_balance(self, value): - return pretty_money(value) + balance = tables.Column(accessor='note__balance', verbose_name=_("Balance")) + + def render_balance(self, record, value): + return pretty_money(value)\ + if PermissionBackend.check_perm(get_current_authenticated_user(), "note.view_note", record.note) else "—" class Meta: attrs = { 'class': 'table table-condensed table-striped table-hover' } template_name = 'django_tables2/bootstrap4.html' - fields = ('last_name', 'first_name', 'username', 'email') + fields = ('last_name', 'first_name', 'username', 'alias', 'email') model = User row_attrs = { 'class': 'table-row', @@ -92,26 +96,30 @@ class MembershipTable(tables.Table): t = pretty_money(value) # If it is required and if the user has the right, the renew button is displayed. - if record.club.membership_start is not None: - if record.date_start < record.club.membership_start: # If the renew is available - if not Membership.objects.filter( - club=record.club, - user=record.user, - date_start__gte=record.club.membership_start, - date_end__lte=record.club.membership_end, - ).exists(): # If the renew is not yet performed - empty_membership = Membership( - club=record.club, - user=record.user, - date_start=datetime.now().date(), - date_end=datetime.now().date(), - fee=0, + if record.club.membership_start is not None \ + and record.date_start < record.club.membership_start: + if not Membership.objects.filter( + club=record.club, + user=record.user, + date_start__gte=record.club.membership_start, + date_end__lte=record.club.membership_end, + ).exists(): # If the renew is not yet performed + empty_membership = Membership( + club=record.club, + user=record.user, + date_start=date.today(), + date_end=date.today(), + fee=0, + ) + if PermissionBackend.check_perm(get_current_authenticated_user(), + "member:add_membership", empty_membership): # If the user has right + renew_url = reverse_lazy('member:club_renew_membership', + kwargs={"pk": record.pk}) + t = format_html( + t + ' ', + renew_url=renew_url, text=_("Renew") ) - if PermissionBackend.check_perm(get_current_authenticated_user(), - "member:add_membership", empty_membership): # If the user has right - t = format_html(t + ' {text}', - url=reverse_lazy('member:club_renew_membership', - kwargs={"pk": record.pk}), text=_("Renew")) return t def render_roles(self, record): @@ -125,7 +133,7 @@ class MembershipTable(tables.Table): class Meta: attrs = { - 'class': 'table table-condensed table-striped table-hover', + 'class': 'table table-condensed table-striped', 'style': 'table-layout: fixed;' } template_name = 'django_tables2/bootstrap4.html' @@ -157,5 +165,5 @@ class ClubManagerTable(tables.Table): 'style': 'table-layout: fixed;' } template_name = 'django_tables2/bootstrap4.html' - fields = ('user', 'user.first_name', 'user.last_name', 'roles', ) + fields = ('user', 'user__first_name', 'user__last_name', 'roles', ) model = Membership diff --git a/apps/member/templates/member/add_members.html b/apps/member/templates/member/add_members.html new file mode 100644 index 00000000..fa0a958c --- /dev/null +++ b/apps/member/templates/member/add_members.html @@ -0,0 +1,75 @@ +{% extends "member/base.html" %} +{% comment %} +SPDX-License-Identifier: GPL-3.0-or-later +{% endcomment %} +{% load crispy_forms_tags i18n pretty_money %} + +{% block profile_content %} +
+

+ {{ title }} +

+
+ {% if additional_fee_renewal %} +
+ {% if renewal %} + {% blocktrans trimmed with clubs=clubs_renewal|join:", " pretty_fee=additional_fee_renewal|pretty_money %} + The user is not a member of the club·s {{ clubs }}. An additional fee of {{ pretty_fee }} + will be charged to renew automatically the membership in this/these club·s. + {% endblocktrans %} + {% else %} + {% blocktrans trimmed with clubs=clubs_renewal|join:", " pretty_fee=additional_fee_renewal|pretty_money %} + This club has parents {{ clubs }}. An additional fee of {{ pretty_fee }} + will be charged to adhere automatically to this/these club·s. + {% endblocktrans %} + {% endif %} +
+ {% endif %} + +
+ {% csrf_token %} + {{ form|crispy }} + +
+
+
+{% endblock %} + +{% block extrajavascript %} + +{% endblock %} \ No newline at end of file diff --git a/apps/member/templates/member/base.html b/apps/member/templates/member/base.html new file mode 100644 index 00000000..e1e9335b --- /dev/null +++ b/apps/member/templates/member/base.html @@ -0,0 +1,184 @@ +{% extends "base.html" %} +{% comment %} +SPDX-License-Identifier: GPL-3.0-or-later +{% endcomment %} +{% load i18n perms %} + +{# Use a fluid-width container #} +{% block containertype %}container-fluid{% endblock %} + +{% block content %} +
+
+ {% block profile_info %} +
+

+ {% if user_object %} + {% trans "Account #" %}{{ user_object.pk }} + {% elif club %} + Club {{ club.name }} + {% endif %} +

+
+ {% if user_object %} + + + + {% elif club %} + + + + {% endif %} +
+ {% if note.inactivity_reason %} +
+ {{ note.get_inactivity_reason_display }} +
+ {% endif %} +
+ {% if user_object %} + {% include "member/includes/profile_info.html" %} + {% elif club %} + {% include "member/includes/club_info.html" %} + {% endif %} +
+ +
+ {% endblock %} +
+
+ {% block profile_content %}{% endblock %} +
+ + {# Popup to confirm the action of locking the note. Managed by a button #} + + + {# Popup to confirm the action of unlocking the note. Managed by a button #} + +
+{% endblock %} + +{% block extrajavascript %} + +{% endblock %} diff --git a/apps/member/templates/member/club_alias.html b/apps/member/templates/member/club_alias.html new file mode 100644 index 00000000..f4b33f42 --- /dev/null +++ b/apps/member/templates/member/club_alias.html @@ -0,0 +1,31 @@ +{% extends "member/base.html" %} +{% comment %} +SPDX-License-Identifier: GPL-3.0-or-later +{% endcomment %} +{% load static django_tables2 i18n %} + +{% block profile_content %} +
+

+ {% trans "Note aliases" %} +

+ +
+ {% if can_create %} +
+ {% csrf_token %} + + +
+ +
+
+ {% endif %} +
+ {% render_table aliases %} +
+{% endblock %} + +{% block extrajavascript %} + +{% endblock%} \ No newline at end of file diff --git a/apps/member/templates/member/club_detail.html b/apps/member/templates/member/club_detail.html new file mode 100644 index 00000000..a0b927e1 --- /dev/null +++ b/apps/member/templates/member/club_detail.html @@ -0,0 +1,48 @@ +{% extends "member/base.html" %} +{% comment %} +SPDX-License-Identifier: GPL-3.0-or-later +{% endcomment %} +{% load render_table from django_tables2 %} +{% load i18n perms %} + +{% block profile_content %} +{% if managers.data %} +
+
+ + {% trans "Club managers" %} + +
+ {% render_table managers %} +
+ +
+{% endif %} + +{% if member_list.data %} +
+
+ + {% trans "Club members" %} + +
+ {% render_table member_list %} +
+ +
+{% endif %} + +{% if history_list.data %} +
+
+ + {% trans "Transaction history" %} + +
+
+ {% render_table history_list %} +
+
+{% endif %} +{% endblock %} diff --git a/apps/member/templates/member/club_form.html b/apps/member/templates/member/club_form.html new file mode 100644 index 00000000..1dc43c4f --- /dev/null +++ b/apps/member/templates/member/club_form.html @@ -0,0 +1,42 @@ +{% extends "member/base.html" %} +{% comment %} +SPDX-License-Identifier: GPL-3.0-or-later +{% endcomment %} +{% load i18n crispy_forms_tags %} + +{% block profile_content %} +
+

+ {{ title }} +

+
+
+ {% csrf_token %} + {{ form|crispy }} + +
+
+
+{% endblock %} + +{% block extrajavascript %} + +{% endblock %} \ No newline at end of file diff --git a/apps/member/templates/member/club_list.html b/apps/member/templates/member/club_list.html new file mode 100644 index 00000000..66863a3c --- /dev/null +++ b/apps/member/templates/member/club_list.html @@ -0,0 +1,16 @@ +{% extends "base_search.html" %} +{% comment %} +SPDX-License-Identifier: GPL-3.0-or-later +{% endcomment %} +{% load i18n perms %} + +{% block content %} +{% if can_add_club %} + + {% trans "Create club" %} + +{% endif %} + +{# Search panel #} +{{ block.super }} +{% endblock %} \ No newline at end of file diff --git a/apps/member/templates/member/club_members.html b/apps/member/templates/member/club_members.html new file mode 100644 index 00000000..3645050a --- /dev/null +++ b/apps/member/templates/member/club_members.html @@ -0,0 +1,69 @@ +{% extends "member/base.html" %} +{% comment %} +SPDX-License-Identifier: GPL-3.0-or-later +{% endcomment %} +{% load i18n %} +{% load render_table from django_tables2 %} + +{% block profile_content %} +
+

+ {{ title }} +

+
+ +
+ +
+
+ + +
+
+
+ {% if table.data %} + {% render_table table %} + {% else %} +
+ {% trans "There is no membership found with this pattern." %} +
+ {% endif %} +
+
+{% endblock %} + +{% block extrajavascript %} + +{% endblock %} \ No newline at end of file diff --git a/apps/member/templates/member/includes/club_info.html b/apps/member/templates/member/includes/club_info.html new file mode 100644 index 00000000..92c7b569 --- /dev/null +++ b/apps/member/templates/member/includes/club_info.html @@ -0,0 +1,57 @@ +{% load i18n pretty_money perms %} + +
+
{% trans 'name'|capfirst %}
+
{{ club.name }}
+ + {% if club.parent_club %} +
+ {% trans 'Club Parent'|capfirst %} +
+
{{ club.parent_club.name }}
+ {% endif %} + + {% if club.require_memberships %} + {% if club.membership_start %} +
{% trans 'membership start'|capfirst %}
+
{{ club.membership_start }}
+ {% endif %} + + {% if club.membership_end %} +
{% trans 'membership end'|capfirst %}
+
{{ club.membership_end }}
+ {% endif %} + + {% if club.membership_duration %} +
{% trans 'membership duration'|capfirst %}
+
{{ club.membership_duration }} {% trans "days" %}
+ {% endif %} + + {% if club.membership_fee_paid == club.membership_fee_unpaid %} +
{% trans 'membership fee'|capfirst %}
+
{{ club.membership_fee_paid|pretty_money }}
+ {% else %} +
{% trans 'membership fee (paid students)'|capfirst %}
+
{{ club.membership_fee_paid|pretty_money }}
+ +
{% trans 'membership fee (unpaid students)'|capfirst %}
+
{{ club.membership_fee_unpaid|pretty_money }}
+ {% endif %} + {% endif %} + + {% if "note.view_note"|has_perm:club.note %} +
{% trans 'balance'|capfirst %}
+
{{ club.note.balance | pretty_money }}
+ {% endif %} + +
{% trans 'aliases'|capfirst %}
+
+ + + {% trans 'Manage aliases' %} ({{ user_object.note.alias_set.all|length }}) + +
+ +
{% trans 'email'|capfirst %}
+
{{ club.email }}
+
\ No newline at end of file diff --git a/apps/member/templates/member/includes/profile_info.html b/apps/member/templates/member/includes/profile_info.html new file mode 100644 index 00000000..04fc6742 --- /dev/null +++ b/apps/member/templates/member/includes/profile_info.html @@ -0,0 +1,54 @@ +{% load i18n pretty_money perms %} + +
+
{% trans 'name'|capfirst %}, {% trans 'first name' %}
+
{{ user_object.last_name }} {{ user_object.first_name }}
+ +
{% trans 'username'|capfirst %}
+
{{ user_object.username }}
+ + {% if user_object.pk == user.pk %} +
{% trans 'password'|capfirst %}
+
+ + + {% trans 'Change password' %} + +
+ {% endif %} + +
{% trans 'aliases'|capfirst %}
+
+ + + {% trans 'Manage aliases' %} ({{ user_object.note.alias_set.all|length }}) + +
+ +
{% trans 'section'|capfirst %}
+
{{ user_object.profile.section }}
+ +
{% trans 'email'|capfirst %}
+
{{ user_object.email }}
+ +
{% trans 'phone number'|capfirst %}
+
{{ user_object.profile.phone_number }} +
+ +
{% trans 'address'|capfirst %}
+
{{ user_object.profile.address }}
+ + {% if "note.view_note"|has_perm:user_object.note %} +
{% trans 'balance'|capfirst %}
+
{{ user_object.note.balance | pretty_money }}
+ +
{% trans 'paid'|capfirst %}
+
{{ user_object.profile.paid|yesno }}
+ {% endif %} +
+ +{% if user_object.pk == user_object.pk %} + + {% trans 'Manage auth token' %} + +{% endif %} \ No newline at end of file diff --git a/apps/member/templates/member/manage_auth_tokens.html b/apps/member/templates/member/manage_auth_tokens.html new file mode 100644 index 00000000..473286c1 --- /dev/null +++ b/apps/member/templates/member/manage_auth_tokens.html @@ -0,0 +1,36 @@ +{% extends "member/base.html" %} +{% comment %} +SPDX-License-Identifier: GPL-3.0-or-later +{% endcomment %} +{% load i18n %} + +{% block content %} +
+

À quoi sert un jeton d'authentification ?

+ + Un jeton vous permet de vous connecter à l'API de la Note Kfet.
+ Il suffit pour cela d'ajouter en en-tête de vos requêtes Authorization: Token <TOKEN> + pour pouvoir vous identifier.

+ + Une documentation de l'API arrivera ultérieurement. +
+ +
+ {%trans 'Token' %} : + {% if 'show' in request.GET %} + {{ token.key }} (cacher) + {% else %} + caché (montrer) + {% endif %} +
+ {%trans 'Created' %} : {{ token.created }} +
+ +
+ Attention : regénérer le jeton va révoquer tout accès autorisé à l'API via ce jeton ! +
+ + + + +{% endblock %} \ No newline at end of file diff --git a/apps/member/templates/member/picture_update.html b/apps/member/templates/member/picture_update.html new file mode 100644 index 00000000..7c9128ce --- /dev/null +++ b/apps/member/templates/member/picture_update.html @@ -0,0 +1,107 @@ +{% extends "member/base.html" %} +{% comment %} +SPDX-License-Identifier: GPL-3.0-or-later +{% endcomment %} +{% load i18n crispy_forms_tags %} + +{% block profile_content %} +
+

+ {{ title }} +

+
+
+
+ {% csrf_token %} + {{ form |crispy }} +
+
+ + +
+
+{% endblock %} + +{% block extracss %} + +{% endblock %} + +{% block extrajavascript%} + + + +{% endblock %} \ No newline at end of file diff --git a/apps/member/templates/member/profile_alias.html b/apps/member/templates/member/profile_alias.html new file mode 100644 index 00000000..78989627 --- /dev/null +++ b/apps/member/templates/member/profile_alias.html @@ -0,0 +1,30 @@ +{% extends "member/base.html" %} +{% comment %} +SPDX-License-Identifier: GPL-3.0-or-later +{% endcomment %} +{% load static django_tables2 i18n %} + +{% block profile_content %} +
+

+ {% trans "Note aliases" %} +

+
+ {% if can_create %} +
+ {% csrf_token %} + + +
+ +
+
+ {% endif %} +
+ {% render_table aliases %} +
+{% endblock %} + +{% block extrajavascript %} + +{% endblock%} \ No newline at end of file diff --git a/apps/member/templates/member/profile_detail.html b/apps/member/templates/member/profile_detail.html new file mode 100644 index 00000000..591fa879 --- /dev/null +++ b/apps/member/templates/member/profile_detail.html @@ -0,0 +1,39 @@ +{% extends "member/base.html" %} +{% comment %} +SPDX-License-Identifier: GPL-3.0-or-later +{% endcomment %} +{% load render_table from django_tables2 %} +{% load i18n perms %} + +{% block profile_content %} +{% if not object.profile.email_confirmed and "member.change_profile_email_confirmed"|has_perm:user_object.profile %} +
+ {% trans "This user doesn't have confirmed his/her e-mail address." %} + + {% trans "Click here to resend a validation link." %} + +
+{% endif %} + +
+
+ + {% trans "View my memberships" %} + +
+ {% render_table club_list %} +
+ +
+
+ + {% trans "Transaction history" %} + +
+
+ {% render_table history_list %} +
+
+{% endblock %} diff --git a/apps/member/templates/member/profile_update.html b/apps/member/templates/member/profile_update.html new file mode 100644 index 00000000..2f018381 --- /dev/null +++ b/apps/member/templates/member/profile_update.html @@ -0,0 +1,23 @@ +{% extends "member/base.html" %} +{% comment %} +SPDX-License-Identifier: GPL-3.0-or-later +{% endcomment %} +{% load i18n crispy_forms_tags %} + +{% block profile_content %} +
+

+ {{ title }} +

+
+
+ {% csrf_token %} + {{ form | crispy }} + {{ profile_form | crispy }} + +
+
+
+{% endblock %} \ No newline at end of file diff --git a/apps/member/templates/member/user_list.html b/apps/member/templates/member/user_list.html new file mode 100644 index 00000000..a41d7e69 --- /dev/null +++ b/apps/member/templates/member/user_list.html @@ -0,0 +1,16 @@ +{% extends "base_search.html" %} +{% comment %} +SPDX-License-Identifier: GPL-3.0-or-later +{% endcomment %} +{% load i18n perms %} + +{% block content %} +{% if "member.change_profile_registration_valid"|has_perm:user %} + + {% trans "Registrations" %} + +{% endif %} + +{# Search panel #} +{{ block.super }} +{% endblock %} diff --git a/apps/member/tests/test_login.py b/apps/member/tests/test_login.py new file mode 100644 index 00000000..51a4ab94 --- /dev/null +++ b/apps/member/tests/test_login.py @@ -0,0 +1,62 @@ +# Copyright (C) 2018-2020 by BDE ENS Paris-Saclay +# SPDX-License-Identifier: GPL-3.0-or-later + +from django.contrib.auth.models import User +from django.test import TestCase +from note.models import TransactionTemplate, TemplateCategory + +""" +Test that login page still works +""" + + +class TemplateLoggedOutTests(TestCase): + def test_login_page(self): + response = self.client.get('/accounts/login/') + self.assertEqual(response.status_code, 200) + + +class TemplateLoggedInTests(TestCase): + fixtures = ('initial', ) + + def setUp(self): + self.user = User.objects.create_superuser( + username="admin", + password="adminadmin", + email="admin@example.com", + ) + self.client.force_login(self.user) + sess = self.client.session + sess["permission_mask"] = 42 + sess.save() + + def test_login_page(self): + response = self.client.get('/accounts/login/') + self.assertEqual(response.status_code, 200) + + def test_admin_index(self): + response = self.client.get('/admin/') + self.assertEqual(response.status_code, 200) + + def test_accounts_password_reset(self): + response = self.client.get('/accounts/password_reset/') + self.assertEqual(response.status_code, 200) + + def test_logout_page(self): + response = self.client.get('/accounts/logout/') + self.assertEqual(response.status_code, 200) + + def test_transfer_page(self): + response = self.client.get('/note/transfer/') + self.assertEqual(response.status_code, 200) + + def test_consos_page(self): + # Create one button and ensure that it is visible + cat = TemplateCategory.objects.create() + TransactionTemplate.objects.create( + destination_id=5, + category=cat, + amount=0, + ) + response = self.client.get('/note/consos/') + self.assertEqual(response.status_code, 200) diff --git a/apps/member/views.py b/apps/member/views.py index 30fbb139..cccffc4a 100644 --- a/apps/member/views.py +++ b/apps/member/views.py @@ -2,7 +2,7 @@ # SPDX-License-Identifier: GPL-3.0-or-later import io -from datetime import datetime, timedelta +from datetime import timedelta, date from PIL import Image from django.conf import settings @@ -10,25 +10,26 @@ from django.contrib.auth import logout from django.contrib.auth.mixins import LoginRequiredMixin from django.contrib.auth.models import User from django.contrib.auth.views import LoginView -from django.db.models import Q +from django.db import transaction +from django.db.models import Q, F from django.shortcuts import redirect from django.urls import reverse_lazy from django.utils import timezone from django.utils.translation import gettext_lazy as _ -from django.views.generic import CreateView, DetailView, UpdateView, TemplateView +from django.views.generic import DetailView, UpdateView, TemplateView from django.views.generic.edit import FormMixin from django_tables2.views import SingleTableView from rest_framework.authtoken.models import Token -from note.forms import ImageForm from note.models import Alias, NoteUser from note.models.transactions import Transaction, SpecialTransaction from note.tables import HistoryTable, AliasTable from note_kfet.middlewares import _set_current_user_and_ip from permission.backends import PermissionBackend from permission.models import Role -from permission.views import ProtectQuerysetMixin +from permission.views import ProtectQuerysetMixin, ProtectedCreateView -from .forms import ProfileForm, ClubForm, MembershipForm, CustomAuthenticationForm, UserForm, MembershipRolesForm +from .forms import UserForm, ProfileForm, ImageForm, ClubForm, MembershipForm,\ + CustomAuthenticationForm, MembershipRolesForm from .models import Club, Membership from .tables import ClubTable, UserTable, MembershipTable, ClubManagerTable @@ -49,6 +50,7 @@ class CustomLoginView(LoginView): class UserUpdateView(ProtectQuerysetMixin, LoginRequiredMixin, UpdateView): """ Update the user information. + On this view both `:models:member.User` and `:models:member.Profile` are updated through forms """ model = User form_class = UserForm @@ -69,47 +71,55 @@ class UserUpdateView(ProtectQuerysetMixin, LoginRequiredMixin, UpdateView): form.fields['email'].required = True form.fields['email'].help_text = _("This address must be valid.") - context['profile_form'] = self.profile_form(instance=context['user_object'].profile) + context['profile_form'] = self.profile_form(instance=context['user_object'].profile, + data=self.request.POST if self.request.POST else None) + if not self.object.profile.report_frequency: + del context['profile_form'].fields["last_report"] + return context def form_valid(self, form): + """ + Check if ProfileForm is correct + then check if username is not already taken by someone else or by the user, + then check if email has changed, and if so ask for new validation. + """ + + profile_form = ProfileForm( + data=self.request.POST, + instance=self.object.profile, + ) + profile_form.full_clean() + if not profile_form.is_valid(): + return super().form_invalid(form) new_username = form.data['username'] - # Si l'utilisateur cherche à modifier son pseudo, le nouveau pseudo ne doit pas être proche d'un alias existant + # Check if the new username is not already taken as an alias of someone else. note = NoteUser.objects.filter( alias__normalized_name=Alias.normalize(new_username)) if note.exists() and note.get().user != self.object: form.add_error('username', _("An alias with a similar name already exists.")) return super().form_invalid(form) + # Check if the username is one of user's aliases. + alias = Alias.objects.filter(name=new_username) + if not alias.exists(): + similar = Alias.objects.filter( + normalized_name=Alias.normalize(new_username)) + if similar.exists(): + similar.delete() + olduser = User.objects.get(pk=form.instance.pk) - profile_form = ProfileForm( - data=self.request.POST, - instance=self.object.profile, - ) - if form.is_valid() and profile_form.is_valid(): - new_username = form.data['username'] - alias = Alias.objects.filter(name=new_username) - # Si le nouveau pseudo n'est pas un de nos alias, - # on supprime éventuellement un alias similaire pour le remplacer - if not alias.exists(): - similar = Alias.objects.filter( - normalized_name=Alias.normalize(new_username)) - if similar.exists(): - similar.delete() + user = form.save(commit=False) - olduser = User.objects.get(pk=form.instance.pk) + if olduser.email != user.email: + # If the user changed her/his email, then it is unvalidated and a confirmation link is sent. + user.profile.email_confirmed = False + user.profile.send_email_validation_link() - user = form.save(commit=False) - profile = profile_form.save(commit=False) - profile.user = user - profile.save() - user.save() - - if olduser.email != user.email: - # If the user changed her/his email, then it is unvalidated and a confirmation link is sent. - user.profile.email_confirmed = False - user.profile.save() - user.profile.send_email_validation_link() + profile = profile_form.save(commit=False) + profile.user = user + profile.save() + user.save() return super().form_valid(form) @@ -120,7 +130,7 @@ class UserUpdateView(ProtectQuerysetMixin, LoginRequiredMixin, UpdateView): class UserDetailView(ProtectQuerysetMixin, LoginRequiredMixin, DetailView): """ - Affiche les informations sur un utilisateur, sa note, ses clubs... + Display all information about a user. """ model = User context_object_name = "user_object" @@ -131,11 +141,18 @@ class UserDetailView(ProtectQuerysetMixin, LoginRequiredMixin, DetailView): """ We can't display information of a not registered user. """ - return super().get_queryset().filter(profile__registration_valid=True) + qs = super().get_queryset() + if self.request.user.is_superuser and self.request.session.get("permission_mask", -1) >= 42: + return qs + return qs.filter(profile__registration_valid=True) def get_context_data(self, **kwargs): + """ + Add history of transaction and list of membership of user. + """ context = super().get_context_data(**kwargs) user = context['user_object'] + context["note"] = user.note history_list = \ Transaction.objects.all().filter(Q(source=user.note) | Q(destination=user.note))\ .order_by("-created_at")\ @@ -144,11 +161,33 @@ class UserDetailView(ProtectQuerysetMixin, LoginRequiredMixin, DetailView): history_table.paginate(per_page=20, page=self.request.GET.get("transaction-page", 1)) context['history_list'] = history_table - club_list = Membership.objects.filter(user=user, date_end__gte=datetime.today())\ + club_list = Membership.objects.filter(user=user, date_end__gte=date.today())\ .filter(PermissionBackend.filter_queryset(self.request.user, Membership, "view")) membership_table = MembershipTable(data=club_list, prefix='membership-') membership_table.paginate(per_page=10, page=self.request.GET.get("membership-page", 1)) context['club_list'] = membership_table + + # Check permissions to see if the authenticated user can lock/unlock the note + with transaction.atomic(): + modified_note = NoteUser.objects.get(pk=user.note.pk) + modified_note.is_active = True + modified_note.inactivity_reason = 'manual' + context["can_lock_note"] = user.note.is_active and PermissionBackend\ + .check_perm(self.request.user, "note.change_noteuser_is_active", + modified_note) + old_note = NoteUser.objects.select_for_update().get(pk=user.note.pk) + modified_note.inactivity_reason = 'forced' + modified_note._force_save = True + modified_note.save() + context["can_force_lock"] = user.note.is_active and PermissionBackend\ + .check_perm(self.request.user, "note.change_note_is_active", modified_note) + old_note._force_save = True + old_note.save() + modified_note.refresh_from_db() + modified_note.is_active = True + context["can_unlock_note"] = not user.note.is_active and PermissionBackend\ + .check_perm(self.request.user, "note.change_note_is_active", modified_note) + return context @@ -165,7 +204,9 @@ class UserListView(ProtectQuerysetMixin, LoginRequiredMixin, SingleTableView): """ Filter the user list with the given pattern. """ - qs = super().get_queryset().distinct().filter(profile__registration_valid=True) + qs = super().get_queryset().distinct("username").annotate(alias=F("note__alias__name"))\ + .annotate(normalized_alias=F("note__alias__normalized_name"))\ + .filter(profile__registration_valid=True).order_by("username") if "search" in self.request.GET: pattern = self.request.GET["search"] @@ -173,17 +214,20 @@ class UserListView(ProtectQuerysetMixin, LoginRequiredMixin, SingleTableView): return qs.none() qs = qs.filter( - Q(first_name__iregex=pattern) - | Q(last_name__iregex=pattern) - | Q(profile__section__iregex=pattern) - | Q(username__iregex="^" + pattern) - | Q(note__alias__name__iregex="^" + pattern) - | Q(note__alias__normalized_name__iregex=Alias.normalize("^" + pattern)) - ) + username__iregex="^" + pattern + ).union( + qs.filter( + (Q(alias__iregex="^" + pattern) + | Q(normalized_alias__iregex="^" + Alias.normalize(pattern)) + | Q(last_name__iregex="^" + pattern) + | Q(first_name__iregex="^" + pattern) + | Q(email__istartswith=pattern)) + & ~Q(username__iregex="^" + pattern) + ), all=True) else: qs = qs.none() - return qs[:20] + return qs class ProfileAliasView(ProtectQuerysetMixin, LoginRequiredMixin, DetailView): @@ -198,7 +242,13 @@ class ProfileAliasView(ProtectQuerysetMixin, LoginRequiredMixin, DetailView): def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) note = context['object'].note - context["aliases"] = AliasTable(note.alias_set.all()) + context["aliases"] = AliasTable(note.alias_set.filter(PermissionBackend + .filter_queryset(self.request.user, Alias, "view")).all()) + context["can_create"] = PermissionBackend.check_perm(self.request.user, "note.add_alias", Alias( + note=context["object"].note, + name="", + normalized_name="", + )) return context @@ -255,7 +305,7 @@ class PictureUpdateView(ProtectQuerysetMixin, LoginRequiredMixin, FormMixin, Det class ProfilePictureUpdateView(PictureUpdateView): model = User - template_name = 'member/profile_picture_update.html' + template_name = 'member/picture_update.html' context_object_name = 'user_object' @@ -286,7 +336,7 @@ class ManageAuthTokens(LoginRequiredMixin, TemplateView): # ******************************* # -class ClubCreateView(ProtectQuerysetMixin, LoginRequiredMixin, CreateView): +class ClubCreateView(ProtectQuerysetMixin, ProtectedCreateView): """ Create Club """ @@ -295,6 +345,12 @@ class ClubCreateView(ProtectQuerysetMixin, LoginRequiredMixin, CreateView): success_url = reverse_lazy('member:club_list') extra_context = {"title": _("Create new club")} + def get_sample_object(self): + return Club( + name="", + email="", + ) + def form_valid(self, form): return super().form_valid(form) @@ -317,12 +373,20 @@ class ClubListView(ProtectQuerysetMixin, LoginRequiredMixin, SingleTableView): qs = qs.filter( Q(name__iregex=pattern) - | Q(note__alias__name__iregex="^" + pattern) - | Q(note__alias__normalized_name__iregex=Alias.normalize("^" + pattern)) + | Q(note__alias__name__iregex=pattern) + | Q(note__alias__normalized_name__iregex=Alias.normalize(pattern)) ) return qs + def get_context_data(self, **kwargs): + context = super().get_context_data(**kwargs) + context["can_add_club"] = PermissionBackend.check_perm(self.request.user, "member.add_club", Club( + name="", + email="club@example.com", + )) + return context + class ClubDetailView(ProtectQuerysetMixin, LoginRequiredMixin, DetailView): """ @@ -333,25 +397,29 @@ class ClubDetailView(ProtectQuerysetMixin, LoginRequiredMixin, DetailView): extra_context = {"title": _("Club detail")} def get_context_data(self, **kwargs): + """ + Add list of managers (peoples with Permission/Roles in this club), history of transactions and members list + """ context = super().get_context_data(**kwargs) club = context["club"] if PermissionBackend.check_perm(self.request.user, "member.change_club_membership_start", club): club.update_membership_dates() - + # managers list managers = Membership.objects.filter(club=self.object, roles__name="Bureau de club")\ .order_by('user__last_name').all() context["managers"] = ClubManagerTable(data=managers, prefix="managers-") - + # transaction history club_transactions = Transaction.objects.all().filter(Q(source=club.note) | Q(destination=club.note))\ .filter(PermissionBackend.filter_queryset(self.request.user, Transaction, "view"))\ .order_by('-created_at') history_table = HistoryTable(club_transactions, prefix="history-") history_table.paginate(per_page=20, page=self.request.GET.get('history-page', 1)) context['history_list'] = history_table + # member list club_member = Membership.objects.filter( club=club, - date_end__gte=datetime.today(), + date_end__gte=date.today(), ).filter(PermissionBackend.filter_queryset(self.request.user, Membership, "view")) membership_table = MembershipTable(data=club_member, prefix="membership-") @@ -362,8 +430,8 @@ class ClubDetailView(ProtectQuerysetMixin, LoginRequiredMixin, DetailView): empty_membership = Membership( club=club, user=User.objects.first(), - date_start=datetime.now().date(), - date_end=datetime.now().date(), + date_start=date.today(), + date_end=date.today(), fee=0, ) context["can_add_members"] = PermissionBackend()\ @@ -384,7 +452,13 @@ class ClubAliasView(ProtectQuerysetMixin, LoginRequiredMixin, DetailView): def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) note = context['object'].note - context["aliases"] = AliasTable(note.alias_set.all()) + context["aliases"] = AliasTable(note.alias_set.filter(PermissionBackend + .filter_queryset(self.request.user, Alias, "view")).all()) + context["can_create"] = PermissionBackend.check_perm(self.request.user, "note.add_alias", Alias( + note=context["object"].note, + name="", + normalized_name="", + )) return context @@ -416,14 +490,14 @@ class ClubPictureUpdateView(PictureUpdateView): Update the profile picture of a club. """ model = Club - template_name = 'member/club_picture_update.html' + template_name = 'member/picture_update.html' context_object_name = 'club' def get_success_url(self): return reverse_lazy('member:club_detail', kwargs={'pk': self.object.id}) -class ClubAddMemberView(ProtectQuerysetMixin, LoginRequiredMixin, CreateView): +class ClubAddMemberView(ProtectQuerysetMixin, ProtectedCreateView): """ Add a membership to a club. """ @@ -432,15 +506,42 @@ class ClubAddMemberView(ProtectQuerysetMixin, LoginRequiredMixin, CreateView): template_name = 'member/add_members.html' extra_context = {"title": _("Add new member to the club")} + def get_sample_object(self): + if "club_pk" in self.kwargs: + club = Club.objects.get(pk=self.kwargs["club_pk"]) + else: + club = Membership.objects.get(pk=self.kwargs["pk"]).club + return Membership( + user=self.request.user, + club=club, + fee=0, + date_start=timezone.now(), + date_end=timezone.now() + timedelta(days=1), + ) + def get_context_data(self, **kwargs): + """ + Membership can be created, or renewed + In case of creation the url is /club//add_member + For a renewal it will be `club/renew_membership/` + """ context = super().get_context_data(**kwargs) form = context['form'] - if "club_pk" in self.kwargs: - # We create a new membership. + if "club_pk" in self.kwargs: # We create a new membership. club = Club.objects.filter(PermissionBackend.filter_queryset(self.request.user, Club, "view"))\ .get(pk=self.kwargs["club_pk"], weiclub=None) form.fields['credit_amount'].initial = club.membership_fee_paid + # Ensure that the user is member of the parent club and all its the family tree. + c = club + clubs_renewal = [] + additional_fee_renewal = 0 + while c.parent_club is not None: + c = c.parent_club + clubs_renewal.append(c) + additional_fee_renewal += c.membership_fee_paid + context["clubs_renewal"] = clubs_renewal + context["additional_fee_renewal"] = additional_fee_renewal # If the concerned club is the BDE, then we add the option that Société générale pays the membership. if club.name != "BDE": @@ -452,28 +553,56 @@ class ClubAddMemberView(ProtectQuerysetMixin, LoginRequiredMixin, CreateView): kfet = Club.objects.get(name="Kfet") fee += kfet.membership_fee_paid context["total_fee"] = "{:.02f}".format(fee / 100, ) - else: - # This is a renewal. Fields can be pre-completed. + else: # This is a renewal. Fields can be pre-completed. + context["renewal"] = True + old_membership = self.get_queryset().get(pk=self.kwargs["pk"]) club = old_membership.club user = old_membership.user + + c = club + clubs_renewal = [] + additional_fee_renewal = 0 + while c.parent_club is not None: + c = c.parent_club + # check if a valid membership exists for the parent club + if c.membership_start and not Membership.objects.filter( + club=c, + user=user, + date_start__gte=c.membership_start, + ).exists(): + clubs_renewal.append(c) + additional_fee_renewal += c.membership_fee_paid if user.profile.paid else c.membership_fee_unpaid + context["clubs_renewal"] = clubs_renewal + context["additional_fee_renewal"] = additional_fee_renewal + form.fields['user'].initial = user form.fields['user'].disabled = True form.fields['date_start'].initial = old_membership.date_end + timedelta(days=1) - form.fields['credit_amount'].initial = club.membership_fee_paid if user.profile.paid \ - else club.membership_fee_unpaid + form.fields['credit_amount'].initial = (club.membership_fee_paid if user.profile.paid + else club.membership_fee_unpaid) + additional_fee_renewal form.fields['last_name'].initial = user.last_name form.fields['first_name'].initial = user.first_name - # If this is a renewal of a BDE membership, Société générale can pays, if it is not yet done - if club.name != "BDE" or user.profile.soge: + # If this is a renewal of a BDE membership, Société générale can pays, if it has not been already done. + if (club.name != "BDE" and club.name != "Kfet") or user.profile.soge: del form.fields['soge'] else: fee = 0 bde = Club.objects.get(name="BDE") - fee += bde.membership_fee_paid if user.profile.paid else bde.membership_fee_unpaid + if not Membership.objects.filter( + club=bde, + user=user, + date_start__gte=bde.membership_start, + ).exists(): + fee += bde.membership_fee_paid if user.profile.paid else bde.membership_fee_unpaid kfet = Club.objects.get(name="Kfet") - fee += kfet.membership_fee_paid if user.profile.paid else kfet.membership_fee_unpaid + if not Membership.objects.filter( + club=kfet, + user=user, + date_start__gte=bde.membership_start, + ).exists(): + fee += kfet.membership_fee_paid if user.profile.paid else kfet.membership_fee_unpaid context["total_fee"] = "{:.02f}".format(fee / 100, ) context['club'] = club @@ -485,11 +614,11 @@ class ClubAddMemberView(ProtectQuerysetMixin, LoginRequiredMixin, CreateView): Create membership, check that all is good, make transactions """ # Get the club that is concerned by the membership - if "club_pk" in self.kwargs: + if "club_pk" in self.kwargs: # get from url of new membership club = Club.objects.filter(PermissionBackend.filter_queryset(self.request.user, Club, "view")) \ .get(pk=self.kwargs["club_pk"]) user = form.instance.user - else: + else: # get from url for renewal old_membership = self.get_queryset().get(pk=self.kwargs["pk"]) club = old_membership.club user = old_membership.user @@ -498,11 +627,12 @@ class ClubAddMemberView(ProtectQuerysetMixin, LoginRequiredMixin, CreateView): # Get form data credit_type = form.cleaned_data["credit_type"] + # but with this way users can customize their section as they want. credit_amount = form.cleaned_data["credit_amount"] last_name = form.cleaned_data["last_name"] first_name = form.cleaned_data["first_name"] bank = form.cleaned_data["bank"] - soge = form.cleaned_data["soge"] and not user.profile.soge and club.name == "BDE" + soge = form.cleaned_data["soge"] and not user.profile.soge and (club.name == "BDE" or club.name == "Kfet") # If Société générale pays, then we store that information but the payment must be controlled by treasurers # later. The membership transaction will be invalidated. @@ -513,28 +643,30 @@ class ClubAddMemberView(ProtectQuerysetMixin, LoginRequiredMixin, CreateView): if credit_type is None: credit_amount = 0 - if user.profile.paid: - fee = club.membership_fee_paid - else: - fee = club.membership_fee_unpaid + fee = 0 + c = club + # collect the fees required to be paid + while c is not None and c.membership_start: + if not Membership.objects.filter( + club=c, + user=user, + date_start__gte=c.membership_start, + ).exists(): + fee += c.membership_fee_paid if user.profile.paid else c.membership_fee_unpaid + c = c.parent_club + if user.note.balance + credit_amount < fee and not Membership.objects.filter( club__name="Kfet", user=user, - date_start__lte=datetime.now().date(), - date_end__gte=datetime.now().date(), + date_start__lte=date.today(), + date_end__gte=date.today(), ).exists(): # Users without a valid Kfet membership can't have a negative balance. - # Club 2 = Kfet (hard-code :'( ) # TODO Send a notification to the user (with a mail?) to tell her/him to credit her/his note form.add_error('user', _("This user don't have enough money to join this club, and can't have a negative balance.")) return super().form_invalid(form) - if club.parent_club is not None: - if not Membership.objects.filter(user=form.instance.user, club=club.parent_club).exists(): - form.add_error('user', _('User is not a member of the parent club') + ' ' + club.parent_club.name) - return super().form_invalid(form) - if Membership.objects.filter( user=form.instance.user, club=club, @@ -556,10 +688,9 @@ class ClubAddMemberView(ProtectQuerysetMixin, LoginRequiredMixin, CreateView): # Now, all is fine, the membership can be created. - if club.name == "BDE": - # When we renew the BDE membership, we update the profile section. - # We could automate that and remove the section field from the Profile model, - # but with this way users can customize their section as they want. + if club.name == "BDE" or club.name == "Kfet": + # When we renew the BDE membership, we update the profile section + # that should happens at least once a year. user.profile.section = user.profile.section_generated user.profile.save() @@ -588,9 +719,16 @@ class ClubAddMemberView(ProtectQuerysetMixin, LoginRequiredMixin, CreateView): transaction._force_save = True transaction.save() + form.instance._force_renew_parent = True + ret = super().form_valid(form) - member_role = Role.objects.filter(name="Membre de club").all() + if club.name == "BDE": + member_role = Role.objects.filter(Q(name="Adhérent BDE") | Q(name="Membre de club")).all() + elif club.name == "Kfet": + member_role = Role.objects.filter(Q(name="Adhérent Kfet") | Q(name="Membre de club")).all() + else: + member_role = Role.objects.filter(name="Membre de club").all() form.instance.roles.set(member_role) form.instance._force_save = True form.instance.save() @@ -598,33 +736,42 @@ class ClubAddMemberView(ProtectQuerysetMixin, LoginRequiredMixin, CreateView): # If Société générale pays, then we assume that this is the BDE membership, and we auto-renew the # Kfet membership. if soge: + # If not already done, create BDE and Kfet memberships + bde = Club.objects.get(name="BDE") kfet = Club.objects.get(name="Kfet") - kfet_fee = kfet.membership_fee_paid if user.profile.paid else kfet.membership_fee_unpaid - # Get current membership, to get the end date - old_membership = Membership.objects.filter( - club__name="Kfet", - user=user, - date_start__lte=datetime.today(), - date_end__gte=datetime.today(), - ) + soge_clubs = [bde, kfet] + for club in soge_clubs: + fee = club.membership_fee_paid if user.profile.paid else club.membership_fee_unpaid - membership = Membership( - club=kfet, - user=user, - fee=kfet_fee, - date_start=old_membership.get().date_end + timedelta(days=1) - if old_membership.exists() else form.instance.date_start, - ) - membership._force_save = True - membership._soge = True - membership.save() - membership.refresh_from_db() - if old_membership.exists(): - membership.roles.set(old_membership.get().roles.all()) - else: - membership.roles.add(Role.objects.get(name="Adhérent Kfet")) - membership.save() + # Get current membership, to get the end date + old_membership = Membership.objects.filter( + club=club, + user=user, + ).order_by("-date_start") + + if old_membership.filter(date_start__gte=club.membership_start).exists(): + # Membership is already renewed + continue + + membership = Membership( + club=club, + user=user, + fee=fee, + date_start=max(old_membership.first().date_end + timedelta(days=1), club.membership_start) + if old_membership.exists() else form.instance.date_start, + ) + membership._force_save = True + membership._soge = True + membership.save() + membership.refresh_from_db() + if old_membership.exists(): + membership.roles.set(old_membership.get().roles.all()) + elif c.name == "BDE": + membership.roles.set(Role.objects.filter(Q(name="Adhérent BDE") | Q(name="Membre de club")).all()) + elif c.name == "Kfet": + membership.roles.set(Role.objects.filter(Q(name="Adhérent Kfet") | Q(name="Membre de club")).all()) + membership.save() return ret diff --git a/apps/note/admin.py b/apps/note/admin.py index 433ef2dc..eee49feb 100644 --- a/apps/note/admin.py +++ b/apps/note/admin.py @@ -119,10 +119,6 @@ class TransactionAdmin(PolymorphicParentModelAdmin): list_display = ('created_at', 'poly_source', 'poly_destination', 'quantity', 'amount', 'valid') list_filter = ('valid',) - readonly_fields = ( - 'source', - 'destination', - ) def poly_source(self, obj): """ @@ -145,10 +141,7 @@ class TransactionAdmin(PolymorphicParentModelAdmin): Only valid can be edited after creation Else the amount of money would not be transferred """ - if obj: # user is editing an existing object - return 'created_at', 'source', 'destination', 'quantity', \ - 'amount' - return [] + return 'created_at', 'source', 'destination', 'quantity', 'amount' if obj else () @admin.register(MembershipTransaction, site=admin_site) @@ -157,6 +150,13 @@ class MembershipTransactionAdmin(PolymorphicChildModelAdmin): Admin customisation for MembershipTransaction """ + def get_readonly_fields(self, request, obj=None): + """ + Only valid can be edited after creation + Else the amount of money would not be transferred + """ + return ('created_at', 'source', 'destination', 'quantity', 'amount') if obj else () + @admin.register(RecurrentTransaction, site=admin_site) class RecurrentTransactionAdmin(PolymorphicChildModelAdmin): @@ -164,6 +164,13 @@ class RecurrentTransactionAdmin(PolymorphicChildModelAdmin): Admin customisation for RecurrentTransaction """ + def get_readonly_fields(self, request, obj=None): + """ + Only valid can be edited after creation + Else the amount of money would not be transferred + """ + return ('created_at', 'source', 'destination', 'quantity', 'amount') if obj else () + @admin.register(SpecialTransaction, site=admin_site) class SpecialTransactionAdmin(PolymorphicChildModelAdmin): @@ -171,6 +178,13 @@ class SpecialTransactionAdmin(PolymorphicChildModelAdmin): Admin customisation for SpecialTransaction """ + def get_readonly_fields(self, request, obj=None): + """ + Only valid can be edited after creation + Else the amount of money would not be transferred + """ + return ('created_at', 'source', 'destination', 'quantity', 'amount') if obj else () + @admin.register(TransactionTemplate, site=admin_site) class TransactionTemplateAdmin(admin.ModelAdmin): diff --git a/apps/note/api/serializers.py b/apps/note/api/serializers.py index bcf0bdf5..a9c2a107 100644 --- a/apps/note/api/serializers.py +++ b/apps/note/api/serializers.py @@ -1,10 +1,16 @@ # Copyright (C) 2018-2020 by BDE ENS Paris-Saclay # SPDX-License-Identifier: GPL-3.0-or-later +from django.conf import settings +from django.utils.translation import gettext_lazy as _ from rest_framework import serializers +from rest_framework.exceptions import ValidationError from rest_polymorphic.serializers import PolymorphicSerializer +from member.api.serializers import MembershipSerializer +from member.models import Membership from note_kfet.middlewares import get_current_authenticated_user from permission.backends import PermissionBackend +from rest_framework.utils import model_meta from ..models.notes import Note, NoteClub, NoteSpecial, NoteUser, Alias from ..models.transactions import TransactionTemplate, Transaction, MembershipTransaction, TemplateCategory, \ @@ -20,7 +26,7 @@ class NoteSerializer(serializers.ModelSerializer): class Meta: model = Note fields = '__all__' - read_only_fields = [f.name for f in model._meta.get_fields()] # Notes are read-only protected + read_only_fields = ('balance', 'last_negative', 'created_at', ) # Note balances are read-only protected class NoteClubSerializer(serializers.ModelSerializer): @@ -33,7 +39,7 @@ class NoteClubSerializer(serializers.ModelSerializer): class Meta: model = NoteClub fields = '__all__' - read_only_fields = ('note', 'club', ) + read_only_fields = ('note', 'club', 'balance', 'last_negative', 'created_at', ) def get_name(self, obj): return str(obj) @@ -49,7 +55,7 @@ class NoteSpecialSerializer(serializers.ModelSerializer): class Meta: model = NoteSpecial fields = '__all__' - read_only_fields = ('note', ) + read_only_fields = ('note', 'balance', 'last_negative', 'created_at', ) def get_name(self, obj): return str(obj) @@ -65,7 +71,7 @@ class NoteUserSerializer(serializers.ModelSerializer): class Meta: model = NoteUser fields = '__all__' - read_only_fields = ('note', 'user', ) + read_only_fields = ('note', 'user', 'balance', 'last_negative', 'created_at', ) def get_name(self, obj): return str(obj) @@ -108,6 +114,8 @@ class ConsumerSerializer(serializers.ModelSerializer): email_confirmed = serializers.SerializerMethodField() + membership = serializers.SerializerMethodField() + class Meta: model = Alias fields = '__all__' @@ -117,15 +125,26 @@ class ConsumerSerializer(serializers.ModelSerializer): Display information about the associated note """ # If the user has no right to see the note, then we only display the note identifier - if PermissionBackend.check_perm(get_current_authenticated_user(), "note.view_note", obj.note): - return NotePolymorphicSerializer().to_representation(obj.note) - return dict(id=obj.note.id, name=str(obj.note)) + return NotePolymorphicSerializer().to_representation(obj.note)\ + if PermissionBackend.check_perm(get_current_authenticated_user(), "note.view_note", obj.note)\ + else dict(id=obj.note.id, name=str(obj.note)) def get_email_confirmed(self, obj): if isinstance(obj.note, NoteUser): return obj.note.user.profile.email_confirmed return True + def get_membership(self, obj): + if isinstance(obj.note, NoteUser): + memberships = Membership.objects.filter( + PermissionBackend.filter_queryset(get_current_authenticated_user(), Membership, "view")).filter( + user=obj.note.user, + club=2, # Kfet + ).order_by("-date_start") + if memberships.exists(): + return MembershipSerializer().to_representation(memberships.first()) + return None + class TemplateCategorySerializer(serializers.ModelSerializer): """ @@ -154,13 +173,24 @@ class TransactionSerializer(serializers.ModelSerializer): REST API Serializer for Transactions. The djangorestframework plugin will analyse the model `Transaction` and parse all fields in the API. """ + def validate_source(self, value): + if not value.is_active: + raise ValidationError(_("The transaction can't be saved since the source note " + "or the destination note is not active.")) + return value + + def validate_destination(self, value): + if not value.is_active: + raise ValidationError(_("The transaction can't be saved since the source note " + "or the destination note is not active.")) + return value class Meta: model = Transaction fields = '__all__' -class RecurrentTransactionSerializer(serializers.ModelSerializer): +class RecurrentTransactionSerializer(TransactionSerializer): """ REST API Serializer for Transactions. The djangorestframework plugin will analyse the model `RecurrentTransaction` and parse all fields in the API. @@ -171,7 +201,7 @@ class RecurrentTransactionSerializer(serializers.ModelSerializer): fields = '__all__' -class MembershipTransactionSerializer(serializers.ModelSerializer): +class MembershipTransactionSerializer(TransactionSerializer): """ REST API Serializer for Membership transactions. The djangorestframework plugin will analyse the model `MembershipTransaction` and parse all fields in the API. @@ -182,7 +212,7 @@ class MembershipTransactionSerializer(serializers.ModelSerializer): fields = '__all__' -class SpecialTransactionSerializer(serializers.ModelSerializer): +class SpecialTransactionSerializer(TransactionSerializer): """ REST API Serializer for Special transactions. The djangorestframework plugin will analyse the model `SpecialTransaction` and parse all fields in the API. @@ -202,12 +232,28 @@ class TransactionPolymorphicSerializer(PolymorphicSerializer): SpecialTransaction: SpecialTransactionSerializer, } - try: + if "activity" in settings.INSTALLED_APPS: from activity.models import GuestTransaction from activity.api.serializers import GuestTransactionSerializer model_serializer_mapping[GuestTransaction] = GuestTransactionSerializer - except ImportError: # Activity app is not loaded - pass + + def validate(self, attrs): + resource_type = attrs.pop(self.resource_type_field_name) + serializer = self._get_serializer_from_resource_type(resource_type) + if self.instance: + instance = self.instance + info = model_meta.get_field_info(instance) + for attr, value in attrs.items(): + if attr in info.relations and info.relations[attr].to_many: + field = getattr(instance, attr) + field.set(value) + else: + setattr(instance, attr, value) + instance.validate() + else: + serializer.Meta.model(**attrs).validate() + attrs[self.resource_type_field_name] = resource_type + return super().validate(attrs) class Meta: model = Transaction diff --git a/apps/note/api/views.py b/apps/note/api/views.py index a365c343..9b213025 100644 --- a/apps/note/api/views.py +++ b/apps/note/api/views.py @@ -9,6 +9,8 @@ from rest_framework import viewsets from rest_framework.response import Response from rest_framework import status from api.viewsets import ReadProtectedModelViewSet, ReadOnlyProtectedModelViewSet +from note_kfet.middlewares import get_current_session +from permission.backends import PermissionBackend from .serializers import NotePolymorphicSerializer, AliasSerializer, ConsumerSerializer,\ TemplateCategorySerializer, TransactionTemplateSerializer, TransactionPolymorphicSerializer @@ -16,7 +18,7 @@ from ..models.notes import Note, Alias from ..models.transactions import TransactionTemplate, Transaction, TemplateCategory -class NotePolymorphicViewSet(ReadOnlyProtectedModelViewSet): +class NotePolymorphicViewSet(ReadProtectedModelViewSet): """ REST API View set. The djangorestframework plugin will get all `Note` objects (with polymorhism), serialize it to JSON with the given serializer, @@ -34,15 +36,16 @@ class NotePolymorphicViewSet(ReadOnlyProtectedModelViewSet): Parse query and apply filters. :return: The filtered set of requested notes """ - queryset = super().get_queryset() + queryset = super().get_queryset().distinct() alias = self.request.query_params.get("alias", ".*") queryset = queryset.filter( - Q(alias__name__regex="^" + alias) - | Q(alias__normalized_name__regex="^" + Alias.normalize(alias)) - | Q(alias__normalized_name__regex="^" + alias.lower())) + Q(alias__name__iregex="^" + alias) + | Q(alias__normalized_name__iregex="^" + Alias.normalize(alias)) + | Q(alias__normalized_name__iregex="^" + alias.lower()) + ) - return queryset.distinct() + return queryset.order_by("id") class AliasViewSet(ReadProtectedModelViewSet): @@ -69,7 +72,6 @@ class AliasViewSet(ReadProtectedModelViewSet): try: self.perform_destroy(instance) except ValidationError as e: - print(e) return Response({e.code: e.message}, status.HTTP_400_BAD_REQUEST) return Response(status=status.HTTP_204_NO_CONTENT) @@ -79,15 +81,26 @@ class AliasViewSet(ReadProtectedModelViewSet): :return: The filtered set of requested aliases """ - queryset = super().get_queryset() + queryset = super().get_queryset().distinct() - alias = self.request.query_params.get("alias", ".*") - queryset = queryset.filter( - Q(name__regex="^" + alias) - | Q(normalized_name__regex="^" + Alias.normalize(alias)) - | Q(normalized_name__regex="^" + alias.lower())) + alias = self.request.query_params.get("alias", None) + if alias: + queryset = queryset.filter( + name__iregex="^" + alias + ).union( + queryset.filter( + Q(normalized_name__iregex="^" + Alias.normalize(alias)) + & ~Q(name__iregex="^" + alias) + ), + all=True).union( + queryset.filter( + Q(normalized_name__iregex="^" + alias.lower()) + & ~Q(normalized_name__iregex="^" + Alias.normalize(alias)) + & ~Q(name__iregex="^" + alias) + ), + all=True) - return queryset + return queryset.order_by("name") class ConsumerViewSet(ReadOnlyProtectedModelViewSet): @@ -106,13 +119,25 @@ class ConsumerViewSet(ReadOnlyProtectedModelViewSet): queryset = super().get_queryset() alias = self.request.query_params.get("alias", ".*") + queryset = queryset.prefetch_related('note') + # We match first an alias if it is matched without normalization, + # then if the normalized pattern matches a normalized alias. queryset = queryset.filter( - Q(name__regex="^" + alias) - | Q(normalized_name__regex="^" + Alias.normalize(alias)) - | Q(normalized_name__regex="^" + alias.lower()))\ - .order_by('name').prefetch_related('note') + name__iregex="^" + alias + ).union( + queryset.filter( + Q(normalized_name__iregex="^" + Alias.normalize(alias)) + & ~Q(name__iregex="^" + alias) + ), + all=True).union( + queryset.filter( + Q(normalized_name__iregex="^" + alias.lower()) + & ~Q(normalized_name__iregex="^" + Alias.normalize(alias)) + & ~Q(name__iregex="^" + alias) + ), + all=True) - return queryset + return queryset.order_by('name').distinct() class TemplateCategoryViewSet(ReadProtectedModelViewSet): @@ -121,7 +146,7 @@ class TemplateCategoryViewSet(ReadProtectedModelViewSet): The djangorestframework plugin will get all `TemplateCategory` objects, serialize it to JSON with the given serializer, then render it on /api/note/transaction/category/ """ - queryset = TemplateCategory.objects.all() + queryset = TemplateCategory.objects.order_by("name").all() serializer_class = TemplateCategorySerializer filter_backends = [SearchFilter] search_fields = ['$name', ] @@ -133,7 +158,7 @@ class TransactionTemplateViewSet(viewsets.ModelViewSet): The djangorestframework plugin will get all `TransactionTemplate` objects, serialize it to JSON with the given serializer, then render it on /api/note/transaction/template/ """ - queryset = TransactionTemplate.objects.all() + queryset = TransactionTemplate.objects.order_by("name").all() serializer_class = TransactionTemplateSerializer filter_backends = [SearchFilter, DjangoFilterBackend] filterset_fields = ['name', 'amount', 'display', 'category', ] @@ -146,7 +171,13 @@ class TransactionViewSet(ReadProtectedModelViewSet): The djangorestframework plugin will get all `Transaction` objects, serialize it to JSON with the given serializer, then render it on /api/note/transaction/transaction/ """ - queryset = Transaction.objects.all() + queryset = Transaction.objects.order_by("-created_at").all() serializer_class = TransactionPolymorphicSerializer filter_backends = [SearchFilter] search_fields = ['$reason', ] + + def get_queryset(self): + user = self.request.user + get_current_session().setdefault("permission_mask", 42) + return self.model.objects.filter(PermissionBackend.filter_queryset(user, self.model, "view"))\ + .order_by("created_at", "id") diff --git a/apps/note/forms.py b/apps/note/forms.py index bc479e20..e4e976e1 100644 --- a/apps/note/forms.py +++ b/apps/note/forms.py @@ -1,22 +1,15 @@ # Copyright (C) 2018-2020 by BDE ENS Paris-Saclay # SPDX-License-Identifier: GPL-3.0-or-later +from datetime import datetime from django import forms from django.contrib.contenttypes.models import ContentType +from django.forms import CheckboxSelectMultiple +from django.utils.timezone import make_aware from django.utils.translation import gettext_lazy as _ -from note_kfet.inputs import Autocomplete, AmountInput +from note_kfet.inputs import Autocomplete, AmountInput, DateTimePickerInput -from .models import TransactionTemplate, NoteClub - - -class ImageForm(forms.Form): - image = forms.ImageField(required=False, - label=_('select an image'), - help_text=_('Maximal size: 2MB')) - x = forms.FloatField(widget=forms.HiddenInput()) - y = forms.FloatField(widget=forms.HiddenInput()) - width = forms.FloatField(widget=forms.HiddenInput()) - height = forms.FloatField(widget=forms.HiddenInput()) +from .models import TransactionTemplate, NoteClub, Alias class TransactionTemplateForm(forms.ModelForm): @@ -38,3 +31,80 @@ class TransactionTemplateForm(forms.ModelForm): ), 'amount': AmountInput(), } + + +class SearchTransactionForm(forms.Form): + source = forms.ModelChoiceField( + queryset=Alias.objects.all(), + label=_("Source"), + required=False, + widget=Autocomplete( + Alias, + resetable=True, + attrs={ + 'api_url': '/api/note/alias/', + 'placeholder': 'Note ...', + }, + ), + ) + + destination = forms.ModelChoiceField( + queryset=Alias.objects.all(), + label=_("Destination"), + required=False, + widget=Autocomplete( + Alias, + resetable=True, + attrs={ + 'api_url': '/api/note/alias/', + 'placeholder': 'Note ...', + }, + ), + ) + + type = forms.ModelMultipleChoiceField( + queryset=ContentType.objects.filter(app_label="note", model__endswith="transaction"), + initial=ContentType.objects.filter(app_label="note", model__endswith="transaction"), + label=_("Type"), + required=False, + widget=CheckboxSelectMultiple(), + ) + + reason = forms.CharField( + label=_("Reason"), + required=False, + ) + + valid = forms.BooleanField( + label=_("Valid"), + initial=False, + required=False, + ) + + amount_gte = forms.Field( + label=_("Total amount greater than"), + initial=0, + required=False, + widget=AmountInput(), + ) + + amount_lte = forms.Field( + initial=2 ** 31 - 1, + label=_("Total amount less than"), + required=False, + widget=AmountInput(), + ) + + created_after = forms.DateTimeField( + label=_("Created after"), + initial=make_aware(datetime(year=2000, month=1, day=1, hour=0, minute=0)), + required=False, + widget=DateTimePickerInput(), + ) + + created_before = forms.DateTimeField( + label=_("Created before"), + initial=make_aware(datetime(year=2042, month=12, day=31, hour=21, minute=42)), + required=False, + widget=DateTimePickerInput(), + ) diff --git a/apps/note/models/notes.py b/apps/note/models/notes.py index 99818602..e5d9c13c 100644 --- a/apps/note/models/notes.py +++ b/apps/note/models/notes.py @@ -4,9 +4,12 @@ import unicodedata from django.conf import settings +from django.conf.global_settings import DEFAULT_FROM_EMAIL from django.core.exceptions import ValidationError +from django.core.mail import send_mail from django.core.validators import RegexValidator from django.db import models +from django.template.loader import render_to_string from django.utils import timezone from django.utils.translation import gettext_lazy as _ from polymorphic.models import PolymorphicModel @@ -24,24 +27,20 @@ class Note(PolymorphicModel): A Note can be searched find throught an :model:`note.Alias` """ - balance = models.IntegerField( + + balance = models.BigIntegerField( verbose_name=_('account balance'), help_text=_('in centimes, money credited for this instance'), default=0, ) + last_negative = models.DateTimeField( verbose_name=_('last negative date'), help_text=_('last time the balance was negative'), null=True, blank=True, ) - is_active = models.BooleanField( - _('active'), - default=True, - help_text=_( - 'Designates whether this note should be treated as active. ' - 'Unselect this instead of deleting notes.'), - ) + display_image = models.ImageField( verbose_name=_('display image'), max_length=255, @@ -50,11 +49,31 @@ class Note(PolymorphicModel): upload_to='pic/', default='pic/default.png' ) + created_at = models.DateTimeField( verbose_name=_('created at'), default=timezone.now, ) + is_active = models.BooleanField( + _('active'), + default=True, + help_text=_( + 'Designates whether this note should be treated as active. ' + 'Unselect this instead of deleting notes.'), + ) + + inactivity_reason = models.CharField( + max_length=255, + choices=[ + ('manual', _("The user blocked his/her note manually, eg. when he/she left the school for holidays. " + "It can be reactivated at any time.")), + ('forced', _("The note is blocked by the the BDE and can't be manually reactivated.")), + ], + null=True, + default=None, + ) + class Meta: verbose_name = _("note") verbose_name_plural = _("notes") @@ -67,26 +86,27 @@ class Note(PolymorphicModel): pretty.short_description = _('Note') + @property + def last_negative_duration(self): + if self.balance >= 0 or self.last_negative is None: + return None + delta = timezone.now() - self.last_negative + return "{:d} jours".format(delta.days) + def save(self, *args, **kwargs): """ Save note with it's alias (called in polymorphic children) """ - aliases = Alias.objects.filter(name=str(self)) - if aliases.exists(): - # Alias exists, so check if it is linked to this note - if aliases.first().note != self: - raise ValidationError(_('This alias is already taken.'), - code="same_alias") + # Check that we can save the alias + self.clean() - # Save note - super().save(*args, **kwargs) - else: - # Alias does not exist yet, so check if it can exist + super().save(*args, **kwargs) + + if not Alias.objects.filter(name=str(self)).exists(): a = Alias(name=str(self)) a.clean() - # Save note and alias - super().save(*args, **kwargs) + # Save alias a.note = self a.save(force_insert=True) @@ -128,6 +148,25 @@ class NoteUser(Note): def pretty(self): return _("%(user)s's note") % {'user': str(self.user)} + def save(self, *args, **kwargs): + if self.pk and self.balance < 0: + old_note = NoteUser.objects.get(pk=self.pk) + super().save(*args, **kwargs) + if old_note.balance >= 0: + # Passage en négatif + self.last_negative = timezone.now() + self._force_save = True + self.save(*args, **kwargs) + self.send_mail_negative_balance() + else: + super().save(*args, **kwargs) + + def send_mail_negative_balance(self): + plain_text = render_to_string("note/mails/negative_balance.txt", dict(note=self)) + html = render_to_string("note/mails/negative_balance.html", dict(note=self)) + self.user.email_user("[Note Kfet] Passage en négatif (compte n°{:d})" + .format(self.user.pk), plain_text, html_message=html) + class NoteClub(Note): """ @@ -150,6 +189,25 @@ class NoteClub(Note): def pretty(self): return _("Note of %(club)s club") % {'club': str(self.club)} + def save(self, *args, **kwargs): + if self.pk and self.balance < 0: + old_note = NoteClub.objects.get(pk=self.pk) + super().save(*args, **kwargs) + if old_note.balance >= 0: + # Passage en négatif + self.last_negative = timezone.now() + self._force_save = True + self.save(*args, **kwargs) + self.send_mail_negative_balance() + else: + super().save(*args, **kwargs) + + def send_mail_negative_balance(self): + plain_text = render_to_string("note/mails/negative_balance.txt", dict(note=self)) + html = render_to_string("note/mails/negative_balance.html", dict(note=self)) + send_mail("[Note Kfet] Passage en négatif (club {})".format(self.club.name), plain_text, DEFAULT_FROM_EMAIL, + [self.club.email], html_message=html) + class NoteSpecial(Note): """ @@ -199,7 +257,7 @@ class Alias(models.Model): normalized_name = models.CharField( max_length=255, unique=True, - default='', + blank=False, editable=False, ) note = models.ForeignKey( @@ -221,18 +279,21 @@ class Alias(models.Model): @staticmethod def normalize(string): """ - Normalizes a string: removes most diacritics and does casefolding + Normalizes a string: removes most diacritics, does casefolding and ignore non-ASCII characters """ return ''.join( - char for char in unicodedata.normalize('NFKD', string.casefold()) + char for char in unicodedata.normalize('NFKD', string.casefold().replace('æ', 'ae').replace('œ', 'oe')) if all(not unicodedata.category(char).startswith(cat) - for cat in {'M', 'P', 'Z', 'C'})).casefold() + for cat in {'M', 'Pc', 'Pe', 'Pf', 'Pi', 'Po', 'Ps', 'Z', 'C'}))\ + .casefold().encode('ascii', 'ignore').decode('ascii') def clean(self): normalized_name = self.normalize(self.name) if len(normalized_name) >= 255: raise ValidationError(_('Alias is too long.'), code='alias_too_long') + if not normalized_name: + raise ValidationError(_('This alias contains only complex character. Please use a more simple alias.')) try: sim_alias = Alias.objects.get(normalized_name=normalized_name) if self != sim_alias: @@ -244,7 +305,7 @@ class Alias(models.Model): self.normalized_name = normalized_name def save(self, *args, **kwargs): - self.normalized_name = self.normalize(self.name) + self.clean() super().save(*args, **kwargs) def delete(self, using=None, keep_parents=False): diff --git a/apps/note/models/transactions.py b/apps/note/models/transactions.py index f504d8e1..d88be5a6 100644 --- a/apps/note/models/transactions.py +++ b/apps/note/models/transactions.py @@ -1,7 +1,8 @@ # Copyright (C) 2018-2020 by BDE ENS Paris-Saclay # SPDX-License-Identifier: GPL-3.0-or-later + from django.core.exceptions import ValidationError -from django.db import models +from django.db import models, transaction from django.urls import reverse from django.utils import timezone from django.utils.translation import gettext_lazy as _ @@ -57,7 +58,6 @@ class TransactionTemplate(models.Model): amount = models.PositiveIntegerField( verbose_name=_('amount'), - help_text=_('in centimes'), ) category = models.ForeignKey( TemplateCategory, @@ -164,16 +164,65 @@ class Transaction(PolymorphicModel): models.Index(fields=['destination']), ] + def validate(self): + previous_source_balance = self.source.balance + previous_dest_balance = self.destination.balance + + source_balance = self.source.balance + dest_balance = self.destination.balance + + created = self.pk is None + to_transfer = self.amount * self.quantity + if not created: + # Revert old transaction + old_transaction = Transaction.objects.get(pk=self.pk) + # Check that nothing important changed + for field_name in ["source_id", "destination_id", "quantity", "amount"]: + if getattr(self, field_name) != getattr(old_transaction, field_name): + raise ValidationError(_("You can't update the {field} on a Transaction. " + "Please invalidate it and create one other.").format(field=field_name)) + + if old_transaction.valid == self.valid: + # Don't change anything + return 0, 0 + if old_transaction.valid: + source_balance += to_transfer + dest_balance -= to_transfer + + if self.valid: + source_balance -= to_transfer + dest_balance += to_transfer + + # When a transaction is declared valid, we ensure that the invalidity reason is null, if it was + # previously invalid + self.invalidity_reason = None + + if source_balance > 9223372036854775807 or source_balance < -9223372036854775808\ + or dest_balance > 9223372036854775807 or dest_balance < -9223372036854775808: + raise ValidationError(_("The note balances must be between - 92 233 720 368 547 758.08 € " + "and 92 233 720 368 547 758.07 €.")) + + return source_balance - previous_source_balance, dest_balance - previous_dest_balance + + @transaction.atomic def save(self, *args, **kwargs): """ When saving, also transfer money between two notes """ + if self.source.pk == self.destination.pk: + # When source == destination, no money is transferred and no transaction is created + return + + # We refresh the notes with the "select for update" tag to avoid concurrency issues + self.source = Note.objects.filter(pk=self.source_id).select_for_update().get() + self.destination = Note.objects.filter(pk=self.destination_id).select_for_update().get() + + # Check that the amounts stay between big integer bounds + diff_source, diff_dest = self.validate() if not self.source.is_active or not self.destination.is_active: - if 'force_insert' not in kwargs or not kwargs['force_insert']: - if 'force_update' not in kwargs or not kwargs['force_update']: - raise ValidationError(_("The transaction can't be saved since the source note " - "or the destination note is not active.")) + raise ValidationError(_("The transaction can't be saved since the source note " + "or the destination note is not active.")) # If the aliases are not entered, we assume that the used alias is the name of the note if not self.source_alias: @@ -182,34 +231,14 @@ class Transaction(PolymorphicModel): if not self.destination_alias: self.destination_alias = str(self.destination) - if self.source.pk == self.destination.pk: - # When source == destination, no money is transferred - super().save(*args, **kwargs) - return - - created = self.pk is None - to_transfer = self.amount * self.quantity - if not created: - # Revert old transaction - old_transaction = Transaction.objects.get(pk=self.pk) - if old_transaction.valid: - self.source.balance += to_transfer - self.destination.balance -= to_transfer - - if self.valid: - self.source.balance -= to_transfer - self.destination.balance += to_transfer - - # When a transaction is declared valid, we ensure that the invalidity reason is null, if it was - # previously invalid - self.invalidity_reason = None - # We save first the transaction, in case of the user has no right to transfer money super().save(*args, **kwargs) # Save notes + self.source.balance += diff_source self.source._force_save = True self.source.save() + self.destination.balance += diff_dest self.destination._force_save = True self.destination.save() @@ -243,15 +272,25 @@ class RecurrentTransaction(Transaction): TransactionTemplate, on_delete=models.PROTECT, ) - category = models.ForeignKey( - TemplateCategory, - on_delete=models.PROTECT, - ) + + def clean(self): + if self.template.destination != self.destination: + raise ValidationError( + _("The destination of this transaction must equal to the destination of the template.")) + return super().clean() + + def save(self, *args, **kwargs): + self.clean() + return super().save(*args, **kwargs) @property def type(self): return _('Template') + class Meta: + verbose_name = _("recurrent transaction") + verbose_name_plural = _("recurrent transactions") + class SpecialTransaction(Transaction): """ @@ -290,6 +329,14 @@ class SpecialTransaction(Transaction): raise(ValidationError(_("A special transaction is only possible between a" " Note associated to a payment method and a User or a Club"))) + def save(self, *args, **kwargs): + self.clean() + super().save(*args, **kwargs) + + class Meta: + verbose_name = _("Special transaction") + verbose_name_plural = _("Special transactions") + class MembershipTransaction(Transaction): """ diff --git a/apps/note/signals.py b/apps/note/signals.py index 0baa39e6..06bb480b 100644 --- a/apps/note/signals.py +++ b/apps/note/signals.py @@ -6,11 +6,7 @@ def save_user_note(instance, raw, **_kwargs): """ Hook to create and save a note when an user is updated """ - if raw: - # When provisionning data, do not try to autocreate - return - - if instance.is_superuser or instance.profile.registration_valid: + if not raw and (instance.is_superuser or instance.profile.registration_valid): # Create note only when the registration is validated from note.models import NoteUser NoteUser.objects.get_or_create(user=instance) diff --git a/apps/note/tables.py b/apps/note/tables.py index 0048a0a5..b1d434ae 100644 --- a/apps/note/tables.py +++ b/apps/note/tables.py @@ -4,7 +4,6 @@ import html import django_tables2 as tables -from django.db.models import F from django.utils.html import format_html from django_tables2.utils import A from django.utils.translation import gettext_lazy as _ @@ -19,8 +18,7 @@ from .templatetags.pretty_money import pretty_money class HistoryTable(tables.Table): class Meta: attrs = { - 'class': - 'table table-condensed table-striped table-hover' + 'class': 'table table-condensed table-striped' } model = Transaction exclude = ("id", "polymorphic_ctype", "invalidity_reason", "source_alias", "destination_alias",) @@ -56,34 +54,27 @@ class HistoryTable(tables.Table): "id": lambda record: "validate_" + str(record.id), "class": lambda record: str(record.valid).lower() - + (' validate' if PermissionBackend.check_perm(get_current_authenticated_user(), - "note.change_transaction_invalidity_reason", - record) else ''), + + (' validate' if record.source.is_active and record.destination.is_active and PermissionBackend + .check_perm(get_current_authenticated_user(), "note.change_transaction_invalidity_reason", record) + else ''), "data-toggle": "tooltip", "title": lambda record: (_("Click to invalidate") if record.valid else _("Click to validate")) if PermissionBackend.check_perm(get_current_authenticated_user(), - "note.change_transaction_invalidity_reason", record) else None, - "onclick": lambda record: 'de_validate(' + str(record.id) + ', ' + str(record.valid).lower() + ')' + "note.change_transaction_invalidity_reason", record) + and record.source.is_active and record.destination.is_active else None, + "onclick": lambda record: 'de_validate(' + str(record.id) + ', ' + str(record.valid).lower() + + ', "' + str(record.__class__.__name__) + '")' if PermissionBackend.check_perm(get_current_authenticated_user(), - "note.change_transaction_invalidity_reason", record) else None, + "note.change_transaction_invalidity_reason", record) + and record.source.is_active and record.destination.is_active else None, "onmouseover": lambda record: '$("#invalidity_reason_' + str(record.id) + '").show();$("#invalidity_reason_' - + str(record.id) + '").focus();' - if PermissionBackend.check_perm(get_current_authenticated_user(), - "note.change_transaction_invalidity_reason", record) else None, - "onmouseout": lambda record: '$("#invalidity_reason_' + str(record.id) + '").hide()' - if PermissionBackend.check_perm(get_current_authenticated_user(), - "note.change_transaction_invalidity_reason", record) else None, + + str(record.id) + '").focus();', + "onmouseout": lambda record: '$("#invalidity_reason_' + str(record.id) + '").hide()', } } ) - def order_total(self, queryset, is_descending): - # needed for rendering - queryset = queryset.annotate(total=F('amount') * F('quantity')) \ - .order_by(('-' if is_descending else '') + 'total') - return queryset, True - def render_amount(self, value): return pretty_money(value) @@ -101,15 +92,18 @@ class HistoryTable(tables.Table): """ When the validation status is hovered, an input field is displayed to let the user specify an invalidity reason """ + has_perm = PermissionBackend \ + .check_perm(get_current_authenticated_user(), "note.change_transaction_invalidity_reason", record) + val = "✔" if value else "✖" - if not PermissionBackend\ - .check_perm(get_current_authenticated_user(), "note.change_transaction_invalidity_reason", record): + + if value and not has_perm: return val val += "" return format_html(val) @@ -124,7 +118,7 @@ DELETE_TEMPLATE = """ class AliasTable(tables.Table): class Meta: attrs = { - 'class': 'table table condensed table-striped table-hover', + 'class': 'table table condensed table-striped', 'id': "alias_table" } model = Alias @@ -136,15 +130,16 @@ class AliasTable(tables.Table): delete_col = tables.TemplateColumn(template_code=DELETE_TEMPLATE, extra_context={"delete_trans": _('delete')}, - attrs={'td': {'class': 'col-sm-1'}}, - verbose_name=_("Delete"),) + attrs={'td': {'class': lambda record: 'col-sm-1' + ( + ' d-none' if not PermissionBackend.check_perm( + get_current_authenticated_user(), "note.delete_alias", + record) else '')}}, verbose_name=_("Delete"), ) class ButtonTable(tables.Table): class Meta: attrs = { - 'class': - 'table table-bordered condensed table-hover' + 'class': 'table table-bordered condensed' } row_attrs = { 'class': lambda record: 'table-row ' + ('table-success' if record.display else 'table-danger'), @@ -154,18 +149,25 @@ class ButtonTable(tables.Table): model = TransactionTemplate exclude = ('id',) - edit = tables.LinkColumn('note:template_update', - args=[A('pk')], - attrs={'td': {'class': 'col-sm-1'}, - 'a': {'class': 'btn btn-sm btn-primary'}}, - text=_('edit'), - accessor='pk', - verbose_name=_("Edit"),) + edit = tables.LinkColumn( + 'note:template_update', + args=[A('pk')], + attrs={ + 'td': {'class': 'col-sm-1'}, + 'a': { + 'class': 'btn btn-sm btn-primary', + 'data-turbolinks': 'false', + } + }, + text=_('edit'), + accessor='pk', + verbose_name=_("Edit"), + ) delete_col = tables.TemplateColumn(template_code=DELETE_TEMPLATE, extra_context={"delete_trans": _('delete')}, attrs={'td': {'class': 'col-sm-1'}}, - verbose_name=_("Delete"),) + verbose_name=_("Delete"), ) def render_amount(self, value): return pretty_money(value) diff --git a/templates/note/amount_input.html b/apps/note/templates/note/amount_input.html similarity index 58% rename from templates/note/amount_input.html rename to apps/note/templates/note/amount_input.html index cd8cd201..d4873115 100644 --- a/templates/note/amount_input.html +++ b/apps/note/templates/note/amount_input.html @@ -1,12 +1,18 @@ +{% comment %} +SPDX-License-Identifier: GPL-3.0-or-later +{% endcomment %} + +{# Select amount to transfert in € #}
-

-
\ No newline at end of file + diff --git a/templates/note/conso_form.html b/apps/note/templates/note/conso_form.html similarity index 88% rename from templates/note/conso_form.html rename to apps/note/templates/note/conso_form.html index e6335c6e..07c63488 100644 --- a/templates/note/conso_form.html +++ b/apps/note/templates/note/conso_form.html @@ -1,9 +1,11 @@ {% extends "base.html" %} - +{% comment %} +SPDX-License-Identifier: GPL-3.0-or-later +{% endcomment %} {% load i18n static pretty_money django_tables2 %} -{# Remove page title #} -{% block contenttitle %}{% endblock %} +{# Use a fluid-width container #} +{% block containertype %}container-fluid{% endblock %} {% block content %}
@@ -11,10 +13,12 @@
{# User details column #}
-
- -
+
+ + + +
@@ -22,7 +26,7 @@ {# User selection column #}
-
+

{% trans "Consum" %} @@ -40,9 +44,9 @@

- + {# Summary of consumption and consume button #}
-
+

{% trans "Select consumptions" %} @@ -53,19 +57,17 @@

-
- {# Buttons column #} -
+ {# Show last used buttons #} -
+

{% trans "Highlighted buttons" %} @@ -84,11 +86,13 @@

+
+ {# Buttons column #} +
{# Regroup buttons under categories #} - {# {% regroup transaction_templates by category as categories %} #} -
+
{# Tabs for button categories #}
- + {# history of transaction #}

diff --git a/apps/note/templates/note/mails/negative_balance.html b/apps/note/templates/note/mails/negative_balance.html new file mode 100644 index 00000000..8c869a54 --- /dev/null +++ b/apps/note/templates/note/mails/negative_balance.html @@ -0,0 +1,46 @@ +{% load pretty_money %} +{% load getenv %} +{% load i18n %} + + + + + + Passage en négatif (compte n°{{ note.pk }}) + + +

+ Bonjour {% if note.user %}{{ note.user }}{% else %}{{ note.club.name }}{% endif %}, +

+ +

+ Ce mail t'a été envoyé parce que le solde de ta Note Kfet {{ note }} est négatif ! +

+ +

+ Ton solde actuel est de {{ note.balance|pretty_money }}. +

+ +

+ Par ailleurs, le BDE ne sert pas d'alcool aux adhérents dont le solde + est inférieur à 0 € depuis plus de 24h. +

+ +

+ Si tu ne comprends pas ton solde, tu peux consulter ton historique + sur ton compte. +

+ +

+ Tu peux venir recharger ta note rapidement à la Kfet, ou envoyer un mail à + la trésorerie du BdE (tresorerie.bde@lists.crans.org) + pour payer par virement bancaire. +

+ +-- +

+ Le BDE
+ {% trans "Mail generated by the Note Kfet on the" %} {% now "j F Y à H:i:s" %} +

+ + \ No newline at end of file diff --git a/apps/note/templates/note/mails/negative_balance.txt b/apps/note/templates/note/mails/negative_balance.txt new file mode 100644 index 00000000..0052995a --- /dev/null +++ b/apps/note/templates/note/mails/negative_balance.txt @@ -0,0 +1,25 @@ +{% load pretty_money %} +{% load getenv %} +{% load i18n %} + +Bonjour {% if note.user %}{{ note.user }}{% else %}{{ note.club.name }}{% endif %}, + +Ce mail t'a été envoyé parce que le solde de ta Note Kfet +{{ note }} est négatif ! + +Ton solde actuel est de {{ note.balance|pretty_money }}. + +Par ailleurs, le BDE ne sert pas d'alcool aux adhérents dont le solde +est inférieur à 0 € depuis plus de 24h. + +Si tu ne comprends pas ton solde, tu peux consulter ton historique +sur ton compte {{ "NOTE_URL"|getenv }}{% if note.user %}{% url "member:user_detail" pk=note.user.pk %}{% else %}{% url "member:club_detail" pk=note.club.pk %}{% endif %} + +Tu peux venir recharger ta note rapidement à la Kfet, ou envoyer un mail à +la trésorerie du BdE (tresorerie.bde@lists.crans.org) pour payer par +virement bancaire. + +-- +Le BDE + +{% trans "Mail generated by the Note Kfet on the" %} {% now "j F Y à H:i:s" %} \ No newline at end of file diff --git a/apps/note/templates/note/mails/negative_notes_report.html b/apps/note/templates/note/mails/negative_notes_report.html new file mode 100644 index 00000000..49254f5d --- /dev/null +++ b/apps/note/templates/note/mails/negative_notes_report.html @@ -0,0 +1,49 @@ +{% load pretty_money %} +{% load i18n %} + + + + + + [Note Kfet] Liste des négatifs + + + + + + + + + + + + + + + {% for note in notes %} + + {% if note.user %} + + + + + {% else %} + + + + + {% endif %} + + + + {% endfor %} + +
NomPrénomPseudoEmailSoldeDurée
{{ note.user.last_name }}{{ note.user.first_name }}{{ note.user.username }}{{ note.user.email }}{{ note.club.name }}{{ note.club.email }}{{ note.balance|pretty_money }}{{ note.last_negative_duration }}
+ +-- +

+ Le BDE
+ {% trans "Mail generated by the Note Kfet on the" %} {% now "j F Y à H:i:s" %} +

+ + \ No newline at end of file diff --git a/apps/note/templates/note/mails/negative_notes_report.txt b/apps/note/templates/note/mails/negative_notes_report.txt new file mode 100644 index 00000000..3209fbb8 --- /dev/null +++ b/apps/note/templates/note/mails/negative_notes_report.txt @@ -0,0 +1,13 @@ +{% load pretty_money %} +{% load i18n %} + + Nom | Prénom | Pseudo | Email | Solde | Durée +---------------------+------------+-----------------+-----------------------------------+----------+----------- +{% for note in notes %} +{% if note.user %}{{ note.user.last_name }} | {{ note.user.first_name }} | {{ note.user.username }} | {{ note.user.email }} | {{ note.balance|pretty_money }} | {{ note.last_negative_duration }}{% else %} | | {{ note.club.name }} | {{ note.club.email }} | {{ note.balance|pretty_money }} | {{ note.last_negative_duration }}{% endif %} +{% endfor %} + +-- +Le BDE + +{% trans "Mail generated by the Note Kfet on the" %} {% now "j F Y à H:i:s" %} \ No newline at end of file diff --git a/apps/note/templates/note/mails/weekly_report.html b/apps/note/templates/note/mails/weekly_report.html new file mode 100644 index 00000000..871e09c2 --- /dev/null +++ b/apps/note/templates/note/mails/weekly_report.html @@ -0,0 +1,57 @@ +{% load pretty_money %} +{% load render_table from django_tables2 %} +{% load i18n %} + + + + + + [Note Kfet] Rapport de la Note Kfet + + + + + +

+ Bonjour, +

+ +

+ Vous recevez ce mail car vous avez défini une « Fréquence des rapports » dans la Note.
+ Le premier rapport récapitule toutes vos consommations depuis la création de votre compte.
+ Ensuite, un rapport vous est envoyé à la fréquence demandée seulement si vous avez consommé + depuis le dernier rapport.
+ Pour arrêter de recevoir des rapports, il vous suffit de modifier votre profil Note et de + mettre la fréquence des rapports à 0 ou -1.
+ Pour toutes suggestions par rapport à ce service, contactez + notekfet2020@lists.crans.org. +

+ +

+ Rapport d'activité de {{ user.first_name }} {{ user.last_name }} (note : {{ user }}) + depuis le {{ last_report }} jusqu'au {{ now }}. +

+ +

+ Dépenses totales : {{ outcoming|pretty_money }}
+ Apports totaux : {{ incoming|pretty_money }}
+ Différentiel : {{ diff|pretty_money }}
+ Nouveau solde : {{ user.note.balance|pretty_money }} +

+ +

Rapport détaillé

+ +{% render_table table %} + +-- +

+ Le BDE
+ {% trans "Mail generated by the Note Kfet on the" %} {% now "j F Y à H:i:s" %} +

+ + \ No newline at end of file diff --git a/apps/note/templates/note/search_transactions.html b/apps/note/templates/note/search_transactions.html new file mode 100644 index 00000000..70a1261f --- /dev/null +++ b/apps/note/templates/note/search_transactions.html @@ -0,0 +1,66 @@ +{% extends "base.html" %} +{% comment %} +SPDX-License-Identifier: GPL-3.0-or-later +{% endcomment %} +{% load render_table from django_tables2 %} +{% load crispy_forms_tags %} + +{# Use a fluid-width container #} +{% block containertype %}container-fluid{% endblock %} + +{% block content %} +
+
+
+

+ {{ title }} +

+
+ {% crispy form %} +
+
+
+
+
+
+ {% render_table table %} +
+
+
+
+{% endblock %} + +{% block extrajavascript %} + +{% endblock %} \ No newline at end of file diff --git a/templates/note/transaction_form.html b/apps/note/templates/note/transaction_form.html similarity index 68% rename from templates/note/transaction_form.html rename to apps/note/templates/note/transaction_form.html index 3549871f..acb09beb 100644 --- a/templates/note/transaction_form.html +++ b/apps/note/templates/note/transaction_form.html @@ -2,14 +2,15 @@ {% comment %} SPDX-License-Identifier: GPL-2.0-or-later {% endcomment %} - {% load i18n static django_tables2 perms %} {% block content %} +

{{ title }}

+{# bandeau transfert/crédit/débit/activité #}
-
+
-
- +
+ {# Preview note profile (picture, username and balance) #}
-
- +
+
+ {# list of emitters #}
-
+

- {% trans "Select emitters" %} +

- + +

- + {% trans "I am the emitter" %}
@@ -65,25 +73,32 @@ SPDX-License-Identifier: GPL-2.0-or-later
+ {# list of receiver #}
-
+

- {% trans "Select receivers" %} +

- + +
+ {# Information on transaction (amount, reason, name,...) #}
-
+

{% trans "Action" %} @@ -102,22 +117,12 @@ SPDX-License-Identifier: GPL-2.0-or-later

- +

- + {# in case of special transaction add identity information #}
-
-
- - -
-
@@ -147,7 +152,7 @@ SPDX-License-Identifier: GPL-2.0-or-later
- +{# transaction history #}

@@ -164,6 +169,12 @@ SPDX-License-Identifier: GPL-2.0-or-later SPECIAL_TRANSFER_POLYMORPHIC_CTYPE = {{ special_polymorphic_ctype }}; user_id = {{ user.note.pk }}; username = "{{ user.username|escapejs }}"; + + select_emitter_label = "{% trans "Select emitter" %}"; + select_emitters_label = "{% trans "Select emitters" %}"; + select_receveir_label = "{% trans "Select receiver" %}"; + select_receveirs_label = "{% trans "Select receivers" %}"; + transfer_type_label = "{% trans "Transfer type" %}"; {% endblock %} diff --git a/apps/note/templates/note/transactiontemplate_form.html b/apps/note/templates/note/transactiontemplate_form.html new file mode 100644 index 00000000..816be9d8 --- /dev/null +++ b/apps/note/templates/note/transactiontemplate_form.html @@ -0,0 +1,34 @@ +{% extends "base.html" %} +{% comment %} +SPDX-License-Identifier: GPL-3.0-or-later +{% endcomment %} +{% load static i18n crispy_forms_tags pretty_money %} + +{% block content %} +{% trans "Buttons list" %} + +

+

+ {{ title }} +

+
+
+ {% csrf_token %} + {{form|crispy}} + +
+ + {% if price_history and price_history.1 %} +
+ +

{% trans "Price history" %}

+
    + {% for price in price_history %} +
  • {{ price.price|pretty_money }} {% if price.time %}({% trans "Obsolete since" %} {{ price.time }}){% else %}({% trans "Current price" %}){% endif %}
  • + {% endfor %} +
+ {% endif %} +
+
+ +{% endblock %} diff --git a/templates/note/transactiontemplate_list.html b/apps/note/templates/note/transactiontemplate_list.html similarity index 68% rename from templates/note/transactiontemplate_list.html rename to apps/note/templates/note/transactiontemplate_list.html index 793b2278..2a19922e 100644 --- a/templates/note/transactiontemplate_list.html +++ b/apps/note/templates/note/transactiontemplate_list.html @@ -1,17 +1,22 @@ {% extends "base.html" %} -{% load pretty_money %} -{% load i18n %} +{% comment %} +SPDX-License-Identifier: GPL-3.0-or-later +{% endcomment %} +{% load pretty_money i18n %} {% load render_table from django_tables2 %} + {% block content %} +

{{ title }}

- + {# Search field , see js #} +
- {% trans "New button" %} + {% trans "New button" %}
-
+
{% trans "buttons listing "%}
@@ -28,12 +33,25 @@ +{% endblock %} diff --git a/apps/permission/tests/__init__.py b/apps/permission/tests/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/apps/permission/tests/test_permission_denied.py b/apps/permission/tests/test_permission_denied.py new file mode 100644 index 00000000..95cc14cd --- /dev/null +++ b/apps/permission/tests/test_permission_denied.py @@ -0,0 +1,157 @@ +# Copyright (C) 2018-2020 by BDE ENS Paris-Saclay +# SPDX-License-Identifier: GPL-3.0-or-later + +from datetime import timedelta, date + +from django.contrib.auth.models import User +from django.test import TestCase +from django.urls import reverse +from django.utils import timezone +from django.utils.crypto import get_random_string +from activity.models import Activity +from member.models import Club, Membership +from note.models import NoteUser +from wei.models import WEIClub, Bus, WEIRegistration + + +class TestPermissionDenied(TestCase): + """ + Load some protected pages and check that we have 403 errors. + """ + fixtures = ('initial',) + + def setUp(self) -> None: + # Create sample user with no rights + self.user = User.objects.create( + username="toto", + ) + NoteUser.objects.create(user=self.user) + self.client.force_login(self.user) + + def test_consos(self): + response = self.client.get(reverse("note:consos")) + self.assertEqual(response.status_code, 403) + + def test_create_activity(self): + response = self.client.get(reverse("activity:activity_create")) + self.assertEqual(response.status_code, 403) + + def test_activity_entries(self): + activity = Activity.objects.create( + name="", + description="", + creater=self.user, + activity_type_id=1, + organizer_id=1, + attendees_club_id=1, + date_start=timezone.now(), + date_end=timezone.now(), + ) + response = self.client.get(reverse("activity:activity_entry", kwargs=dict(pk=activity.pk))) + self.assertEqual(response.status_code, 403) + + def test_invite_activity(self): + activity = Activity.objects.create( + name="", + description="", + creater=self.user, + activity_type_id=1, + organizer_id=1, + attendees_club_id=1, + date_start=timezone.now(), + date_end=timezone.now(), + ) + response = self.client.get(reverse("activity:activity_invite", kwargs=dict(pk=activity.pk))) + self.assertEqual(response.status_code, 403) + + def test_create_club(self): + response = self.client.get(reverse("member:club_create")) + self.assertEqual(response.status_code, 403) + + def test_add_member_club(self): + club = Club.objects.create(name=get_random_string(127)) + response = self.client.get(reverse("member:club_add_member", kwargs=dict(club_pk=club.pk))) + self.assertEqual(response.status_code, 403) + + def test_renew_membership(self): + club = Club.objects.create(name=get_random_string(127)) + membership = Membership.objects.create(user=self.user, club=club) + response = self.client.get(reverse("member:club_renew_membership", kwargs=dict(pk=membership.pk))) + self.assertEqual(response.status_code, 403) + + def test_create_weiclub(self): + response = self.client.get(reverse("wei:wei_create")) + self.assertEqual(response.status_code, 403) + + def test_create_wei_bus(self): + wei = WEIClub.objects.create( + membership_start=date.today(), + date_start=date.today() + timedelta(days=1), + date_end=date.today() + timedelta(days=1), + ) + response = self.client.get(reverse("wei:add_bus", kwargs=dict(pk=wei.pk))) + self.assertEqual(response.status_code, 403) + + def test_create_wei_team(self): + wei = WEIClub.objects.create( + membership_start=date.today(), + date_start=date.today() + timedelta(days=1), + date_end=date.today() + timedelta(days=1), + ) + bus = Bus.objects.create(wei=wei) + response = self.client.get(reverse("wei:add_team", kwargs=dict(pk=bus.pk))) + self.assertEqual(response.status_code, 403) + + def test_create_1a_weiregistration(self): + wei = WEIClub.objects.create( + membership_start=date.today(), + date_start=date.today() + timedelta(days=1), + date_end=date.today() + timedelta(days=1), + ) + response = self.client.get(reverse("wei:wei_register_1A", kwargs=dict(wei_pk=wei.pk))) + self.assertEqual(response.status_code, 403) + + def test_create_old_weiregistration(self): + wei = WEIClub.objects.create( + membership_start=date.today(), + date_start=date.today() + timedelta(days=1), + date_end=date.today() + timedelta(days=1), + ) + response = self.client.get(reverse("wei:wei_register_2A", kwargs=dict(wei_pk=wei.pk))) + self.assertEqual(response.status_code, 403) + + def test_validate_weiregistration(self): + wei = WEIClub.objects.create( + membership_start=date.today(), + date_start=date.today() + timedelta(days=1), + date_end=date.today() + timedelta(days=1), + ) + registration = WEIRegistration.objects.create(wei=wei, user=self.user, birth_date="2000-01-01") + response = self.client.get(reverse("wei:validate_registration", kwargs=dict(pk=registration.pk))) + self.assertEqual(response.status_code, 403) + + def test_create_invoice(self): + response = self.client.get(reverse("treasury:invoice_create")) + self.assertEqual(response.status_code, 403) + + def test_list_invoices(self): + response = self.client.get(reverse("treasury:invoice_list")) + self.assertEqual(response.status_code, 403) + + def test_create_remittance(self): + response = self.client.get(reverse("treasury:remittance_create")) + self.assertEqual(response.status_code, 403) + + def test_list_remittance(self): + response = self.client.get(reverse("treasury:remittance_list")) + self.assertEqual(response.status_code, 403) + + def test_list_soge_credits(self): + response = self.client.get(reverse("treasury:soge_credits")) + self.assertEqual(response.status_code, 403) + + +class TestLoginRedirect(TestCase): + def test_consos_page(self): + response = self.client.get(reverse("note:consos")) + self.assertRedirects(response, reverse("login") + "?next=" + reverse("note:consos"), 302, 200) diff --git a/apps/permission/test.py b/apps/permission/tests/test_permission_queries.py similarity index 90% rename from apps/permission/test.py rename to apps/permission/tests/test_permission_queries.py index e728e9a6..e0af9cf0 100644 --- a/apps/permission/test.py +++ b/apps/permission/tests/test_permission_queries.py @@ -1,6 +1,8 @@ # Copyright (C) 2018-2020 by BDE ENS Paris-Saclay # SPDX-License-Identifier: GPL-3.0-or-later +from datetime import date + from django.contrib.auth.models import User from django.core.exceptions import FieldError from django.db.models import F, Q @@ -10,7 +12,7 @@ from member.models import Club, Membership from note.models import NoteUser, Note, NoteClub, NoteSpecial from wei.models import WEIMembership, WEIRegistration, WEIClub, Bus, BusTeam -from .models import Permission +from ..models import Permission class PermissionQueryTestCase(TestCase): @@ -22,14 +24,14 @@ class PermissionQueryTestCase(TestCase): NoteUser.objects.create(user=user) wei = WEIClub.objects.create( name="wei", - date_start=timezone.now().date(), - date_end=timezone.now().date(), + date_start=date.today(), + date_end=date.today(), ) NoteClub.objects.create(club=wei) weiregistration = WEIRegistration.objects.create( user=user, wei=wei, - birth_date=timezone.now().date(), + birth_date=date.today(), ) bus = Bus.objects.create( name="bus", @@ -68,7 +70,7 @@ class PermissionQueryTestCase(TestCase): F=F, Q=Q, now=timezone.now(), - today=timezone.now().date(), + today=date.today(), ) try: instanced.update_query() @@ -82,5 +84,3 @@ class PermissionQueryTestCase(TestCase): if instanced.query: print("Compiled query:", instanced.query) raise - - print("All permission queries are well formed") diff --git a/apps/permission/views.py b/apps/permission/views.py index 83deddac..70aa7184 100644 --- a/apps/permission/views.py +++ b/apps/permission/views.py @@ -1,14 +1,20 @@ # Copyright (C) 2018-2020 by BDE ENS Paris-Saclay # SPDX-License-Identifier: GPL-3.0-or-later + from datetime import date +from django.contrib.auth.mixins import LoginRequiredMixin +from django.contrib.auth.models import User +from django.core.exceptions import PermissionDenied +from django.db.models import Q from django.forms import HiddenInput from django.utils.translation import gettext_lazy as _ -from django.views.generic import UpdateView, TemplateView +from django.views.generic import UpdateView, TemplateView, CreateView from member.models import Membership from .backends import PermissionBackend from .models import Role +from .tables import RightsTable, SuperuserTable class ProtectQuerysetMixin: @@ -20,7 +26,7 @@ class ProtectQuerysetMixin: """ def get_queryset(self, **kwargs): qs = super().get_queryset(**kwargs) - return qs.filter(PermissionBackend.filter_queryset(self.request.user, qs.model, "view")) + return qs.filter(PermissionBackend.filter_queryset(self.request.user, qs.model, "view")).distinct() def get_form(self, form_class=None): form = super().get_form(form_class) @@ -38,6 +44,52 @@ class ProtectQuerysetMixin: return form + def form_valid(self, form): + """ + Submit the form, if the page is a FormView. + If a PermissionDenied exception is raised, catch the error and display it at the top of the form. + """ + try: + return super().form_valid(form) + except PermissionDenied: + if isinstance(self, UpdateView): + form.add_error(None, _("You don't have the permission to update this instance of the model \"{model}\"" + " with these parameters. Please correct your data and retry.") + .format(model=self.model._meta.verbose_name)) + else: + form.add_error(None, _("You don't have the permission to create an instance of the model \"{model}\"" + " with these parameters. Please correct your data and retry.") + .format(model=self.model._meta.verbose_name)) + return self.form_invalid(form) + + +class ProtectedCreateView(LoginRequiredMixin, CreateView): + """ + Extends a CreateView to check is the user has the right to create a sample instance of the given Model. + If not, a 403 error is displayed. + """ + + def get_sample_object(self): + """ + return a sample instance of the Model. + It should be valid (can be stored properly in database), but must not collide with existing data. + """ + raise NotImplementedError + + def dispatch(self, request, *args, **kwargs): + # Check that the user is authenticated before that he/she has the permission to access here + if not request.user.is_authenticated: + return self.handle_no_permission() + + model_class = self.model + # noinspection PyProtectedMember + app_label, model_name = model_class._meta.app_label, model_class._meta.model_name.lower() + perm = app_label + ".add_" + model_name + if not PermissionBackend.check_perm(request.user, perm, self.get_sample_object()): + raise PermissionDenied(_("You don't have the permission to add an instance of model " + "{app_label}.{model_name}.").format(app_label=app_label, model_name=model_name)) + return super().dispatch(request, *args, **kwargs) + class RightsView(TemplateView): template_name = "permission/all_rights.html" @@ -59,4 +111,19 @@ class RightsView(TemplateView): for role in roles: role.clubs = [membership.club for membership in active_memberships if role in membership.roles.all()] + if self.request.user.is_authenticated: + special_memberships = Membership.objects.filter( + date_start__lte=date.today(), + date_end__gte=date.today(), + ).filter(roles__in=Role.objects.filter((~(Q(name="Adhérent BDE") + | Q(name="Adhérent Kfet") + | Q(name="Membre de club") + | Q(name="Bureau de club")) + & Q(weirole__isnull=True))))\ + .order_by("club__name", "user__last_name")\ + .distinct().all() + context["special_memberships_table"] = RightsTable(special_memberships, prefix="clubs-") + context["superusers"] = SuperuserTable(User.objects.filter(is_superuser=True).order_by("last_name").all(), + prefix="superusers-") + return context diff --git a/apps/registration/forms.py b/apps/registration/forms.py index 46559487..ad258cb1 100644 --- a/apps/registration/forms.py +++ b/apps/registration/forms.py @@ -5,7 +5,7 @@ from django import forms from django.contrib.auth.forms import UserCreationForm from django.contrib.auth.models import User from django.utils.translation import gettext_lazy as _ -from note.models import NoteSpecial +from note.models import NoteSpecial, Alias from note_kfet.inputs import AmountInput @@ -22,6 +22,23 @@ class SignUpForm(UserCreationForm): self.fields['email'].required = True self.fields['email'].help_text = _("This address must be valid.") + # Give some example + self.fields['first_name'].widget.attrs.update({"placeholder": "Sacha"}) + self.fields['last_name'].widget.attrs.update({"placeholder": "Ketchum"}) + self.fields['email'].widget.attrs.update({"placeholder": "mail@example.com"}) + + def clean_username(self): + value = self.cleaned_data["username"] + if Alias.objects.filter(normalized_name=Alias.normalize(value)).exists(): + self.add_error("username", _("An alias with a similar name already exists.")) + return value + + def clean_email(self): + email = self.cleaned_data["email"] + if User.objects.filter(email=email).exists(): + self.add_error("email", _("This email address is already used.")) + return email + class Meta: model = User fields = ('first_name', 'last_name', 'username', 'email', ) diff --git a/apps/registration/static/registration/css/login.css b/apps/registration/static/registration/css/login.css new file mode 100644 index 00000000..b789b2f7 --- /dev/null +++ b/apps/registration/static/registration/css/login.css @@ -0,0 +1,19 @@ +/* +Add icons to login form +Font-Awesome attribution is already done inside SVG files +*/ + +#login-form input[type="text"] { + background: #fff right 1rem top 50% / 5% no-repeat url('../img/fa-user.svg'); + padding-right: 3rem; +} + +#login-form input[type="password"] { + background: #fff right 1rem top 50% / 5% no-repeat url('../img/fa-lock.svg'); + padding-right: 3rem; +} + +#login-form select { + -moz-appearance: none; + cursor: pointer; +} \ No newline at end of file diff --git a/apps/registration/static/registration/img/fa-lock.svg b/apps/registration/static/registration/img/fa-lock.svg new file mode 100644 index 00000000..24b8dc75 --- /dev/null +++ b/apps/registration/static/registration/img/fa-lock.svg @@ -0,0 +1,2 @@ + + \ No newline at end of file diff --git a/apps/registration/static/registration/img/fa-user.svg b/apps/registration/static/registration/img/fa-user.svg new file mode 100644 index 00000000..ac145d32 --- /dev/null +++ b/apps/registration/static/registration/img/fa-user.svg @@ -0,0 +1,2 @@ + + \ No newline at end of file diff --git a/apps/registration/tables.py b/apps/registration/tables.py index 7068f6ca..274369fe 100644 --- a/apps/registration/tables.py +++ b/apps/registration/tables.py @@ -9,9 +9,9 @@ class FutureUserTable(tables.Table): """ Display the list of pre-registered users """ - phone_number = tables.Column(accessor='profile.phone_number') + phone_number = tables.Column(accessor='profile__phone_number') - section = tables.Column(accessor='profile.section') + section = tables.Column(accessor='profile__section') class Meta: attrs = { diff --git a/templates/registration/email_validation_complete.html b/apps/registration/templates/registration/email_validation_complete.html similarity index 89% rename from templates/registration/email_validation_complete.html rename to apps/registration/templates/registration/email_validation_complete.html index b54432f3..dca26470 100644 --- a/templates/registration/email_validation_complete.html +++ b/apps/registration/templates/registration/email_validation_complete.html @@ -1,4 +1,7 @@ {% extends "base.html" %} +{% comment %} +SPDX-License-Identifier: GPL-3.0-or-later +{% endcomment %} {% load i18n %} {% block content %} diff --git a/apps/registration/templates/registration/email_validation_email_sent.html b/apps/registration/templates/registration/email_validation_email_sent.html new file mode 100644 index 00000000..627c864b --- /dev/null +++ b/apps/registration/templates/registration/email_validation_email_sent.html @@ -0,0 +1,17 @@ +{% extends "base.html" %} +{% comment %} +SPDX-License-Identifier: GPL-3.0-or-later +{% endcomment %} +{% load i18n %} + +{% block content %} +

{% trans "Account activation" %}

+ +

+ {% trans "An email has been sent. Please click on the link to activate your account." %} +

+ +

+ {% trans "You must also go to the Kfet to pay your membership. The WEI registration includes the BDE membership." %} +

+{% endblock %} \ No newline at end of file diff --git a/templates/registration/future_profile_detail.html b/apps/registration/templates/registration/future_profile_detail.html similarity index 97% rename from templates/registration/future_profile_detail.html rename to apps/registration/templates/registration/future_profile_detail.html index 1d2d08c7..914b4224 100644 --- a/templates/registration/future_profile_detail.html +++ b/apps/registration/templates/registration/future_profile_detail.html @@ -1,8 +1,8 @@ {% extends "base.html" %} -{% load static %} -{% load i18n %} -{% load crispy_forms_tags %} -{% load perms %} +{% comment %} +SPDX-License-Identifier: GPL-3.0-or-later +{% endcomment %} +{% load i18n crispy_forms_tags perms %} {% block content %}
diff --git a/apps/registration/templates/registration/future_user_list.html b/apps/registration/templates/registration/future_user_list.html new file mode 100644 index 00000000..1e82403f --- /dev/null +++ b/apps/registration/templates/registration/future_user_list.html @@ -0,0 +1,14 @@ +{% extends "base_search.html" %} +{% comment %} +SPDX-License-Identifier: GPL-3.0-or-later +{% endcomment %} +{% load i18n %} + +{% block content %} + + {% trans "New user" %} + + +{# Search panel #} +{{ block.super }} +{% endblock %} diff --git a/apps/registration/templates/registration/mails/email_validation_email.html b/apps/registration/templates/registration/mails/email_validation_email.html new file mode 100644 index 00000000..0d5b41f5 --- /dev/null +++ b/apps/registration/templates/registration/mails/email_validation_email.html @@ -0,0 +1,41 @@ +{% load i18n %} + + + + + + Passage en négatif (compte n°{{ note.user.pk }}) + + + +

+ {% trans "Hi" %} {{ user.username }}, +

+ +

+ {% trans "You recently registered on the Note Kfet. Please click on the link below to confirm your registration." %} +

+ +

+ + https://{{ domain }}{% url 'registration:email_validation' uidb64=uid token=token %} + +

+ +

+ {% trans "This link is only valid for a couple of days, after that you will need to contact us to validate your email." %} +

+ +

+ {% trans "After that, you'll have to wait that someone validates your account before you can log in. You will need to pay your membership in the Kfet. Note that the WEI registration includes the Kfet membership." %} +

+ +

+ {% trans "Thanks" %}, +

+ +-- +

+ {% trans "The Note Kfet team." %}
+ {% trans "Mail generated by the Note Kfet on the" %} {% now "j F Y à H:i:s" %} +

\ No newline at end of file diff --git a/templates/registration/mails/email_validation_email.html b/apps/registration/templates/registration/mails/email_validation_email.txt similarity index 77% rename from templates/registration/mails/email_validation_email.html rename to apps/registration/templates/registration/mails/email_validation_email.txt index 577c1220..5ce48110 100644 --- a/templates/registration/mails/email_validation_email.html +++ b/apps/registration/templates/registration/mails/email_validation_email.txt @@ -8,8 +8,9 @@ https://{{ domain }}{% url 'registration:email_validation' uidb64=uid token=toke {% trans "This link is only valid for a couple of days, after that you will need to contact us to validate your email." %} -{% trans "After that, you'll have to wait that someone validates your account before you can log in. You will need to pay your membership in the Kfet." %} +{% trans "After that, you'll have to wait that someone validates your account before you can log in. You will need to pay your membership in the Kfet. Note that the WEI registration includes the Kfet membership." %} {% trans "Thanks" %}, {% trans "The Note Kfet team." %} +{% trans "Mail generated by the Note Kfet on the" %} {% now "j F Y à H:i:s" %} \ No newline at end of file diff --git a/apps/registration/views.py b/apps/registration/views.py index 804c9fa9..bf68a8ed 100644 --- a/apps/registration/views.py +++ b/apps/registration/views.py @@ -29,7 +29,7 @@ from .tokens import email_validation_token class UserCreateView(CreateView): """ - Une vue pour inscrire un utilisateur et lui créer un profil + A view to create a User and add a Profile """ form_class = SignUpForm @@ -39,8 +39,10 @@ class UserCreateView(CreateView): def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) - context["profile_form"] = self.second_form() + context["profile_form"] = self.second_form(self.request.POST if self.request.POST else None) del context["profile_form"].fields["section"] + del context["profile_form"].fields["report_frequency"] + del context["profile_form"].fields["last_report"] return context @@ -179,8 +181,6 @@ class FutureUserListView(ProtectQuerysetMixin, LoginRequiredMixin, SingleTableVi | Q(profile__section__iregex=pattern) | Q(username__iregex="^" + pattern) ) - else: - qs = qs.none() return qs[:20] diff --git a/apps/scripts b/apps/scripts index dce51ad2..c1c0a879 160000 --- a/apps/scripts +++ b/apps/scripts @@ -1 +1 @@ -Subproject commit dce51ad26134d396d7cbfca7c63bd2ed391dd969 +Subproject commit c1c0a8797179d110ad919912378f05b030f44f61 diff --git a/apps/treasury/admin.py b/apps/treasury/admin.py index 33224ba7..1db820b2 100644 --- a/apps/treasury/admin.py +++ b/apps/treasury/admin.py @@ -4,7 +4,8 @@ from django.contrib import admin from note_kfet.admin import admin_site -from .models import RemittanceType, Remittance, SogeCredit +from .forms import ProductForm +from .models import RemittanceType, Remittance, SogeCredit, Invoice, Product @admin.register(RemittanceType, site=admin_site) @@ -39,3 +40,20 @@ class SogeCreditAdmin(admin.ModelAdmin): def has_add_permission(self, request): # Don't create a credit manually return False + + +class ProductInline(admin.StackedInline): + """ + Inline product in invoice admin + """ + model = Product + form = ProductForm + + +@admin.register(Invoice, site=admin_site) +class InvoiceAdmin(admin.ModelAdmin): + """ + Admin customisation for Invoice + """ + list_display = ('object', 'id', 'bde', 'name', 'date', 'acquitted',) + inlines = (ProductInline,) diff --git a/apps/treasury/forms.py b/apps/treasury/forms.py index 4c761ef2..38da324d 100644 --- a/apps/treasury/forms.py +++ b/apps/treasury/forms.py @@ -1,13 +1,11 @@ # Copyright (C) 2018-2020 by BDE ENS Paris-Saclay # SPDX-License-Identifier: GPL-3.0-or-later -import datetime - from crispy_forms.helper import FormHelper from crispy_forms.layout import Submit from django import forms from django.utils.translation import gettext_lazy as _ -from note_kfet.inputs import DatePickerInput, AmountInput +from note_kfet.inputs import AmountInput from .models import Invoice, Product, Remittance, SpecialTransactionProxy @@ -17,18 +15,25 @@ class InvoiceForm(forms.ModelForm): Create and generate invoices. """ - # Django forms don't support date fields. We have to add it manually - date = forms.DateField( - initial=datetime.date.today, - widget=DatePickerInput() - ) + def clean(self): + if self.instance and self.instance.locked: + for field_name in self.fields: + self.cleaned_data[field_name] = getattr(self.instance, field_name) + self.errors.clear() + return self.cleaned_data + return super().clean() - def clean_date(self): - self.instance.date = self.data.get("date") + def save(self, commit=True): + """ + If the invoice is locked, don't save it + """ + if not self.instance.locked: + super().save(commit) + return self.instance class Meta: model = Invoice - exclude = ('bde', ) + exclude = ('bde', 'date', 'tex', ) class ProductForm(forms.ModelForm): @@ -36,7 +41,11 @@ class ProductForm(forms.ModelForm): model = Product fields = '__all__' widgets = { - "amount": AmountInput() + "amount": AmountInput( + attrs={ + "negative": True, + } + ) } @@ -115,6 +124,12 @@ class LinkTransactionToRemittanceForm(forms.ModelForm): """ Attach a special transaction to a remittance. """ + remittance = forms.ModelChoiceField( + queryset=Remittance.objects.none(), + label=_("Remittance"), + empty_label=_("No attached remittance"), + required=False, + ) # Since we use a proxy model for special transactions, we add manually the fields related to the transaction last_name = forms.CharField(label=_("Last name")) @@ -123,7 +138,7 @@ class LinkTransactionToRemittanceForm(forms.ModelForm): bank = forms.Field(label=_("Bank")) - amount = forms.IntegerField(label=_("Amount"), min_value=0) + amount = forms.IntegerField(label=_("Amount"), min_value=0, widget=AmountInput(), disabled=True, required=False) def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) @@ -133,33 +148,19 @@ class LinkTransactionToRemittanceForm(forms.ModelForm): self.fields["remittance"].queryset = Remittance.objects.filter(closed=False) - def clean_last_name(self): - """ - Replace the first name in the information of the transaction. - """ - self.instance.transaction.last_name = self.data.get("last_name") - self.instance.transaction.clean() + def clean(self): + cleaned_data = super().clean() + self.instance.transaction.last_name = cleaned_data["last_name"] + self.instance.transaction.first_name = cleaned_data["first_name"] + self.instance.transaction.bank = cleaned_data["bank"] + return cleaned_data - def clean_first_name(self): + def save(self, commit=True): """ - Replace the last name in the information of the transaction. + Save the transaction and the remittance. """ - self.instance.transaction.first_name = self.data.get("first_name") - self.instance.transaction.clean() - - def clean_bank(self): - """ - Replace the bank in the information of the transaction. - """ - self.instance.transaction.bank = self.data.get("bank") - self.instance.transaction.clean() - - def clean_amount(self): - """ - Replace the amount of the transaction. - """ - self.instance.transaction.amount = self.data.get("amount") - self.instance.transaction.clean() + self.instance.transaction.save() + return super().save(commit) class Meta: model = SpecialTransactionProxy diff --git a/apps/treasury/models.py b/apps/treasury/models.py index 6cfb55c1..6d5b4021 100644 --- a/apps/treasury/models.py +++ b/apps/treasury/models.py @@ -1,11 +1,13 @@ # Copyright (C) 2018-2020 by BDE ENS Paris-Saclay # SPDX-License-Identifier: GPL-3.0-or-later -from datetime import datetime + +from datetime import date from django.contrib.auth.models import User from django.core.exceptions import ValidationError from django.db import models from django.db.models import Q +from django.template.loader import render_to_string from django.utils import timezone from django.utils.translation import gettext_lazy as _ from note.models import NoteSpecial, SpecialTransaction, MembershipTransaction @@ -54,14 +56,55 @@ class Invoice(models.Model): ) date = models.DateField( - default=timezone.now, - verbose_name=_("Place"), + default=date.today, + verbose_name=_("Date"), ) acquitted = models.BooleanField( verbose_name=_("Acquitted"), + default=False, ) + locked = models.BooleanField( + verbose_name=_("Locked"), + help_text=_("An invoice can't be edited when it is locked."), + default=False, + ) + + tex = models.TextField( + default="", + verbose_name=_("tex source"), + ) + + def save(self, *args, **kwargs): + """ + When an invoice is generated, we store the tex source. + The advantage is to never change the template. + Warning: editing this model regenerate the tex source, so be careful. + """ + + old_invoice = Invoice.objects.filter(id=self.id) + if old_invoice.exists(): + if old_invoice.get().locked: + raise ValidationError(_("This invoice is locked and can no longer be edited.")) + + products = self.products.all() + + self.place = "Gif-sur-Yvette" + self.my_name = "BDE ENS Cachan" + self.my_address_street = "4 avenue des Sciences" + self.my_city = "91190 Gif-sur-Yvette" + self.bank_code = 30003 + self.desk_code = 3894 + self.account_number = 37280662 + self.rib_key = 14 + self.bic = "SOGEFRPP" + + # Fill the template with the information + self.tex = render_to_string("treasury/invoice_sample.tex", dict(obj=self, products=products)) + + return super().save(*args, **kwargs) + class Meta: verbose_name = _("invoice") verbose_name_plural = _("invoices") @@ -74,7 +117,9 @@ class Product(models.Model): invoice = models.ForeignKey( Invoice, - on_delete=models.PROTECT, + on_delete=models.CASCADE, + related_name="products", + verbose_name=_("invoice"), ) designation = models.CharField( @@ -92,7 +137,7 @@ class Product(models.Model): @property def amount_euros(self): - return self.amount / 100 + return "{:.2f}".format(self.amount / 100) @property def total(self): @@ -100,7 +145,7 @@ class Product(models.Model): @property def total_euros(self): - return self.total / 100 + return "{:.2f}".format(self.total / 100) class Meta: verbose_name = _("product") @@ -276,13 +321,14 @@ class SogeCredit(models.Model): last_name=self.user.last_name, first_name=self.user.first_name, bank="Société générale", + created_at=self.transactions.order_by("-created_at").first().created_at, ) self.save() for transaction in self.transactions.all(): transaction.valid = True transaction._force_save = True - transaction.created_at = datetime.now() + transaction.created_at = timezone.now() transaction.save() def delete(self, **kwargs): @@ -301,7 +347,7 @@ class SogeCredit(models.Model): for transaction in self.transactions.all(): transaction._force_save = True transaction.valid = True - transaction.created_at = datetime.now() + transaction.created_at = timezone.now() transaction.save() super().delete(**kwargs) diff --git a/apps/treasury/tables.py b/apps/treasury/tables.py index 9f4e43e6..14044f1c 100644 --- a/apps/treasury/tables.py +++ b/apps/treasury/tables.py @@ -14,19 +14,39 @@ class InvoiceTable(tables.Table): """ List all invoices. """ - id = tables.LinkColumn("treasury:invoice_update", - args=[A("pk")], - text=lambda record: _("Invoice #{:d}").format(record.id), ) + id = tables.LinkColumn( + "treasury:invoice_update", + args=[A("pk")], + text=lambda record: _("Invoice #{:d}").format(record.id), + ) - invoice = tables.LinkColumn("treasury:invoice_render", - verbose_name=_("Invoice"), - args=[A("pk")], - accessor="pk", - text="", - attrs={ - 'a': {'class': 'fa fa-file-pdf-o'}, - 'td': {'data-turbolinks': 'false'} - }) + invoice = tables.LinkColumn( + "treasury:invoice_render", + verbose_name=_("Invoice"), + args=[A("pk")], + accessor="pk", + text="", + attrs={ + 'a': {'class': 'fa fa-file-pdf-o'}, + 'td': {'data-turbolinks': 'false'} + } + ) + + delete = tables.LinkColumn( + 'treasury:invoice_delete', + args=[A('pk')], + verbose_name=_("delete"), + text=_("Delete"), + attrs={ + 'th': { + 'id': 'delete-membership-header' + }, + 'a': { + 'class': 'btn btn-danger', + 'data-type': 'delete-membership' + } + }, + ) class Meta: attrs = { @@ -64,6 +84,7 @@ class RemittanceTable(tables.Table): model = Remittance template_name = 'django_tables2/bootstrap4.html' fields = ('id', 'date', 'remittance_type', 'comment', 'count', 'amount', 'view',) + order_by = ('-date',) class SpecialTransactionTable(tables.Table): @@ -74,7 +95,7 @@ class SpecialTransactionTable(tables.Table): # Display add and remove buttons. Use the `exclude` field to select what is needed. remittance_add = tables.LinkColumn("treasury:link_transaction", verbose_name=_("Remittance"), - args=[A("specialtransactionproxy.pk")], + args=[A("specialtransactionproxy__pk")], text=_("Add"), attrs={ 'a': {'class': 'btn btn-primary'} @@ -82,7 +103,7 @@ class SpecialTransactionTable(tables.Table): remittance_remove = tables.LinkColumn("treasury:unlink_transaction", verbose_name=_("Remittance"), - args=[A("specialtransactionproxy.pk")], + args=[A("specialtransactionproxy__pk")], text=_("Remove"), attrs={ 'a': {'class': 'btn btn-primary btn-danger'} @@ -100,7 +121,8 @@ class SpecialTransactionTable(tables.Table): } model = SpecialTransaction template_name = 'django_tables2/bootstrap4.html' - fields = ('id', 'source', 'destination', 'last_name', 'first_name', 'bank', 'amount', 'reason',) + fields = ('created_at', 'source', 'destination', 'last_name', 'first_name', 'bank', 'amount', 'reason',) + order_by = ('-created_at',) class SogeCreditTable(tables.Table): diff --git a/apps/treasury/templates/treasury/invoice_confirm_delete.html b/apps/treasury/templates/treasury/invoice_confirm_delete.html new file mode 100644 index 00000000..e1de9c83 --- /dev/null +++ b/apps/treasury/templates/treasury/invoice_confirm_delete.html @@ -0,0 +1,35 @@ +{% extends "base.html" %} +{% comment %} +SPDX-License-Identifier: GPL-3.0-or-later +{% endcomment %} +{% load i18n crispy_forms_tags %} + +{% block content %} +
+
+

{% trans "Delete invoice" %}

+
+ {% if object.locked %} +
+
+ {% blocktrans %}This invoice is locked and can't be deleted.{% endblocktrans %} +
+
+ {% else %} +
+
+ {% blocktrans %}Are you sure you want to delete this invoice? This action can't be undone.{% endblocktrans %} +
+
+ {% endif %} + +
+{% endblock %} diff --git a/apps/treasury/templates/treasury/invoice_form.html b/apps/treasury/templates/treasury/invoice_form.html new file mode 100644 index 00000000..fee4140d --- /dev/null +++ b/apps/treasury/templates/treasury/invoice_form.html @@ -0,0 +1,118 @@ +{% extends "base.html" %} +{% comment %} +SPDX-License-Identifier: GPL-3.0-or-later +{% endcomment %} +{% load i18n crispy_forms_tags %} + +{% block content %} +
+

+ {{ title }} +

+
+ {% if object.pk and not object.locked %} +
+ {% blocktrans trimmed %} + Warning: the LaTeX template is saved with this object. Updating the invoice implies regenerate it. + Be careful if you manipulate old invoices. + {% endblocktrans %} +
+ {% elif object.locked %} +
+ {% blocktrans trimmed %} + This invoice is locked and can no longer be edited. + {% endblocktrans %} +
+ {% endif %} +
+ +
+ {% csrf_token %} + + {# Render the invoice form #} +
+ {% crispy form %} +
+ + {# The next part concerns the product formset #} + {# Generate some hidden fields that manage the number of products, and make easier the parsing #} + {{ formset.management_form }} + + {# Fill initial data #} + {% for form in formset %} + {% if forloop.first %} + + + + + + + + + {% endif %} + + + + + {# These fields are hidden but handled by the formset to link the id and the invoice id #} + {{ form.invoice }} + {{ form.id }} + + {% endfor %} + +
{{ form.designation.label }}*{{ form.quantity.label }}*{{ form.amount.label }}*
{{ form.designation }}{{ form.quantity }}{{ form.amount }}
+ + {# Display buttons to add and remove products #} +
+ {% if not object.locked %} +
+ + +
+ {% endif %} + + +
+
+
+ +{# Hidden div that store an empty product form, to be copied into new forms #} + +{% endblock %} + +{% block extrajavascript %} + +{% endblock %} \ No newline at end of file diff --git a/apps/treasury/templates/treasury/invoice_list.html b/apps/treasury/templates/treasury/invoice_list.html new file mode 100644 index 00000000..32c1b1c1 --- /dev/null +++ b/apps/treasury/templates/treasury/invoice_list.html @@ -0,0 +1,34 @@ +{% extends "base.html" %} +{% comment %} +SPDX-License-Identifier: GPL-3.0-or-later +{% endcomment %} +{% load render_table from django_tables2 %} +{% load i18n %} + +{% block content %} + + +
+

+ {{ title }} +

+ {% render_table table %} + +
+{% endblock %} \ No newline at end of file diff --git a/templates/treasury/invoice_sample.tex b/apps/treasury/templates/treasury/invoice_sample.tex similarity index 78% rename from templates/treasury/invoice_sample.tex rename to apps/treasury/templates/treasury/invoice_sample.tex index 3c76403e..4e6342b0 100644 --- a/templates/treasury/invoice_sample.tex +++ b/apps/treasury/templates/treasury/invoice_sample.tex @@ -1,3 +1,5 @@ +{% load escape_tex %} + \nonstopmode \documentclass[11pt]{article} @@ -22,14 +24,13 @@ \FPround{\prix}{#3}{2} \FPround{\montant}{#4}{2} \FPadd{\TotalHT}{\TotalHT}{\montant} - - \eaddto\ListeProduits{#1 & \prix & #2 & \montant \cr} } \newcommand{\AfficheResultat}{% \ListeProduits - \FPeval{\TotalTVA}{\TotalHT * \TVA / 100} + \FPmul{\TotalTVA}{\TotalHT}{\TVA} + \FPdiv{\TotalTVA}{\TotalTVA}{100} \FPadd{\TotalTTC}{\TotalHT}{\TotalTVA} \FPround{\TotalHT}{\TotalHT}{2} \FPround{\TotalTVA}{\TotalTVA}{2} @@ -45,15 +46,12 @@ \textbf{Total TTC} & & & \TotalTTC } -\newcommand*\eaddto[2]{% version développée de \addto - \edef\tmp{#2}% - \expandafter\addto - \expandafter#1% - \expandafter{\tmp}% +\newcommand {\ListeProduits}{ + {% for product in products %} + {{ product.designation|escape_tex }} & {{ product.amount_euros }} & {{ product.quantity }} & {{ product.total_euros }} \cr + {% endfor %} } -\newcommand {\ListeProduits}{} - % Logo du BDE \AddToShipoutPicture*{ \put(0,0){ @@ -69,9 +67,9 @@ %%%%%%%%%%%%%%%%%%%%% A MODIFIER DANS LA FACTURE %%%%%%%%%%%%%%%%%%%%% % Infos Association -\def\MonNom{{"{"}}{{ obj.my_name }}} % Nom de l'association -\def\MonAdresseRue{{"{"}}{{ obj.my_address_street }}} % Adresse de l'association -\def\MonAdresseVille{{"{"}}{{ obj.my_city }}} +\def\MonNom{{"{"}}{{ obj.my_name|escape_tex }}} % Nom de l'association +\def\MonAdresseRue{{"{"}}{{ obj.my_address_street|escape_tex }}} % Adresse de l'association +\def\MonAdresseVille{{"{"}}{{ obj.my_city|escape_tex }}} % Informations bancaires de l'association \def\CodeBanque{{"{"}}{{ obj.bank_code|stringformat:".05d" }}} @@ -81,22 +79,22 @@ \def\IBAN{FR76\CodeBanque\CodeGuichet\NCompte\CleRib} \def\CodeBic{{"{"}}{{ obj.bic }}} -\def\FactureNum {{"{"}}{{obj.id}}} % Numéro de facture +\def\FactureNum {{"{"}}{{ obj.id }}} % Numéro de facture \def\FactureAcquittee {% if obj.acquitted %} {oui} {% else %} {non} {% endif %} % Facture acquittée : oui/non -\def\FactureLieu {{"{"}}{{ obj.place }}} % Lieu de l'édition de la facture +\def\FactureLieu {{"{"}}{{ obj.place|escape_tex }}} % Lieu de l'édition de la facture \def\FactureDate {{"{"}}{{ obj.date }}} % Date de l'édition de la facture -\def\FactureObjet {{"{"}}{{ obj.object|safe }} } % Objet du document +\def\FactureObjet {{"{"}}{{ obj.object|escape_tex }} } % Objet du document % Description de la facture -\def\FactureDescr {{"{"}}{{ obj.description|safe }}} +\def\FactureDescr {{"{"}}{{ obj.description|escape_tex }}} % Infos Client -\def\ClientNom{{"{"}}{{obj.name|safe}}} % Nom du client -\def\ClientAdresse{{"{"}}{{ obj.address|safe }}} % Adresse du client +\def\ClientNom{{"{"}}{{ obj.name|escape_tex }}} % Nom du client +\def\ClientAdresse{{"{"}}{{ obj.address|escape_tex }}} % Adresse du client % Liste des produits facturés : Désignation, quantité, prix unitaire HT {% for product in products %} -\AjouterProduit{ {{product.designation|safe}}} { {{product.quantity|safe}}} { {{product.amount_euros|safe}}} { {{product.total_euros|safe}}} +\AjouterProduit{{"{"}}{{ product.designation|escape_tex }}} {{"{"}}{{ product.quantity }}} {{"{"}}{{ product.amount_euros }}} {{"{"}}{{ product.total_euros }}} {% endfor %} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% diff --git a/apps/treasury/templates/treasury/remittance_form.html b/apps/treasury/templates/treasury/remittance_form.html new file mode 100644 index 00000000..9d9baf27 --- /dev/null +++ b/apps/treasury/templates/treasury/remittance_form.html @@ -0,0 +1,47 @@ +{% extends "base.html" %} +{% comment %} +SPDX-License-Identifier: GPL-3.0-or-later +{% endcomment %} +{% load i18n %} +{% load crispy_forms_tags pretty_money %} +{% load render_table from django_tables2 %} + +{% block content %} +
+

+ {% trans "Remittance #" %}{{ object.pk }} +

+
+ {% if object.pk %} +
+ + +
+ +
+ + +
+ {% endif %} + + {% crispy form %} +
+
+ +
+

+ {% trans "Linked transactions" %} +

+ {% if special_transactions.data %} + {% render_table special_transactions %} + {% else %} +
+
+ {% trans "There is no transaction linked with this remittance." %} +
+
+ {% endif %} +
+{% endblock %} \ No newline at end of file diff --git a/apps/treasury/templates/treasury/remittance_list.html b/apps/treasury/templates/treasury/remittance_list.html new file mode 100644 index 00000000..c400f18f --- /dev/null +++ b/apps/treasury/templates/treasury/remittance_list.html @@ -0,0 +1,87 @@ +{% extends "base.html" %} +{% comment %} +SPDX-License-Identifier: GPL-3.0-or-later +{% endcomment %} +{% load render_table from django_tables2 %} +{% load i18n %} + +{% block content %} + + +
+

+ {% trans "Opened remittances" %} +

+ {% if opened_remittances.data %} + {% render_table opened_remittances %} + {% else %} +
+
+ {% trans "There is no opened remittance." %} +
+
+ {% endif %} + +
+ +
+

+ {% trans "Transfers without remittances" %} +

+ {% if special_transactions_no_remittance.data %} + {% render_table special_transactions_no_remittance %} + {% else %} +
+
+ {% trans "There is no transaction without any linked remittance." %} +
+
+ {% endif %} +
+ +
+

+ {% trans "Transfers with opened remittances" %} +

+ {% if special_transactions_with_remittance.data %} + {% render_table special_transactions_with_remittance %} + {% else %} +
+
+ {% trans "There is no transaction with an opened linked remittance." %} +
+
+ {% endif %} +
+ +
+

+ {% trans "Closed remittances" %} +

+ {% if closed_remittances.data %} + {% render_table closed_remittances %} + {% else %} +
+
+ {% trans "There is no closed remittance yet." %} +
+
+ {% endif %} +
+{% endblock %} \ No newline at end of file diff --git a/apps/treasury/templates/treasury/sogecredit_detail.html b/apps/treasury/templates/treasury/sogecredit_detail.html new file mode 100644 index 00000000..994aca08 --- /dev/null +++ b/apps/treasury/templates/treasury/sogecredit_detail.html @@ -0,0 +1,68 @@ +{% extends "base.html" %} +{% comment %} +SPDX-License-Identifier: GPL-3.0-or-later +{% endcomment %} +{% load i18n pretty_money perms %} + +{% block content %} +
+
+

{% trans "Credit from the Société générale" %}

+
+
+
+
{% trans 'user'|capfirst %}
+
{{ object.user }}
+ + {% if "note.view_note_balance"|has_perm:object.user.note %} +
{% trans 'balance'|capfirst %}
+
{{ object.user.note.balance|pretty_money }}
+ {% endif %} + +
{% trans 'transactions'|capfirst %}
+
+ {% for transaction in object.transactions.all %} + {{ transaction.membership.club }} ({{ transaction.amount|pretty_money }})
+ {% endfor %} +
+ +
{% trans 'total amount'|capfirst %}
+
{{ object.amount|pretty_money }}
+
+
+ +
+ {% trans 'Warning: Validating this credit implies that all membership transactions will be validated.' %} + {% trans 'If you delete this credit, there all membership transactions will be also validated, but no credit will be operated.' %} + {% trans "If this credit is validated, then the user won't be able to ask for a credit from the Société générale." %} + {% trans 'If you think there is an error, please contact the "respos info".' %} +
+ + +
+{% endblock %} \ No newline at end of file diff --git a/apps/treasury/templates/treasury/sogecredit_list.html b/apps/treasury/templates/treasury/sogecredit_list.html new file mode 100644 index 00000000..c3862811 --- /dev/null +++ b/apps/treasury/templates/treasury/sogecredit_list.html @@ -0,0 +1,77 @@ +{% extends "base.html" %} +{% comment %} +SPDX-License-Identifier: GPL-3.0-or-later +{% endcomment %} +{% load render_table from django_tables2 %} +{% load i18n %} + +{% block content %} + + +
+

+ {{ title }} +

+
+ +
+ +
+
+
+ {% if table.data %} + {% render_table table %} + {% else %} +
+
+ {% trans "There is no matched user that have asked for a Société générale credit." %} +
+
+ {% endif %} +
+
+{% endblock %} + +{% block extrajavascript %} + +{% endblock %} \ No newline at end of file diff --git a/apps/treasury/templates/treasury/specialtransactionproxy_form.html b/apps/treasury/templates/treasury/specialtransactionproxy_form.html new file mode 100644 index 00000000..5d80c904 --- /dev/null +++ b/apps/treasury/templates/treasury/specialtransactionproxy_form.html @@ -0,0 +1,16 @@ +{% extends "base.html" %} +{% comment %} +SPDX-License-Identifier: GPL-3.0-or-later +{% endcomment %} +{% load crispy_forms_tags %} + +{% block content %} +
+

+ {{ title }} +

+
+ {% crispy form %} +
+
+{% endblock %} \ No newline at end of file diff --git a/apps/treasury/templatetags/escape_tex.py b/apps/treasury/templatetags/escape_tex.py new file mode 100644 index 00000000..bd700943 --- /dev/null +++ b/apps/treasury/templatetags/escape_tex.py @@ -0,0 +1,23 @@ +# Copyright (C) 2018-2020 by BDE ENS Paris-Saclay +# SPDX-License-Identifier: GPL-3.0-or-later + +from django import template +from django.utils.safestring import mark_safe + + +def do_latex_escape(value): + return mark_safe( + value.replace("&", "\\&") + .replace("$", "\\$") + .replace("%", "\\%") + .replace("#", "\\#") + .replace("_", "\\_") + .replace("{", "\\{") + .replace("}", "\\}") + .replace("\n", "\\\\") + .replace("\r", "") + ) + + +register = template.Library() +register.filter("escape_tex", do_latex_escape) diff --git a/apps/treasury/urls.py b/apps/treasury/urls.py index 8606fb5b..e7c09639 100644 --- a/apps/treasury/urls.py +++ b/apps/treasury/urls.py @@ -3,9 +3,9 @@ from django.urls import path -from .views import InvoiceCreateView, InvoiceListView, InvoiceUpdateView, InvoiceRenderView, RemittanceListView,\ - RemittanceCreateView, RemittanceUpdateView, LinkTransactionToRemittanceView, UnlinkTransactionToRemittanceView,\ - SogeCreditListView, SogeCreditManageView +from .views import InvoiceCreateView, InvoiceListView, InvoiceUpdateView, InvoiceDeleteView, InvoiceRenderView,\ + RemittanceListView, RemittanceCreateView, RemittanceUpdateView, LinkTransactionToRemittanceView,\ + UnlinkTransactionToRemittanceView, SogeCreditListView, SogeCreditManageView app_name = 'treasury' urlpatterns = [ @@ -13,6 +13,7 @@ urlpatterns = [ path('invoice/', InvoiceListView.as_view(), name='invoice_list'), path('invoice/create/', InvoiceCreateView.as_view(), name='invoice_create'), path('invoice//', InvoiceUpdateView.as_view(), name='invoice_update'), + path('invoice//delete/', InvoiceDeleteView.as_view(), name='invoice_delete'), path('invoice/render//', InvoiceRenderView.as_view(), name='invoice_render'), # Remittance app paths diff --git a/apps/treasury/views.py b/apps/treasury/views.py index 22215254..c2265289 100644 --- a/apps/treasury/views.py +++ b/apps/treasury/views.py @@ -8,29 +8,28 @@ from tempfile import mkdtemp from crispy_forms.helper import FormHelper from django.contrib.auth.mixins import LoginRequiredMixin -from django.core.exceptions import ValidationError +from django.core.exceptions import ValidationError, PermissionDenied from django.db.models import Q from django.forms import Form from django.http import HttpResponse from django.shortcuts import redirect -from django.template.loader import render_to_string from django.urls import reverse_lazy from django.utils.translation import gettext_lazy as _ -from django.views.generic import CreateView, UpdateView, DetailView +from django.views.generic import UpdateView, DetailView from django.views.generic.base import View, TemplateView -from django.views.generic.edit import BaseFormView +from django.views.generic.edit import BaseFormView, DeleteView from django_tables2 import SingleTableView from note.models import SpecialTransaction, NoteSpecial, Alias from note_kfet.settings.base import BASE_DIR from permission.backends import PermissionBackend -from permission.views import ProtectQuerysetMixin +from permission.views import ProtectQuerysetMixin, ProtectedCreateView from .forms import InvoiceForm, ProductFormSet, ProductFormSetHelper, RemittanceForm, LinkTransactionToRemittanceForm from .models import Invoice, Product, Remittance, SpecialTransactionProxy, SogeCredit from .tables import InvoiceTable, RemittanceTable, SpecialTransactionTable, SogeCreditTable -class InvoiceCreateView(ProtectQuerysetMixin, LoginRequiredMixin, CreateView): +class InvoiceCreateView(ProtectQuerysetMixin, ProtectedCreateView): """ Create Invoice """ @@ -38,6 +37,15 @@ class InvoiceCreateView(ProtectQuerysetMixin, LoginRequiredMixin, CreateView): form_class = InvoiceForm extra_context = {"title": _("Create new invoice")} + def get_sample_object(self): + return Invoice( + id=0, + object="", + description="", + name="", + address="", + ) + def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) @@ -49,7 +57,6 @@ class InvoiceCreateView(ProtectQuerysetMixin, LoginRequiredMixin, CreateView): form_set = ProductFormSet(instance=form.instance) context['formset'] = form_set context['helper'] = ProductFormSetHelper() - context['no_cache'] = True return context @@ -73,7 +80,7 @@ class InvoiceCreateView(ProtectQuerysetMixin, LoginRequiredMixin, CreateView): return reverse_lazy('treasury:invoice_list') -class InvoiceListView(ProtectQuerysetMixin, LoginRequiredMixin, SingleTableView): +class InvoiceListView(LoginRequiredMixin, SingleTableView): """ List existing Invoices """ @@ -81,6 +88,22 @@ class InvoiceListView(ProtectQuerysetMixin, LoginRequiredMixin, SingleTableView) table_class = InvoiceTable extra_context = {"title": _("Invoices list")} + def dispatch(self, request, *args, **kwargs): + # Check that the user is authenticated + if not request.user.is_authenticated: + return self.handle_no_permission() + + sample_invoice = Invoice( + id=0, + object="", + description="", + name="", + address="", + ) + if not PermissionBackend.check_perm(self.request.user, "treasury.add_invoice", sample_invoice): + raise PermissionDenied(_("You are not able to see the treasury interface.")) + return super().dispatch(request, *args, **kwargs) + class InvoiceUpdateView(ProtectQuerysetMixin, LoginRequiredMixin, UpdateView): """ @@ -97,13 +120,17 @@ class InvoiceUpdateView(ProtectQuerysetMixin, LoginRequiredMixin, UpdateView): form.helper = FormHelper() # Remove form tag on the generation of the form in the template (already present on the template) form.helper.form_tag = False - # Fill the intial value for the date field, with the initial date of the model instance - form.fields['date'].initial = form.instance.date # The formset handles the set of the products - form_set = ProductFormSet(instance=form.instance) + form_set = ProductFormSet(instance=self.object) context['formset'] = form_set context['helper'] = ProductFormSetHelper() - context['no_cache'] = True + + if self.object.locked: + for field_name in form.fields: + form.fields[field_name].disabled = True + for f in form_set.forms: + for field_name in f.fields: + f.fields[field_name].disabled = True return context @@ -131,6 +158,17 @@ class InvoiceUpdateView(ProtectQuerysetMixin, LoginRequiredMixin, UpdateView): return reverse_lazy('treasury:invoice_list') +class InvoiceDeleteView(ProtectQuerysetMixin, LoginRequiredMixin, DeleteView): + """ + Delete a non-validated WEI registration + """ + model = Invoice + extra_context = {"title": _("Delete invoice")} + + def get_success_url(self): + return reverse_lazy('treasury:invoice_list') + + class InvoiceRenderView(LoginRequiredMixin, View): """ Render Invoice as a generated PDF with the given information and a LaTeX template @@ -139,24 +177,7 @@ class InvoiceRenderView(LoginRequiredMixin, View): def get(self, request, **kwargs): pk = kwargs["pk"] invoice = Invoice.objects.filter(PermissionBackend.filter_queryset(request.user, Invoice, "view")).get(pk=pk) - products = Product.objects.filter(invoice=invoice).all() - - # Informations of the BDE. Should be updated when the school will move. - invoice.place = "Cachan" - invoice.my_name = "BDE ENS Cachan" - invoice.my_address_street = "61 avenue du Président Wilson" - invoice.my_city = "94230 Cachan" - invoice.bank_code = 30003 - invoice.desk_code = 3894 - invoice.account_number = 37280662 - invoice.rib_key = 14 - invoice.bic = "SOGEFRPP" - - # Replace line breaks with the LaTeX equivalent - invoice.description = invoice.description.replace("\r", "").replace("\n", "\\\\ ") - invoice.address = invoice.address.replace("\r", "").replace("\n", "\\\\ ") - # Fill the template with the information - tex = render_to_string("treasury/invoice_sample.tex", dict(obj=invoice, products=products)) + tex = invoice.tex try: os.mkdir(BASE_DIR + "/tmp") @@ -196,7 +217,7 @@ class InvoiceRenderView(LoginRequiredMixin, View): return response -class RemittanceCreateView(ProtectQuerysetMixin, LoginRequiredMixin, CreateView): +class RemittanceCreateView(ProtectQuerysetMixin, ProtectedCreateView): """ Create Remittance """ @@ -204,6 +225,12 @@ class RemittanceCreateView(ProtectQuerysetMixin, LoginRequiredMixin, CreateView) form_class = RemittanceForm extra_context = {"title": _("Create a new remittance")} + def get_sample_object(self): + return Remittance( + remittance_type_id=1, + comment="", + ) + def get_success_url(self): return reverse_lazy('treasury:remittance_list') @@ -225,6 +252,19 @@ class RemittanceListView(LoginRequiredMixin, TemplateView): template_name = "treasury/remittance_list.html" extra_context = {"title": _("Remittances list")} + def dispatch(self, request, *args, **kwargs): + # Check that the user is authenticated + if not request.user.is_authenticated: + return self.handle_no_permission() + + sample_remittance = Remittance( + remittance_type_id=1, + comment="", + ) + if not PermissionBackend.check_perm(self.request.user, "treasury.add_remittance", sample_remittance): + raise PermissionDenied(_("You are not able to see the treasury interface.")) + return super().dispatch(request, *args, **kwargs) + def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) @@ -238,7 +278,7 @@ class RemittanceListView(LoginRequiredMixin, TemplateView): closed_remittances = RemittanceTable( data=Remittance.objects.filter(closed=True).filter( - PermissionBackend.filter_queryset(self.request.user, Remittance, "view")).reverse().all(), + PermissionBackend.filter_queryset(self.request.user, Remittance, "view")).all(), prefix="closed-remittances-", ) closed_remittances.paginate(page=self.request.GET.get("closed-remittances-page", 1), per_page=10) @@ -281,8 +321,6 @@ class RemittanceUpdateView(ProtectQuerysetMixin, LoginRequiredMixin, UpdateView) def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) - context["table"] = RemittanceTable(data=Remittance.objects.filter( - PermissionBackend.filter_queryset(self.request.user, Remittance, "view")).all()) data = SpecialTransaction.objects.filter(specialtransactionproxy__remittance=self.object).filter( PermissionBackend.filter_queryset(self.request.user, Remittance, "view")).all() context["special_transactions"] = SpecialTransactionTable( @@ -344,6 +382,15 @@ class SogeCreditListView(LoginRequiredMixin, ProtectQuerysetMixin, SingleTableVi table_class = SogeCreditTable extra_context = {"title": _("List of credits from the Société générale")} + def dispatch(self, request, *args, **kwargs): + # Check that the user is authenticated + if not request.user.is_authenticated: + return self.handle_no_permission() + + if not self.get_queryset().exists(): + raise PermissionDenied(_("You are not able to see the treasury interface.")) + return super().dispatch(request, *args, **kwargs) + def get_queryset(self, **kwargs): """ Filter the table with the given parameter. @@ -353,18 +400,13 @@ class SogeCreditListView(LoginRequiredMixin, ProtectQuerysetMixin, SingleTableVi qs = super().get_queryset() if "search" in self.request.GET: pattern = self.request.GET["search"] - - if not pattern: - return qs.none() - - qs = qs.filter( - Q(user__first_name__iregex=pattern) - | Q(user__last_name__iregex=pattern) - | Q(user__note__alias__name__iregex="^" + pattern) - | Q(user__note__alias__normalized_name__iregex="^" + Alias.normalize(pattern)) - ) - else: - qs = qs.none() + if pattern: + qs = qs.filter( + Q(user__first_name__iregex=pattern) + | Q(user__last_name__iregex=pattern) + | Q(user__note__alias__name__iregex="^" + pattern) + | Q(user__note__alias__normalized_name__iregex="^" + Alias.normalize(pattern)) + ) if "valid" in self.request.GET: q = Q(credit_transaction=None) diff --git a/apps/wei/forms/registration.py b/apps/wei/forms/registration.py index 9ce3a350..0e00705a 100644 --- a/apps/wei/forms/registration.py +++ b/apps/wei/forms/registration.py @@ -4,7 +4,9 @@ from django import forms from django.contrib.auth.models import User from django.db.models import Q +from django.forms import CheckboxSelectMultiple from django.utils.translation import gettext_lazy as _ +from note.models import NoteSpecial from note_kfet.inputs import AmountInput, DatePickerInput, Autocomplete, ColorWidget from ..models import WEIClub, WEIRegistration, Bus, BusTeam, WEIMembership, WEIRole @@ -37,7 +39,9 @@ class WEIRegistrationForm(forms.ModelForm): 'placeholder': 'Nom ...', }, ), - "birth_date": DatePickerInput(), + "birth_date": DatePickerInput(options={'defaultDate': '2000-01-01', + 'minDate': '1900-01-01', + 'maxDate': '2100-01-01'}), } @@ -47,6 +51,7 @@ class WEIChooseBusForm(forms.Form): label=_("bus"), help_text=_("This choice is not definitive. The WEI organizers are free to attribute for you a bus and a team," + " in particular if you are a free eletron."), + widget=CheckboxSelectMultiple(), ) team = forms.ModelMultipleChoiceField( @@ -54,17 +59,53 @@ class WEIChooseBusForm(forms.Form): label=_("Team"), required=False, help_text=_("Leave this field empty if you won't be in a team (staff, bus chief, free electron)"), + widget=CheckboxSelectMultiple(), ) roles = forms.ModelMultipleChoiceField( queryset=WEIRole.objects.filter(~Q(name="1A")), label=_("WEI Roles"), help_text=_("Select the roles that you are interested in."), + initial=WEIRole.objects.filter(name="Adhérent WEI").all(), + widget=CheckboxSelectMultiple(), ) class WEIMembershipForm(forms.ModelForm): - roles = forms.ModelMultipleChoiceField(queryset=WEIRole.objects, label=_("WEI Roles")) + roles = forms.ModelMultipleChoiceField( + queryset=WEIRole.objects, + label=_("WEI Roles"), + widget=CheckboxSelectMultiple(), + ) + + credit_type = forms.ModelChoiceField( + queryset=NoteSpecial.objects.all(), + label=_("Credit type"), + empty_label=_("No credit"), + required=False, + ) + + credit_amount = forms.IntegerField( + label=_("Credit amount"), + widget=AmountInput(), + initial=0, + required=False, + ) + + last_name = forms.CharField( + label=_("Last name"), + required=False, + ) + + first_name = forms.CharField( + label=_("First name"), + required=False, + ) + + bank = forms.CharField( + label=_("Bank"), + required=False, + ) def clean(self): cleaned_data = super().clean() @@ -88,7 +129,8 @@ class WEIMembershipForm(forms.ModelForm): attrs={ 'api_url': '/api/wei/team/', 'placeholder': 'Équipe ...', - } + }, + resetable=True, ), } diff --git a/apps/wei/forms/surveys/base.py b/apps/wei/forms/surveys/base.py index f43dafc2..e8b0cbba 100644 --- a/apps/wei/forms/surveys/base.py +++ b/apps/wei/forms/surveys/base.py @@ -25,9 +25,7 @@ class WEISurveyInformation: If the algorithm ran, return the prefered bus according to the survey. In the other case, return None. """ - if not self.valid: - return None - return Bus.objects.get(pk=self.selected_bus_pk) + return Bus.objects.get(pk=self.selected_bus_pk) if self.valid else None def save(self, registration) -> None: """ @@ -44,6 +42,13 @@ class WEIBusInformation: def __init__(self, bus: Bus): self.__dict__.update(bus.information) self.bus = bus + self.save() + + def save(self): + d = self.__dict__.copy() + d.pop("bus") + self.bus.information = d + self.bus.save() class WEISurveyAlgorithm: diff --git a/apps/wei/forms/surveys/wei2020.py b/apps/wei/forms/surveys/wei2020.py index 4f60f6d4..df528e1b 100644 --- a/apps/wei/forms/surveys/wei2020.py +++ b/apps/wei/forms/surveys/wei2020.py @@ -1,27 +1,56 @@ # Copyright (C) 2018-2020 by BDE ENS Paris-Saclay # SPDX-License-Identifier: GPL-3.0-or-later -from django import forms +from random import choice -from .base import WEISurvey, WEISurveyInformation, WEISurveyAlgorithm +from django import forms +from django.utils.translation import gettext_lazy as _ + +from .base import WEISurvey, WEISurveyInformation, WEISurveyAlgorithm, WEIBusInformation from ...models import Bus +# TODO: Use new words +WORDS = ['Rap', 'Retro', 'DJ', 'Rock', 'Jazz', 'Chansons Populaires', 'Chansons Paillardes', 'Pop', 'Fanfare', + 'Biere', 'Pastis', 'Vodka', 'Cocktails', 'Eau', 'Sirop', 'Jus de fruit', 'Binge Drinking', 'Rhum', + 'Eau de vie', 'Apéro', 'Morning beer', 'Huit-six', 'Jeux de societé', 'Jeux de cartes', 'Danse', 'Karaoké', + 'Bière Pong', 'Poker', 'Loup Garou', 'Films', "Jeux d'alcool", 'Sport', 'Rangées de cul', 'Chips', 'BBQ', + 'Kebab', 'Saucisse', 'Vegan', 'Vege', 'LGBTIQ+', 'Dab', 'Solitaire', 'Séducteur', 'Sociale', 'Chanteur', + 'Se lacher', 'Chill', 'Débile', 'Beauf', 'Bon enfant'] + + class WEISurveyForm2020(forms.Form): """ Survey form for the year 2020. - For now, that's only a Bus selector. - TODO: Do a better survey (later) + Members choose 20 words, from which we calculate the best associated bus. """ - bus = forms.ModelChoiceField( - Bus.objects, + + word = forms.ChoiceField( + label=_("Choose a word:"), + widget=forms.RadioSelect(), ) def set_registration(self, registration): """ Filter the bus selector with the buses of the current WEI. """ - self.fields["bus"].queryset = Bus.objects.filter(wei=registration.wei) + words = [choice(WORDS) for _ in range(10)] + words = [(w, w) for w in words] + if self.data: + self.fields["word"].choices = [(w, w) for w in WORDS] + if self.is_valid(): + return + self.fields["word"].choices = words + + +class WEIBusInformation2020(WEIBusInformation): + """ + For each word, the bus has a score + """ + def __init__(self, bus): + for word in WORDS: + setattr(self, word, 0.0) + super().__init__(bus) class WEISurveyInformation2020(WEISurveyInformation): @@ -29,14 +58,19 @@ class WEISurveyInformation2020(WEISurveyInformation): We store the id of the selected bus. We store only the name, but is not used in the selection: that's only for humans that try to read data. """ - chosen_bus_pk = None - chosen_bus_name = None + step = 0 + + def __init__(self, registration): + for i in range(1, 21): + setattr(self, "word" + str(i), None) + super().__init__(registration) class WEISurvey2020(WEISurvey): """ Survey for the year 2020. """ + @classmethod def get_year(cls): return 2020 @@ -55,9 +89,9 @@ class WEISurvey2020(WEISurvey): form.set_registration(self.registration) def form_valid(self, form): - bus = form.cleaned_data["bus"] - self.information.chosen_bus_pk = bus.pk - self.information.chosen_bus_name = bus.name + word = form.cleaned_data["word"] + self.information.step += 1 + setattr(self.information, "word" + str(self.information.step), word) self.save() @classmethod @@ -68,7 +102,7 @@ class WEISurvey2020(WEISurvey): """ The survey is complete once the bus is chosen. """ - return self.information.chosen_bus_pk is not None + return self.information.step == 20 class WEISurveyAlgorithm2020(WEISurveyAlgorithm): @@ -82,8 +116,12 @@ class WEISurveyAlgorithm2020(WEISurveyAlgorithm): def get_survey_class(cls): return WEISurvey2020 + @classmethod + def get_bus_information_class(cls): + return WEIBusInformation2020 + def run_algorithm(self): for registration in self.get_registrations(): survey = self.get_survey_class()(registration) - survey.select_bus(Bus.objects.get(pk=survey.information.chosen_bus_pk)) + survey.select_bus(choice(Bus.objects.all())) survey.save() diff --git a/apps/wei/management/commands/extract_ml_registrations.py b/apps/wei/management/commands/extract_ml_registrations.py index b67bf10b..9bc82418 100644 --- a/apps/wei/management/commands/extract_ml_registrations.py +++ b/apps/wei/management/commands/extract_ml_registrations.py @@ -21,6 +21,13 @@ class Command(BaseCommand): help='Select the year of the concerned WEI. Default: last year') def handle(self, *args, **options): + ########################################################### + # WARNING # + ########################################################### + # + # This code is obsolete. + # TODO: Improve the mailing list extraction system, and link it automatically with Mailman. + if options["type"] == "members": for membership in Membership.objects.filter( club__name="BDE", diff --git a/apps/wei/models.py b/apps/wei/models.py index df353338..46d9383f 100644 --- a/apps/wei/models.py +++ b/apps/wei/models.py @@ -8,6 +8,7 @@ from django.conf import settings from django.contrib.auth.models import User from django.db import models from django.utils.translation import gettext_lazy as _ +from phonenumber_field.modelfields import PhoneNumberField from member.models import Club, Membership from note.models import MembershipTransaction from permission.models import Role @@ -223,26 +224,11 @@ class WEIRegistration(models.Model): verbose_name=_("emergency contact name"), ) - emergency_contact_phone = models.CharField( + emergency_contact_phone = PhoneNumberField( max_length=32, verbose_name=_("emergency contact phone"), ) - ml_events_registration = models.BooleanField( - default=False, - verbose_name=_("Register on the mailing list to stay informed of the events of the campus (1 mail/week)"), - ) - - ml_sport_registration = models.BooleanField( - default=False, - verbose_name=_("Register on the mailing list to stay informed of the sport events of the campus (1 mail/week)"), - ) - - ml_art_registration = models.BooleanField( - default=False, - verbose_name=_("Register on the mailing list to stay informed of the art events of the campus (1 mail/week)"), - ) - first_year = models.BooleanField( default=False, verbose_name=_("first year"), diff --git a/apps/wei/tables.py b/apps/wei/tables.py index 41c35a47..f0a7868e 100644 --- a/apps/wei/tables.py +++ b/apps/wei/tables.py @@ -1,10 +1,15 @@ # Copyright (C) 2018-2020 by BDE ENS Paris-Saclay # SPDX-License-Identifier: GPL-3.0-or-later +from datetime import date + import django_tables2 as tables from django.urls import reverse_lazy +from django.utils.html import format_html from django.utils.translation import gettext_lazy as _ from django_tables2 import A +from note_kfet.middlewares import get_current_authenticated_user +from permission.backends import PermissionBackend from .models import WEIClub, WEIRegistration, Bus, BusTeam, WEIMembership @@ -33,7 +38,7 @@ class WEIRegistrationTable(tables.Table): """ user = tables.LinkColumn( 'member:user_detail', - args=[A('user.pk')], + args=[A('user__pk')], ) edit = tables.LinkColumn( @@ -43,7 +48,8 @@ class WEIRegistrationTable(tables.Table): text=_("Edit"), attrs={ 'a': { - 'class': 'btn btn-warning' + 'class': 'btn btn-warning', + 'data-turbolinks': 'false', } } ) @@ -53,8 +59,12 @@ class WEIRegistrationTable(tables.Table): verbose_name=_("Validate"), text=_("Validate"), attrs={ + 'th': { + 'id': 'validate-membership-header' + }, 'a': { - 'class': 'btn btn-success' + 'class': 'btn btn-success', + 'data-type': 'validate-membership' } } ) @@ -65,19 +75,40 @@ class WEIRegistrationTable(tables.Table): verbose_name=_("delete"), text=_("Delete"), attrs={ + 'th': { + 'id': 'delete-membership-header' + }, 'a': { - 'class': 'btn btn-danger' + 'class': 'btn btn-danger', + 'data-type': 'delete-membership' } }, ) + def render_validate(self, record): + hasperm = PermissionBackend.check_perm( + get_current_authenticated_user(), "wei.add_weimembership", WEIMembership( + club=record.wei, + user=record.user, + date_start=date.today(), + date_end=date.today(), + fee=0, + registration=record, + ) + ) + return _("Validate") if hasperm else format_html("") + + def render_delete(self, record): + hasperm = PermissionBackend.check_perm(get_current_authenticated_user(), "wei.delete_weimembership", record) + return _("Delete") if hasperm else format_html("") + class Meta: attrs = { 'class': 'table table-condensed table-striped table-hover' } model = WEIRegistration template_name = 'django_tables2/bootstrap4.html' - fields = ('user', 'user.first_name', 'user.last_name', 'first_year',) + fields = ('user', 'user__first_name', 'user__last_name', 'first_year',) row_attrs = { 'class': 'table-row', 'id': lambda record: "row-" + str(record.pk), @@ -88,7 +119,7 @@ class WEIRegistrationTable(tables.Table): class WEIMembershipTable(tables.Table): user = tables.LinkColumn( 'wei:wei_update_registration', - args=[A('registration.pk')], + args=[A('registration__pk')], ) year = tables.Column( @@ -98,12 +129,12 @@ class WEIMembershipTable(tables.Table): bus = tables.LinkColumn( 'wei:manage_bus', - args=[A('bus.pk')], + args=[A('bus__pk')], ) team = tables.LinkColumn( 'wei:manage_bus_team', - args=[A('team.pk')], + args=[A('team__pk')], ) def render_year(self, record): @@ -115,7 +146,7 @@ class WEIMembershipTable(tables.Table): } model = WEIMembership template_name = 'django_tables2/bootstrap4.html' - fields = ('user', 'user.last_name', 'user.first_name', 'registration.gender', 'user.profile.department', + fields = ('user', 'user__last_name', 'user__first_name', 'registration__gender', 'user__profile__department', 'year', 'bus', 'team', ) row_attrs = { 'class': 'table-row', diff --git a/apps/wei/templates/wei/base.html b/apps/wei/templates/wei/base.html new file mode 100644 index 00000000..a6521bd2 --- /dev/null +++ b/apps/wei/templates/wei/base.html @@ -0,0 +1,109 @@ +{% extends "base.html" %} +{% comment %} +SPDX-License-Identifier: GPL-3.0-or-later +{% endcomment %} +{% load i18n pretty_money perms %} + +{# Use a fluid-width container #} +{% block containertype %}container-fluid{% endblock %} + +{% block content %} +
+
+ {% block profile_info %} + {% if club %} +
+

+ {{ club.name }} +

+
+ + + +
+
+
+
{% trans 'name'|capfirst %}
+
{{ club.name }}
+ + {% if club.require_memberships %} +
{% trans 'date start'|capfirst %}
+
{{ club.date_start }}
+ +
{% trans 'date end'|capfirst %}
+
{{ club.date_end }}
+ +
{% trans 'year'|capfirst %}
+
{{ club.year }}
+ + {% if club.membership_fee_paid == club.membership_fee_unpaid %} +
{% trans 'membership fee'|capfirst %}
+
{{ club.membership_fee_paid|pretty_money }}
+ {% else %} + {% with bde_kfet_fee=club.parent_club.membership_fee_paid|add:club.parent_club.parent_club.membership_fee_paid %} +
{% trans 'WEI fee (paid students)'|capfirst %}
+
{{ club.membership_fee_paid|add:bde_kfet_fee|pretty_money }} +
+ {% endwith %} + + {% with bde_kfet_fee=club.parent_club.membership_fee_unpaid|add:club.parent_club.parent_club.membership_fee_unpaid %} +
{% trans 'WEI fee (unpaid students)'|capfirst %}
+
{{ club.membership_fee_unpaid|add:bde_kfet_fee|pretty_money }} +
+ {% endwith %} + {% endif %} + {% endif %} + + {% if "note.view_note"|has_perm:club.note %} +
{% trans 'balance'|capfirst %}
+
{{ club.note.balance | pretty_money }}
+ {% endif %} + + {% if "note.change_alias"|has_perm:club.note.alias_set.first %} +
{% trans 'aliases'|capfirst %}
+
{{ club.note.alias_set.all|join:", " }}
+ {% endif %} + +
{% trans 'email'|capfirst %}
+
{{ club.email }}
+
+
+ +
+ {% endif %} + {% endblock %} +
+
+ {% block profile_content %}{% endblock %} +
+
+{% endblock %} diff --git a/apps/wei/templates/wei/bus_detail.html b/apps/wei/templates/wei/bus_detail.html new file mode 100644 index 00000000..c8f3ce20 --- /dev/null +++ b/apps/wei/templates/wei/bus_detail.html @@ -0,0 +1,57 @@ +{% extends "wei/base.html" %} +{% comment %} +SPDX-License-Identifier: GPL-3.0-or-later +{% endcomment %} +{% load render_table from django_tables2 %} +{% load i18n %} + +{% block profile_content %} +
+
+

{{ object.name }}

+
+ +
+ {{ object.description }} +
+ + +
+ +
+ +{% if teams.data %} +
+ + {% render_table teams %} +
+ +
+{% endif %} + +{% if memberships.data %} +
+ + {% render_table memberships %} +
+ +
+ + + + +{% endif %} +{% endblock %} \ No newline at end of file diff --git a/apps/wei/templates/wei/bus_form.html b/apps/wei/templates/wei/bus_form.html new file mode 100644 index 00000000..c62fec40 --- /dev/null +++ b/apps/wei/templates/wei/bus_form.html @@ -0,0 +1,21 @@ +{% extends "wei/base.html" %} +{% comment %} +SPDX-License-Identifier: GPL-3.0-or-later +{% endcomment %} +{% load crispy_forms_tags %} +{% load i18n %} + +{% block profile_content %} +
+

+ {{ title }} +

+
+
+ {% csrf_token %} + {{ form|crispy }} + +
+
+
+{% endblock %} \ No newline at end of file diff --git a/apps/wei/templates/wei/busteam_detail.html b/apps/wei/templates/wei/busteam_detail.html new file mode 100644 index 00000000..27348d03 --- /dev/null +++ b/apps/wei/templates/wei/busteam_detail.html @@ -0,0 +1,63 @@ +{% extends "wei/base.html" %} +{% comment %} +SPDX-License-Identifier: GPL-3.0-or-later +{% endcomment %} +{% load render_table from django_tables2 %} +{% load i18n %} + +{% block profile_content %} +
+
+

{{ bus.name }}

+
+ +
+ {{ bus.description }} +
+ + +
+ +
+ +
+
+

{{ object.name }}

+
+ +
+ {{ object.description }} +
+ + +
+ +
+ +{% if memberships.data or True %} +
+ + {% render_table memberships %} +
+ +
+ + + + +{% endif %} +{% endblock %} \ No newline at end of file diff --git a/apps/wei/templates/wei/busteam_form.html b/apps/wei/templates/wei/busteam_form.html new file mode 100644 index 00000000..c62fec40 --- /dev/null +++ b/apps/wei/templates/wei/busteam_form.html @@ -0,0 +1,21 @@ +{% extends "wei/base.html" %} +{% comment %} +SPDX-License-Identifier: GPL-3.0-or-later +{% endcomment %} +{% load crispy_forms_tags %} +{% load i18n %} + +{% block profile_content %} +
+

+ {{ title }} +

+
+
+ {% csrf_token %} + {{ form|crispy }} + +
+
+
+{% endblock %} \ No newline at end of file diff --git a/templates/wei/survey.html b/apps/wei/templates/wei/survey.html similarity index 85% rename from templates/wei/survey.html rename to apps/wei/templates/wei/survey.html index 36553849..9eabab59 100644 --- a/templates/wei/survey.html +++ b/apps/wei/templates/wei/survey.html @@ -1,11 +1,10 @@ -{% extends "member/noteowner_detail.html" %} +{% extends "wei/base.html" %} +{% comment %} +SPDX-License-Identifier: GPL-3.0-or-later +{% endcomment %} {% load i18n %} {% load crispy_forms_tags %} -{% block profile_info %} -{% include "wei/weiclub_info.html" %} -{% endblock %} - {% block profile_content %}
diff --git a/templates/wei/survey_closed.html b/apps/wei/templates/wei/survey_closed.html similarity index 82% rename from templates/wei/survey_closed.html rename to apps/wei/templates/wei/survey_closed.html index 28c182ef..aac9e833 100644 --- a/templates/wei/survey_closed.html +++ b/apps/wei/templates/wei/survey_closed.html @@ -1,11 +1,10 @@ -{% extends "member/noteowner_detail.html" %} +{% extends "wei/base.html" %} +{% comment %} +SPDX-License-Identifier: GPL-3.0-or-later +{% endcomment %} {% load i18n %} {% load crispy_forms_tags %} -{% block profile_info %} -{% include "wei/weiclub_info.html" %} -{% endblock %} - {% block profile_content %}
diff --git a/templates/wei/survey_end.html b/apps/wei/templates/wei/survey_end.html similarity index 76% rename from templates/wei/survey_end.html rename to apps/wei/templates/wei/survey_end.html index 888290f7..3152f6e1 100644 --- a/templates/wei/survey_end.html +++ b/apps/wei/templates/wei/survey_end.html @@ -1,11 +1,10 @@ -{% extends "member/noteowner_detail.html" %} +{% extends "wei/base.html" %} +{% comment %} +SPDX-License-Identifier: GPL-3.0-or-later +{% endcomment %} {% load i18n %} {% load crispy_forms_tags %} -{% block profile_info %} -{% include "wei/weiclub_info.html" %} -{% endblock %} - {% block profile_content %}
diff --git a/apps/wei/templates/wei/weiclub_detail.html b/apps/wei/templates/wei/weiclub_detail.html new file mode 100644 index 00000000..40786add --- /dev/null +++ b/apps/wei/templates/wei/weiclub_detail.html @@ -0,0 +1,118 @@ +{% extends "wei/base.html" %} +{% comment %} +SPDX-License-Identifier: GPL-3.0-or-later +{% endcomment %} +{% load render_table from django_tables2 %} +{% load i18n perms %} + +{% block profile_content %} +
+
+

Week-End d'Intégration

+
+
+

+ Le WEI (Week-End d’Intégration), ou 3 jours d’immersion dans les profondeurs du + monde post-préparatoire. +

+

+ Que serait une école sans son week-end d’intégration ? Quelques semaines après la + rentrée, on embarque tous et toutes à bord de bus à thèmes pour quelques jours + inoubliables dans une destination inconnue. L’objectif de ce week-end : permettre aux + nouvel·les arrivant·es de se lâcher après 2 ans de dur labeur (voire 3 pour les plus + chanceux), de découvrir l’ambiance familiale de l’ENS ainsi que de nouer des liens avec + ceux·elles qu’ils côtoieront par la suite. Dose de chants et de fun garantie ! +

+
+ {% if club.is_current_wei %} + + {% endif %} +
+ +{% if buses.data %} +
+
+ + {% trans "Buses" %} + +
+ {% render_table buses %} +
+{% endif %} + +{% if member_list.data %} +
+ + {% render_table member_list %} +
+{% endif %} + +{% if history_list.data %} +
+ +
+ {% render_table history_list %} +
+
+{% endif %} + +{% if pre_registrations.data %} +
+ +
+ {% render_table pre_registrations %} +
+
+{% endif %} +{% endblock %} + +{% block extrajavascript %} + +{% endblock %} \ No newline at end of file diff --git a/apps/wei/templates/wei/weiclub_form.html b/apps/wei/templates/wei/weiclub_form.html new file mode 100644 index 00000000..c62fec40 --- /dev/null +++ b/apps/wei/templates/wei/weiclub_form.html @@ -0,0 +1,21 @@ +{% extends "wei/base.html" %} +{% comment %} +SPDX-License-Identifier: GPL-3.0-or-later +{% endcomment %} +{% load crispy_forms_tags %} +{% load i18n %} + +{% block profile_content %} +
+

+ {{ title }} +

+
+
+ {% csrf_token %} + {{ form|crispy }} + +
+
+
+{% endblock %} \ No newline at end of file diff --git a/templates/wei/weiclub_list.html b/apps/wei/templates/wei/weiclub_list.html similarity index 96% rename from templates/wei/weiclub_list.html rename to apps/wei/templates/wei/weiclub_list.html index 2739e5bf..1202a667 100644 --- a/templates/wei/weiclub_list.html +++ b/apps/wei/templates/wei/weiclub_list.html @@ -1,6 +1,10 @@ {% extends "base.html" %} +{% comment %} +SPDX-License-Identifier: GPL-3.0-or-later +{% endcomment %} {% load render_table from django_tables2 %} {% load i18n %} + {% block content %}
diff --git a/templates/wei/weilist_sample.tex b/apps/wei/templates/wei/weilist_sample.tex similarity index 100% rename from templates/wei/weilist_sample.tex rename to apps/wei/templates/wei/weilist_sample.tex diff --git a/templates/wei/weimembership_form.html b/apps/wei/templates/wei/weimembership_form.html similarity index 84% rename from templates/wei/weimembership_form.html rename to apps/wei/templates/wei/weimembership_form.html index d33c3de7..1225175d 100644 --- a/templates/wei/weimembership_form.html +++ b/apps/wei/templates/wei/weimembership_form.html @@ -1,12 +1,8 @@ -{% extends "member/noteowner_detail.html" %} -{% load crispy_forms_tags %} -{% load i18n %} -{% load pretty_money %} -{% load perms %} - -{% block profile_info %} - {% include "wei/weiclub_info.html" %} -{% endblock %} +{% extends "wei/base.html" %} +{% comment %} +SPDX-License-Identifier: GPL-3.0-or-later +{% endcomment %} +{% load i18n crispy_forms_tags pretty_money perms %} {% block profile_content %}
@@ -77,15 +73,6 @@
{% trans 'emergency contact phone'|capfirst %}
{{ registration.emergency_contact_phone }}
-
{% trans 'Register on the mailing list to stay informed of the events of the campus (1 mail/week)' %}
-
{{ registration.ml_events_registration|yesno }}
- -
{% trans 'Register on the mailing list to stay informed of the sport events of the campus (1 mail/week)' %}
-
{{ registration.ml_sport_registration|yesno }}
- -
{% trans 'Register on the mailing list to stay informed of the art events of the campus (1 mail/week)' %}
-
{{ registration.ml_art_registration|yesno }}
-
{% trans 'Payment from Société générale' %}
{{ registration.soge_credit|yesno }}
@@ -125,7 +112,7 @@