a
    f                     @   sF  d dl mZmZ d dlmZ d dlmZ d dlmZ d dl	m
Z
 d dlmZmZ d dlT d dlT d dlZd d	lmZ d d
lmZ d dl mZ d dlmZ d dlmZ d dlmZ d dlmZ d dlmZ d dlmZ d dl m!Z! d dl"m#Z#mZ d dl$m%Z% d dl&m'Z' d dl(Z(d dl)m*Z* d dl+m,Z,m-Z-m.Z. d dl+m/Z/ d dl0m1Z1 d dl2m3Z3 d dl4m5Z5 d dl mZm6Z6 d dl7m8Z8 d dl9Z9d dl:Z:d dl;Z;d dl<Z<d dl=Z=d dl>m?Z? d dl@mAZA d d lBmCZC d dlDZDd dlmZ d dlmZ d d!lEmFZFmGZG G d"d# d#eZHe(Id$ZJG d%d& d&e
ZKG d'd( d(e
ZLG d)d* d*e
ZMdS )+    )HttpResponseJsonResponse)reverse)PageNumberPagination)Response)APIView)api_viewpermission_classes)*N)response)	Paginator)r   )defunct_hash_message)settings)Web3)&construct_sign_and_send_raw_middleware)Account)encode_defunct)LocalAccount)renderr   )Mnemonic)signing)IsAuthenticated)genericspermissionsserializers)status)openapi)swagger_auto_schema)CategorySerializer)r   HttpResponseRedirect)authenticate)ContentFile)
SignupForm)timezone)send_image_to_pinataunpin_imagec                   @   s   e Zd ZdZdZdZdS )CustomPagination
   	page_sized   N)__name__
