-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from Atomnp/certificate
Certificate
- Loading branch information
Showing
12 changed files
with
228 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,13 @@ | ||
from django.db import models | ||
from event.models import Event | ||
|
||
# Create your models here. | ||
class Category(models.Model): | ||
name = models.CharField(max_length=50) | ||
created_at = models.DateTimeField(auto_now_add=True) | ||
updated_at = models.DateTimeField(auto_now=True) | ||
event=models.ForeignKey(Event, related_name="categories", on_delete=models.CASCADE) | ||
|
||
|
||
def __str__(self): | ||
return self.name |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
from rest_framework import serializers | ||
from .models import Category | ||
|
||
class CategorySerializer(serializers.ModelSerializer): | ||
class Meta: | ||
model = Category | ||
read_only_fields = ('created_at', 'updated_at') | ||
fields = '__all__' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,13 @@ | ||
from category.models import Category | ||
from django.shortcuts import render | ||
|
||
# Create your views here. | ||
from rest_framework import viewsets | ||
from rest_framework import permissions | ||
from category.serializers import CategorySerializer | ||
|
||
class CategoryViewSet(viewsets.ModelViewSet): | ||
queryset = Category.objects.all() | ||
serializer_class = CategorySerializer | ||
# permission_classes = [permissions.IsAuthenticated] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,25 @@ | ||
from django.db import models | ||
from category.models import Category | ||
from event.models import Event | ||
import uuid | ||
from certification_system.utils.images import get_file_path | ||
|
||
# Create your models here. | ||
class Certificate(models.Model): | ||
name = models.CharField(max_length=50) | ||
email = models.EmailField() | ||
active = models.BooleanField() | ||
category = models.ForeignKey( | ||
Category, related_name="certificates", on_delete=models.CASCADE | ||
) | ||
event = models.ForeignKey( | ||
Event, related_name="certificates", on_delete=models.CASCADE | ||
) | ||
# save certificates to media/certificates folder | ||
image = models.ImageField(upload_to=get_file_path, null=True, blank=True) | ||
|
||
created_at = models.DateTimeField(auto_now_add=True) | ||
updated_at = models.DateTimeField(auto_now=True) | ||
|
||
def __str__(self): | ||
return self.name |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# serializers for certificate | ||
from rest_framework import serializers | ||
from .models import Certificate | ||
|
||
class CertificateSerializer(serializers.ModelSerializer): | ||
class Meta: | ||
model = Certificate | ||
# read only attributes | ||
read_only_fields = ('created_at', 'updated_at') | ||
fields = '__all__' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,79 @@ | ||
from django.shortcuts import render | ||
import uuid | ||
from certificate.models import Certificate | ||
from rest_framework.views import APIView | ||
from rest_framework.response import Response | ||
from rest_framework import status | ||
import csv | ||
import io | ||
from io import BytesIO | ||
from django.core.files.base import ContentFile | ||
|
||
# Create your views here. | ||
from rest_framework import viewsets | ||
from rest_framework import permissions | ||
from certificate.serializers import CertificateSerializer | ||
from PIL import Image | ||
from certification_system.utils.images import save_temporary_image, delete_temporary_image | ||
|
||
|
||
def generate_certificate_dummy(template, data, mappings): | ||
""" | ||
Generate certificate from template and data | ||
""" | ||
file_path = save_temporary_image(template) | ||
image = Image.open(file_path) | ||
delete_temporary_image(file_path) | ||
image = image.convert("L") | ||
f = BytesIO() | ||
try: | ||
image.save(f, format="png") | ||
return ContentFile(f.getvalue(), name=uuid.uuid4().hex + ".png") | ||
finally: | ||
f.close() | ||
|
||
|
||
class CertificateViewSet(viewsets.ModelViewSet): | ||
queryset = Certificate.objects.all().order_by("name") | ||
serializer_class = CertificateSerializer | ||
# permission_classes = [permissions.IsAuthenticated] | ||
|
||
|
||
# route to generate bulk certificates using template image and csv file from the request | ||
class BulkCertificateGenerator(APIView): | ||
def post(self, request, format=None): | ||
template_image = request.FILES["template_image"] | ||
csv_file = request.FILES["csv_file"] | ||
mapping = request.data["mapping"] | ||
|
||
file = csv_file.read().decode("utf-8") | ||
reader = csv.DictReader(io.StringIO(file)) | ||
|
||
for person in reader: | ||
data = { | ||
"name": person["name"], | ||
"email": person["email"], | ||
"active": True, | ||
"category": request.data["category"], | ||
"event": request.data["event"], | ||
"image": generate_certificate_dummy(template_image, person, mapping), | ||
} | ||
|
||
certificate = CertificateSerializer(data=data) | ||
try: | ||
certificate.is_valid(raise_exception=True) | ||
except Exception as e: | ||
print(e) | ||
return Response( | ||
{"error": "Invalid data", "message": str(e)}, | ||
status=status.HTTP_400_BAD_REQUEST, | ||
) | ||
certificate.save() | ||
|
||
return Response( | ||
data={ | ||
"success": True, | ||
"message": "Certificates generated successfully", | ||
"data": [], | ||
}, | ||
status=status.HTTP_201_CREATED, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import uuid | ||
import os | ||
from django.core.files.storage import default_storage | ||
|
||
|
||
def get_file_path(instance, filename): | ||
ext = filename.split(".")[-1] | ||
filename = "%s.%s" % (uuid.uuid4(), ext) | ||
return os.path.join("certificates", filename) | ||
|
||
|
||
def save_temporary_image(file_obj): | ||
filename = str(uuid.uuid4()) + ".png" | ||
with default_storage.open(filename, "wb+") as destination: | ||
for chunk in file_obj.chunks(): | ||
destination.write(chunk) | ||
import os | ||
|
||
print(os.getcwd()) | ||
return "media/" + filename | ||
|
||
|
||
def delete_temporary_image(filename): | ||
default_storage.delete(filename.split("/")[-1]) |