o
    3phd                     @   s  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
 G dd deZed	d
ZddlmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZ G dd dejZG dd dejZG dd dejZ d dl!m"Z" d dl#Z#d dl$m$Z$ G dd dejZ%G dd dejZ&G dd dejZ'G dd dejZ(G dd dejZ)G d d! d!ejZ*G d"d# d#ejZ+G d$d% d%ejZ,G d&d' d'ejZ-G d(d) d)ejZ.e/eG d*d+ d+ejZ0G d,d- d-ejZ1e/ee e/ee e/ee  e/ee' e/ee( e/ee) e/ee, e/ee% e/ee& e/ee. e/ee* e/ee+ e/ee- e/ee0 e/ee1 dS ).    )adminmessagestimezone)format_html)	AdminSite)reversec                       sD   e Zd ZdZdZdZ fddZd fdd	ZG d	d
 d
Z  Z	S )BloggersEMSAdminSitezBloggers of Zed EMSzBloggers of Zed EMS - AdminzAdmin Dashboardc                    s   t  |}d|d< |S )N/site_url)supereach_context)selfrequestcontext	__class__ */var/www/html/bloggers_ems/events/admin.pyr      s   z!BloggersEMSAdminSite.each_contextNc                    s.   t  |}|dddddddgdg7 }|S )	Nz	Main Site	main_sitez	View Site	view_siter   T)nameobject_name	admin_url	view_only)r   	app_labelmodels)r   get_app_list)r   r   r   app_listr   r   r   r      s   z!BloggersEMSAdminSite.get_app_listc                   @      e Zd ZddiZdS )zBloggersEMSAdminSite.Mediaallzcss/admin_custom.cssN__name__
__module____qualname__cssr   r   r   r   Media#       r(   N)
r$   r%   r&   
site_titlesite_headerindex_titler   r   r(   __classcell__r   r   r   r   r
   	   s    r
   bloggers_ems_adminr      )EventSpeakerAgendaCallForProposalGalleryRoleUserProfileRegistrationSessionTypeProposalSectionImagePublicationPublicationTypeCarouselImageSponsorc                   @   sl   e Zd ZdZg dZdZdddifddd	d
fdddd
ffZdd Zde_dd Z	de	_dd Z
de
_dS )
EventAdmin)title	date_timeend_timevenue
get_statusregistration_openregistration_counthas_live_stream)rC   statusrG   rB   descriptionzEvent Detailsfields)rB   rL   rC   rD   rE   banner_imager9   )rG   z1Control whether users can register for this event)rM   rL   zLive Stream)
stream_urlzAdd a Facebook video URL to enable live streaming during the event. The URL should be in the format: https://www.facebook.com/username/videos/VIDEO_IDc                 C   s*   dddd}| |jd}td|| S )z*Display the event status with colored textprimarysuccess	secondary)upcomingongoingarchivedz#<span class="badge bg-{}">{}</span>)getrJ   r   get_status_display)r   objstatus_colorscolorr   r   r   rF   N   s   zEventAdmin.get_statusStatusc                 C   s   |j jdd S )z&Display the number of registered users
registeredrJ   )registrationsfiltercountr   rX   r   r   r   rH   ]   s   zEventAdmin.registration_countzRegistered Usersc                 C   s8   |j stdS |jrtdS |jr|jrtdS tdS )Nu+   <span class="badge bg-secondary">✗</span>z*<span class="badge bg-success">Live</span>z.<span class="badge bg-primary">Recorded</span>z,<span class="badge bg-info">Scheduled</span>)
has_streamr   
is_ongoingis_archivedrO   ra   r   r   r   rI   b   s   zEventAdmin.has_live_streamzStream StatusN)r$   r%   r&   list_displaylist_filtersearch_fields	fieldsetsrF   short_descriptionrH   rI   r   r   r   r   rA   <   s,    
	rA   c                   @   *   e Zd ZdZdZdZdZdd Zde_dS )	SpeakerAdmin)r   organizationwebsite_link
