
    gA7                     h   d dl Z d dlmZ d dl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mZ d d	lmZ d d
lmZ d dlmZ d dlmZmZmZ 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% d dl&m'Z' d dl(m)Z)m*Z* d dl+m,Z, d dl-m.Z.m/Z/ d dl0m1Z1m2Z2m3Z3  ee,d      d        Z4 ee,d      d        Z5 ee,d      d        Z6d Z7d Z8 ee,d      d        Z9 ee,d      d         Z: ee,d      d!        Z;d" Z<d# Z= ee,d      d$        Z>d% Z? G d& d'e       Z@ G d( d)e"e      ZAy)*    N)datetime)settings)messages)authenticatelogin)user_passes_test)UserPassesTestMixin)User)	send_mail)Q)HttpResponse)renderget_object_or_404redirect)reversereverse_lazy)timezone)	LoginView)FormView)Workbook)FileUploadForm)process_excel_file)ProfileReview)check_user_is_administrator)OrganisationForm
SignupForm)StaffLinkRequestAdministratorOrganisationz/organisations/login)	login_urlc                 p   | j                   j                  j                  j                         j	                         }|j
                  j                         }|gt        |      z   }t        j                  j                  |      j                         j                  d      d d }||d}t        | d|      S )N user__profile__organisations__inz-created_at   )organisationreviewszorgs/dashboard.html)useradministratororganisationsallfirstchildrenlistr   objectsfilterdistinctorder_byr   )requestr&   child_organisationsall_organisationsr'   contexts         6/var/www/peopleoo.sandbox-dev.co.uk/apps/orgs/views.py	dashboardr8       s     <<--;;??AGGIL&//335%.A)BB nn##): $ hj-(".G
 %G
 '0'::    c                 |    | j                   j                  j                  j                         }t	        | dd|i      S )Nzorgs/find_your_staff.htmlr&   )r(   r)   r*   r,   r   )r3   r&   s     r7   find_your_staffr;   4   s7    <<--;;AACL '68VWWr9   c                     | j                   j                  d      }t        j                  j	                  |      }t        | dd|i      S )N
staffEmail)emailz)orgs/components/staff-search-results.htmlresultsr6   )GETgetr
   r/   r0   r   )r3   r>   r?   s      r7   staff_searchrC   ;   sB    KKOOL)Ell!!!.G'FQZ\cPdeer9   c                 b    t         j                  j                  |      }t        | dd|i      S )N)idzorgs/see_profile.htmlr(   r@   )r
   r/   rB   r   )r3   user_idr(   s      r7   see_profilerG   B   s-    <<w'D'2VTNKKr9   c                     t        j                         }t               }|j                  }| d|_        |j                  g d       | D ]  }|j                  j                   d|j                  j                   }|j                  ||j                  |j                  |j                  |j                  |j                  |j                  |j                  j!                  d      g        |j#                  |       |j%                  d       |S )Nz reviews)zEmployee NamezRating (1 - 5)zReview TextzReviewer NamezReviewer PhonezReviewer EmailzRelationship to UserzDate of Review %d/%m/%Yr   )ioBytesIOr   activetitleappendr(   
first_name	last_nameratingtextreviewer_namereviewer_phonereviewer_emailrelationship_to_user
created_atstrftimesaveseek)r'   organisation_nameoutputworkbooksheetreview	user_names          r7   create_xlsx_with_reviewsrb   H   s    ZZ\FzHOOE&'x0EK 
LL < =  {{--.a0E0E/FG	MMKK  !!!!''&&z2	
 		 MM& KKNMr9   c                    | j                   j                  j                  j                         j	                         }|j
                  j                         }|gt        |      z   }| j                  r| j                  j                  dd      }|r|j                  d      ng }t        j                  j                  ||      j                         }t        ||j                        }t!        j"                         }|j%                  d      }	t'        |d      }
