def add_member(self):
     """
 Adds this member to the database. Assumes data has already been validated.
 Returns True if successful, otherwise False.
 """
     # If the user is already in the database, skip this user.
     if validation_utils.check_uid_exists(self.uid):
         return False
     # Generate an account creation key.
     create_account_key = auth_utils.generate_create_account_key()
     query = sqlalchemy.text("""
   INSERT INTO members (first_name, last_name, matriculation_year, graduation_year,
     uid, email, member_type, create_account_key)
   VALUES (:first_name, :last_name, :matriculation_year, :graduation_year,
     :uid, :email, :member_type, :create_account_key)
   """)
     flask.g.db.execute(query,
                        first_name=self.first_name,
                        last_name=self.last_name,
                        matriculation_year=self.matriculation_year,
                        graduation_year=self.graduation_year,
                        uid=self.uid,
                        email=self.email,
                        member_type=self.member_type,
                        create_account_key=create_account_key)
     # Email the user.
     subject = "Welcome to the Ruddock House website!"
     msg = email_templates.AddedToWebsiteEmail.format(
         self.name,
         flask.url_for('account.create_account',
                       create_account_key=create_account_key,
                       _external=True))
     to = self.email
     email_utils.send_email(to, msg, subject)
     return True
Exemple #2
0
 def add_member(self):
   """
   Adds this member to the database. Assumes data has already been validated.
   Returns True if successful, otherwise False.
   """
   # If the user is already in the database, skip this user.
   if validation_utils.check_uid_exists(self.uid):
     return False
   # Generate an account creation key.
   create_account_key = auth_utils.generate_create_account_key()
   query = sqlalchemy.text("""
     INSERT INTO members (first_name, last_name, matriculation_year, graduation_year,
       uid, email, member_type, create_account_key)
     VALUES (:first_name, :last_name, :matriculation_year, :graduation_year,
       :uid, :email, :member_type, :create_account_key)
     """)
   flask.g.db.execute(query, first_name=self.first_name,
       last_name=self.last_name,
       matriculation_year=self.matriculation_year,
       graduation_year=self.graduation_year,
       uid=self.uid,
       email=self.email,
       member_type=self.member_type,
       create_account_key=create_account_key)
   # Email the user.
   subject = "Welcome to the Ruddock House website!"
   msg = email_templates.AddedToWebsiteEmail.format(self.name,
       flask.url_for('account.create_account',
         create_account_key=create_account_key,
         _external=True))
   to = self.email
   email_utils.send_email(to, msg, subject)
   return True
Exemple #3
0
def handle_request_account(uid, last_name):
    """Handles a request to create an account.

  Checks that the email and UID match. If so, a create account link is sent to
  the user.

  Returns:
    Tuple (bool, string). The bool indicates success. If not successful,
    the string is an error message.
  """
    query = sqlalchemy.text("""
    SELECT name, last_name, email, username
    FROM members
      NATURAL JOIN members_extra
      NATURAL LEFT JOIN users
    WHERE uid = :uid
    """)
    result = flask.g.db.execute(query, uid=uid).first()
    if result is None or result["last_name"].lower() != last_name.lower():
        return (False, "Incorrect UID and/or name.")
    if result["username"] is not None:
        return (False, "You already have an account. Try recovering it?")
    email = result["email"]
    name = result["name"]

    # Generate a new account creation key.
    create_account_key = auth_utils.generate_create_account_key()
    query = sqlalchemy.text("""
    UPDATE members
    SET create_account_key = :create_account_key
    WHERE uid = :uid
    """)
    flask.g.db.execute(query, create_account_key=create_account_key, uid=uid)

    create_account_link = flask.url_for("account.create_account",
                                        create_account_key=create_account_key,
                                        _external=True)
    msg = email_templates.CreateAccountRequestEmail.format(
        name, create_account_link)
    subject = "Account creation request"
    email_utils.send_email(email, msg, subject)
    return (True, "")
def handle_request_account(uid, last_name):
  """Handles a request to create an account.

  Checks that the email and UID match. If so, a create account link is sent to
  the user.

  Returns:
    Tuple (bool, string). The bool indicates success. If not successful,
    the string is an error message.
  """
  query = sqlalchemy.text("""
    SELECT name, last_name, email, username
    FROM members
      NATURAL JOIN members_extra
      NATURAL LEFT JOIN users
    WHERE uid = :uid
    """)
  result = flask.g.db.execute(query, uid=uid).first()
  if result is None or result["last_name"].lower() != last_name.lower():
    return (False, "Incorrect UID and/or name.")
  if result["username"] is not None:
    return (False, "You already have an account. Try recovering it?")
  email = result["email"]
  name = result["name"]

  # Generate a new account creation key.
  create_account_key = auth_utils.generate_create_account_key()
  query = sqlalchemy.text("""
    UPDATE members
    SET create_account_key = :create_account_key
    WHERE uid = :uid
    """)
  flask.g.db.execute(query, create_account_key=create_account_key, uid=uid)

  create_account_link = flask.url_for("account.create_account",
      create_account_key=create_account_key,
      _external=True)
  msg = email_templates.CreateAccountRequestEmail.format(name,
      create_account_link)
  subject = "Account creation request"
  email_utils.send_email(email, msg, subject)
  return (True, "")