created_at)rl   rn   )r   biorl   )eventsc                 C   s   |j rtd|j |j S dS )Nz#<a href="{}" target="_blank">{}</a>-)websiter   ra   r   r   r   rm   s   s   zSpeakerAdmin.website_linkWebsiteN)	r$   r%   r&   re   rf   rg   filter_horizontalrm   ri   r   r   r   r   rk   m   s    
rk   c                   @   rj   )	AgendaAdmin)session_titleeventsession_timeget_speakers)rw   rx   speakers)rv   rL   )rz   c                 C   s   d dd |j D pdS )z(Display comma-separated list of speakers, c                 S      g | ]}|j qS r   r0   ).0speakerr   r   r   
<listcomp>       z,AgendaAdmin.get_speakers.<locals>.<listcomp>rq   )joinrz   r!   ra   r   r   r   ry      s   zAgendaAdmin.get_speakersSpeakersN)	r$   r%   r&   re   rf   rg   rt   ry   ri   r   r   r   r   ru   y   s    
ru   )HttpResponseN)datetimec                   @   sn   e Zd ZdZdZdZdZg dZdZdd Z	d	e	_
d
d Zde_
dd Zde_
dd Zde_
dd Zde_
dS )RegistrationAdmin)userrw   rJ   registration_dateattendance_date
user_email
user_phone)rJ   rw   r   )user__usernameuser__emailevent__titlenotes)r   )mark_as_attendedmark_as_cancelledexport_as_csvz/admin/registration/registration_changelist.htmlc                 C      |j jS r*   r   emailra   r   r   r   r         zRegistrationAdmin.user_emailEmailc                 C   s"   t jj|jd }|r|jS dS )Nr   rq   )r8   objectsr_   r   firstphone_number)r   rX   profiler   r   r   r      s   zRegistrationAdmin.user_phonePhonec                 C   s   |j dt d d S )Nattended)rJ   r   )updater   nowr   r   querysetr   r   r   r      s   z"RegistrationAdmin.mark_as_attendedz'Mark selected registrations as attendedc                 C   s   |j dd d S )N	cancelledr]   )r   r   r   r   r   r      s   z#RegistrationAdmin.mark_as_cancelledz(Mark selected registrations as cancelledc           
      C   s   | j j}g d}tdd}dt d d|d< t|}|g d |D ]5}t	j
j|jd	 }|r9|jnd
}	||jj|jj|	|jj|j|jd|jrW|jdnd
|jg q(|S )N)r   rw   rJ   r   r   r   ztext/csv)content_typez#attachment; filename=registrations-z%Y%m%d-%H%M%Sz.csvzContent-Disposition)Usernamer   r   r2   r[   zRegistration DatezAttendance DateNotesr   rq   %Y-%m-%d %H:%M:%S)model_metar   r   r   strftimecsvwriterwriterowr8   r   r_   r   r   r   usernamer   rw   rB   rJ   r   r   r   )
r   r   r   metafield_namesresponser   rX   r   phoner   r   r   r      s(   


zRegistrationAdmin.export_as_csvz$Export selected registrations as CSVN)r$   r%   r&   re   rf   rg   readonly_fieldsactionschange_list_templater   ri   r   r   r   r   r   r   r   r   r      s"    
r   c                   @   s   e Zd ZdZdZdZdZdZdddifd	d
ddffZg dZ	dd Z
de
_dd Zde_dd Zde_dd Zdd Zde_dd Zde_dd  Zd!e_d"d# Zd$e_d%d& Zd'e_d(d) Zd*d+ ZG d,d- d-Zd.S )/UserProfileAdmin)username_linkr   rl   r   rn   )rolerl   rn   user__is_staffuser__is_superuser)r   r   rl   ro   r   )rn   
updated_atuser_detailsz-admin/userprofile/userprofile_changelist.htmlzUser InformationrM   )r   r   ro   rl   r   profile_picturezAccount Details)r   rn   r   )collapse)rM   classes)
make_staffremove_staff
make_adminremove_adminc                 C   s*   t dd|jjid}td||jj|jjS )Nzevents:view_profiler   )kwargsz<a href="{}" class="profile-link" title="View profile">{}</a> <a href="/admin/auth/user/{}/change/" class="edit-link" title="Edit user"><i class="fas fa-edit"></i></a>)r	   r   r   r   id)r   rX   urlr   r   r   r      s
   zUserProfileAdmin.username_linkr   c                 C   r   r*   r   ra   r   r   r   r      r   zUserProfileAdmin.emailr   c              	   C   s<   |j }td|j|j|jd|jr|jdnd| |S )Nz<div class="user-details"><p><strong>Username:</strong> {}</p><p><strong>Email:</strong> {}</p><p><strong>Date Joined:</strong> {}</p><p><strong>Last Login:</strong> {}</p><p><strong>Status:</strong> {}</p></div>r   Never)r   r   r   r   date_joinedr   
