1818This file provides the event class
1919"""
2020from typing import Optional , Any
21+ import time
2122
2223from datetime import datetime
2324
@@ -32,12 +33,15 @@ class Event(AdminAPIBase):
3233 One of the events available through the Tito IO AdminAPI
3334 """
3435
35- def __init__ (self , account_slug :str , event_slug :str ,
36+ def __init__ (self , * , account_slug :str , event_slug :str ,
3637 json_content :Optional [dict [str , Any ]]= None ,
37- api_key : Optional [str ] = None ) -> None :
38- super ().__init__ (json_content = json_content , api_key = api_key )
38+ api_key : Optional [str ] = None ,
39+ allow_automatic_json_retrieval :bool = False ) -> None :
40+ super ().__init__ (json_content = json_content , api_key = api_key ,
41+ allow_automatic_json_retrieval = allow_automatic_json_retrieval )
3942 self .__account_slug = account_slug
4043 self .__event_slug = event_slug
44+ self .__api_key_internal = api_key
4145 if json_content is not None :
4246 if self ._json_content ['_type' ] != "event" :
4347 raise ValueError ('JSON content type was expected to be ticket' )
@@ -60,17 +64,35 @@ def _populate_json(self) -> None:
6064 raise ValueError ('JSON content type was expected to be ticket' )
6165
6266 def _update (self , payload : dict [str , Any ]) -> None :
63- self ._patch_reponse (value = {'event' : payload })
67+ self ._patch_response (value = {'event' : payload })
6468 for key , value in payload .items ():
6569 self ._json_content [key ] = value
6670
71+ def _update_slug (self , new_slug : str ) -> None :
72+ """
73+ The Slug is a unique component of the data used to reference the release in the API.
74+ It is sometimes desirable to change this
75+
76+ .. Warning::
77+ Changing the slug may break things, especially if it clashes with another slug.
78+ Use this method with caution. In particular, the slug is used to key other
79+ dictionaries within the data model. Once changing the clug it is recommended that
80+ the whole data model is refreshed
81+ """
82+ self ._update ({'slug' : new_slug })
83+ self .__event_slug = new_slug
84+
6785 @property
6886 def title (self ) -> str :
6987 """
7088 Event title
7189 """
7290 return self ._json_content ['title' ]
7391
92+ @title .setter
93+ def title (self , value : str ) -> None :
94+ self ._update ({'title' : value })
95+
7496 def __ticket_getter (self ) -> list [Ticket ]:
7597
7698 def ticket_factory (json_content :dict [str , Any ]) -> Ticket :
@@ -104,7 +126,7 @@ def start_at(self, value: datetime) -> None:
104126 # date and time
105127 payload = {'start_date' : value .strftime ("%Y-%m-%d" ),
106128 'start_time' : value .strftime ("%H:%M" )}
107- self ._patch_reponse (value = {'event' : payload })
129+ self ._patch_response (value = {'event' : payload })
108130 value_str = datetime_to_json (value )
109131 self ._json_content ['start_at' ] = value_str
110132
@@ -124,7 +146,7 @@ def end_at(self, value: datetime) -> None:
124146 # date and time
125147 payload = {'end_date' : value .strftime ("%Y-%m-%d" ),
126148 'end_time' : value .strftime ("%H:%M" )}
127- self ._patch_reponse (value = {'event' : payload })
149+ self ._patch_response (value = {'event' : payload })
128150 value_str = datetime_to_json (value )
129151 self ._json_content ['end_at' ] = value_str
130152
@@ -177,3 +199,55 @@ def test_mode(self) -> bool:
177199 Whether the event is in test mode
178200 """
179201 return self ._json_content ['test_mode' ]
202+
203+ def duplicate_event (self , title :str , slug :Optional [str ]= None ) -> "Event" :
204+ """
205+ Duplicate the event and then update the title and optionally the new slug for the
206+ created event
207+ :param title: New event title
208+ :param slug: New event slug, a value of None will leave the automatically created slug in
209+ place
210+ :return: The newly created event
211+ """
212+ self ._post_response ('duplication' , value = {})
213+ for _ in range (120 ):
214+ time .sleep (1 )
215+ duplication_status = self ._get_duplication_status ()
216+ status = duplication_status ['status' ]
217+ if status == 'processing' :
218+ # pylint:disable-next=bad-builtin
219+ print ('Duplication in progress' )
220+ continue
221+ if status == 'complete' :
222+ new_slug = duplication_status ['slug' ]
223+ new_title = duplication_status ['title' ]
224+ new_event = Event (account_slug = self .__account_slug ,
225+ event_slug = new_slug ,
226+ json_content = None ,
227+ api_key = self .__api_key_internal ,
228+ allow_automatic_json_retrieval = True )
229+ if new_event .title != new_title :
230+ raise ValueError (f'New event has different title to reported value:{ new_title } ' )
231+ new_event .title = title
232+ if slug is not None :
233+ # The update slug method is a powerful option that is not normally exposed
234+ # to the users so is private
235+ # pylint:disable-next=protected-access
236+ new_event ._update_slug (slug )
237+ return new_event
238+
239+ raise ValueError ('Unhandled {status=}' )
240+
241+ raise RuntimeError ('Timeout During Event Duplication' )
242+
243+ def _get_duplication_status (self ) -> dict [str , Any ]:
244+ duplication_status = self ._get_response ('duplication' )['duplication' ]
245+ if duplication_status ['_type' ] != '_duplication' :
246+ raise RuntimeError ('Duplication response does not have a value of _type=_duplication' )
247+ return duplication_status
248+
249+ def _delete_event (self ) -> None :
250+ """
251+ Delete the event
252+ """
253+ self ._delete_response ()
0 commit comments