__author__ = 'Mael Beuget, Pierre Monnin & Thibaut Smith'

from handler.BaseHandler import *
from model.Absentees import *


class ShowAbsenteeHandler(BaseHandler):
    def __init__(self, request=None, response=None):
        self.initialize(request, response)
        self.page_name = "administration"

    def get(self):
        if self.is_connected() and get_is_admin_from_id(self.request.cookies.get('user_id').split('|')[0]):
            if self.request.get('id') and self.request.get('id') != "":
                absentee = get_absentee_from_id(self.request.get('id'))

                self.render("administration_absentee_show.html", absentee=absentee)

            else:
                self.render("message.html", title="Invalid URL", text="It seems you're using an invalid URL")
        else:
            self.render("message.html", title="Access forbidden",
                        text="It seems you're not an administrator nor a connected user")

    def post(self):
        if self.is_connected() and get_is_admin_from_id(self.request.cookies.get('user_id').split('|')[0]):
            if self.request.get('id') and self.request.get('id') != "":
                #Update absentee entry
                absentee = get_absentee_from_id(self.request.get('id'))

                if self.request.get('justified'):
                    absentee.justification_bool = True
                else:
                    absentee.justification_bool = False

                if self.request.get('justification') and self.request.get('justification') != "":
                    absentee.justification_text = self.request.get('justification')
                else:
                    absentee.justification_text = "/"

                absentee.put()

                #Redirect to correct page
                self.redirect('/show_absentee?id=' + self.request.get('id'))

            else:
                self.render("message.html", title="Invalid URL", text="It seems you're using an invalid URL")

        else:
            self.render("message.html", title="Access forbidden",
                        text="It seems you're not an administrator nor a connected user")

@@ -116,16 +116,41 @@
 mport *%0A
+from model.Logs import *%0A
 %0A%0Aclass 
@@ -1170,32 +1170,69 @@
 et('id') != %22%22:%0A
+                log_sentence = None%0A%0A
                 
@@ -1452,354 +1452,1630 @@
     
-else:%0A                    absentee.justification_bool = False%0A%0A                if self.request.get('justification') and self.request.get('justification') != %22%22:%0A                    absentee.justification_text = self.request.get('justification')%0A                else:%0A                    absentee.justification_text = %22/%22%0A%0A                absentee
+    log_sentence = %22%25(admin)s accepted the absence of %25(student)s. (%25(lesson)s from %22 %5C%0A                                   %22%25(hour_start)s to %25(hour_end)s%22%0A                else:%0A                    absentee.justification_bool = False%0A                    log_sentence = %22%25(admin)s refused the absence of %25(student)s. (%25(lesson)s from %22 %5C%0A                                   %22%25(hour_start)s to %25(hour_end)s%22%0A%0A                if self.request.get('justification') and self.request.get('justification') != %22%22:%0A                    absentee.justification_text = self.request.get('justification')%0A                else:%0A                    absentee.justification_text = %22/%22%0A%0A                absentee.put()%0A                user_id = self.request.cookies.get('user_id').split('%7C')%5B0%5D%0A                account = get_account_from_id(user_id)%0A%0A                log_sentence = log_sentence %25 %7B'admin': account.name,%0A                                               'student': absentee.student_name,%0A                                               'lesson': absentee.class_title,%0A                                               'hour_start': absentee.start_hour,%0A                                               'hour_end': absentee.end_hour%0A                %7D%0A%0A                # I don't know why, the date won't accept the same way%0A                log_sentence = log_sentence + %22 on the %22 + absentee.class_date + %22.)%22%0A                Logs(%0A                    date_time=self.date_details.now(),%0A                    category=%22absentee control%22,%0A                    author=account.name,%0A                    description=log_sentence%0A                )
 .put

Add logs when accepting/refusing an excuse.