last_loginget_user_status)r   rX   r   r   r   r   r      s   
zUserProfileAdmin.user_detailszAccount Informationc                 C   s$   |j rtdS |jrtdS tdS )Nz*<span style="color: red;">Superuser</span>z'<span style="color: blue;">Staff</span>z/<span style="color: green;">Regular User</span>)is_superuserr   is_staff)r   r   r   r   r   r      s
   z UserProfileAdmin.get_user_statusc                 C   sT   g }|j jr|j js|td|j j n|j js#|td|j j td|S )NzB<button onclick="makeAdmin({})" class="button">Make Admin</button>zB<button onclick="makeStaff({})" class="button">Make Staff</button>z&nbsp;)r   r   r   appendr   r   r   )r   rX   r   r   r   r   user_actions   s    zUserProfileAdmin.user_actionsActionsc                 C   4   |D ]}|j jsd|j _|j   q| |d d S )NTz.Selected users have been granted staff access.r   r   r   savemessage_userr   r   r   r   r   r   r   r        
zUserProfileAdmin.make_staffzGrant staff accessc                 C   r   )NFz-Selected users have had staff access removed.r   r   r   r   r   r     r   zUserProfileAdmin.remove_staffzRemove staff accessc                 C   s4   |D ]}d|j _d|j _|j   q| |d d S )NTz.Selected users have been granted admin access.)r   r   r   r   r   r   r   r   r   r     s
   zUserProfileAdmin.make_adminzGrant admin accessc                 C   s,   |D ]}d|j _|j   q| |d d S )NFz-Selected users have had admin access removed.)r   r   r   r   r   r   r   r   r   $  s   zUserProfileAdmin.remove_adminzRemove admin accessc                 C   s   |j }|  dS )z8Override delete to properly remove both user and profileN)r   delete)r   r   rX   r   r   r   r   delete_model+  s   zUserProfileAdmin.delete_modelc                 C   s$   dd |D }|D ]}|   q	dS )z:Override bulk delete to properly remove users and profilesc                 S   r|   r   r   )r}   r   r   r   r   r   4  r   z4UserProfileAdmin.delete_queryset.<locals>.<listcomp>N)r   )r   r   r   usersr   r   r   r   delete_queryset1  s   
z UserProfileAdmin.delete_querysetc                   @   s   e Zd ZdZdS )zUserProfileAdmin.Media)zjs/user_profile_admin.jsN)r$   r%   r&   jsr   r   r   r   r(   9  s    r(   N)r$   r%   r&   re   rf   rg   r   r   rh   r   r   ri   r   r   r   r   r   r   r   r   r   r   r(   r   r   r   r   r      sF    	r   c                   @   rj   )	CallForProposalAdmin)rB   rw   rJ   submission_deadlineimage_preview
created_by)rJ   rw   r   rK   )rn   r   r   c                 C      |j r
td|j jS dS Nz;<img src="{}" style="max-height: 50px; max-width: 100px" />zNo Imageimager   r   ra   r   r   r   r   B     z"CallForProposalAdmin.image_previewPreviewN)	r$   r%   r&   re   rf   rg   r   r   ri   r   r   r   r   r   <  s    
r   c                   @   s   e Zd ZdZdZdZdS )SessionTypeAdmin)r   rL   r0   N)r$   r%   r&   re   rg   orderingr   r   r   r   r   I  s    r   c                   @   s    e Zd ZdZdZdZdZdZdS )ProposalAdmin)rB   cfporganisation	submittersession_typesponsorshiprJ   submitted_at)rJ   r   r   r   
cfp__event)rB   abstractsubmitter__usernamer   contact_number)r   r   reviewed_atz'admin/proposal/proposal_changelist.htmlN)r$   r%   r&   re   rf   rg   r   r   r   r   r   r   r   O  s    r   c                       s*   e Zd ZdZdZdZd fdd	Z  ZS )PublicationTypeAdmin)r   iconallowed_extensionsr0   Nc                    s    |r	|j dv r	dS t ||S )N)PresentationDocumentzResearch PaperF)r   r   has_delete_permission)r   r   rX   r   r   r   r   \  s   z*PublicationTypeAdmin.has_delete_permissionr*   )r$   r%   r&   re   rg   r   r   r.   r   r   r   r   r   W  s
    r   c                       sz   e Zd ZdZdZdZdZddgZdd Zd	e_	d
d Z
de
_	dd Zde_	dd Zde_	dd Zde_	 fddZ  ZS )PublicationAdmin)rB   authorget_type	is_publicpublished_at	file_linkpublication_status)r   r   )rB   r   author__usernameauthor__email)r   make_publicmake_privatec                 C   s   |j r|j jS dS )Nrq   )publication_typer   ra   r   r   r   r   i  s   zPublicationAdmin.get_typeTypec                 C   s,   |j r|jr
|jjnd}td|j j|S dS )Nzbi-file-earmarkz=<a href="{}" target="_blank"><i class="{}"></i> View File</a>rq   )filer  r   r   r   )r   rX   
icon_classr   r   r   r   m  s   zPublicationAdmin.file_linkFilec                 C   s   |j stdS tdS )Nz(<span style="color: red;">Private</span>z)<span style="color: green;">Public</span>)r   r   ra   r   r   r   r   x  s   z#PublicationAdmin.publication_statusr[   c                 C   s^   d}|D ]}|  rd|_|  |d7 }q|r%| || dtj d S | |dtj d S )Nr   Tr1   z! publication(s) were made public.zANo publications were made public. Check publication requirements.)can_be_publishedr   r   r   r   SUCCESSWARNING)r   r   r   made_publicpubr   r   r   r    s$   zPublicationAdmin.make_publicz!Make selected publications publicc                 C   sF   |j ddjdd}|r| || dtj d S | |dtj d S )NT)r   Fz" publication(s) were made private.z"No publications were made private.)r_   r   r   r   r	  r
  )r   r   r   updatedr   r   r   r    s   zPublicationAdmin.make_privatez"Make selected publications privatec                    s,   |s|j |_t |_t |||| d S r*   )r   r   r   r   r   r   
save_model)r   r   rX   formchanger   r   r   r    s   
zPublicationAdmin.save_model)r$   r%   r&   re   rf   rg   r   r   r   ri   r   r   r  r  r  r.   r   r   r   r   r   b  s"    	r   c                       sB   e Zd ZdZdZdZdZdd Zde_ fdd	Z	d
d Z
  ZS )GalleryAdmin)captionrw   r   rw   )r  r   z%admin/gallery/gallery_changelist.htmlc                 C   r   Nz)<img src="{}" style="max-height: 50px;"/>rq   r   ra   r   r   r   r     r   zGalleryAdmin.image_previewr   c                    s0   ddl m} t  }|d| jddg}|| S )Nr   )pathzbulk-upload/gallery_bulk_uploadr0   )django.urlsr  r   get_urlsbulk_upload_view)r   r  urlscustom_urlsr   r   r   r    s
   