d|j                   d	|	 d
|
d<   |
S t        j                  j                  |      j                         }|||d}t)        | d|      S )Nselected_reviews ,)pk__inr$   rJ   zAapplication/vnd.openxmlformats-officedocument.spreadsheetml.sheet)content_typezattachment; filename=_reviews_export_z.xlsxzContent-Dispositionr#   )r'   r-   r&   zorgs/export_review.htmlr@   )r(   r)   r*   r+   r,   r-   r.   POSTrB   splitr   r/   r0   r1   rb   namer   nowrY   r   r   )r3   r&   r4   r5   selected_ids_stringselected_idsr'   r]   rm   date_stringresponser6   s               r7   export_reviewrr   j   sq    <<--;;??AGGIL '//335%.A)BB ||%ll../A2F9L*005RT ..''-> ( 
 (* 	 *'<3D3DEllnll:.  \
 -B,BSBSATTdepdqqv*w&' nn##): $ hj 
 '$G
 '4gFFr9   c                 ~   | j                   j                  j                  j                         j	                         }| j
                  dk(  rUt        | j                  | j                  |      }|j                         r|j                          n||d}t        | d|      S t        |      }||d}t        | d|      S )Nrj   )instance)formr&   zorgs/account.html)r(   r)   r*   r+   r,   methodr   rj   FILESis_validrZ   r   )r3   r&   ru   r6   s       r7   accountry      s     <<--;;??AGGIL ~~gmmlS==?IIK  ,G '#6@@\2D$G '.88r9   c           	         | j                   j                  j                  j                         j	                         }t        t        |      }t        j                  j                  ||       d|j                   }t        j                   t        d       }d| d|j                   d| d|j                   d		}|j                   j                  g}t        ||t        j                   |d
       t#        d      S )Npk)r&   profilezRequest to join r   z
    Hello z%,

    You have been invited to join z on our platform. By joining, you will be able to share your special mentions with the organisation.

    To accept this invitation, please log in to your account and follow the instructions.

    [Click here to login and accept the invitation](z)

    Best regards,
    The z
 Team
    F)fail_silentlyzRequest Sent)r(   r)   r*   r+   r,   r   r   r   r/   createrl   r   SITE_BASE_URLr   r>   r   DEFAULT_FROM_EMAILr   )r3   
profile_pkr&   r}   subjectr!   messagerecipient_lists           r7   send_link_requestr      s    <<--;;??AGGILJ7G##w#O !2!2 34G))*77+;*<=I) ##/#4#4"5 65 6?K @	 
		 G ll(()N## ''r9   c                 h   t        t        |      }|j                  }|j                  }|j                  j                  |j                         |j                  |_        |j                  |_	        |j                          |j                          t        d|j                  j                   d      S )Nr{   zYou are now a member of .)r   r   r&   r}   staffaddrl   employer_namer>   employer_emailrZ   deleter   )r3   link_request_pklink_requestr&   r}   s        r7   accept_link_requestr      s    $%5/JL,,L""G<//0(--G)//GLLN2<3L3L3Q3Q2RRSTUUr9   c                    | j                   dk(  rt        | j                        }|j                         r|j                  d   }|j                  d   }|j                  d   }|j                  d   }|j                  d   }t
        j                  j                  |||||      }t        j                  j                  |      }t        j                  j                  ||	      }	|	j                  j                  |       t        ||
      }|t        | |       t        d      S t               }t!        | dd|i      S )Nrj   rP   rQ   r>   org_namepassword)usernamer>   r   rP   rQ   )r(   )rl   r>   )r   r   org_dashboardzorgs/signup.htmlru   )rv   r   rj   rx   cleaned_datar
   r/   create_userr   r   r    adminsr   r   r   r   r   )
r3   ru   rP   rQ   r>   r   r   r(   r)   r&   s
             r7   
org_signupr      s*   ~~',,'==?**<8J))+6I%%g.E((4H((4H<<++U%RZgq  ~G+  HD)1188d8CM'//66HE6RL##M2BDgt$O,, |'-~>>r9   c                    | j                   j                  j                  j                         j	                         }| j
                  j                  dd       }| j
                  j                  dd       }| j
                  j                  dd       }| j
                  j                  dd       }| j
                  j                  dd       }t        j                  j                  |j                  j                               }|r	 t        |      }|j                  |      }|r8t        j                  |d      j                         }|r|j                  |	      }|r8t        j                  |d      j                         }	|	r|j                  |	
      }|rB|j                  t!        |      t!        |      z  t!        |      z  t!        |      z        }|r=	 t#        t$        ||      }
|j                  |
j                  j                               }||d}t+        | d|      S # t        $ r Y w xY w# t$        j&                  $ r |j)                         }Y Hw xY w)NsearchrR   startendr-   )user__profile__in)rating__gtez%Y-%m-%d)created_at__gte)created_at__lte)text__icontains)(user__profile__preferred_name__icontains)user__first_name__icontains)user__last_name__icontains)r|   parent)r'   r&   zorgs/reviews_table.htmlr@   )r(   r)   r*   r+   r,   rA   rB   r   r/   r0   r   int
ValueErrorr   strptimedater   r   r    DoesNotExistnoner   )r3   r&   search_stringrR   
start_dateend_dater-   r'   start_date_objectend_date_object	child_orgr6   s               r7   filter_reviewsr      s(   <<--;;??AGGILKKOOHd3M[[__Xt,F$/J{{ud+H{{z40H nn##l6H6H6L6L6N#OG 	[Fnnn8G
 $--j*EJJLnn5FnGG "++HjAFFHnn_nEG ..=!A1n{C|!|  @A  ^k  @l  "l  op  LY  oZ  "Z  [ 	%),8LYInny7J7J7LnMG $G
 '4gFFI  		6 (( 	%llnG	%s$   H+ <H; +	H87H8;#I! I!c                     t        t        |      }t        t        |      }|| j                  j                  j
                  j                         v r|j                  j                  |       d|i}t        | d|      S )Nr{   r&   z orgs/components/staff_table.htmlr@   )
r   r   r    r(   r)   r*   r+   r   remover   )r3   r   org_pkr}   r&   r6   s         r7   remove_staff_from_organisationr   I  so    J7G$\f=Lw||11??CCEE!!'* 	G '=wOOr9   c                   &    e Zd ZdZ ed      Zd Zy)OrganisationLoginViewzorgs/login.htmlr   c                 4    | j                   xs t        d      S Nr   )	next_pager   selfs    r7   get_success_urlz%OrganisationLoginView.get_success_url[  s    ~~>o!>>r9   N)__name__
__module____qualname__template_namer   r   r    r9   r7   r   r   W  s    %M_-I?r9   r   c                   <     e Zd ZdZeZd Z fdZd Z fdZ	 xZ
S )UploadStaffFormzorgs/upload_staff.htmlc                     	 | j                   j                  j                  }t        j                  j                  |      j                         S #  Y yxY w)N)r   F)r3   r(   r)   r    r/   r0   exists)r   r)   s     r7   	test_funczUploadStaffForm.test_funcc  sI    	 LL--;;M''..m.DKKMM	s   AA Ac           	      @   | j                   j                  j                  j                  j	                         }|j
                  d   }t        ||dd      \  }}t        j                  | j                   t        j                  d| d| d       t        | -  |      S )NfileFT)r   send_emailszFile processed. Imported z rows out of r   )r3   r(   r)   r*   r,   r   r   r   add_messageINFOsuper
form_valid)r   ru   r&   r   number_added
total_rows	__class__s         r7   r   zUploadStaffForm.form_validk  s    ||((66DDJJL  (#5dLQVdh#i jT\\8==8mT^S__`a	c w!$''r9   c                     t        d      S r   )r   r   s    r7   r   zUploadStaffForm.get_success_urlt  s    ''r9   c                     t         |          }| j                  j                  j                  j
                  j                         }||d<   |S )Nr&   )r   get_context_datar3   r(   r)   r*   r,   )r   kwargsr6   r&   r   s       r7   r   z UploadStaffForm.get_context_dataw  sC    '*,||((66DDJJL".r9   )r   r   r   r   r   
form_classr   r   r   r   __classcell__)r   s   @r7   r   r   _  s&    ,MJ(( r9   r   )BrK   r   openpyxldjango.confr   django.contribr   django.contrib.authr   r   django.contrib.auth.decoratorsr   django.contrib.auth.mixinsr	   django.contrib.auth.modelsr
   django.core.mailr   django.db.modelsr   django.httpr   django.shortcutsr   r   r   django.urlsr   r   django.utilsr   django.contrib.auth.viewsr   django.views.genericr   r   apps.carers.formsr   apps.carers.lib.process_excelr   apps.carers.modelsr   r   apps.orgs.decoratorsr   apps.orgs.formsr   r   apps.orgs.modelsr   r   r    r8   r;   rC   rG   rb   rr   ry   r   r   r   r   r   r   r   r   r9   r7   <module>r      sj   	     # 3 ; : + &  $ @ @ - ! / )  , < . < 8 J J -9OP; Q;& -9OPX QX -9OPf QfLD -9OP-G Q-Gb -9OP9 Q9. -9OP( Q(@V?8 -9OP4G Q4GRP?I ?h 3 r9   