Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate Certificate #32

Merged
merged 1 commit into from
Jun 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to

### Added

- Use marion and howard to generate certificate for an order
- Use marion and howard to generate invoice for an order
- Implement Address model for billing and add routes API to get, create,
update and delete address.
Expand Down
2 changes: 1 addition & 1 deletion src/backend/joanie/core/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class CertificateDefinitionAdmin(TranslatableAdmin):
class CertificateAdmin(admin.ModelAdmin):
"""Admin class for the Certificate model"""

list_display = ("certificate_definition", "order", "issued_on")
list_display = ("order", "issued_on")


@admin.register(models.Course)
Expand Down
3 changes: 3 additions & 0 deletions src/backend/joanie/core/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class Meta:

title = factory.Sequence(lambda n: "Certificate definition %s" % n)
name = factory.Sequence(lambda n: "certificate-definition-%s" % n)
template = settings.MARION_CERTIFICATE_DOCUMENT_ISSUER


class OrganizationFactory(factory.django.DjangoModelFactory):
Expand All @@ -49,6 +50,8 @@ class Meta:

code = factory.Faker("ean", length=8)
title = factory.Sequence(lambda n: "Organization %s" % n)
signature = factory.django.FileField(filename="signature.png")
logo = factory.django.FileField(filename="logo.png")


class CourseFactory(factory.django.DjangoModelFactory):
Expand Down
37 changes: 37 additions & 0 deletions src/backend/joanie/core/migrations/0003_auto_20210607_0938.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Generated by Django 3.2 on 2021-06-07 09:38

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("core", "0002_auto_20210604_2357"),
]

operations = [
migrations.RemoveField(
model_name="certificate",
name="certificate_definition",
),
migrations.AddField(
model_name="organization",
name="logo",
field=models.ImageField(blank=True, upload_to="", verbose_name="logo"),
),
migrations.AddField(
model_name="organization",
name="representative",
field=models.CharField(
blank=True,
help_text="representative fullname (to sign certificate for example)",
max_length=100,
verbose_name="representative",
),
),
migrations.AddField(
model_name="organization",
name="signature",
field=models.ImageField(blank=True, upload_to="", verbose_name="signature"),
),
]
14 changes: 4 additions & 10 deletions src/backend/joanie/core/models/certifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@

from parler import models as parler_models

from . import products as products_models


class CertificateDefinition(parler_models.TranslatableModel):
"""
Expand Down Expand Up @@ -42,14 +40,10 @@ class Certificate(models.Model):
Certificate represents and records all user certificates issued as part of an order
"""

certificate_definition = models.ForeignKey(
CertificateDefinition,
verbose_name=_("certificate definition"),
related_name="certificates_issued",
on_delete=models.RESTRICT,
)
order = models.OneToOneField(
products_models.Order,
# disable=all is necessary to avoid an AstroidImportError because of our models structure
# Astroid is looking for a module models.py that does not exist
"core.Order", # pylint: disable=all
sdemagny marked this conversation as resolved.
Show resolved Hide resolved
verbose_name=_("order"),
on_delete=models.PROTECT,
)
Expand All @@ -63,4 +57,4 @@ class Meta:
verbose_name_plural = _("Certificates")

def __str__(self):
return f"{self.certificate_definition} for {self.order.owner}"
return f"Certificate for {self.order.owner}"
8 changes: 8 additions & 0 deletions src/backend/joanie/core/models/courses.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ class Organization(parler_models.TranslatableModel):
translations = parler_models.TranslatedFields(
title=models.CharField(_("title"), max_length=255)
)
representative = models.CharField(
_("representative"),
help_text=_("representative fullname (to sign certificate for example)"),
max_length=100,
blank=True,
)
signature = models.ImageField(_("signature"), blank=True)
logo = models.ImageField(_("logo"), blank=True)

class Meta:
db_table = "joanie_organization"
Expand Down
28 changes: 28 additions & 0 deletions src/backend/joanie/core/models/products.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

from .. import enums
from . import accounts as customers_models
from . import certifications as certifications_models
from . import courses as courses_models

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -268,6 +269,33 @@ def generate_invoice(self):
self.save()
return invoice

def generate_certificate(self):
"""Generate a pdf certificate for the order's owner"""
organization = self.course.organization
context_query = {
"student": {
"name": self.owner.get_full_name(),
},
"course": {
"name": self.product.title, # pylint: disable=no-member
"organization": {
"name": organization.title,
"representative": organization.representative,
"signature": organization.signature.path,
"logo": organization.logo.path,
sdemagny marked this conversation as resolved.
Show resolved Hide resolved
},
},
}
document_request = DocumentRequest.objects.create(
issuer=self.product.certificate_definition.template, # pylint: disable=no-member
context_query=context_query,
)
certificate, _ = certifications_models.Certificate.objects.update_or_create(
order=self,
defaults={"attachment": document_request.get_document_path().name},
)
return certificate


class OrderCourseRelation(models.Model):
"""
Expand Down
1 change: 1 addition & 0 deletions src/backend/joanie/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ class Base(Configuration):
# Marion
MARION_DOCUMENT_ISSUER_CHOICES_CLASS = "howard.defaults.DocumentIssuerChoices"
MARION_INVOICE_DOCUMENT_ISSUER = "howard.issuers.InvoiceDocument"
MARION_CERTIFICATE_DOCUMENT_ISSUER = "howard.issuers.CertificateDocument"

# Joanie settings
JOANIE_CURRENCY = values.Value(
Expand Down
17 changes: 17 additions & 0 deletions src/backend/joanie/tests/test_models_product.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,20 @@ def test_generate_invoice(self):
order.refresh_from_db()
now = timezone.localtime(timezone.now())
self.assertTrue(order.invoice_ref.startswith(now.strftime("%Y")))

def test_model_order_generate_certificate(self):
"""Generate a certificate for a product order"""

course = factories.CourseFactory()
product = factories.ProductFactory(
courses=[course],
certificate_definition=factories.CertificateDefinitionFactory(),
)
order = factories.OrderFactory(product=product)

certificate = order.generate_certificate()
self.assertEqual(DocumentRequest.objects.count(), 1)
self.assertEqual(
certificate.attachment.name,
f"{DocumentRequest.objects.get().document_id}.pdf",
)
2 changes: 1 addition & 1 deletion src/backend/setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ install_requires =
django-cors-headers==3.7.0
django-countries==7.1
django-marion==0.3.0
django-marion-howard==0.2.1
django-marion-howard==0.2.4
django-parler==2.2
djangorestframework==3.12.4
djangorestframework-simplejwt==4.6.0
Expand Down