use google convention for docstrings

This commit is contained in:
thomas girod
2024-07-12 09:34:16 +02:00
parent 07b625d4aa
commit 8c69a94488
72 changed files with 970 additions and 1694 deletions

View File

@ -21,11 +21,10 @@
class PaymentResultConverter:
"""
Converter used for url mapping of the ``eboutic.views.payment_result``
view.
"""Converter used for url mapping of the `eboutic.views.payment_result` view.
It's meant to build an url that can match
either ``/eboutic/pay/success/`` or ``/eboutic/pay/failure/``
either `/eboutic/pay/success/` or `/eboutic/pay/failure/`
but nothing else.
"""

View File

@ -34,9 +34,8 @@ from eboutic.models import get_eboutic_products
class BasketForm:
"""
Class intended to perform checks on the request sended to the server when
the user submits his basket from /eboutic/
"""Class intended to perform checks on the request sended to the server when
the user submits his basket from /eboutic/.
Because it must check an unknown number of fields, coming from a cookie
and needing some databases checks to be performed, inheriting from forms.Form
@ -45,6 +44,7 @@ class BasketForm:
However, it still tries to share some similarities with a standard django Form.
Example:
-------
::
def my_view(request):
@ -62,6 +62,7 @@ class BasketForm:
You can also use a little shortcut by directly calling `form.is_valid()`
without calling `form.clean()`. In this case, the latter method shall be
implicitly called.
"""
# check the json is an array containing non-nested objects.
@ -85,8 +86,7 @@ class BasketForm:
self.correct_cookie = []
def clean(self) -> None:
"""
Perform all the checks, but return nothing.
"""Perform all the checks, but return nothing.
To know if the form is valid, the `is_valid()` method must be used.
The form shall be considered as valid if it meets all the following conditions :
@ -170,9 +170,9 @@ class BasketForm:
# the form is invalid
def is_valid(self) -> bool:
"""
return True if the form is correct else False.
If the `clean()` method has not been called beforehand, call it
"""Return True if the form is correct else False.
If the `clean()` method has not been called beforehand, call it.
"""
if self.error_messages == set() and self.correct_cookie == []:
self.clean()

View File

@ -12,10 +12,10 @@
# OR WITHIN THE LOCAL FILE "LICENSE"
#
#
from __future__ import annotations
import hmac
import typing
from datetime import datetime
from typing import List
from dict2xml import dict2xml
from django.conf import settings
@ -29,7 +29,7 @@ from core.models import User
from counter.models import BillingInfo, Counter, Customer, Product, Refilling, Selling
def get_eboutic_products(user: User) -> List[Product]:
def get_eboutic_products(user: User) -> list[Product]:
products = (
Counter.objects.get(type="EBOUTIC")
.products.filter(product_type__isnull=False)
@ -43,9 +43,7 @@ def get_eboutic_products(user: User) -> List[Product]:
class Basket(models.Model):
"""
Basket is built when the user connects to an eboutic page
"""
"""Basket is built when the user connects to an eboutic page."""
user = models.ForeignKey(
User,
@ -60,8 +58,7 @@ class Basket(models.Model):
return f"{self.user}'s basket ({self.items.all().count()} items)"
def add_product(self, p: Product, q: int = 1):
"""
Given p an object of the Product model and q an integer,
"""Given p an object of the Product model and q an integer,
add q items corresponding to this Product from the basket.
If this function is called with a product not in the basket, no error will be raised
@ -81,8 +78,7 @@ class Basket(models.Model):
item.save()
def del_product(self, p: Product, q: int = 1):
"""
Given p an object of the Product model and q an integer,
"""Given p an object of the Product model and q an integer
remove q items corresponding to this Product from the basket.
If this function is called with a product not in the basket, no error will be raised
@ -98,9 +94,7 @@ class Basket(models.Model):
item.save()
def clear(self) -> None:
"""
Remove all items from this basket without deleting the basket
"""
"""Remove all items from this basket without deleting the basket."""
self.items.all().delete()
@cached_property
@ -116,11 +110,8 @@ class Basket(models.Model):
return float(total) if total is not None else 0
@classmethod
def from_session(cls, session) -> typing.Union["Basket", None]:
"""
Given an HttpRequest django object, return the basket used in the current session
if it exists else None
"""
def from_session(cls, session) -> Basket | None:
"""The basket stored in the session object, if it exists."""
if "basket_id" in session:
try:
return cls.objects.get(id=session["basket_id"])
@ -129,23 +120,22 @@ class Basket(models.Model):
return None
def generate_sales(self, counter, seller: User, payment_method: str):
"""
Generate a list of sold items corresponding to the items
of this basket WITHOUT saving them NOR deleting the basket
"""Generate a list of sold items corresponding to the items
of this basket WITHOUT saving them NOR deleting the basket.
Example:
::
```python
counter = Counter.objects.get(name="Eboutic")
sales = basket.generate_sales(counter, "SITH_ACCOUNT")
# here the basket is in the same state as before the method call
counter = Counter.objects.get(name="Eboutic")
sales = basket.generate_sales(counter, "SITH_ACCOUNT")
# here the basket is in the same state as before the method call
with transaction.atomic():
for sale in sales:
sale.save()
basket.delete()
# all the basket items are deleted by the on_delete=CASCADE relation
# thus only the sales remain
with transaction.atomic():
for sale in sales:
sale.save()
basket.delete()
# all the basket items are deleted by the on_delete=CASCADE relation
# thus only the sales remain
```
"""
# I must proceed with two distinct requests instead of
# only one with a join because the AbstractBaseItem model has been
@ -212,9 +202,7 @@ class Basket(models.Model):
class Invoice(models.Model):
"""
Invoices are generated once the payment has been validated
"""
"""Invoices are generated once the payment has been validated."""
user = models.ForeignKey(
User,
@ -297,11 +285,12 @@ class BasketItem(AbstractBaseItem):
@classmethod
def from_product(cls, product: Product, quantity: int):
"""
Create a BasketItem with the same characteristics as the
product passed in parameters, with the specified quantity
WARNING : the basket field is not filled, so you must set
it yourself before saving the model
"""Create a BasketItem with the same characteristics as the
product passed in parameters, with the specified quantity.
Warnings:
the basket field is not filled, so you must set
it yourself before saving the model.
"""
return cls(
product_id=product.id,

View File

@ -52,9 +52,9 @@ class EbouticTest(TestCase):
cls.public = User.objects.get(username="public")
def get_busy_basket(self, user) -> Basket:
"""
Create and return a basket with 3 barbar and 1 cotis in it.
Edit the client session to store the basket id in it
"""Create and return a basket with 3 barbar and 1 cotis in it.
Edit the client session to store the basket id in it.
"""
session = self.client.session
basket = Basket.objects.create(user=user)

View File

@ -43,8 +43,8 @@ from eboutic.models import Basket, Invoice, InvoiceItem, get_eboutic_products
@login_required
@require_GET
def eboutic_main(request: HttpRequest) -> HttpResponse:
"""
Main view of the eboutic application.
"""Main view of the eboutic application.
Return an Http response whose content is of type text/html.
The latter represents the page from which a user can see
the catalogue of products that he can buy and fill