__module____qualname__r(   page_size_query_parammax_page_size r/   r/   /Users/christophesurbier/Dropbox/Mon Mac (MacBook-Air-de-christophe.local)/Documents/Programmation/SellTix/tixsellbackend/api_partner/views_tickets.pyr&   -   s   r&   djangoc                    @   s  e Zd ZegZed ddgejejejej	ej
ddejej	ejddejej	ddejejddejejd	dejejd
dejej	ejddejej	ejddejej	ddejejddejej	ejddejej	ddejejddejejddejejddejejddejejddejej	ejddejejddddddddddd d!d!d"dd!d#d$d%d&d'd(ejd)ejejejej	ej
d*dejej	ej
ddejej	d+dejej	ddejejddejejd	dejejd
dejej	ejddejej	ejd,dejej	ddejejddejej	ejd-dejej	d.dejejddejejddejejd/dejejddejejd0dejej	ejd1dejejddd2d3d4d5ddddd6d7d8d d!d9d9d!d:d;d!d<d=d%d>id?ejd@ejejejej	dAdejej	dBddCd3dDdEdCdDdFdCdDdGdCdDdHdCdDdIdCdDdJdCdDdKdCdDdLdCdDdMdCdDdNdCdDdOdCdDdPdCdDdQdCdDdRdCdDdSdCdTd?ejdUd4dVdWdXdYdZd[gd\id]d^d_dbd`daZd9S )ccreateTicketa  Create a Ticket for your event.
        <br><br>To each date provided (booking start date, ...) we will use the timezone of the event.
        <br>For price, we will use the currency of the event.
        <br><br><b>Note:</b>
        Because the image needs to be uploaded on a decentralized server, it can take few seconds before receiving the api response.
        <br><br>
        Once your ticket created you should be able to see It on the <b>sellTixEventPage</b> provided while creating your event.
        <br><br><b><i>Information:</i></b> For simplification management there is no update operation method for tickets.
        <br><br>If you have made a mistake or the ticket design doesn't suit you, you have two options:<br>
         <ul>
          <li>delete the ticket and create a new one with your updated image</li>
          <li>login to your SellTix Dashboard on our website to be able to modify the ticket using our NFT designer widget.</li>
          </ul>
        Ticketsz;Reference to the event for which the ticket will be createdtypeformatdescriptionzgBase64 encoded image data. Example  'image':'data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/......'-Ticket Name ie VIP Pass, Standard Ticket, etcr5   r7   *Maximum number of tickets that can be sold8Maximum number of tickets that can be purchased by usersTicket price~Date and time of the start of the booking. <b>Very important</b> should be in <strong>UTC format</strong> : %Y-%m-%dT%H:%M:%SZzDate and time of the end of the booking. Should be before the event date.<b>Very important</b> should be in <strong>UTC format</strong> : %Y-%m-%dT%H:%M:%SZ7Ticket description. Will be displayed on the event pageTicket is revealed or not. If revealed, the ticket image will be visible on the event page. If not revealed the hidden uri image will be displayedzDate and time of the start of the reveal. At that specific date, your ticket design (image) will be revealed. <b>Very important</b> should be in <strong>UTC format</strong> : %Y-%m-%dT%H:%M:%SZzqHidden uri of the ticket image while it's not revealed. Will be displayed on the event page until reveal is done.<Is Ticket sellable or not on marketplace such as Opensea.io.GMaximum price the ticket can be sold on marketplace such as Opensea.io.zRoyalty you will receive for each sell on marketplace. Will override the event royalty. Please provide a percentage (between 0 and 100).Do you offer a discount.zCDiscount price of the ticket. Should be less than the ticket price.zDate and time of the end of the discount. Should end before bookingEndDate. <b>Very important</b> should be in <strong>UTC format</strong> : %Y-%m-%dT%H:%M:%SZiIf your event is a streaming managed by SellTix, does the ticket allow owners to stream during the event.)refEventimagename
maxTicketsmaxTicketsPerUserticketPricebookingStartDatebookingEndDater7   revealedrevealStartDate	hiddenurisellablemaxSellablePriceroyaltySellableearlyBiddiscountPricediscountEndDate	canStream$ade56c0f-8175-462d-b548-fac938fcab14Ticket standardr)         z2024-07-01T10:00:00Zz2024-08-31T10:00:00ZGet access to our conferenceT   r'   z2024-07-30T10:00:00ZFz%data:image/jpeg;base64,/9j/4Q/.......)rD   rF   rG   rH   rI   rJ   rK   r7   rL   rO   rP   rQ   rR   rS   rT   rU   rE   )r5   
propertiesexamplezTicket created successfully	Ticket IDImage URL for the event|Date and time of the end of the booking. <b>Very important</b> should be in <strong>UTC format</strong> : %Y-%m-%dT%H:%M:%SZ}Date and time of the start of the reveal. <b>Very important</b> should be in <strong>UTC format</strong> : %Y-%m-%dT%H:%M:%SZCHidden uri of the ticket image. Will be displayed on the event pagegRoyalty you will receive for each sell on marketplace. Please provide a percentage (between 0 and 100).,Price of the ticket if early bid is offered.}Date and time of the end of the discount. <b>Very important</b> should be in <strong>UTC format</strong> : %Y-%m-%dT%H:%M:%SZidrD   rE   rF   rG   rH   rI   rJ   rK   r7   rL   rM   rN   rO   rP   rQ   rR   rS   rT   rU   r5   r\   application/jsonz$b79aaf14-87bd-415f-be38-1898b6c96e8a      4@2024-07-01T10:00:002024-08-31T10:00:00N      .@     @@      $@2024-07-30T10:00:00rg   rD   rF   rG   rH   rI   rJ   rK   r7   rL   rM   rN   rO   rP   rQ   rR   rS   rT   rU   r7   schemaexamplesBad requestStatus of the operation KO message of the failure exceptionr   reasonKOzThe reason of the failureEvent does not exist=You are not authorized to create a ticket type for this eventEvent is already publishedPlease provide an image4Booking start date should be before booking end date,Booking end date should be before event date-Reveal start date should be before event datePPlease provide a hidden uri that will be used before revealing the ticket design4Max Tickets Per User should be less than max Tickets$Max Tickets should be greater than 04maxSellablePrice should be less or equal ticketPrice,Royalty sellable should be between 0 and 100.Discount price should be less than ticketPrice3Discount end date should be before booking end date)ri   zEvent not foundzNot authorizedzEvent already publishedzNo image providedzStart date incoherencezEnd date incoherencezReveal date incoherencezHiddenuri missingzTickets incoherencezNumber of tickets incoherencezSellable price incoherencezRoyalty incoherencezDiscount price incoherencezDiscount end date incoherenceUnauthorized(Given token not valid for any token typetoken_not_validAccessTokenaccessToken is invalid or expiredtoken_class
token_typemessagedetailcodemessagesr7   rt   )       )operation_idoperation_descriptiontagsrequest_body	responsesc           "   
   C   s  z$t jj|jjd}t|jd}| r|jd}t	
d t	
| |r|jj|jkrzddd}t|dd	d
W S |jdkrddd}t|dd	d
W S |jd}|d u rddd}t|dd	d
W S t	
d |jd}|jd}	||	krddd}t|dd	d
W S |	|jkr8ddd}t|dd	d
W S |jd}
|
dkr|jd}||jkrddd}t|dd	d
W S |jd}|d u rddd}t|dd	d
W S nd }d }t	
d |jd}|jd}|dkr ddd}t|dd	d
W S ||kr$ddd}t|dd	d
W S |jd}|jd}|jd }|jd!}|r||kr~dd"d}t|dd	d
W S |dk s|d#krdd$d}t|dd	d
W S |d# }|jd%}|jd&}|jd'}|r&||krdd(d}t|dd	d
W S ||	kr&dd)d}t|dd	d
W S t }|jd*|_d+}tj}t||}|r^d,| }nd+}||_d-|_d.|_dd/lm} ||j\}}d0|_d1|_||_||_d2|_d3|_d4|_d3|_ d2|_!d3|_"t#|d5 |j$ |_%d3|_&d6|_'d3|_(d6|_)d7|_*d+|_+|,  |jd*}|jd8}|jd9}t- } || _.|| _/|| _|| _0|| _1|| _2|| _3|	| _4|| _5|
| _6|| _7|| _8|| _9|| _:|| _;|| _<|| _=|| _>|j?rd:| _@n|jArd;| _@nd<| _@|| _B| ,  ttC| jdd=d
W S ndd>d}t|dd	d
W S nd|jDd}t|dd	d
W S W nX tEy~ }! z>t	F|! t	F|j dt#|!d}t|dd	d
W  Y d }!~!S d }!~!0 0 d S )?Nrg   )datarD   z===RefEvent===rz   r|   rx   Fr   safer   r   r}   rE   r~   z===Check DAtes===rJ   rK   r   r   rL   rM   r   rN   r   z===Check number of tickets===rG   rH   r   r   rI   rO   rP   rQ   r   r)   r   rR   rS   rT   r   r   rF    z8https://amber-large-nightingale-955.mypinata.cloud/ipfs/zwhttps://fonts.googleapis.com/css2?family=Alumni+Sans+Collegiate+One&amp;family=Montserrat:wght@400;800&amp;display=swap   )
get_titlesz#0dcaf0z#FF009025z#fff20 15Onliner7   rU   g?g333333?g333333?r   r{   )GUserobjectsgetuserrg   TicketTypeSerializerForCreater   is_validvalidated_dataloggerinforefOrganiserr   r   	eventDateTicketTypeTemplaterF   r   PINATA_API_KEYr$   rE   fontUrl
templateIdbackoffice.helpersr   gradient1Colorgradient2ColoreventTitleOneeventTitleTWoeventTitleFont
eventColorcategoryFontcategoryColorticketTypeFontticketTypeColorstrcurrencyprice
priceColor	priceFonttypeEventColortypeEventFontvenuesvgUrlsave
TicketTyperD   refTicketTypeTemplaterG   rH   rI   rJ   rK   r7   rL   rM   rN   rO   rP   rQ   rR   rS   rT   isConference	fixAmountstreamWithSelltixrU   TicketTypeForReadSerializererrors	Exceptionerror)"selfrequestr6   	organizer
serializertheEventjsonToReturnrE   rJ   rK   rL   rM   rN   rG   rH   rI   rO   rP   rQ   rR   rS   rT   ZticketTypeTemplate	fullImagepinata_api_key	image_urlr   ZtitleOneZtitleTworF   r7   rU   
ticketTypeer/   r/   r0   post8   sZ    T
















zcreateTicket.post)N)r*   r+   r,   r   r	   r   r   SchemaTYPE_OBJECTTYPE_STRINGFORMAT_UUIDFORMAT_BASE64TYPE_INTEGERTYPE_NUMBERFORMAT_DATETIMETYPE_BOOLEANr   r   r/   r/   r/   r0   r2   5   sN  .3	H Rr2   c                   @   s   e Zd ZejgZedddgejddejdej	ej
ej	ejddej	ejd	dd
dddd
ddd
ddd
ddd
ddejddddddddgdidddd"d d!ZdS )#DeleteTicketZdeleteTicketzDelete a ticket.
        <br><br><b>Note:</b> If the event is already published the delete operation will not be allowed.
          r3   zTicket deleted successfully)r7   ru   rv   r9   rw   rx   rh   rz   zTicket does not existTicket not foundNot allowedEvent is published)ri   r   z	Not ownerzEvent publishedrr   r   ri   r   r   r   r   r   r   r   r      r   r   r   r   r   r   Nc              
   C   s
  zt jj|d}|jjj|jjkr8tddddddW S |jjdkrZtdd	ddddW S |j	j
}t|d
krvt| |j	  |  ti dddW S  t jy   tdddddd Y S  ty } z.t| tdt|ddddW  Y d }~S d }~0 0 d S )Nr   rz   r   rx   Fr   r   r   r   rX   r   r   i  )r   r   r   rD   r   rg   r   r   r   r   rE   lenr%   deleteDoesNotExistr   r   r   r   )r   r   pkr6   r   rE   r   r/   r/   r0   r     s"    <

zDeleteTicket.delete)NN)r*   r+   r,   r   r   r	   r   r   r   r   r   r   r   r/   r/   r/   r0   r     s\   	;r   c                   @   s  e Zd ZejgZedddgejdej	ej
ej	ejejddej	ejejddej	ejdd	ej	ejd
d	ej	ejdd	ej	ejdd	ej	ejdd	ej	ejejddej	ejejddej	ejdd	ej	ejdd	ej	ejejddej	ejdd	ej	ejdd	ej	ejdd	ej	ejdd	ej	ejdd	ej	ejdd	ej	ejejddej	ejdd	ddddddd d!d"d#d$d%d&d'd(d)ddd)d*d+d)d,d-d.d/gd0id1ejd2ej	ej
ej	ejd3d	ej	ejd4d	d5dd6ejd7dd8d9d:d;d<d=gd>id?d@dAdDdBdCZdS )ETicketsAPIListViewZ
getTicketszGet all your tickets.
        <br><br><b>Note:</b> 
        <br><br>If you want tickets for a specific event you can use the refEvent parameter and pass it in the query url.
        r3   zList of all organizer ticketsr^   r4   z<Reference to the event for which the ticket has been createdr_   r9   r8   r:   r;   r<   r=   r`   r>   r?   ra   rb   r@   rA   rc   rB   rd   re   rC   rf   rh   ri      Nz$0bbcd4ad-604a-4687-8e0b-ffefc6d994b5rV   rW   r)   rX   rj   rk   rl   rZ   Trm   rn   ro   rp   Frq   )countnextpreviousresultsrr   ru   rv   rw   rx   )r7   rs   r   r   r   r   r   r   r   r   r   r   r   c           	   
   C   s   z`|j d}|r&tjj||jd}ntjj|jd}t }|||}t|dd}|	|j
W S  ty } z.t| tdt|ddd	d
W  Y d }~S d }~0 0 d S )NrD   )ZrefEvent__idrefEvent__refOrganiser)r   T)manyrz   rx   Fr   r   )query_paramsr   r   r   filterr   r&   paginate_querysetr   get_paginated_responser   r   r   r   r   r   )	r   r   r6   Z	ref_eventZtickets	paginatorZpaginated_ticketsr   r   r/   r/   r0   r   9  s    b
zTicketsAPIListView.get)N)r*   r+   r,   r   r   r	   r   r   r   r   r   r   r   r   r   r   r   r   r/   r/   r/   r0   r   6  s   :
ar   )Ndjango.httpr   r   django.urlsr   Zrest_framework.paginationr   rest_framework.responser   rest_framework.viewsr   rest_framework.decoratorsr   r	   api_partner.serializersbackoffice.modelsdatetimeurllibr   django.core.paginatorr   eth_account.messagesr   tixsellr   web3r   web3.middlewarer   eth_accountr   r   eth_account.signers.localr   django.shortcutsr   mnemonicr   django.corer   loggingZrest_framework.permissionsr   rest_frameworkr   r   r   r   drf_yasgr   Zdrf_yasg.utilsr   api.serializersr   r   django.contrib.authr    base64tempfileosstripejsondjango.core.files.baser!   backoffice.forms.signupr"   django.utilsr#   pytzZapi_partner.views_eventsr$   r%   r&   	getLoggerr   r2   r   r   r/   r/   r/   r0   <module>   sd   
   1S