Repository: fossasia/open-event-server

Issue:
If ticketing system is used store "ticket URL" of open event server in DB and export/API-access throug URL to server
If we use the ticketing system, there is no event URL saved in the DB. And therefore if we export the info in the JSON or offer access through the API there is no ticketing link. We need this ticketing link for the generators.
- [x] Implement to store ticket URL on server if ticketing system is used
- [x] Ensure ticket URL is exported and offered through API



Discussion:
@mariobehling the ticketing URL should be accessible only if the event is Published right ? 

>  the ticketing URL should be accessible only if the event is Published right ?

Yes, good point.


Resolving patch:
diff --git a/app/helpers/data.py b/app/helpers/data.py
--- a/app/helpers/data.py
+++ b/app/helpers/data.py
@@ -1001,6 +1001,11 @@ def create_event(form, img_files):
             # Save Tickets
             module = DataGetter.get_module()
             if module and module.ticket_include:
+
+                event.ticket_url = url_for('event_detail.display_event_tickets',
+                                           identifier=event.identifier,
+                                           _external=True)
+
                 ticket_names = form.getlist('tickets[name]')
                 ticket_types = form.getlist('tickets[type]')
                 ticket_prices = form.getlist('tickets[price]')
@@ -1243,7 +1248,7 @@ def create_event_copy(event_id):
                       topic=event_old.topic,
                       sub_topic=event_old.sub_topic,
                       privacy=event_old.privacy,
-                      ticket_url=event_old.ticket_url,
+                      ticket_url=None,
                       show_map=event_old.show_map,
                       organizer_name=event_old.organizer_name,
                       organizer_description=event_old.organizer_description)
@@ -1396,6 +1401,10 @@ def edit_event(request, event_id, event, session_types, tracks, social_links, mi
         event.ticket_url = form.get('ticket_url', None)
 
         if not event.ticket_url:
+
+            event.ticket_url = url_for('event_detail.display_event_tickets',
+                                       identifier=event.identifier,
+                                       _external=True)
             if form['taxAllow'] == 'taxNo':
                 event.tax_allow = False
                 delete_from_db(tax, "Tax options deleted")
diff --git a/app/helpers/ticketing.py b/app/helpers/ticketing.py
--- a/app/helpers/ticketing.py
+++ b/app/helpers/ticketing.py
@@ -1,6 +1,7 @@
 """Copyright 2016 Niranjan Rajendran"""
 import binascii
 import os
+import uuid
 
 from datetime import timedelta, datetime
 
@@ -85,7 +86,7 @@ def get_order_expiry():
 
     @staticmethod
     def get_new_order_identifier():
-        identifier = binascii.b2a_hex(os.urandom(32))
+        identifier = str(uuid.uuid4())
         count = get_count(Order.query.filter_by(identifier=identifier))
         if count == 0:
             return identifier
diff --git a/app/views/public/event_detail.py b/app/views/public/event_detail.py
--- a/app/views/public/event_detail.py
+++ b/app/views/public/event_detail.py
@@ -149,6 +149,23 @@ def display_event_coc(self, identifier):
                            accepted_sessions=accepted_sessions,
                            call_for_speakers=call_for_speakers)
 
+    @expose('/<identifier>/tickets/')
+    def display_event_tickets(self, identifier):
+        event = get_published_event_or_abort(identifier)
+        placeholder_images = DataGetter.get_event_default_images()
+        if event.copyright:
+            licence_details = DataGetter.get_licence_details(event.copyright.licence)
+        else:
+            licence_details = None
+        module = DataGetter.get_module()
+        tickets = DataGetter.get_sales_open_tickets(event.id)
+        return self.render('/gentelella/guest/event/details.html',
+                           event=event,
+                           placeholder_images=placeholder_images,
+                           licence_details=licence_details,
+                           module=module,
+                           tickets=tickets if tickets else [])
+
     # SLUGGED PATHS
 
     @expose('/<identifier>/<slug>/')


Test patch: