from tornado.web import authenticated

from amgut.handlers.base_handlers import BaseHandler
from amgut.util import AG_DATA_ACCESS


class NewParticipantHandler(BaseHandler):
    """"""
    @authenticated
    def get(self):
        self.render("new_participant.html", skid=self.current_user)

    @authenticated
    def post(self):
        deceased_parent = self.get_argument("deceased_parent", None)
        participant_name = self.get_argument("participant_name")
        is_juvenile = self.get_argument("is_juvenile", 'off')

        ag_login_id = AG_DATA_ACCESS.get_user_for_kit(self.current_user)
        kit_email = AG_DATA_ACCESS.TODO_SOMETHING(self.current_user)

        # Get the list of participants attached to that login id
        participants = AG_DATA_ACCESS.getHumanParticipants(ag_login_id)

        # Check if the participant is on the exceptions list
        is_exception = (
            participant_name
            in AG_DATA_ACCESS.getParticipantExceptions(ag_login_id))

        # If the participant already exists, stop them outright
        if participant_name in participants:
            self.render("portal.html", errmsg="Participant %s already exists!"
                                              % participant_name)

        if is_juvenile == 'off' and is_exception:
            self.render("portal.html", errmsg="We are expecting a survey from "
                                              "that juvenile user (%s)"
                                              % participant_name)

        if is_juvenile == 'on':
            # If they aren't already an exception, we need to verify them
            if not is_exception:
                juvenile_age = self.get_argument("juvenile_age")
                parent_1_name = self.get_argument("parent_1_name")
                parent_2_name = self.get_argument("parent_2_name")

                alert_message = ("Thank you for your interest in this study. "
                                 "Because of your status as a minor, we will "
                                 "contact you within 24 hours to verify "
                                 "parent/guardian consent.")

                subject = ("AGJUVENILE: %s (ag_login_id: %s) is a child"
                           % (participant_name, ag_login_id))

                message = ("""Contact: %s
        --------------------------------------------------------------------------------
        Message:
        This participant is a child, the person filling out the survey for them
        needs to provide proof of consent. Email them for proof.

        Juvenile age: %s
        Parent/Guardian 1: %s
        Parent/Guardian 2: %s
        Deceased: %s
        Kit id: %s
        Email: %s
        --------------------------------------------------------------------------------
        """ % (participant_name, juvenile_age, parent_1_name, parent_2_name,
               deceased_parent, self.current_user, kit_email))

@@ -123,16 +123,578 @@
 _ACCESS%0A
+from amgut.lib.mail import send_email%0A%0A%0AMESSAGE_TEMPLATE = %22%22%22Contact: %25s%0A        --------------------------------------------------------------------------------%0A        Message:%0A        This participant is a child, the person filling out the survey for them%0A        needs to provide proof of consent. Email them for proof.%0A%0A        Juvenile age: %25s%0A        Parent/Guardian 1: %25s%0A        Parent/Guardian 2: %25s%0A        Deceased: %25s%0A        Kit id: %25s%0A        Email: %25s%0A        --------------------------------------------------------------------------------%0A%22%22%22%0A
 %0A%0Aclass 
@@ -1195,22 +1195,21 @@
 ESS.
-TODO_SOMETHING
+get_user_info
 (sel
@@ -1219,24 +1219,33 @@
 urrent_user)
+%5B'email'%5D
 %0A%0A        # 
@@ -1681,43 +1681,18 @@
     
- self.render(%22portal.html%22,
  errmsg
-=
+ = 
 %22Par
@@ -1723,73 +1723,86 @@
 ts!%22
-%0A                                              %25 participant_name
+ %25 participant_name%0A            self.redirect(%22portal.html?errmsg=%25s%22 %25 errmsg
 )%0A%0A 
@@ -1865,43 +1865,19 @@
     
- self.render(%22portal.html%22,
  errmsg
-=
+ = (
 %22We 
@@ -1908,57 +1908,8 @@
 rom 
-%22%0A                                              %22
 that
@@ -1955,32 +1955,8 @@
     
-       
-                 
 %25 pa
@@ -1969,16 +1969,76 @@
 ant_name
+)%0A            self.redirect(%22portal.html?errmsg=%25s%22 %25 errmsg
 )%0A%0A     
@@ -2831,644 +2831,922 @@
 e = 
-(%22%22%22Contact: %25s%0A        --------------------------------------------------------------------------------%0A        Message:%0A        This participant is a child, the person filling out the survey for them%0A        needs to provide proof of consent. Email them for proof.%0A%0A        Juvenile age: %25s%0A        Parent/Guardian 1: %25s%0A        Parent/Guardian 2: %25s%0A        Deceased: %25s%0A        Kit id: %25s%0A        Email: %25s%0A        --------------------------------------------------------------------------------%0A        %22%22%22 %25 (participant_name, juvenile_age, parent_1_name, parent_2_name,%0A               deceased_parent, self.current_user, kit_email)
+MESSAGE_TEMPLATE %25 (participant_name, juvenile_age,%0A                                              parent_1_name, parent_2_name,%0A                                              deceased_parent,%0A                                              self.current_user, kit_email)%0A%0A                try:%0A                    send_email(message, subject, sender=kit_email)%0A                    alert_message = (%22Your message has been sent.%22%0A                                     %22 We will reply shortly%22)%0A                except:%0A                    alert_message = (%22There was a problem sending your email.%22%0A                                     %22 Please contact us directly at %22%0A                                     %22%3Ca href='mailto:info@americangut.org'%3E%22%0A                                     %22info@americangut.org%3C/a%3E%22)%0A%0A                self.redirect(%22portal.html?errmsg=%25s%22 %25 alert_message)%0A%0A        self.redirect(%22survey_main.html%22
 )%0A

Complete port