Skip to content

Commit

Permalink
Fix ticket delivery for production
Browse files Browse the repository at this point in the history
  • Loading branch information
Suryansh5545 committed Aug 9, 2023
1 parent 9add8b7 commit 3110638
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -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'),
),
]
2 changes: 1 addition & 1 deletion apps/ticket/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion apps/ticket/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
24 changes: 10 additions & 14 deletions apps/ticket/utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import io
from cryptography.fernet import Fernet
from celery import shared_task
from django.template.loader import get_template
Expand All @@ -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):
Expand Down Expand Up @@ -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


Expand All @@ -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()
Expand Down
3 changes: 2 additions & 1 deletion requirements/common.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
djangorestframework-simplejwt==5.2.2
django-storages==1.13.2
3 changes: 1 addition & 2 deletions requirements/prod.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
boto3==1.28.20

0 comments on commit 3110638

Please sign in to comment.