diff --git a/apps/ticket/migrations/0004_remove_ticket_ticket_image_location_and_more.py b/apps/ticket/migrations/0004_remove_ticket_ticket_image_location_and_more.py new file mode 100644 index 0000000..d301dbd --- /dev/null +++ b/apps/ticket/migrations/0004_remove_ticket_ticket_image_location_and_more.py @@ -0,0 +1,22 @@ +# Generated by Django 4.2.3 on 2023-08-09 06:42 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('ticket', '0003_remove_ticket_ticket_mail_log'), + ] + + operations = [ + migrations.RemoveField( + model_name='ticket', + name='ticket_image_location', + ), + migrations.AddField( + model_name='ticket', + name='ticket_image', + field=models.ImageField(blank=True, null=True, upload_to='ticket_images'), + ), + ] diff --git a/apps/ticket/models.py b/apps/ticket/models.py index 155ae71..69ccd01 100644 --- a/apps/ticket/models.py +++ b/apps/ticket/models.py @@ -21,7 +21,7 @@ class Ticket(models.Model): created_at = models.DateTimeField(auto_now_add=True) updated_at = models.DateTimeField(auto_now=True) ticket_image_generated = models.BooleanField(default=False) - ticket_image_location = models.TextField(default="", null=True, blank=True) + ticket_image = models.ImageField(upload_to='ticket_images', null=True, blank=True) order_id = models.CharField(max_length=100, default="", unique=True, null=True) transaction_id = models.OneToOneField(Transaction, on_delete=models.CASCADE, null=True, blank=True, unique=True) referral = models.CharField(max_length=100, null=True, blank=True) diff --git a/apps/ticket/serializers.py b/apps/ticket/serializers.py index 858f3da..0425778 100644 --- a/apps/ticket/serializers.py +++ b/apps/ticket/serializers.py @@ -6,7 +6,7 @@ class TicketSerializer(serializers.Serializer): customer_name = serializers.CharField() customer_email = serializers.EmailField() customer_phone = serializers.CharField() - referral = serializers.CharField(required=False) + referral = serializers.CharField(required=False, allow_blank=True) event_id = serializers.IntegerField() selected_sub_events = serializers.ListField(child=serializers.IntegerField(), required=False) selected_addons = serializers.ListField(child=serializers.IntegerField(), required=False) diff --git a/apps/ticket/utils.py b/apps/ticket/utils.py index 6e40591..e756027 100644 --- a/apps/ticket/utils.py +++ b/apps/ticket/utils.py @@ -1,3 +1,4 @@ +import io from cryptography.fernet import Fernet from celery import shared_task from django.template.loader import get_template @@ -10,6 +11,7 @@ from .models import Ticket, TicketEmailLog from event.models import Event from django.conf import settings +from django.core.files import File def create_ticket(request, order_id): @@ -69,16 +71,13 @@ def generate_ticket_image (ticket_id): 'zoom': '2.0', } ticket_name = f"ticket_{encrypted_ticket_id}.jpg" - tickets_dir = os.path.join(settings.MEDIA_ROOT, 'tickets') - if not os.path.exists(tickets_dir): - # Create the 'tickets' directory if it doesn't exist - os.makedirs(tickets_dir) - image_path = os.path.join(settings.MEDIA_ROOT, 'tickets', ticket_name) - imgkit.from_string(html_content, image_path, options=options) - ticket.ticket_image_location = image_path - ticket.save() + img_bytes = imgkit.from_string(html_content, None, options=options) + img_io = io.BytesIO(img_bytes) + ticket.ticket_image.save(ticket_name, File(img_io), save=True) send_ticket(ticket.id) - image_url = f"{settings.TICKETIFY_API_SERVER}{settings.MEDIA_URL}{'tickets/'}{ticket_name}" + image_url = ticket.ticket_image.url + if settings.DEBUG: + image_url = settings.TICKETIFY_API_SERVER + image_url return image_url @@ -103,13 +102,10 @@ def send_ticket(ticket_id): ticket = Ticket.objects.get(pk=ticket_id) recipient_email = ticket.customer_email event_name = ticket.event.name - image_path = ticket.ticket_image_location # Create the email message email = EmailMultiAlternatives(f"Your ticket for {event_name}", "Please find your ticket attached", settings.DEFAULT_FROM_EMAIL, [recipient_email]) - - # Attach the image to the email - with open(image_path, 'rb') as f: - email.attach('ticket_image.jpg', f.read(), 'image/jpeg') + image_data = ticket.ticket_image.read() + email.attach('ticket_image.jpg', image_data, 'image/jpeg') # Send the email email.send() diff --git a/requirements/common.txt b/requirements/common.txt index bf10fca..7b87599 100644 --- a/requirements/common.txt +++ b/requirements/common.txt @@ -12,4 +12,5 @@ celery==5.3.1 redis==4.6.0 hiredis==2.2.3 dj-rest-auth==4.0.1 -djangorestframework-simplejwt==5.2.2 \ No newline at end of file +djangorestframework-simplejwt==5.2.2 +django-storages==1.13.2 \ No newline at end of file diff --git a/requirements/prod.txt b/requirements/prod.txt index 808e09d..16755df 100644 --- a/requirements/prod.txt +++ b/requirements/prod.txt @@ -3,5 +3,4 @@ sentry-sdk==1.28.1 psycopg==3.1.9 django-extensions==3.2.1 uWSGI==2.0.21 -boto3==1.28.20 -django-storages==1.13.2 \ No newline at end of file +boto3==1.28.20 \ No newline at end of file