o
    ۵g                     @   s   d dl mZmZ d dlmZmZmZmZmZm	Z	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Zd dlZd dlmZ G d	d
 d
eZdS )    )
UploadFilelogger)ColumnStringIntegerTextJSONDateTimeIndex
ForeignKey)BIGINT)relationship)Base)nowN)BytesIOc                   @   s  e Zd ZdZeddeddedddfZeedd	ddd
Zee	dddZ
eeddZeeedddZeeddZeedddZeeddZeedddZeededZeedeedZedd ddZedddddZedd Zdededefd d!Z d"S )#ProductproductsZ
idx_statusstatusZ
idx_pointspoints_requiredZidx_product_categorycategory_idT)unsigned)primary_keyautoincrement   F)nullablezproduct_categories.idr   )r   default   )r   r   onupdatec                   C   s   t ddgdjS )Nzapp.models.product_categoryProductCategory)fromlist)
__import__r    r!   r!   /app/app/models/product.py<lambda>   s    zProduct.<lambda>)back_populatesProductImagezProductImage.sort_orderproductzall, delete-orphan)order_byr$   cascadec                 C   s   | j r	| j d jS dS )zqReturns the thumbnail image for the product. If there are multiple images, returns the first one; otherwise None.r   N)imagesZ	image_url)selfr!   r!   r"   	thumbnail(   s   zProduct.thumbnailupload_filefolder_pathreturnc              
   C   s   d| d}z|  | W n ty, } ztd| dt|  W Y d}~nd}~ww |j}tj|d }t	
  | }|| }|j }	t|	}
|jd t|	}| jj| j|||
|jd td	|  d
| j d| }|S )a  
        Uploads the given file to the media server under the path:
          product/{folder_path}/{unique_filename}
        This method creates the folder structure if needed, uploads the file,
        and returns the public URL of the uploaded image.
        zproduct//zFolder creation failed for z: Nr   r   )content_typez)Uploaded image to media server with key: z0https://adimsayar.s3.eu-central-1.amazonaws.com/)Zcreate_folder	Exceptionr   errorstrfilenameospathsplitextuuiduuid4filereadlenseekr   clientZ
put_objectbucket_namer0   info)r*   r,   r-   Zfull_foldereZoriginal_filename	extensionZunique_filenameZfile_keycontent	file_sizeZin_memory_fileurlr!   r!   r"   upload_image/   s2   &
zProduct.upload_imageN)!__name__
__module____qualname____tablename__r
   __table_args__r   r   idr   namer   descriptionr   r   r   r   Zstockr   Zspecificationsr   r	   r   
created_at
updated_atr   categoryr)   propertyr+   r   r3   rF   r!   r!   r!   r"   r      s4    

r   )fastapir   r   
sqlalchemyr   r   r   r   r   r	   r
   r   Zsqlalchemy.dialects.mysqlr   sqlalchemy.ormr   app.db.baser   app.core.timer   r5   r8   ior   r   r!   r!   r!   r"   <module>   s   (