mirror of
https://github.com/ae-utbm/sith.git
synced 2025-07-10 03:49:24 +00:00
All: Apply Black coding rules
This commit is contained in:
@ -21,4 +21,3 @@
|
||||
# Place - Suite 330, Boston, MA 02111-1307, USA.
|
||||
#
|
||||
#
|
||||
|
||||
|
@ -8,56 +8,127 @@ from django.conf import settings
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
|
||||
]
|
||||
dependencies = [migrations.swappable_dependency(settings.AUTH_USER_MODEL)]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name='Basket',
|
||||
name="Basket",
|
||||
fields=[
|
||||
('id', models.AutoField(primary_key=True, serialize=False, verbose_name='ID', auto_created=True)),
|
||||
('date', models.DateTimeField(verbose_name='date', auto_now=True)),
|
||||
('user', models.ForeignKey(verbose_name='user', to=settings.AUTH_USER_MODEL, related_name='baskets')),
|
||||
(
|
||||
"id",
|
||||
models.AutoField(
|
||||
primary_key=True,
|
||||
serialize=False,
|
||||
verbose_name="ID",
|
||||
auto_created=True,
|
||||
),
|
||||
),
|
||||
("date", models.DateTimeField(verbose_name="date", auto_now=True)),
|
||||
(
|
||||
"user",
|
||||
models.ForeignKey(
|
||||
verbose_name="user",
|
||||
to=settings.AUTH_USER_MODEL,
|
||||
related_name="baskets",
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
migrations.CreateModel(
|
||||
name='BasketItem',
|
||||
name="BasketItem",
|
||||
fields=[
|
||||
('id', models.AutoField(primary_key=True, serialize=False, verbose_name='ID', auto_created=True)),
|
||||
('product_id', models.IntegerField(verbose_name='product id')),
|
||||
('product_name', models.CharField(max_length=255, verbose_name='product name')),
|
||||
('type_id', models.IntegerField(verbose_name='product type id')),
|
||||
('product_unit_price', accounting.models.CurrencyField(decimal_places=2, max_digits=12, verbose_name='unit price')),
|
||||
('quantity', models.IntegerField(verbose_name='quantity')),
|
||||
('basket', models.ForeignKey(verbose_name='basket', to='eboutic.Basket', related_name='items')),
|
||||
(
|
||||
"id",
|
||||
models.AutoField(
|
||||
primary_key=True,
|
||||
serialize=False,
|
||||
verbose_name="ID",
|
||||
auto_created=True,
|
||||
),
|
||||
),
|
||||
("product_id", models.IntegerField(verbose_name="product id")),
|
||||
(
|
||||
"product_name",
|
||||
models.CharField(max_length=255, verbose_name="product name"),
|
||||
),
|
||||
("type_id", models.IntegerField(verbose_name="product type id")),
|
||||
(
|
||||
"product_unit_price",
|
||||
accounting.models.CurrencyField(
|
||||
decimal_places=2, max_digits=12, verbose_name="unit price"
|
||||
),
|
||||
),
|
||||
("quantity", models.IntegerField(verbose_name="quantity")),
|
||||
(
|
||||
"basket",
|
||||
models.ForeignKey(
|
||||
verbose_name="basket", to="eboutic.Basket", related_name="items"
|
||||
),
|
||||
),
|
||||
],
|
||||
options={
|
||||
'abstract': False,
|
||||
},
|
||||
options={"abstract": False},
|
||||
),
|
||||
migrations.CreateModel(
|
||||
name='Invoice',
|
||||
name="Invoice",
|
||||
fields=[
|
||||
('id', models.AutoField(primary_key=True, serialize=False, verbose_name='ID', auto_created=True)),
|
||||
('date', models.DateTimeField(verbose_name='date', auto_now=True)),
|
||||
('validated', models.BooleanField(verbose_name='validated', default=False)),
|
||||
('user', models.ForeignKey(verbose_name='user', to=settings.AUTH_USER_MODEL, related_name='invoices')),
|
||||
(
|
||||
"id",
|
||||
models.AutoField(
|
||||
primary_key=True,
|
||||
serialize=False,
|
||||
verbose_name="ID",
|
||||
auto_created=True,
|
||||
),
|
||||
),
|
||||
("date", models.DateTimeField(verbose_name="date", auto_now=True)),
|
||||
(
|
||||
"validated",
|
||||
models.BooleanField(verbose_name="validated", default=False),
|
||||
),
|
||||
(
|
||||
"user",
|
||||
models.ForeignKey(
|
||||
verbose_name="user",
|
||||
to=settings.AUTH_USER_MODEL,
|
||||
related_name="invoices",
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
migrations.CreateModel(
|
||||
name='InvoiceItem',
|
||||
name="InvoiceItem",
|
||||
fields=[
|
||||
('id', models.AutoField(primary_key=True, serialize=False, verbose_name='ID', auto_created=True)),
|
||||
('product_id', models.IntegerField(verbose_name='product id')),
|
||||
('product_name', models.CharField(max_length=255, verbose_name='product name')),
|
||||
('type_id', models.IntegerField(verbose_name='product type id')),
|
||||
('product_unit_price', accounting.models.CurrencyField(decimal_places=2, max_digits=12, verbose_name='unit price')),
|
||||
('quantity', models.IntegerField(verbose_name='quantity')),
|
||||
('invoice', models.ForeignKey(verbose_name='invoice', to='eboutic.Invoice', related_name='items')),
|
||||
(
|
||||
"id",
|
||||
models.AutoField(
|
||||
primary_key=True,
|
||||
serialize=False,
|
||||
verbose_name="ID",
|
||||
auto_created=True,
|
||||
),
|
||||
),
|
||||
("product_id", models.IntegerField(verbose_name="product id")),
|
||||
(
|
||||
"product_name",
|
||||
models.CharField(max_length=255, verbose_name="product name"),
|
||||
),
|
||||
("type_id", models.IntegerField(verbose_name="product type id")),
|
||||
(
|
||||
"product_unit_price",
|
||||
accounting.models.CurrencyField(
|
||||
decimal_places=2, max_digits=12, verbose_name="unit price"
|
||||
),
|
||||
),
|
||||
("quantity", models.IntegerField(verbose_name="quantity")),
|
||||
(
|
||||
"invoice",
|
||||
models.ForeignKey(
|
||||
verbose_name="invoice",
|
||||
to="eboutic.Invoice",
|
||||
related_name="items",
|
||||
),
|
||||
),
|
||||
],
|
||||
options={
|
||||
'abstract': False,
|
||||
},
|
||||
options={"abstract": False},
|
||||
),
|
||||
]
|
||||
|
@ -35,14 +35,23 @@ class Basket(models.Model):
|
||||
"""
|
||||
Basket is built when the user connects to an eboutic page
|
||||
"""
|
||||
user = models.ForeignKey(User, related_name='baskets', verbose_name=_('user'), blank=False)
|
||||
date = models.DateTimeField(_('date'), auto_now=True)
|
||||
|
||||
user = models.ForeignKey(
|
||||
User, related_name="baskets", verbose_name=_("user"), blank=False
|
||||
)
|
||||
date = models.DateTimeField(_("date"), auto_now=True)
|
||||
|
||||
def add_product(self, p, q=1):
|
||||
item = self.items.filter(product_id=p.id).first()
|
||||
if item is None:
|
||||
BasketItem(basket=self, product_id=p.id, product_name=p.name, type_id=p.product_type.id,
|
||||
quantity=q, product_unit_price=p.selling_price).save()
|
||||
BasketItem(
|
||||
basket=self,
|
||||
product_id=p.id,
|
||||
product_name=p.name,
|
||||
type_id=p.product_type.id,
|
||||
quantity=q,
|
||||
product_unit_price=p.selling_price,
|
||||
).save()
|
||||
else:
|
||||
item.quantity += q
|
||||
item.save()
|
||||
@ -69,8 +78,11 @@ class Invoice(models.Model):
|
||||
"""
|
||||
Invoices are generated once the payment has been validated
|
||||
"""
|
||||
user = models.ForeignKey(User, related_name='invoices', verbose_name=_('user'), blank=False)
|
||||
date = models.DateTimeField(_('date'), auto_now=True)
|
||||
|
||||
user = models.ForeignKey(
|
||||
User, related_name="invoices", verbose_name=_("user"), blank=False
|
||||
)
|
||||
date = models.DateTimeField(_("date"), auto_now=True)
|
||||
validated = models.BooleanField(_("validated"), default=False)
|
||||
|
||||
def __str__(self):
|
||||
@ -86,9 +98,14 @@ class Invoice(models.Model):
|
||||
if self.validated:
|
||||
raise DataError(_("Invoice already validated"))
|
||||
from counter.models import Customer
|
||||
|
||||
if not Customer.objects.filter(user=self.user).exists():
|
||||
number = Customer.objects.count() + 1
|
||||
Customer(user=self.user, account_id=Customer.generate_account_id(number), amount=0).save()
|
||||
Customer(
|
||||
user=self.user,
|
||||
account_id=Customer.generate_account_id(number),
|
||||
amount=0,
|
||||
).save()
|
||||
eboutic = Counter.objects.filter(type="EBOUTIC").first()
|
||||
for i in self.items.all():
|
||||
if i.type_id == settings.SITH_COUNTER_PRODUCTTYPE_REFILLING:
|
||||
@ -123,22 +140,28 @@ class Invoice(models.Model):
|
||||
|
||||
|
||||
class AbstractBaseItem(models.Model):
|
||||
product_id = models.IntegerField(_('product id'))
|
||||
product_name = models.CharField(_('product name'), max_length=255)
|
||||
type_id = models.IntegerField(_('product type id'))
|
||||
product_unit_price = CurrencyField(_('unit price'))
|
||||
quantity = models.IntegerField(_('quantity'))
|
||||
product_id = models.IntegerField(_("product id"))
|
||||
product_name = models.CharField(_("product name"), max_length=255)
|
||||
type_id = models.IntegerField(_("product type id"))
|
||||
product_unit_price = CurrencyField(_("unit price"))
|
||||
quantity = models.IntegerField(_("quantity"))
|
||||
|
||||
class Meta:
|
||||
abstract = True
|
||||
|
||||
def __str__(self):
|
||||
return "Item: %s (%s) x%d" % (self.product_name, self.product_unit_price, self.quantity)
|
||||
return "Item: %s (%s) x%d" % (
|
||||
self.product_name,
|
||||
self.product_unit_price,
|
||||
self.quantity,
|
||||
)
|
||||
|
||||
|
||||
class BasketItem(AbstractBaseItem):
|
||||
basket = models.ForeignKey(Basket, related_name='items', verbose_name=_('basket'))
|
||||
basket = models.ForeignKey(Basket, related_name="items", verbose_name=_("basket"))
|
||||
|
||||
|
||||
class InvoiceItem(AbstractBaseItem):
|
||||
invoice = models.ForeignKey(Invoice, related_name='items', verbose_name=_('invoice'))
|
||||
invoice = models.ForeignKey(
|
||||
Invoice, related_name="items", verbose_name=_("invoice")
|
||||
)
|
||||
|
245
eboutic/tests.py
245
eboutic/tests.py
@ -62,112 +62,193 @@ class EbouticTest(TestCase):
|
||||
sig = crypto.sign(privkey, query, "sha1")
|
||||
b64sig = base64.b64encode(sig).decode("ascii")
|
||||
|
||||
url = reverse("eboutic:etransation_autoanswer") + "?%s&Sig=%s" % (query, urllib.parse.quote_plus(b64sig))
|
||||
url = reverse("eboutic:etransation_autoanswer") + "?%s&Sig=%s" % (
|
||||
query,
|
||||
urllib.parse.quote_plus(b64sig),
|
||||
)
|
||||
response = self.client.get(url)
|
||||
self.assertTrue(response.status_code == 200)
|
||||
self.assertTrue(response.content.decode("utf-8") == "")
|
||||
return response
|
||||
|
||||
def test_buy_simple_product_with_sith_account(self):
|
||||
self.client.login(username='subscriber', password='plop')
|
||||
Refilling(amount=10, counter=self.eboutic, operator=self.skia, customer=self.subscriber.customer).save()
|
||||
response = self.client.post(reverse("eboutic:main"), {
|
||||
"action": "add_product",
|
||||
"product_id": self.barbar.id})
|
||||
self.assertTrue("<input type=\"hidden\" name=\"action\" value=\"add_product\">\\n"
|
||||
" <button type=\"submit\" name=\"product_id\" value=\"4\"> + </button>\\n"
|
||||
"</form>\\n Barbar: 1.70 \\xe2\\x82\\xac</li>" in str(response.content))
|
||||
self.client.login(username="subscriber", password="plop")
|
||||
Refilling(
|
||||
amount=10,
|
||||
counter=self.eboutic,
|
||||
operator=self.skia,
|
||||
customer=self.subscriber.customer,
|
||||
).save()
|
||||
response = self.client.post(
|
||||
reverse("eboutic:main"),
|
||||
{"action": "add_product", "product_id": self.barbar.id},
|
||||
)
|
||||
self.assertTrue(
|
||||
'<input type="hidden" name="action" value="add_product">\\n'
|
||||
' <button type="submit" name="product_id" value="4"> + </button>\\n'
|
||||
"</form>\\n Barbar: 1.70 \\xe2\\x82\\xac</li>" in str(response.content)
|
||||
)
|
||||
response = self.client.post(reverse("eboutic:command"))
|
||||
self.assertTrue("<tr>\\n <td>Barbar</td>\\n <td>1</td>\\n"
|
||||
" <td>1.70 \\xe2\\x82\\xac</td>\\n </tr>" in str(response.content))
|
||||
response = self.client.post(reverse("eboutic:pay_with_sith"), {
|
||||
"action": "pay_with_sith_account"
|
||||
})
|
||||
self.assertTrue("Le paiement a \\xc3\\xa9t\\xc3\\xa9 effectu\\xc3\\xa9\\n" in str(response.content))
|
||||
response = self.client.get(reverse("core:user_account_detail", kwargs={
|
||||
"user_id": self.subscriber.id,
|
||||
"year": datetime.now().year,
|
||||
"month": datetime.now().month,
|
||||
}))
|
||||
self.assertTrue("class=\"selected_tab\">Compte (8.30 \\xe2\\x82\\xac)</a>" in str(response.content))
|
||||
self.assertTrue("<td>Eboutic</td>\\n <td><a href=\"/user/3/\">Subscribed User</a></td>\\n"
|
||||
" <td>Barbar</td>\\n <td>1</td>\\n <td>1.70 \\xe2\\x82\\xac</td>\\n"
|
||||
" <td>Compte utilisateur</td>" in str(response.content))
|
||||
self.assertTrue(
|
||||
"<tr>\\n <td>Barbar</td>\\n <td>1</td>\\n"
|
||||
" <td>1.70 \\xe2\\x82\\xac</td>\\n </tr>"
|
||||
in str(response.content)
|
||||
)
|
||||
response = self.client.post(
|
||||
reverse("eboutic:pay_with_sith"), {"action": "pay_with_sith_account"}
|
||||
)
|
||||
self.assertTrue(
|
||||
"Le paiement a \\xc3\\xa9t\\xc3\\xa9 effectu\\xc3\\xa9\\n"
|
||||
in str(response.content)
|
||||
)
|
||||
response = self.client.get(
|
||||
reverse(
|
||||
"core:user_account_detail",
|
||||
kwargs={
|
||||
"user_id": self.subscriber.id,
|
||||
"year": datetime.now().year,
|
||||
"month": datetime.now().month,
|
||||
},
|
||||
)
|
||||
)
|
||||
self.assertTrue(
|
||||
'class="selected_tab">Compte (8.30 \\xe2\\x82\\xac)</a>'
|
||||
in str(response.content)
|
||||
)
|
||||
self.assertTrue(
|
||||
'<td>Eboutic</td>\\n <td><a href="/user/3/">Subscribed User</a></td>\\n'
|
||||
" <td>Barbar</td>\\n <td>1</td>\\n <td>1.70 \\xe2\\x82\\xac</td>\\n"
|
||||
" <td>Compte utilisateur</td>" in str(response.content)
|
||||
)
|
||||
|
||||
def test_buy_simple_product_with_credit_card(self):
|
||||
self.client.login(username='subscriber', password='plop')
|
||||
response = self.client.post(reverse("eboutic:main"), {
|
||||
"action": "add_product",
|
||||
"product_id": self.barbar.id})
|
||||
self.assertTrue("<input type=\"hidden\" name=\"action\" value=\"add_product\">\\n"
|
||||
" <button type=\"submit\" name=\"product_id\" value=\"4\"> + </button>\\n"
|
||||
"</form>\\n Barbar: 1.70 \\xe2\\x82\\xac</li>" in str(response.content))
|
||||
self.client.login(username="subscriber", password="plop")
|
||||
response = self.client.post(
|
||||
reverse("eboutic:main"),
|
||||
{"action": "add_product", "product_id": self.barbar.id},
|
||||
)
|
||||
self.assertTrue(
|
||||
'<input type="hidden" name="action" value="add_product">\\n'
|
||||
' <button type="submit" name="product_id" value="4"> + </button>\\n'
|
||||
"</form>\\n Barbar: 1.70 \\xe2\\x82\\xac</li>" in str(response.content)
|
||||
)
|
||||
response = self.client.post(reverse("eboutic:command"))
|
||||
self.assertTrue("<tr>\\n <td>Barbar</td>\\n <td>1</td>\\n"
|
||||
" <td>1.70 \\xe2\\x82\\xac</td>\\n </tr>" in str(response.content))
|
||||
self.assertTrue(
|
||||
"<tr>\\n <td>Barbar</td>\\n <td>1</td>\\n"
|
||||
" <td>1.70 \\xe2\\x82\\xac</td>\\n </tr>"
|
||||
in str(response.content)
|
||||
)
|
||||
|
||||
response = self.generate_bank_valid_answer_from_page_content(response.content)
|
||||
|
||||
response = self.client.get(reverse("core:user_account_detail", kwargs={
|
||||
"user_id": self.subscriber.id,
|
||||
"year": datetime.now().year,
|
||||
"month": datetime.now().month,
|
||||
}))
|
||||
self.assertTrue("class=\"selected_tab\">Compte (0.00 \\xe2\\x82\\xac)</a>" in str(response.content))
|
||||
self.assertTrue("<td>Eboutic</td>\\n <td><a href=\"/user/3/\">Subscribed User</a></td>\\n"
|
||||
" <td>Barbar</td>\\n <td>1</td>\\n <td>1.70 \\xe2\\x82\\xac</td>\\n"
|
||||
" <td>Carte bancaire</td>" in str(response.content))
|
||||
response = self.client.get(
|
||||
reverse(
|
||||
"core:user_account_detail",
|
||||
kwargs={
|
||||
"user_id": self.subscriber.id,
|
||||
"year": datetime.now().year,
|
||||
"month": datetime.now().month,
|
||||
},
|
||||
)
|
||||
)
|
||||
self.assertTrue(
|
||||
'class="selected_tab">Compte (0.00 \\xe2\\x82\\xac)</a>'
|
||||
in str(response.content)
|
||||
)
|
||||
self.assertTrue(
|
||||
'<td>Eboutic</td>\\n <td><a href="/user/3/">Subscribed User</a></td>\\n'
|
||||
" <td>Barbar</td>\\n <td>1</td>\\n <td>1.70 \\xe2\\x82\\xac</td>\\n"
|
||||
" <td>Carte bancaire</td>" in str(response.content)
|
||||
)
|
||||
|
||||
def test_buy_refill_product_with_credit_card(self):
|
||||
self.client.login(username='subscriber', password='plop')
|
||||
response = self.client.post(reverse("eboutic:main"), {
|
||||
"action": "add_product",
|
||||
"product_id": self.refill.id})
|
||||
self.assertTrue("<input type=\"hidden\" name=\"action\" value=\"add_product\">\\n"
|
||||
" <button type=\"submit\" name=\"product_id\" value=\"3\"> + </button>\\n"
|
||||
"</form>\\n Rechargement 15 \\xe2\\x82\\xac: 15.00 \\xe2\\x82\\xac</li>" in str(response.content))
|
||||
self.client.login(username="subscriber", password="plop")
|
||||
response = self.client.post(
|
||||
reverse("eboutic:main"),
|
||||
{"action": "add_product", "product_id": self.refill.id},
|
||||
)
|
||||
self.assertTrue(
|
||||
'<input type="hidden" name="action" value="add_product">\\n'
|
||||
' <button type="submit" name="product_id" value="3"> + </button>\\n'
|
||||
"</form>\\n Rechargement 15 \\xe2\\x82\\xac: 15.00 \\xe2\\x82\\xac</li>"
|
||||
in str(response.content)
|
||||
)
|
||||
response = self.client.post(reverse("eboutic:command"))
|
||||
self.assertTrue("<tr>\\n <td>Rechargement 15 \\xe2\\x82\\xac</td>\\n <td>1</td>\\n"
|
||||
" <td>15.00 \\xe2\\x82\\xac</td>\\n </tr>" in str(response.content))
|
||||
self.assertTrue(
|
||||
"<tr>\\n <td>Rechargement 15 \\xe2\\x82\\xac</td>\\n <td>1</td>\\n"
|
||||
" <td>15.00 \\xe2\\x82\\xac</td>\\n </tr>"
|
||||
in str(response.content)
|
||||
)
|
||||
|
||||
response = self.generate_bank_valid_answer_from_page_content(response.content)
|
||||
|
||||
response = self.client.get(reverse("core:user_account_detail", kwargs={
|
||||
"user_id": self.subscriber.id,
|
||||
"year": datetime.now().year,
|
||||
"month": datetime.now().month,
|
||||
}))
|
||||
self.assertTrue("class=\"selected_tab\">Compte (15.00 \\xe2\\x82\\xac)</a>" in str(response.content))
|
||||
self.assertTrue("<td>\\n <ul>\\n \\n "
|
||||
"<li>1 x Rechargement 15 \\xe2\\x82\\xac - 15.00 \\xe2\\x82\\xac</li>\\n"
|
||||
" \\n </ul>\\n </td>\\n"
|
||||
" <td>15.00 \\xe2\\x82\\xac</td>" in str(response.content))
|
||||
response = self.client.get(
|
||||
reverse(
|
||||
"core:user_account_detail",
|
||||
kwargs={
|
||||
"user_id": self.subscriber.id,
|
||||
"year": datetime.now().year,
|
||||
"month": datetime.now().month,
|
||||
},
|
||||
)
|
||||
)
|
||||
self.assertTrue(
|
||||
'class="selected_tab">Compte (15.00 \\xe2\\x82\\xac)</a>'
|
||||
in str(response.content)
|
||||
)
|
||||
self.assertTrue(
|
||||
"<td>\\n <ul>\\n \\n "
|
||||
"<li>1 x Rechargement 15 \\xe2\\x82\\xac - 15.00 \\xe2\\x82\\xac</li>\\n"
|
||||
" \\n </ul>\\n </td>\\n"
|
||||
" <td>15.00 \\xe2\\x82\\xac</td>" in str(response.content)
|
||||
)
|
||||
|
||||
def test_buy_subscribe_product_with_credit_card(self):
|
||||
self.client.login(username='old_subscriber', password='plop')
|
||||
response = self.client.get(reverse("core:user_profile", kwargs={"user_id": self.old_subscriber.id}))
|
||||
self.client.login(username="old_subscriber", password="plop")
|
||||
response = self.client.get(
|
||||
reverse("core:user_profile", kwargs={"user_id": self.old_subscriber.id})
|
||||
)
|
||||
self.assertTrue("Non cotisant" in str(response.content))
|
||||
response = self.client.post(reverse("eboutic:main"), {
|
||||
"action": "add_product",
|
||||
"product_id": self.cotis.id})
|
||||
self.assertTrue("<input type=\"hidden\" name=\"action\" value=\"add_product\">\\n"
|
||||
" <button type=\"submit\" name=\"product_id\" value=\"1\"> + </button>\\n"
|
||||
"</form>\\n Cotis 1 semestre: 15.00 \\xe2\\x82\\xac</li>" in str(response.content))
|
||||
response = self.client.post(
|
||||
reverse("eboutic:main"),
|
||||
{"action": "add_product", "product_id": self.cotis.id},
|
||||
)
|
||||
self.assertTrue(
|
||||
'<input type="hidden" name="action" value="add_product">\\n'
|
||||
' <button type="submit" name="product_id" value="1"> + </button>\\n'
|
||||
"</form>\\n Cotis 1 semestre: 15.00 \\xe2\\x82\\xac</li>"
|
||||
in str(response.content)
|
||||
)
|
||||
response = self.client.post(reverse("eboutic:command"))
|
||||
self.assertTrue("<tr>\\n <td>Cotis 1 semestre</td>\\n <td>1</td>\\n"
|
||||
" <td>15.00 \\xe2\\x82\\xac</td>\\n </tr>" in str(response.content))
|
||||
self.assertTrue(
|
||||
"<tr>\\n <td>Cotis 1 semestre</td>\\n <td>1</td>\\n"
|
||||
" <td>15.00 \\xe2\\x82\\xac</td>\\n </tr>"
|
||||
in str(response.content)
|
||||
)
|
||||
|
||||
response = self.generate_bank_valid_answer_from_page_content(response.content)
|
||||
|
||||
response = self.client.get(reverse("core:user_account_detail", kwargs={
|
||||
"user_id": self.old_subscriber.id,
|
||||
"year": datetime.now().year,
|
||||
"month": datetime.now().month,
|
||||
}))
|
||||
self.assertTrue("class=\"selected_tab\">Compte (0.00 \\xe2\\x82\\xac)</a>" in str(response.content))
|
||||
self.assertTrue("<td>\\n <ul>\\n \\n "
|
||||
"<li>1 x Cotis 1 semestre - 15.00 \\xe2\\x82\\xac</li>\\n"
|
||||
" \\n </ul>\\n </td>\\n"
|
||||
" <td>15.00 \\xe2\\x82\\xac</td>" in str(response.content))
|
||||
response = self.client.get(reverse("core:user_profile", kwargs={"user_id": self.old_subscriber.id}))
|
||||
response = self.client.get(
|
||||
reverse(
|
||||
"core:user_account_detail",
|
||||
kwargs={
|
||||
"user_id": self.old_subscriber.id,
|
||||
"year": datetime.now().year,
|
||||
"month": datetime.now().month,
|
||||
},
|
||||
)
|
||||
)
|
||||
self.assertTrue(
|
||||
'class="selected_tab">Compte (0.00 \\xe2\\x82\\xac)</a>'
|
||||
in str(response.content)
|
||||
)
|
||||
self.assertTrue(
|
||||
"<td>\\n <ul>\\n \\n "
|
||||
"<li>1 x Cotis 1 semestre - 15.00 \\xe2\\x82\\xac</li>\\n"
|
||||
" \\n </ul>\\n </td>\\n"
|
||||
" <td>15.00 \\xe2\\x82\\xac</td>" in str(response.content)
|
||||
)
|
||||
response = self.client.get(
|
||||
reverse("core:user_profile", kwargs={"user_id": self.old_subscriber.id})
|
||||
)
|
||||
self.assertTrue("Cotisant jusqu\\'au" in str(response.content))
|
||||
|
@ -32,7 +32,3 @@ try:
|
||||
print("Verify OK")
|
||||
except:
|
||||
print("Verify failed")
|
||||
|
||||
|
||||
|
||||
|
||||
|
@ -28,8 +28,12 @@ from eboutic.views import *
|
||||
|
||||
urlpatterns = [
|
||||
# Subscription views
|
||||
url(r'^$', EbouticMain.as_view(), name='main'),
|
||||
url(r'^command$', EbouticCommand.as_view(), name='command'),
|
||||
url(r'^pay$', EbouticPayWithSith.as_view(), name='pay_with_sith'),
|
||||
url(r'^et_autoanswer$', EtransactionAutoAnswer.as_view(), name='etransation_autoanswer'),
|
||||
url(r"^$", EbouticMain.as_view(), name="main"),
|
||||
url(r"^command$", EbouticCommand.as_view(), name="command"),
|
||||
url(r"^pay$", EbouticPayWithSith.as_view(), name="pay_with_sith"),
|
||||
url(
|
||||
r"^et_autoanswer$",
|
||||
EtransactionAutoAnswer.as_view(),
|
||||
name="etransation_autoanswer",
|
||||
),
|
||||
]
|
||||
|
200
eboutic/views.py
200
eboutic/views.py
@ -41,44 +41,50 @@ from eboutic.models import Basket, Invoice, InvoiceItem
|
||||
|
||||
|
||||
class EbouticMain(TemplateView):
|
||||
template_name = 'eboutic/eboutic_main.jinja'
|
||||
template_name = "eboutic/eboutic_main.jinja"
|
||||
|
||||
def make_basket(self, request):
|
||||
if 'basket_id' not in request.session.keys(): # Init the basket session entry
|
||||
if "basket_id" not in request.session.keys(): # Init the basket session entry
|
||||
self.basket = Basket(user=request.user)
|
||||
self.basket.save()
|
||||
else:
|
||||
self.basket = Basket.objects.filter(id=request.session['basket_id']).first()
|
||||
self.basket = Basket.objects.filter(id=request.session["basket_id"]).first()
|
||||
if self.basket is None:
|
||||
self.basket = Basket(user=request.user)
|
||||
self.basket.save()
|
||||
request.session['basket_id'] = self.basket.id
|
||||
request.session["basket_id"] = self.basket.id
|
||||
request.session.modified = True
|
||||
|
||||
def get(self, request, *args, **kwargs):
|
||||
if not request.user.is_authenticated():
|
||||
return HttpResponseRedirect(reverse_lazy('core:login', args=self.args, kwargs=kwargs) + "?next=" +
|
||||
request.path)
|
||||
return HttpResponseRedirect(
|
||||
reverse_lazy("core:login", args=self.args, kwargs=kwargs)
|
||||
+ "?next="
|
||||
+ request.path
|
||||
)
|
||||
self.object = Counter.objects.filter(type="EBOUTIC").first()
|
||||
self.make_basket(request)
|
||||
return super(EbouticMain, self).get(request, *args, **kwargs)
|
||||
|
||||
def post(self, request, *args, **kwargs):
|
||||
if not request.user.is_authenticated():
|
||||
return HttpResponseRedirect(reverse_lazy('core:login', args=self.args, kwargs=kwargs) + "?next=" +
|
||||
request.path)
|
||||
return HttpResponseRedirect(
|
||||
reverse_lazy("core:login", args=self.args, kwargs=kwargs)
|
||||
+ "?next="
|
||||
+ request.path
|
||||
)
|
||||
self.object = Counter.objects.filter(type="EBOUTIC").first()
|
||||
self.make_basket(request)
|
||||
if 'add_product' in request.POST['action']:
|
||||
if "add_product" in request.POST["action"]:
|
||||
self.add_product(request)
|
||||
elif 'del_product' in request.POST['action']:
|
||||
elif "del_product" in request.POST["action"]:
|
||||
self.del_product(request)
|
||||
return self.render_to_response(self.get_context_data(**kwargs))
|
||||
|
||||
def add_product(self, request):
|
||||
""" Add a product to the basket """
|
||||
try:
|
||||
p = self.object.products.filter(id=int(request.POST['product_id'])).first()
|
||||
p = self.object.products.filter(id=int(request.POST["product_id"])).first()
|
||||
if not p.buying_groups.exists():
|
||||
self.basket.add_product(p)
|
||||
for g in p.buying_groups.all():
|
||||
@ -91,79 +97,122 @@ class EbouticMain(TemplateView):
|
||||
def del_product(self, request):
|
||||
""" Delete a product from the basket """
|
||||
try:
|
||||
p = self.object.products.filter(id=int(request.POST['product_id'])).first()
|
||||
p = self.object.products.filter(id=int(request.POST["product_id"])).first()
|
||||
self.basket.del_product(p)
|
||||
except:
|
||||
pass
|
||||
|
||||
def get_context_data(self, **kwargs):
|
||||
kwargs = super(EbouticMain, self).get_context_data(**kwargs)
|
||||
kwargs['basket'] = self.basket
|
||||
kwargs['eboutic'] = Counter.objects.filter(type="EBOUTIC").first()
|
||||
kwargs['categories'] = ProductType.objects.all()
|
||||
kwargs["basket"] = self.basket
|
||||
kwargs["eboutic"] = Counter.objects.filter(type="EBOUTIC").first()
|
||||
kwargs["categories"] = ProductType.objects.all()
|
||||
if not self.request.user.was_subscribed:
|
||||
kwargs['categories'] = kwargs['categories'].exclude(id=settings.SITH_PRODUCTTYPE_SUBSCRIPTION)
|
||||
kwargs["categories"] = kwargs["categories"].exclude(
|
||||
id=settings.SITH_PRODUCTTYPE_SUBSCRIPTION
|
||||
)
|
||||
return kwargs
|
||||
|
||||
|
||||
class EbouticCommand(TemplateView):
|
||||
template_name = 'eboutic/eboutic_makecommand.jinja'
|
||||
template_name = "eboutic/eboutic_makecommand.jinja"
|
||||
|
||||
def get(self, request, *args, **kwargs):
|
||||
if not request.user.is_authenticated():
|
||||
return HttpResponseRedirect(reverse_lazy('core:login', args=self.args, kwargs=kwargs) + "?next=" +
|
||||
request.path)
|
||||
return HttpResponseRedirect(reverse_lazy('eboutic:main', args=self.args, kwargs=kwargs))
|
||||
return HttpResponseRedirect(
|
||||
reverse_lazy("core:login", args=self.args, kwargs=kwargs)
|
||||
+ "?next="
|
||||
+ request.path
|
||||
)
|
||||
return HttpResponseRedirect(
|
||||
reverse_lazy("eboutic:main", args=self.args, kwargs=kwargs)
|
||||
)
|
||||
|
||||
def post(self, request, *args, **kwargs):
|
||||
if not request.user.is_authenticated():
|
||||
return HttpResponseRedirect(reverse_lazy('core:login', args=self.args, kwargs=kwargs) + "?next=" +
|
||||
request.path)
|
||||
if 'basket_id' not in request.session.keys():
|
||||
return HttpResponseRedirect(reverse_lazy('eboutic:main', args=self.args, kwargs=kwargs))
|
||||
self.basket = Basket.objects.filter(id=request.session['basket_id']).first()
|
||||
return HttpResponseRedirect(
|
||||
reverse_lazy("core:login", args=self.args, kwargs=kwargs)
|
||||
+ "?next="
|
||||
+ request.path
|
||||
)
|
||||
if "basket_id" not in request.session.keys():
|
||||
return HttpResponseRedirect(
|
||||
reverse_lazy("eboutic:main", args=self.args, kwargs=kwargs)
|
||||
)
|
||||
self.basket = Basket.objects.filter(id=request.session["basket_id"]).first()
|
||||
if self.basket is None:
|
||||
return HttpResponseRedirect(reverse_lazy('eboutic:main', args=self.args, kwargs=kwargs))
|
||||
return HttpResponseRedirect(
|
||||
reverse_lazy("eboutic:main", args=self.args, kwargs=kwargs)
|
||||
)
|
||||
else:
|
||||
kwargs['basket'] = self.basket
|
||||
kwargs["basket"] = self.basket
|
||||
return self.render_to_response(self.get_context_data(**kwargs))
|
||||
|
||||
def get_context_data(self, **kwargs):
|
||||
kwargs = super(EbouticCommand, self).get_context_data(**kwargs)
|
||||
kwargs['et_request'] = OrderedDict()
|
||||
kwargs['et_request']['PBX_SITE'] = settings.SITH_EBOUTIC_PBX_SITE
|
||||
kwargs['et_request']['PBX_RANG'] = settings.SITH_EBOUTIC_PBX_RANG
|
||||
kwargs['et_request']['PBX_IDENTIFIANT'] = settings.SITH_EBOUTIC_PBX_IDENTIFIANT
|
||||
kwargs['et_request']['PBX_TOTAL'] = int(self.basket.get_total() * 100)
|
||||
kwargs['et_request']['PBX_DEVISE'] = 978 # This is Euro. ET support only this value anyway
|
||||
kwargs['et_request']['PBX_CMD'] = self.basket.id
|
||||
kwargs['et_request']['PBX_PORTEUR'] = self.basket.user.email
|
||||
kwargs['et_request']['PBX_RETOUR'] = "Amount:M;BasketID:R;Auto:A;Error:E;Sig:K"
|
||||
kwargs['et_request']['PBX_HASH'] = "SHA512"
|
||||
kwargs['et_request']['PBX_TYPEPAIEMENT'] = "CARTE"
|
||||
kwargs['et_request']['PBX_TYPECARTE'] = "CB"
|
||||
kwargs['et_request']['PBX_TIME'] = str(datetime.now().replace(microsecond=0).isoformat('T'))
|
||||
kwargs['et_request']['PBX_HMAC'] = hmac.new(settings.SITH_EBOUTIC_HMAC_KEY,
|
||||
bytes("&".join(["%s=%s" % (k, v) for k, v in kwargs['et_request'].items()]), 'utf-8'),
|
||||
"sha512").hexdigest().upper()
|
||||
kwargs["et_request"] = OrderedDict()
|
||||
kwargs["et_request"]["PBX_SITE"] = settings.SITH_EBOUTIC_PBX_SITE
|
||||
kwargs["et_request"]["PBX_RANG"] = settings.SITH_EBOUTIC_PBX_RANG
|
||||
kwargs["et_request"]["PBX_IDENTIFIANT"] = settings.SITH_EBOUTIC_PBX_IDENTIFIANT
|
||||
kwargs["et_request"]["PBX_TOTAL"] = int(self.basket.get_total() * 100)
|
||||
kwargs["et_request"][
|
||||
"PBX_DEVISE"
|
||||
] = 978 # This is Euro. ET support only this value anyway
|
||||
kwargs["et_request"]["PBX_CMD"] = self.basket.id
|
||||
kwargs["et_request"]["PBX_PORTEUR"] = self.basket.user.email
|
||||
kwargs["et_request"]["PBX_RETOUR"] = "Amount:M;BasketID:R;Auto:A;Error:E;Sig:K"
|
||||
kwargs["et_request"]["PBX_HASH"] = "SHA512"
|
||||
kwargs["et_request"]["PBX_TYPEPAIEMENT"] = "CARTE"
|
||||
kwargs["et_request"]["PBX_TYPECARTE"] = "CB"
|
||||
kwargs["et_request"]["PBX_TIME"] = str(
|
||||
datetime.now().replace(microsecond=0).isoformat("T")
|
||||
)
|
||||
kwargs["et_request"]["PBX_HMAC"] = (
|
||||
hmac.new(
|
||||
settings.SITH_EBOUTIC_HMAC_KEY,
|
||||
bytes(
|
||||
"&".join(
|
||||
["%s=%s" % (k, v) for k, v in kwargs["et_request"].items()]
|
||||
),
|
||||
"utf-8",
|
||||
),
|
||||
"sha512",
|
||||
)
|
||||
.hexdigest()
|
||||
.upper()
|
||||
)
|
||||
return kwargs
|
||||
|
||||
|
||||
class EbouticPayWithSith(TemplateView):
|
||||
template_name = 'eboutic/eboutic_payment_result.jinja'
|
||||
template_name = "eboutic/eboutic_payment_result.jinja"
|
||||
|
||||
def post(self, request, *args, **kwargs):
|
||||
try:
|
||||
with transaction.atomic():
|
||||
if 'basket_id' not in request.session.keys() or not request.user.is_authenticated():
|
||||
return HttpResponseRedirect(reverse_lazy('eboutic:main', args=self.args, kwargs=kwargs))
|
||||
b = Basket.objects.filter(id=request.session['basket_id']).first()
|
||||
if b is None or b.items.filter(type_id=settings.SITH_COUNTER_PRODUCTTYPE_REFILLING).exists():
|
||||
return HttpResponseRedirect(reverse_lazy('eboutic:main', args=self.args, kwargs=kwargs))
|
||||
if (
|
||||
"basket_id" not in request.session.keys()
|
||||
or not request.user.is_authenticated()
|
||||
):
|
||||
return HttpResponseRedirect(
|
||||
reverse_lazy("eboutic:main", args=self.args, kwargs=kwargs)
|
||||
)
|
||||
b = Basket.objects.filter(id=request.session["basket_id"]).first()
|
||||
if (
|
||||
b is None
|
||||
or b.items.filter(
|
||||
type_id=settings.SITH_COUNTER_PRODUCTTYPE_REFILLING
|
||||
).exists()
|
||||
):
|
||||
return HttpResponseRedirect(
|
||||
reverse_lazy("eboutic:main", args=self.args, kwargs=kwargs)
|
||||
)
|
||||
c = Customer.objects.filter(user__id=b.user.id).first()
|
||||
if c is None:
|
||||
return HttpResponseRedirect(reverse_lazy('eboutic:main', args=self.args, kwargs=kwargs))
|
||||
kwargs['not_enough'] = True
|
||||
return HttpResponseRedirect(
|
||||
reverse_lazy("eboutic:main", args=self.args, kwargs=kwargs)
|
||||
)
|
||||
kwargs["not_enough"] = True
|
||||
if c.amount < b.get_total():
|
||||
raise DataError(_("You do not have enough money to buy the basket"))
|
||||
else:
|
||||
@ -182,33 +231,44 @@ class EbouticPayWithSith(TemplateView):
|
||||
payment_method="SITH_ACCOUNT",
|
||||
).save()
|
||||
b.delete()
|
||||
kwargs['not_enough'] = False
|
||||
request.session.pop('basket_id', None)
|
||||
kwargs["not_enough"] = False
|
||||
request.session.pop("basket_id", None)
|
||||
except DataError as e:
|
||||
kwargs['not_enough'] = True
|
||||
kwargs["not_enough"] = True
|
||||
return self.render_to_response(self.get_context_data(**kwargs))
|
||||
|
||||
|
||||
class EtransactionAutoAnswer(View):
|
||||
def get(self, request, *args, **kwargs):
|
||||
if (not 'Amount' in request.GET.keys() or
|
||||
not 'BasketID' in request.GET.keys() or
|
||||
not 'Auto' in request.GET.keys() or
|
||||
not 'Error' in request.GET.keys() or
|
||||
not 'Sig' in request.GET.keys()):
|
||||
if (
|
||||
not "Amount" in request.GET.keys()
|
||||
or not "BasketID" in request.GET.keys()
|
||||
or not "Auto" in request.GET.keys()
|
||||
or not "Error" in request.GET.keys()
|
||||
or not "Sig" in request.GET.keys()
|
||||
):
|
||||
return HttpResponse("Bad arguments", status=400)
|
||||
key = crypto.load_publickey(crypto.FILETYPE_PEM, settings.SITH_EBOUTIC_PUB_KEY)
|
||||
cert = crypto.X509()
|
||||
cert.set_pubkey(key)
|
||||
sig = base64.b64decode(request.GET['Sig'])
|
||||
sig = base64.b64decode(request.GET["Sig"])
|
||||
try:
|
||||
crypto.verify(cert, sig, '&'.join(request.META['QUERY_STRING'].split('&')[:-1]), "sha1")
|
||||
crypto.verify(
|
||||
cert,
|
||||
sig,
|
||||
"&".join(request.META["QUERY_STRING"].split("&")[:-1]),
|
||||
"sha1",
|
||||
)
|
||||
except:
|
||||
return HttpResponse("Bad signature", status=400)
|
||||
if request.GET['Error'] == "00000":
|
||||
if request.GET["Error"] == "00000":
|
||||
try:
|
||||
with transaction.atomic():
|
||||
b = Basket.objects.select_for_update().filter(id=request.GET['BasketID']).first()
|
||||
b = (
|
||||
Basket.objects.select_for_update()
|
||||
.filter(id=request.GET["BasketID"])
|
||||
.first()
|
||||
)
|
||||
if b is None:
|
||||
raise SuspiciousOperation("Basket does not exists")
|
||||
i = Invoice()
|
||||
@ -216,12 +276,20 @@ class EtransactionAutoAnswer(View):
|
||||
i.payment_method = "CARD"
|
||||
i.save()
|
||||
for it in b.items.all():
|
||||
InvoiceItem(invoice=i, product_id=it.product_id, product_name=it.product_name, type_id=it.type_id,
|
||||
product_unit_price=it.product_unit_price, quantity=it.quantity).save()
|
||||
InvoiceItem(
|
||||
invoice=i,
|
||||
product_id=it.product_id,
|
||||
product_name=it.product_name,
|
||||
type_id=it.type_id,
|
||||
product_unit_price=it.product_unit_price,
|
||||
quantity=it.quantity,
|
||||
).save()
|
||||
i.validate()
|
||||
b.delete()
|
||||
except Exception as e:
|
||||
return HttpResponse("Payment failed with error: " + repr(e), status=400)
|
||||
return HttpResponse()
|
||||
else:
|
||||
return HttpResponse("Payment failed with error: " + request.GET['Error'], status=400)
|
||||
return HttpResponse(
|
||||
"Payment failed with error: " + request.GET["Error"], status=400
|
||||
)
|
||||
|
Reference in New Issue
Block a user