zGalleryAdmin.get_urlsc                 C   sV  ddl m}m} ddlm} ddlm} ddlm}m	}m
}m}	 ddlm}
m} dd l}dd l}|t}|jdkrk|jdd	k}z|jd
}|sed}|rZ|
d|dddW S ||| |dW S |jd}|sd}|r{|
d|dddW S ||| |dW S z	tjj|d}W n% tjy   d}|r|
d|ddd Y W S ||| |d Y W S w d}d}ttjj|dj ddj!ddd}|D ]%}|j"}||v r|d7 }qtjj#||d|j$ |d |d7 }|%| q|&d| d| d|j$ d |j' d!	 |dkr| d"| d#}n| d$}|r%|
d%|dW S |(|| |d&W S  t)yj } z.|d't*|  d(}|rV|
d|dd)dW  Y d }~S ||| |dW  Y d }~S d }~ww |+ }||j,d*d+ }tjj|d,j-d-d tjj||d.j-d/d tjj|d0j-d1d tj.d2}|d3dd4}||d5|S )6Nr   )renderredirectr   r   )CaseWhenValue	CharField)JsonResponseHttpResponseBadRequestPOSTzX-Requested-WithXMLHttpRequestrw   zNo event selectederror)rJ   messagei  r]   .imageszNo images selected)r   zSelected event does not existr  T)original_filename__isnulloriginal_filename)flatr1   zEvent photo - )rw   r   r  r+  zBulk upload: z images uploaded, z skipped for event z (ID: )z  images uploaded successfully! (z duplicate images were skipped)z images uploaded successfully!rQ   z..zBulk upload error: zAn error occurred during uploadi     )hours)date_time__gtrS   )r0  date_time__lterT   )r1  rU   z
-date_timezBulk Upload Images)rp   rB   has_file_fieldzadmin/gallery/bulk_upload.html)/django.shortcutsr  r  django.contribr   django.utilsr   django.db.modelsr  r  r   r!  django.httpr"  r#  jsonlogging	getLoggerr$   methodheadersrV   r$  r&  FILESgetlistr2   r   DoesNotExistsetr6   r_   excludevalues_listr   createrB   addinfor   rQ   	Exceptionstrr   	timedeltar   order_by)r   r   r  r  r   r   r  r  r   r!  r"  r#  r8  r9  loggeris_ajaxevent_id	error_msgr)  rw   created_countskipped_countexisting_filenamesr   filenamesuccess_msger   two_hours_agorp   r   r   r   r   r    s   



(



zGalleryAdmin.bulk_upload_view)r$   r%   r&   re   rf   rg   r   r   ri   r  r  r.   r   r   r   r   r    s    r  c                   @   s@   e Zd ZdZdZdZdZddgZdd Zd	e_	G d
d dZ
dS )CarouselImageAdmin)rB   r   	is_activeorderrn   )rV  rW  )rV  rn   )rB   r  rW  -created_atc                 C   r   r  r   ra   r   r   r   r   >  r   z CarouselImageAdmin.image_previewr   c                   @   r    )zCarouselImageAdmin.Mediar!   r"   Nr#   r   r   r   r   r(   D  r)   r(   N)r$   r%   r&   re   list_editablerf   rg   r   r   ri   r(   r   r   r   r   rU  7  s    rU  c                   @   s"   e Zd ZdZdZdd Zde_dS )	RoleAdmin)r   get_permissions)permissionsc                 C   s$   |j rddd |j  D S dS )Nr{   c                 S   s   g | ]\}}|r|qS r   r   )r}   kvr   r   r   r   O  s    z-RoleAdmin.get_permissions.<locals>.<listcomp>rq   )r\  r   itemsra   r   r   r   r[  M  s   zRoleAdmin.get_permissionszActive PermissionsN)r$   r%   r&   re   r   r[  ri   r   r   r   r   rZ  I  s
    
rZ  c                   @   s:   e Zd Zg dZddgZddgZddgZdgZddgZdS )SponsorAdmin)r   rr   rV  rW  rV  rW  rp   r   rr   N)	r$   r%   r&   re   rY  rf   rg   rt   r   r   r   r   r   r`  S  s    r`  c                       sP   e Zd ZdZdZdZdZddgZ fddZd	d
 Z	de	_
G dd dZ  ZS )SectionImageAdmin)rB   sectionr   rV  rn   )rV  )rb  rV  rn   )rB   r  rb  rb  rX  c                    s(   |j dkr
dg|d< t j|fi |S )Nrb  )join_communityzJoin Community Sectionchoices)r   r   formfield_for_dbfield)r   db_fieldr   r   r   r   re  c  s   
z'SectionImageAdmin.formfield_for_dbfieldc                 C   r   r   r   ra   r   r   r   r   m  r   zSectionImageAdmin.image_previewr   c                   @   r    )zSectionImageAdmin.Mediar!   r"   Nr#   r   r   r   r   r(   t  r)   r(   )r$   r%   r&   re   rY  rf   rg   r   re  r   ri   r(   r.   r   r   r   r   ra  \  s    
ra  )2r4  r   r   r5  r   django.utils.htmlr   django.contrib.adminr   r  r	   r
   
admin_siter   r2   r3   r4   r5   r6   r7   r8   r9   r:   r;   r<   r=   r>   r?   r@   
ModelAdminrA   rk   ru   r7  r   r   r   r   r   r   r   r   r   r   r  rU  rZ  registerr`  ra  r   r   r   r   <module>   sX    
 D17}I 
