mirror of
https://github.com/ae-utbm/sith.git
synced 2024-11-18 12:13:24 +00:00
b9298792ae
* integration of 3D secure v2 for eboutic bank payment * edit yml to avoid git conflict when deploying on test * escape html characters on xml (#505) * Change country id to ISO 3166 1 numeric for 3DSV2 (#510) * remove useless tests * Fix le panier de l'Eboutic pour Safari (#518) Co-authored-by: Théo DURR <git@theodurr.fr> Co-authored-by: thomas girod <56346771+imperosol@users.noreply.github.com> * update some dependencies (#523) * [Eboutic] Fix double quote issue & improved user experience on small screen (#522) * Fix #511 Regex issue with escaped double quotes * Fix basket being when reloading the page (when cookie != "") + Added JSDoc + Cleaned some code * Fix #509 Improved user experience on small screens * Fix css class not being added back when reloading page * CSS Fixes (see description) + Fixed overlaping item title with the cart emoji on small screen + Fixed minimal size of the basket on small screen (full width) * Added darkened background circle to items with no image * Fix issue were the basket could be None * Edited CSS to have bette img ratio & the 🛒 icon Adapt, Improve, Overcome * Moved basket down on small screen size * enhance admin pages * update documentation * Update doc/about/tech.rst Co-authored-by: Julien Constant <49886317+Juknum@users.noreply.github.com> * remove csrf_token * Fix 3DSv2 implementation (#542) * Fixed wrong HMAC signature generation * Fix xml du panier Co-authored-by: Julien Constant <julienconstant190@gmail.com> * [FIX] 3DSv2 - Echappement du XML et modif tables (#543) * Fixed wrong HMAC signature generation * Updated migration files Co-authored-by: Julien Constant <julienconstant190@gmail.com> * Update doc/about/tech.rst * Update doc/start/install.rst * Updated lock file according to pyproject * unify account_id creation * upgrade re_path to path (#533) * redirect directly on counter if user is barman * Passage de vue à Alpine pour les comptoirs (#561) Vue, c'est cool, mais avec Django c'est un peu chiant à utiliser. Alpine a l'avantage d'être plus léger et d'avoir une syntaxe qui ne ressemble pas à celle de Jinja (ce qui évite d'avoir à mettre des {% raw %} partout). * resolved importError (#565) * Add galaxy (#562) * style.scss: lint * style.scss: add 'th' padding * core: populate: add much more data for development * Add galaxy * repair user merging tool (#498) * Disabled galaxy feature (only visually) * Disabled Galaxy button & Removed 404 exception display * Update 404.jinja * Fixed broken test * Added eurocks links to eboutic * fix typo * fix wording Co-authored-by: Théo DURR <git@theodurr.fr> * Edited unit tests This test caused a breach in security due to the alert block displaying sensitive data. * Repair NaN bug for autocomplete on counter click * remove-useless-queries-counter-stats (#519) * Amélioration des pages utilisateurs pour les petits écrans (#578, #520) - Refonte de l'organisation des pages utilisateurs (principalement du front) - Page des parrains/fillots - Page d'édition du profil - Page du profil - Page des outils - Page des préférences - Page des stats utilisateurs - Refonte du CSS / organisation de la navbar principale (en haut de l'écran) - Refonte du CSS de la navbar bleu clair (le menu) - Refonte du CSS du SAS : - Page de photo - Page d'albums * Added GA/Clubs Google Calendar to main page (#585) * Added GA/Clubs google calendar to main page * Made tables full width * Create dependabot.yml (#587) * Bump django from 3.2.16 to 3.2.18 (#574) * [CSS] Follow up of #578 (#589) * [FIX] Broken link in readme and license fix (& update) (#591) * Fixes pour la mise à jour de mars (#598) * Fix problème de cache dans le SAS & améliore le CSS du SAS Co-authored-by: Bartuccio Antoine <klmp200@users.noreply.github.com> * Fixes & améliorations du nouveau CSS (#616) * [UPDATE] Bump sentry-sdk from 1.12.1 to 1.19.1 (#620) * [FIX] Fixes supplémentaires pour la màj de mars (#622) - Les photos de l'onglet de la page utilisateur utilise désormais leur version thumbnail au lieu de leur version HD - Une des classes du CSS du SAS a été renommée car elle empiétait sur une class de la navbar - Le profil utilisateur a été revu pour ajouter plus d'espacement entre le tableau des cotisations et le numéro de cotisants - Les images de forum & blouse sont de nouveau cliquable pour les afficher en grands - Sur mobile, lorsqu'on cliquait sur le premier élément de la navbar, ce dernier avait un overlay avec des angles arrondis - Sur mobile, les utilisateurs avec des images de profils non carrées dépassait dans l'onglet Famille * [UPDATE] Bump dict2xml from 1.7.2 to 1.7.3 (#592) Bumps [dict2xml](https://github.com/delfick/python-dict2xml) from 1.7.2 to 1.7.3. - [Release notes](https://github.com/delfick/python-dict2xml/releases) - [Commits](https://github.com/delfick/python-dict2xml/compare/release-1.7.2...release-1.7.3) --- updated-dependencies: - dependency-name: dict2xml dependency-type: direct:production update-type: version-update:semver-patch ... * [UPDATE] Bump django-debug-toolbar from 3.8.1 to 4.0.0 (#593) Bumps [django-debug-toolbar](https://github.com/jazzband/django-debug-toolbar) from 3.8.1 to 4.0.0. - [Release notes](https://github.com/jazzband/django-debug-toolbar/releases) - [Changelog](https://github.com/jazzband/django-debug-toolbar/blob/main/docs/changes.rst) - [Commits](https://github.com/jazzband/django-debug-toolbar/compare/3.8.1...4.0.0) --- updated-dependencies: - dependency-name: django-debug-toolbar dependency-type: direct:development update-type: version-update:semver-major ... Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> * [UPDATE] Bump cryptography from 37.0.4 to 40.0.1 (#594) * [UPDATE] Bump cryptography from 37.0.4 to 40.0.1 Bumps [cryptography](https://github.com/pyca/cryptography) from 37.0.4 to 40.0.1. - [Release notes](https://github.com/pyca/cryptography/releases) - [Changelog](https://github.com/pyca/cryptography/blob/main/CHANGELOG.rst) - [Commits](https://github.com/pyca/cryptography/compare/37.0.4...40.0.1) --- updated-dependencies: - dependency-name: cryptography dependency-type: direct:production update-type: version-update:semver-major ... Signed-off-by: dependabot[bot] <support@github.com> * Updated pyOpenSSL to match cryptography requirements --------- Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: Julien Constant <julienconstant190@gmail.com> * Mise à jour de Black vers la version 23.3 (#629) * update link for poetry install * [UPDATE] Bump django-countries from 7.5 to 7.5.1 (#624) Bumps [django-countries](https://github.com/SmileyChris/django-countries) from 7.5 to 7.5.1. - [Release notes](https://github.com/SmileyChris/django-countries/releases) - [Changelog](https://github.com/SmileyChris/django-countries/blob/main/CHANGES.rst) - [Commits](https://github.com/SmileyChris/django-countries/compare/v7.5...v7.5.1) --- updated-dependencies: - dependency-name: django-countries dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> * [UPDATE] Bump sentry-sdk from 1.19.1 to 1.21.0 Bumps [sentry-sdk](https://github.com/getsentry/sentry-python) from 1.19.1 to 1.21.0. - [Release notes](https://github.com/getsentry/sentry-python/releases) - [Changelog](https://github.com/getsentry/sentry-python/blob/master/CHANGELOG.md) - [Commits](https://github.com/getsentry/sentry-python/compare/1.19.1...1.21.0) --- updated-dependencies: - dependency-name: sentry-sdk dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] <support@github.com> * Speed up tests (#638) * Better usage of cache for groups and clubs related operations (#634) * Better usage of cache for group retrieval * Cache clearing on object deletion or update * replace signals by save and delete override * add is_anonymous check in is_owned_by Add in many is_owned_by(self, user) methods that user is not anonymous. Since many of those functions do db queries, this should reduce a little bit the load of the db. * Stricter usage of User.is_in_group Constrain the parameters that can be passed to the function to make sure only a str or an int can be used. Also force to explicitly specify if the group id or the group name is used. * write test and correct bugs * remove forgotten populate commands * Correct test * [FIX] Correction de bugs (#617) * Fix #600 * Fix #602 * Fixes & améliorations du nouveau CSS (#616) * Fix #604 * should fix #605 * Fix #608 * Update core/views/site.py Co-Authored-By: thomas girod <56346771+imperosol@users.noreply.github.com> * Added back the permission denied * Should fix #609 * Fix failing test when 2 user are merged * Should fix #610 * Should fix #627 * Should fix #109 Block les URLs suivantes lorsque le fichier se trouve dans le dir `profiles` ou `SAS` : - `/file/<id>/` - `/file/<id>/[delete|prop|edit]` > Les urls du SAS restent accessiblent pour les roots & les admins SAS > Les urls de profiles sont uniquement accessiblent aux roots * Fix root dir of SAS being unnaccessible for sas admins ⚠️ need to edit the SAS directory & save it (no changes required in sas directory properties) * Remove overwritten code * Should fix duplicated albums in user profile (wtf) * Fix typo * Extended profiles picture access to board members * Should fix #607 * Fix keyboard navigation not working properly * Fix user tagged pictures section inside python rather than in the template * Update utils.py * Apply suggested changes * Fix #604 * Fix #608 * Added back the permission denied * Should fix duplicated albums in user profile (wtf) * Fix user tagged pictures section inside python rather than in the template * Apply suggested changes --------- Co-authored-by: thomas girod <56346771+imperosol@users.noreply.github.com> * Remove duplicated css * Galaxy improvements (#628) * galaxy: improve logging and performance reporting * galaxy: add a full galaxy state test * galaxy: optimize user self score computation * galaxy: add 'generate_galaxy_test_data' command for development at scale * galaxy: big refactor Main changes: - Multiple Galaxy objects can now exist at the same time in DB. This allows for ruling a new galaxy while still displaying the old one. - The criteria to quickly know whether a user is a possible citizen is now a simple query on picture count. This avoids a very complicated query to database, that could often result in huge working memory load. With this change, it should be possible to run the galaxy even on a vanilla Postgres that didn't receive fine tuning for the Sith's galaxy. * galaxy: template: make the galaxy graph work and be usable with a lot of stars - Display focused star and its connections clearly - Display star label faintly by default for other stars to avoid overloading the graph - Hide non-focused lanes - Avoid clicks on non-highlighted, too far stars - Make the canva adapt its width to initial screen size, doesn't work dynamically * galaxy: better docstrings * galaxy: use bulk_create whenever possible This is a big performance gain, especially for the tests. Examples: ---- `./manage.py test galaxy.tests.GalaxyTest.test_full_galaxy_state` Measurements averaged over 3 run on *my machine*™: Before: 2min15s After: 1m41s ---- `./manage.py generate_galaxy_test_data --user-pack-count 1` Before: 48s After: 25s ---- `./manage.py rule_galaxy` (for 600 citizen, corresponding to 1 user-pack) Before: 14m4s After: 12m34s * core: populate: use a less ambiguous 'timezone.now()' When running the tests around midnight, the day is changing, leading to some values being offset to the next day depending on the timezone, and making some tests to fail. This ensure to use a less ambiguous `now` when populating the database. * write more extensive documentation - add documentation to previously documented classes and functions and refactor some of the documented one, in accordance to the PEP257 and ReStructuredText standards ; - add some type hints ; - use a NamedTuple for the `Galaxy.compute_users_score` method instead of a raw tuple. Also change a little bit the logic in the function which call the latter ; - add some additional parameter checks on a few functions ; - change a little bit the logic of the log level setting for the galaxy related commands. * galaxy: tests: split Model and View for more efficient data usage --------- Co-authored-by: maréchal <thgirod@hotmail.com> * [UPDATE] Bump libsass from 0.21.0 to 0.22.0 (#640) Bumps [libsass](https://github.com/sass/libsass-python) from 0.21.0 to 0.22.0. - [Release notes](https://github.com/sass/libsass-python/releases) - [Changelog](https://github.com/sass/libsass-python/blob/main/docs/changes.rst) - [Commits](https://github.com/sass/libsass-python/compare/0.21.0...0.22.0) --- updated-dependencies: - dependency-name: libsass dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> * [FIX] Fix cached groups (#647) * Bump sqlparse from 0.4.3 to 0.4.4 (#645) Bumps [sqlparse](https://github.com/andialbrecht/sqlparse) from 0.4.3 to 0.4.4. - [Release notes](https://github.com/andialbrecht/sqlparse/releases) - [Changelog](https://github.com/andialbrecht/sqlparse/blob/master/CHANGELOG) - [Commits](https://github.com/andialbrecht/sqlparse/compare/0.4.3...0.4.4) --- updated-dependencies: - dependency-name: sqlparse dependency-type: indirect ... Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> * [UPDATE] Bump django-ordered-model from 3.6 to 3.7.4 (#625) Bumps [django-ordered-model](https://github.com/django-ordered-model/django-ordered-model) from 3.6 to 3.7.4. - [Release notes](https://github.com/django-ordered-model/django-ordered-model/releases) - [Changelog](https://github.com/django-ordered-model/django-ordered-model/blob/master/CHANGES.md) - [Commits](https://github.com/django-ordered-model/django-ordered-model/compare/3.6...3.7.4) --- updated-dependencies: - dependency-name: django-ordered-model dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> * Fix immutable default variable in `get_start_of_semester` (#656) Le serveur ne percevait pas le changement de semestre, parce que la valeur par défaut passée à la fonction `get_start_of_semester()` était une fonction appelée une seule fois, lors du lancement du serveur. Bref, c'était ça : https://beta.ruff.rs/docs/rules/function-call-in-default-argument/ --------- Co-authored-by: imperosol <thgirod@hotmail.com> * Add missing method on AnonymousUser (#649) --------- Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: Thomas Girod <thgirod@hotmail.com> Co-authored-by: thomas girod <56346771+imperosol@users.noreply.github.com> Co-authored-by: Théo DURR <git@theodurr.fr> Co-authored-by: Skia <skia@hya.sk> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Bartuccio Antoine <klmp200@users.noreply.github.com>
1090 lines
37 KiB
Python
1090 lines
37 KiB
Python
# -*- coding:utf-8 -*
|
|
#
|
|
# Copyright 2023 © AE UTBM
|
|
# ae@utbm.fr / ae.info@utbm.fr
|
|
#
|
|
# This file is part of the website of the UTBM Student Association (AE UTBM),
|
|
# https://ae.utbm.fr.
|
|
#
|
|
# You can find the source code of the website at https://github.com/ae-utbm/sith3
|
|
#
|
|
# LICENSED UNDER THE GNU GENERAL PUBLIC LICENSE VERSION 3 (GPLv3)
|
|
# SEE : https://raw.githubusercontent.com/ae-utbm/sith3/master/LICENSE
|
|
# OR WITHIN THE LOCAL FILE "LICENSE"
|
|
#
|
|
#
|
|
from __future__ import annotations
|
|
|
|
from typing import Tuple, Optional
|
|
|
|
from django.db import models
|
|
from django.db.models import F, Value, Sum, QuerySet, OuterRef, Exists
|
|
from django.db.models.functions import Concat, Length
|
|
from django.utils.translation import gettext_lazy as _
|
|
from django.utils import timezone
|
|
from django.conf import settings
|
|
from django.urls import reverse
|
|
from django.core.validators import MinLengthValidator
|
|
from django.forms import ValidationError
|
|
from django.utils.functional import cached_property
|
|
|
|
from datetime import timedelta, date, datetime
|
|
import random
|
|
import string
|
|
import os
|
|
import base64
|
|
from dict2xml import dict2xml
|
|
|
|
from core.utils import get_start_of_semester
|
|
from sith.settings import SITH_COUNTER_OFFICES, SITH_MAIN_CLUB
|
|
from club.models import Club, Membership
|
|
from accounting.models import CurrencyField
|
|
from core.models import Group, User, Notification
|
|
from subscription.models import Subscription
|
|
|
|
from django_countries.fields import CountryField
|
|
|
|
|
|
class Customer(models.Model):
|
|
"""
|
|
This class extends a user to make a customer. It adds some basic customers' information, such as the account ID, and
|
|
is used by other accounting classes as reference to the customer, rather than using User
|
|
"""
|
|
|
|
user = models.OneToOneField(User, primary_key=True, on_delete=models.CASCADE)
|
|
account_id = models.CharField(_("account id"), max_length=10, unique=True)
|
|
amount = CurrencyField(_("amount"), default=0)
|
|
recorded_products = models.IntegerField(_("recorded product"), default=0)
|
|
|
|
class Meta:
|
|
verbose_name = _("customer")
|
|
verbose_name_plural = _("customers")
|
|
ordering = ["account_id"]
|
|
|
|
def __str__(self):
|
|
return "%s - %s" % (self.user.username, self.account_id)
|
|
|
|
@property
|
|
def can_record(self):
|
|
return self.recorded_products > -settings.SITH_ECOCUP_LIMIT
|
|
|
|
def can_record_more(self, number):
|
|
return self.recorded_products - number >= -settings.SITH_ECOCUP_LIMIT
|
|
|
|
@property
|
|
def can_buy(self) -> bool:
|
|
"""
|
|
Check if whether this customer has the right to
|
|
purchase any item.
|
|
This must be not confused with the Product.can_be_sold_to(user)
|
|
method as the present method returns an information
|
|
about a customer whereas the other tells something
|
|
about the relation between a User (not a Customer,
|
|
don't mix them) and a Product.
|
|
"""
|
|
subscription = self.user.subscriptions.order_by("subscription_end").last()
|
|
if subscription is None:
|
|
return False
|
|
return (date.today() - subscription.subscription_end) < timedelta(days=90)
|
|
|
|
@classmethod
|
|
def get_or_create(cls, user: User) -> Tuple[Customer, bool]:
|
|
"""
|
|
Work in pretty much the same way as the usual get_or_create method,
|
|
but with the default field replaced by some under the hood.
|
|
|
|
If the user has an account, return it as is.
|
|
Else create a new account with no money on it and a new unique account id
|
|
|
|
Example : ::
|
|
|
|
user = User.objects.get(pk=1)
|
|
account, created = Customer.get_or_create(user)
|
|
if created:
|
|
print(f"created a new account with id {account.id}")
|
|
else:
|
|
print(f"user has already an account, with {account.id} € on it"
|
|
"""
|
|
if hasattr(user, "customer"):
|
|
return user.customer, False
|
|
|
|
# account_id are always a number with a letter appended
|
|
account_id = (
|
|
Customer.objects.order_by(Length("account_id"), "account_id")
|
|
.values("account_id")
|
|
.last()
|
|
)
|
|
if account_id is None:
|
|
# legacy from the old site
|
|
account = cls.objects.create(user=user, account_id="1504a")
|
|
return account, True
|
|
|
|
account_id = account_id["account_id"]
|
|
account_num = int(account_id[:-1])
|
|
while Customer.objects.filter(account_id=account_id).exists():
|
|
# when entering the first iteration, we are using an already existing account id
|
|
# so the loop should always execute at least one time
|
|
account_num += 1
|
|
account_id = f"{account_num}{random.choice(string.ascii_lowercase)}"
|
|
|
|
account = cls.objects.create(user=user, account_id=account_id)
|
|
return account, True
|
|
|
|
def save(self, allow_negative=False, is_selling=False, *args, **kwargs):
|
|
"""
|
|
is_selling : tell if the current action is a selling
|
|
allow_negative : ignored if not a selling. Allow a selling to put the account in negative
|
|
Those two parameters avoid blocking the save method of a customer if his account is negative
|
|
"""
|
|
if self.amount < 0 and (is_selling and not allow_negative):
|
|
raise ValidationError(_("Not enough money"))
|
|
super(Customer, self).save(*args, **kwargs)
|
|
|
|
def recompute_amount(self):
|
|
refillings = self.refillings.aggregate(sum=Sum(F("amount")))["sum"]
|
|
self.amount = refillings if refillings is not None else 0
|
|
purchases = (
|
|
self.buyings.filter(payment_method="SITH_ACCOUNT")
|
|
.annotate(amount=F("quantity") * F("unit_price"))
|
|
.aggregate(sum=Sum(F("amount")))
|
|
)["sum"]
|
|
if purchases is not None:
|
|
self.amount -= purchases
|
|
self.save()
|
|
|
|
def get_absolute_url(self):
|
|
return reverse("core:user_account", kwargs={"user_id": self.user.pk})
|
|
|
|
def get_full_url(self):
|
|
return "".join(["https://", settings.SITH_URL, self.get_absolute_url()])
|
|
|
|
|
|
class BillingInfo(models.Model):
|
|
"""
|
|
Represent the billing information of a user, which are required
|
|
by the 3D-Secure v2 system used by the etransaction module
|
|
"""
|
|
|
|
customer = models.OneToOneField(
|
|
Customer, related_name="billing_infos", on_delete=models.CASCADE
|
|
)
|
|
|
|
# declaring surname and name even though they are already defined
|
|
# in User add some redundancy, but ensures that the billing infos
|
|
# shall stay correct, whatever shenanigans the user commits on its profile
|
|
first_name = models.CharField(_("First name"), max_length=22)
|
|
last_name = models.CharField(_("Last name"), max_length=22)
|
|
address_1 = models.CharField(_("Address 1"), max_length=50)
|
|
address_2 = models.CharField(_("Address 2"), max_length=50, blank=True, null=True)
|
|
zip_code = models.CharField(_("Zip code"), max_length=16) # code postal
|
|
city = models.CharField(_("City"), max_length=50)
|
|
country = CountryField(blank_label=_("Country"))
|
|
|
|
def to_3dsv2_xml(self) -> str:
|
|
"""
|
|
Convert the data from this model into a xml usable
|
|
by the online paying service of the Crédit Agricole bank.
|
|
see : `https://www.ca-moncommerce.com/espace-client-mon-commerce/up2pay-e-transactions/ma-documentation/manuel-dintegration-focus-3ds-v2/principes-generaux/#integration-3dsv2-developpeur-webmaster`
|
|
"""
|
|
data = {
|
|
"Address": {
|
|
"FirstName": self.first_name,
|
|
"LastName": self.last_name,
|
|
"Address1": self.address_1,
|
|
"ZipCode": self.zip_code,
|
|
"City": self.city,
|
|
"CountryCode": self.country.numeric, # ISO-3166-1 numeric code
|
|
}
|
|
}
|
|
if self.address_2:
|
|
data["Address"]["Address2"] = self.address_2
|
|
xml = dict2xml(data, wrap="Billing", newlines=False)
|
|
return '<?xml version="1.0" encoding="UTF-8" ?>' + xml
|
|
|
|
def __str__(self):
|
|
return f"{self.first_name} {self.last_name}"
|
|
|
|
|
|
class ProductType(models.Model):
|
|
"""
|
|
This describes a product type
|
|
Useful only for categorizing, changes are made at the product level for now
|
|
"""
|
|
|
|
name = models.CharField(_("name"), max_length=30)
|
|
description = models.TextField(_("description"), null=True, blank=True)
|
|
comment = models.TextField(_("comment"), null=True, blank=True)
|
|
icon = models.ImageField(upload_to="products", null=True, blank=True)
|
|
|
|
# priority holds no real backend logic but helps to handle the order in which
|
|
# the items are to be shown to the user
|
|
priority = models.PositiveIntegerField(default=0)
|
|
|
|
class Meta:
|
|
verbose_name = _("product type")
|
|
ordering = ["-priority", "name"]
|
|
|
|
def is_owned_by(self, user):
|
|
"""
|
|
Method to see if that object can be edited by the given user
|
|
"""
|
|
if user.is_anonymous:
|
|
return False
|
|
if user.is_in_group(pk=settings.SITH_GROUP_ACCOUNTING_ADMIN_ID):
|
|
return True
|
|
return False
|
|
|
|
def __str__(self):
|
|
return self.name
|
|
|
|
def get_absolute_url(self):
|
|
return reverse("counter:producttype_list")
|
|
|
|
|
|
class Product(models.Model):
|
|
"""
|
|
This describes a product, with all its related informations
|
|
"""
|
|
|
|
name = models.CharField(_("name"), max_length=64)
|
|
description = models.TextField(_("description"), blank=True)
|
|
product_type = models.ForeignKey(
|
|
ProductType,
|
|
related_name="products",
|
|
verbose_name=_("product type"),
|
|
null=True,
|
|
blank=True,
|
|
on_delete=models.SET_NULL,
|
|
)
|
|
code = models.CharField(_("code"), max_length=16, blank=True)
|
|
purchase_price = CurrencyField(_("purchase price"))
|
|
selling_price = CurrencyField(_("selling price"))
|
|
special_selling_price = CurrencyField(_("special selling price"))
|
|
icon = models.ImageField(
|
|
upload_to="products", null=True, blank=True, verbose_name=_("icon")
|
|
)
|
|
club = models.ForeignKey(
|
|
Club, related_name="products", verbose_name=_("club"), on_delete=models.CASCADE
|
|
)
|
|
limit_age = models.IntegerField(_("limit age"), default=0)
|
|
tray = models.BooleanField(_("tray price"), default=False)
|
|
parent_product = models.ForeignKey(
|
|
"self",
|
|
related_name="children_products",
|
|
verbose_name=_("parent product"),
|
|
null=True,
|
|
blank=True,
|
|
on_delete=models.SET_NULL,
|
|
)
|
|
buying_groups = models.ManyToManyField(
|
|
Group, related_name="products", verbose_name=_("buying groups"), blank=True
|
|
)
|
|
archived = models.BooleanField(_("archived"), default=False)
|
|
|
|
class Meta:
|
|
verbose_name = _("product")
|
|
|
|
@property
|
|
def is_record_product(self):
|
|
return settings.SITH_ECOCUP_CONS == self.id
|
|
|
|
@property
|
|
def is_unrecord_product(self):
|
|
return settings.SITH_ECOCUP_DECO == self.id
|
|
|
|
def is_owned_by(self, user):
|
|
"""
|
|
Method to see if that object can be edited by the given user
|
|
"""
|
|
if user.is_anonymous:
|
|
return False
|
|
if user.is_in_group(
|
|
pk=settings.SITH_GROUP_ACCOUNTING_ADMIN_ID
|
|
) or user.is_in_group(pk=settings.SITH_GROUP_COUNTER_ADMIN_ID):
|
|
return True
|
|
return False
|
|
|
|
def get_absolute_url(self):
|
|
return reverse("counter:product_list")
|
|
|
|
def can_be_sold_to(self, user: User) -> bool:
|
|
"""
|
|
Check if whether the user given in parameter has the right to buy
|
|
this product or not.
|
|
|
|
This must be not confused with the Customer.can_buy()
|
|
method as the present method returns an information
|
|
about the relation between a User and a Product,
|
|
whereas the other tells something about a Customer
|
|
(and not a user, they are not the same model).
|
|
|
|
:return: True if the user can buy this product else False
|
|
"""
|
|
if not self.buying_groups.exists():
|
|
return True
|
|
for group_id in self.buying_groups.values_list("pk", flat=True):
|
|
if user.is_in_group(pk=group_id):
|
|
return True
|
|
return False
|
|
|
|
@property
|
|
def profit(self):
|
|
return self.selling_price - self.purchase_price
|
|
|
|
def __str__(self):
|
|
return "%s (%s)" % (self.name, self.code)
|
|
|
|
|
|
class CounterQuerySet(models.QuerySet):
|
|
def annotate_has_barman(self, user: User) -> CounterQuerySet:
|
|
"""
|
|
Annotate the queryset with the `user_is_barman` field.
|
|
For each counter, this field has value True if the user
|
|
is a barman of this counter, else False.
|
|
|
|
:param user: the user we want to check if he is a barman
|
|
|
|
Example::
|
|
|
|
sli = User.objects.get(username="sli")
|
|
counters = (
|
|
Counter.objects
|
|
.annotate_has_barman(sli) # add the user_has_barman boolean field
|
|
.filter(has_annotated_barman=True) # keep only counters where this user is barman
|
|
)
|
|
print("Sli est barman dans les comptoirs suivants :")
|
|
for counter in counters:
|
|
print(f"- {counter.name}")
|
|
"""
|
|
subquery = user.counters.filter(pk=OuterRef("pk"))
|
|
# noinspection PyTypeChecker
|
|
return self.annotate(has_annotated_barman=Exists(subquery))
|
|
|
|
|
|
class Counter(models.Model):
|
|
name = models.CharField(_("name"), max_length=30)
|
|
club = models.ForeignKey(
|
|
Club, related_name="counters", verbose_name=_("club"), on_delete=models.CASCADE
|
|
)
|
|
products = models.ManyToManyField(
|
|
Product, related_name="counters", verbose_name=_("products"), blank=True
|
|
)
|
|
type = models.CharField(
|
|
_("counter type"),
|
|
max_length=255,
|
|
choices=[("BAR", _("Bar")), ("OFFICE", _("Office")), ("EBOUTIC", _("Eboutic"))],
|
|
)
|
|
sellers = models.ManyToManyField(
|
|
User, verbose_name=_("sellers"), related_name="counters", blank=True
|
|
)
|
|
edit_groups = models.ManyToManyField(
|
|
Group, related_name="editable_counters", blank=True
|
|
)
|
|
view_groups = models.ManyToManyField(
|
|
Group, related_name="viewable_counters", blank=True
|
|
)
|
|
token = models.CharField(_("token"), max_length=30, null=True, blank=True)
|
|
|
|
objects = CounterQuerySet.as_manager()
|
|
|
|
class Meta:
|
|
verbose_name = _("counter")
|
|
|
|
def __getattribute__(self, name):
|
|
if name == "edit_groups":
|
|
return Group.objects.filter(
|
|
name=self.club.unix_name + settings.SITH_BOARD_SUFFIX
|
|
).all()
|
|
return object.__getattribute__(self, name)
|
|
|
|
def __str__(self):
|
|
return self.name
|
|
|
|
def get_absolute_url(self):
|
|
if self.type == "EBOUTIC":
|
|
return reverse("eboutic:main")
|
|
return reverse("counter:details", kwargs={"counter_id": self.id})
|
|
|
|
def is_owned_by(self, user):
|
|
if user.is_anonymous:
|
|
return False
|
|
mem = self.club.get_membership_for(user)
|
|
if mem and mem.role >= 7:
|
|
return True
|
|
return user.is_in_group(pk=settings.SITH_GROUP_COUNTER_ADMIN_ID)
|
|
|
|
def can_be_viewed_by(self, user):
|
|
if self.type == "BAR":
|
|
return True
|
|
return user.is_board_member or user in self.sellers.all()
|
|
|
|
def gen_token(self):
|
|
"""Generate a new token for this counter"""
|
|
self.token = "".join(
|
|
random.choice(string.ascii_letters + string.digits) for x in range(30)
|
|
)
|
|
self.save()
|
|
|
|
def add_barman(self, user):
|
|
"""
|
|
Logs a barman in to the given counter
|
|
A user is stored as a tuple with its login time
|
|
"""
|
|
Permanency(user=user, counter=self, start=timezone.now(), end=None).save()
|
|
|
|
def del_barman(self, user):
|
|
"""
|
|
Logs a barman out and store its permanency
|
|
"""
|
|
perm = Permanency.objects.filter(counter=self, user=user, end=None).all()
|
|
for p in perm:
|
|
p.end = p.activity
|
|
p.save()
|
|
|
|
@cached_property
|
|
def barmen_list(self):
|
|
return self.get_barmen_list()
|
|
|
|
def get_barmen_list(self):
|
|
"""
|
|
Returns the barman list as list of User
|
|
|
|
Also handle the timeout of the barmen
|
|
"""
|
|
pl = Permanency.objects.filter(counter=self, end=None).all()
|
|
bl = []
|
|
for p in pl:
|
|
if timezone.now() - p.activity < timedelta(
|
|
minutes=settings.SITH_BARMAN_TIMEOUT
|
|
):
|
|
bl.append(p.user)
|
|
else:
|
|
p.end = p.activity
|
|
p.save()
|
|
return bl
|
|
|
|
def get_random_barman(self):
|
|
"""
|
|
Return a random user being currently a barman
|
|
"""
|
|
bl = self.get_barmen_list()
|
|
return bl[random.randrange(0, len(bl))]
|
|
|
|
def update_activity(self):
|
|
"""
|
|
Update the barman activity to prevent timeout
|
|
"""
|
|
for p in Permanency.objects.filter(counter=self, end=None).all():
|
|
p.save() # Update activity
|
|
|
|
def is_open(self):
|
|
return len(self.barmen_list) > 0
|
|
|
|
def is_inactive(self):
|
|
"""
|
|
Returns True if the counter self is inactive from SITH_COUNTER_MINUTE_INACTIVE's value minutes, else False
|
|
"""
|
|
return self.is_open() and (
|
|
(timezone.now() - self.permanencies.order_by("-activity").first().activity)
|
|
> timedelta(minutes=settings.SITH_COUNTER_MINUTE_INACTIVE)
|
|
)
|
|
|
|
def barman_list(self):
|
|
"""
|
|
Returns the barman id list
|
|
"""
|
|
return [b.id for b in self.get_barmen_list()]
|
|
|
|
def can_refill(self):
|
|
"""
|
|
Show if the counter authorize the refilling with physic money
|
|
"""
|
|
|
|
if self.type != "BAR":
|
|
return False
|
|
if self.id in SITH_COUNTER_OFFICES:
|
|
# If the counter is either 'AE' or 'BdF', refills are authorized
|
|
return True
|
|
is_ae_member = False
|
|
ae = Club.objects.get(unix_name=SITH_MAIN_CLUB["unix_name"])
|
|
for barman in self.get_barmen_list():
|
|
if ae.get_membership_for(barman):
|
|
is_ae_member = True
|
|
return is_ae_member
|
|
|
|
def get_top_barmen(self) -> QuerySet:
|
|
"""
|
|
Return a QuerySet querying the office hours stats of all the barmen of all time
|
|
of this counter, ordered by descending number of hours.
|
|
|
|
Each element of the QuerySet corresponds to a barman and has the following data :
|
|
- the full name (first name + last name) of the barman
|
|
- the nickname of the barman
|
|
- the promo of the barman
|
|
- the total number of office hours the barman did attend
|
|
"""
|
|
return (
|
|
self.permanencies.exclude(end=None)
|
|
.annotate(
|
|
name=Concat(F("user__first_name"), Value(" "), F("user__last_name"))
|
|
)
|
|
.annotate(nickname=F("user__nick_name"))
|
|
.annotate(promo=F("user__promo"))
|
|
.values("user", "name", "nickname", "promo")
|
|
.annotate(perm_sum=Sum(F("end") - F("start")))
|
|
.exclude(perm_sum=None)
|
|
.order_by("-perm_sum")
|
|
)
|
|
|
|
def get_top_customers(self, since: Optional[date] = None) -> QuerySet:
|
|
"""
|
|
Return a QuerySet querying the money spent by customers of this counter
|
|
since the specified date, ordered by descending amount of money spent.
|
|
|
|
Each element of the QuerySet corresponds to a customer and has the following data :
|
|
- the full name (first name + last name) of the customer
|
|
- the nickname of the customer
|
|
- the amount of money spent by the customer
|
|
"""
|
|
if since is None:
|
|
since = get_start_of_semester()
|
|
return (
|
|
self.sellings.filter(date__gte=since)
|
|
.annotate(
|
|
name=Concat(
|
|
F("customer__user__first_name"),
|
|
Value(" "),
|
|
F("customer__user__last_name"),
|
|
)
|
|
)
|
|
.annotate(nickname=F("customer__user__nick_name"))
|
|
.annotate(promo=F("customer__user__promo"))
|
|
.annotate(user=F("customer__user"))
|
|
.values("user", "promo", "name", "nickname")
|
|
.annotate(
|
|
selling_sum=Sum(
|
|
F("unit_price") * F("quantity"), output_field=CurrencyField()
|
|
)
|
|
)
|
|
.filter(selling_sum__gt=0)
|
|
.order_by("-selling_sum")
|
|
)
|
|
|
|
def get_total_sales(self, since=None) -> CurrencyField:
|
|
"""
|
|
Compute and return the total turnover of this counter
|
|
since the date specified in parameter (by default, since the start of the current
|
|
semester)
|
|
:param since: timestamp from which to perform the calculation
|
|
:type since: datetime | date | None
|
|
:return: Total revenue earned at this counter
|
|
"""
|
|
if since is None:
|
|
since = get_start_of_semester()
|
|
if isinstance(since, date):
|
|
since = datetime.combine(since, datetime.min.time())
|
|
total = self.sellings.filter(date__gte=since).aggregate(
|
|
total=Sum(F("quantity") * F("unit_price"), output_field=CurrencyField())
|
|
)["total"]
|
|
return total if total is not None else CurrencyField(0)
|
|
|
|
|
|
class Refilling(models.Model):
|
|
"""
|
|
Handle the refilling
|
|
"""
|
|
|
|
counter = models.ForeignKey(
|
|
Counter, related_name="refillings", blank=False, on_delete=models.CASCADE
|
|
)
|
|
amount = CurrencyField(_("amount"))
|
|
operator = models.ForeignKey(
|
|
User,
|
|
related_name="refillings_as_operator",
|
|
blank=False,
|
|
on_delete=models.CASCADE,
|
|
)
|
|
customer = models.ForeignKey(
|
|
Customer, related_name="refillings", blank=False, on_delete=models.CASCADE
|
|
)
|
|
date = models.DateTimeField(_("date"))
|
|
payment_method = models.CharField(
|
|
_("payment method"),
|
|
max_length=255,
|
|
choices=settings.SITH_COUNTER_PAYMENT_METHOD,
|
|
default="CASH",
|
|
)
|
|
bank = models.CharField(
|
|
_("bank"), max_length=255, choices=settings.SITH_COUNTER_BANK, default="OTHER"
|
|
)
|
|
is_validated = models.BooleanField(_("is validated"), default=False)
|
|
|
|
class Meta:
|
|
verbose_name = _("refilling")
|
|
|
|
def __str__(self):
|
|
return "Refilling: %.2f for %s" % (
|
|
self.amount,
|
|
self.customer.user.get_display_name(),
|
|
)
|
|
|
|
def is_owned_by(self, user):
|
|
if user.is_anonymous:
|
|
return False
|
|
return user.is_owner(self.counter) and self.payment_method != "CARD"
|
|
|
|
def delete(self, *args, **kwargs):
|
|
self.customer.amount -= self.amount
|
|
self.customer.save()
|
|
super(Refilling, self).delete(*args, **kwargs)
|
|
|
|
def save(self, *args, **kwargs):
|
|
if not self.date:
|
|
self.date = timezone.now()
|
|
self.full_clean()
|
|
if not self.is_validated:
|
|
self.customer.amount += self.amount
|
|
self.customer.save()
|
|
self.is_validated = True
|
|
if self.customer.user.preferences.notify_on_refill:
|
|
Notification(
|
|
user=self.customer.user,
|
|
url=reverse(
|
|
"core:user_account_detail",
|
|
kwargs={
|
|
"user_id": self.customer.user.id,
|
|
"year": self.date.year,
|
|
"month": self.date.month,
|
|
},
|
|
),
|
|
param=str(self.amount),
|
|
type="REFILLING",
|
|
).save()
|
|
super(Refilling, self).save(*args, **kwargs)
|
|
|
|
|
|
class Selling(models.Model):
|
|
"""
|
|
Handle the sellings
|
|
"""
|
|
|
|
label = models.CharField(_("label"), max_length=64)
|
|
product = models.ForeignKey(
|
|
Product,
|
|
related_name="sellings",
|
|
null=True,
|
|
blank=True,
|
|
on_delete=models.SET_NULL,
|
|
)
|
|
counter = models.ForeignKey(
|
|
Counter,
|
|
related_name="sellings",
|
|
null=True,
|
|
blank=False,
|
|
on_delete=models.SET_NULL,
|
|
)
|
|
club = models.ForeignKey(
|
|
Club, related_name="sellings", null=True, blank=False, on_delete=models.SET_NULL
|
|
)
|
|
unit_price = CurrencyField(_("unit price"))
|
|
quantity = models.IntegerField(_("quantity"))
|
|
seller = models.ForeignKey(
|
|
User,
|
|
related_name="sellings_as_operator",
|
|
null=True,
|
|
blank=False,
|
|
on_delete=models.SET_NULL,
|
|
)
|
|
customer = models.ForeignKey(
|
|
Customer,
|
|
related_name="buyings",
|
|
null=True,
|
|
blank=False,
|
|
on_delete=models.SET_NULL,
|
|
)
|
|
date = models.DateTimeField(_("date"))
|
|
payment_method = models.CharField(
|
|
_("payment method"),
|
|
max_length=255,
|
|
choices=[("SITH_ACCOUNT", _("Sith account")), ("CARD", _("Credit card"))],
|
|
default="SITH_ACCOUNT",
|
|
)
|
|
is_validated = models.BooleanField(_("is validated"), default=False)
|
|
|
|
class Meta:
|
|
verbose_name = _("selling")
|
|
|
|
def __str__(self):
|
|
return "Selling: %d x %s (%f) for %s" % (
|
|
self.quantity,
|
|
self.label,
|
|
self.quantity * self.unit_price,
|
|
self.customer.user.get_display_name(),
|
|
)
|
|
|
|
def is_owned_by(self, user):
|
|
if user.is_anonymous:
|
|
return False
|
|
return user.is_owner(self.counter) and self.payment_method != "CARD"
|
|
|
|
def can_be_viewed_by(self, user):
|
|
if (
|
|
not hasattr(self, "customer") or self.customer is None
|
|
): # Customer can be set to Null
|
|
return False
|
|
return user == self.customer.user
|
|
|
|
def delete(self, *args, **kwargs):
|
|
if self.payment_method == "SITH_ACCOUNT":
|
|
self.customer.amount += self.quantity * self.unit_price
|
|
self.customer.save()
|
|
super(Selling, self).delete(*args, **kwargs)
|
|
|
|
def send_mail_customer(self):
|
|
event = self.product.eticket.event_title or _("Unknown event")
|
|
subject = _("Eticket bought for the event %(event)s") % {"event": event}
|
|
message_html = _(
|
|
"You bought an eticket for the event %(event)s.\nYou can download it directly from this link %(eticket)s.\nYou can also retrieve all your e-tickets on your account page %(url)s."
|
|
) % {
|
|
"event": event,
|
|
"url": "".join(
|
|
(
|
|
'<a href="',
|
|
self.customer.get_full_url(),
|
|
'">',
|
|
self.customer.get_full_url(),
|
|
"</a>",
|
|
)
|
|
),
|
|
"eticket": "".join(
|
|
(
|
|
'<a href="',
|
|
self.get_eticket_full_url(),
|
|
'">',
|
|
self.get_eticket_full_url(),
|
|
"</a>",
|
|
)
|
|
),
|
|
}
|
|
message_txt = _(
|
|
"You bought an eticket for the event %(event)s.\nYou can download it directly from this link %(eticket)s.\nYou can also retrieve all your e-tickets on your account page %(url)s."
|
|
) % {
|
|
"event": event,
|
|
"url": self.customer.get_full_url(),
|
|
"eticket": self.get_eticket_full_url(),
|
|
}
|
|
self.customer.user.email_user(subject, message_txt, html_message=message_html)
|
|
|
|
def save(self, allow_negative=False, *args, **kwargs):
|
|
"""
|
|
allow_negative : Allow this selling to use more money than available for this user
|
|
"""
|
|
if not self.date:
|
|
self.date = timezone.now()
|
|
self.full_clean()
|
|
if not self.is_validated:
|
|
self.customer.amount -= self.quantity * self.unit_price
|
|
self.customer.save(allow_negative=allow_negative, is_selling=True)
|
|
self.is_validated = True
|
|
u = User.objects.filter(id=self.customer.user.id).first()
|
|
if u.was_subscribed:
|
|
if (
|
|
self.product
|
|
and self.product.id == settings.SITH_PRODUCT_SUBSCRIPTION_ONE_SEMESTER
|
|
):
|
|
sub = Subscription(
|
|
member=u,
|
|
subscription_type="un-semestre",
|
|
payment_method="EBOUTIC",
|
|
location="EBOUTIC",
|
|
)
|
|
sub.subscription_start = Subscription.compute_start()
|
|
sub.subscription_start = Subscription.compute_start(
|
|
duration=settings.SITH_SUBSCRIPTIONS[sub.subscription_type][
|
|
"duration"
|
|
]
|
|
)
|
|
sub.subscription_end = Subscription.compute_end(
|
|
duration=settings.SITH_SUBSCRIPTIONS[sub.subscription_type][
|
|
"duration"
|
|
],
|
|
start=sub.subscription_start,
|
|
)
|
|
sub.save()
|
|
elif (
|
|
self.product
|
|
and self.product.id == settings.SITH_PRODUCT_SUBSCRIPTION_TWO_SEMESTERS
|
|
):
|
|
u = User.objects.filter(id=self.customer.user.id).first()
|
|
sub = Subscription(
|
|
member=u,
|
|
subscription_type="deux-semestres",
|
|
payment_method="EBOUTIC",
|
|
location="EBOUTIC",
|
|
)
|
|
sub.subscription_start = Subscription.compute_start()
|
|
sub.subscription_start = Subscription.compute_start(
|
|
duration=settings.SITH_SUBSCRIPTIONS[sub.subscription_type][
|
|
"duration"
|
|
]
|
|
)
|
|
sub.subscription_end = Subscription.compute_end(
|
|
duration=settings.SITH_SUBSCRIPTIONS[sub.subscription_type][
|
|
"duration"
|
|
],
|
|
start=sub.subscription_start,
|
|
)
|
|
sub.save()
|
|
if self.customer.user.preferences.notify_on_click:
|
|
Notification(
|
|
user=self.customer.user,
|
|
url=reverse(
|
|
"core:user_account_detail",
|
|
kwargs={
|
|
"user_id": self.customer.user.id,
|
|
"year": self.date.year,
|
|
"month": self.date.month,
|
|
},
|
|
),
|
|
param="%d x %s" % (self.quantity, self.label),
|
|
type="SELLING",
|
|
).save()
|
|
super(Selling, self).save(*args, **kwargs)
|
|
try:
|
|
# The product has no id until it's saved
|
|
if self.product.eticket:
|
|
self.send_mail_customer()
|
|
except:
|
|
pass
|
|
|
|
def get_eticket_full_url(self):
|
|
eticket_url = reverse("counter:eticket_pdf", kwargs={"selling_id": self.id})
|
|
return "".join(["https://", settings.SITH_URL, eticket_url])
|
|
|
|
|
|
class Permanency(models.Model):
|
|
"""
|
|
This class aims at storing a traceability of who was barman where and when
|
|
"""
|
|
|
|
user = models.ForeignKey(
|
|
User,
|
|
related_name="permanencies",
|
|
verbose_name=_("user"),
|
|
on_delete=models.CASCADE,
|
|
)
|
|
counter = models.ForeignKey(
|
|
Counter,
|
|
related_name="permanencies",
|
|
verbose_name=_("counter"),
|
|
on_delete=models.CASCADE,
|
|
)
|
|
start = models.DateTimeField(_("start date"))
|
|
end = models.DateTimeField(_("end date"), null=True, db_index=True)
|
|
activity = models.DateTimeField(_("last activity date"), auto_now=True)
|
|
|
|
class Meta:
|
|
verbose_name = _("permanency")
|
|
|
|
def __str__(self):
|
|
return "%s in %s from %s (last activity: %s) to %s" % (
|
|
self.user,
|
|
self.counter,
|
|
self.start.strftime("%Y-%m-%d %H:%M:%S"),
|
|
self.activity.strftime("%Y-%m-%d %H:%M:%S"),
|
|
self.end.strftime("%Y-%m-%d %H:%M:%S") if self.end else "",
|
|
)
|
|
|
|
@property
|
|
def duration(self):
|
|
if self.end is None:
|
|
return self.activity - self.start
|
|
return self.end - self.start
|
|
|
|
|
|
class CashRegisterSummary(models.Model):
|
|
user = models.ForeignKey(
|
|
User,
|
|
related_name="cash_summaries",
|
|
verbose_name=_("user"),
|
|
on_delete=models.CASCADE,
|
|
)
|
|
counter = models.ForeignKey(
|
|
Counter,
|
|
related_name="cash_summaries",
|
|
verbose_name=_("counter"),
|
|
on_delete=models.CASCADE,
|
|
)
|
|
date = models.DateTimeField(_("date"))
|
|
comment = models.TextField(_("comment"), null=True, blank=True)
|
|
emptied = models.BooleanField(_("emptied"), default=False)
|
|
|
|
class Meta:
|
|
verbose_name = _("cash register summary")
|
|
|
|
def __str__(self):
|
|
return "At %s by %s - Total: %s €" % (self.counter, self.user, self.get_total())
|
|
|
|
def __getattribute__(self, name):
|
|
if name[:5] == "check":
|
|
checks = self.items.filter(check=True).order_by("value").all()
|
|
if name == "ten_cents":
|
|
return self.items.filter(value=0.1, check=False).first()
|
|
elif name == "twenty_cents":
|
|
return self.items.filter(value=0.2, check=False).first()
|
|
elif name == "fifty_cents":
|
|
return self.items.filter(value=0.5, check=False).first()
|
|
elif name == "one_euro":
|
|
return self.items.filter(value=1, check=False).first()
|
|
elif name == "two_euros":
|
|
return self.items.filter(value=2, check=False).first()
|
|
elif name == "five_euros":
|
|
return self.items.filter(value=5, check=False).first()
|
|
elif name == "ten_euros":
|
|
return self.items.filter(value=10, check=False).first()
|
|
elif name == "twenty_euros":
|
|
return self.items.filter(value=20, check=False).first()
|
|
elif name == "fifty_euros":
|
|
return self.items.filter(value=50, check=False).first()
|
|
elif name == "hundred_euros":
|
|
return self.items.filter(value=100, check=False).first()
|
|
elif name == "check_1":
|
|
return checks[0] if 0 < len(checks) else None
|
|
elif name == "check_2":
|
|
return checks[1] if 1 < len(checks) else None
|
|
elif name == "check_3":
|
|
return checks[2] if 2 < len(checks) else None
|
|
elif name == "check_4":
|
|
return checks[3] if 3 < len(checks) else None
|
|
elif name == "check_5":
|
|
return checks[4] if 4 < len(checks) else None
|
|
else:
|
|
return object.__getattribute__(self, name)
|
|
|
|
def is_owned_by(self, user):
|
|
"""
|
|
Method to see if that object can be edited by the given user
|
|
"""
|
|
if user.is_anonymous:
|
|
return False
|
|
if user.is_in_group(pk=settings.SITH_GROUP_COUNTER_ADMIN_ID):
|
|
return True
|
|
return False
|
|
|
|
def get_total(self):
|
|
t = 0
|
|
for it in self.items.all():
|
|
t += it.quantity * it.value
|
|
return t
|
|
|
|
def save(self, *args, **kwargs):
|
|
if not self.id:
|
|
self.date = timezone.now()
|
|
return super(CashRegisterSummary, self).save(*args, **kwargs)
|
|
|
|
def get_absolute_url(self):
|
|
return reverse("counter:cash_summary_list")
|
|
|
|
|
|
class CashRegisterSummaryItem(models.Model):
|
|
cash_summary = models.ForeignKey(
|
|
CashRegisterSummary,
|
|
related_name="items",
|
|
verbose_name=_("cash summary"),
|
|
on_delete=models.CASCADE,
|
|
)
|
|
value = CurrencyField(_("value"))
|
|
quantity = models.IntegerField(_("quantity"), default=0)
|
|
check = models.BooleanField(_("check"), default=False)
|
|
|
|
class Meta:
|
|
verbose_name = _("cash register summary item")
|
|
|
|
|
|
class Eticket(models.Model):
|
|
"""
|
|
Eticket can be linked to a product an allows PDF generation
|
|
"""
|
|
|
|
product = models.OneToOneField(
|
|
Product,
|
|
related_name="eticket",
|
|
verbose_name=_("product"),
|
|
on_delete=models.CASCADE,
|
|
)
|
|
banner = models.ImageField(
|
|
upload_to="etickets", null=True, blank=True, verbose_name=_("banner")
|
|
)
|
|
event_date = models.DateField(_("event date"), null=True, blank=True)
|
|
event_title = models.CharField(
|
|
_("event title"), max_length=64, null=True, blank=True
|
|
)
|
|
secret = models.CharField(_("secret"), max_length=64, unique=True)
|
|
|
|
def __str__(self):
|
|
return "%s" % (self.product.name)
|
|
|
|
def get_absolute_url(self):
|
|
return reverse("counter:eticket_list")
|
|
|
|
def save(self, *args, **kwargs):
|
|
if not self.id:
|
|
self.secret = base64.b64encode(os.urandom(32))
|
|
return super(Eticket, self).save(*args, **kwargs)
|
|
|
|
def is_owned_by(self, user):
|
|
"""
|
|
Method to see if that object can be edited by the given user
|
|
"""
|
|
if user.is_anonymous:
|
|
return False
|
|
return user.is_in_group(pk=settings.SITH_GROUP_COUNTER_ADMIN_ID)
|
|
|
|
def get_hash(self, string):
|
|
import hashlib
|
|
import hmac
|
|
|
|
return hmac.new(
|
|
bytes(self.secret, "utf-8"), bytes(string, "utf-8"), hashlib.sha1
|
|
).hexdigest()
|
|
|
|
|
|
class StudentCard(models.Model):
|
|
"""
|
|
Alternative way to connect a customer into a counter
|
|
We are using Mifare DESFire EV1 specs since it's used for izly cards
|
|
https://www.nxp.com/docs/en/application-note/AN10927.pdf
|
|
UID is 7 byte long that means 14 hexa characters
|
|
"""
|
|
|
|
UID_SIZE = 14
|
|
|
|
@staticmethod
|
|
def is_valid(uid):
|
|
return (
|
|
(uid.isupper() or uid.isnumeric())
|
|
and len(uid) == StudentCard.UID_SIZE
|
|
and not StudentCard.objects.filter(uid=uid).exists()
|
|
)
|
|
|
|
@staticmethod
|
|
def can_create(customer, user):
|
|
return user.pk == customer.user.pk or user.is_board_member or user.is_root
|
|
|
|
def can_be_edited_by(self, obj):
|
|
if isinstance(obj, User):
|
|
return StudentCard.can_create(self.customer, obj)
|
|
return False
|
|
|
|
uid = models.CharField(
|
|
_("uid"), max_length=14, unique=True, validators=[MinLengthValidator(4)]
|
|
)
|
|
customer = models.ForeignKey(
|
|
Customer,
|
|
related_name="student_cards",
|
|
verbose_name=_("student cards"),
|
|
null=False,
|
|
blank=False,
|
|
on_delete=models.CASCADE,
|
|
)
|