<commit_before>import boto3
import re
from flask_mail import Message
from boto3.dynamodb.conditions import Key, Attr
import uuid
import hashlib

def register(jsonData, mailer, dbInstance):
  if validateRegistrationJSON(jsonData):
    email = parseEmail(jsonData)
    hashedPassword = hashPassword(parsePassword(jsonData))
    activationId = addUserAndSendEmail(email, hashedPassword, mailer, dbInstance)
    return activationId
  else:
    return 'Failing Registration Validation'

def hashPassword(password):
  return hashlib.md5( password ).hexdigest()

def validateRegistrationJSON(jsonData):
  '''
  Validate that the JSON object contains
  an email and password attributes
  '''
  if jsonData is not None:
    return 'password' in jsonData.keys() and 'email' in jsonData.keys()
  return False

def parseEmail(jsonData):
  try:
    email = jsonData['email']
    return email
  except Exception:
    return None

def parsePassword(jsonData):
  try:
    password = jsonData['password']
    return password
  except Exception:
    return None

def isEmailValid(email):
  '''
  Validate that thee email is matches the
  format required.
  '''
  emailRegex = re.compile(r"[^@]+@[^@]+\.[^@]+")
  return emailRegex.match(email) is not None

def isPasswordValid(password):
  return len(password) >= 8

def addUserAndSendEmail(email, password, mailer, dbInstance):
  if isEmailValid(email) and isPasswordValid(password) and not isEmailInSystem(email, dbInstance):
    activationId = str(uuid.uuid4())
    dynamodb = dbInstance
    table = dynamodb.Table('users')

    #This will change an existing user with the same email.
    response = table.put_item(
      Item={
        'email': email,
        'password': password,
        'activationId': activationId
      }
    )
    try:
      sendEmail(email, activationId, mailer)
    except:
      print("Unable to send email")

    return activationId
  else:
    return 'none'

def sendEmail(email, activationId, mailer):
  '''
  Create a message and send it from our email to
  the passed in email. The message should contain
  a link built with the activationId
  '''
  #Build Message
  msg = Message('Mentii: Thank You for Creating an Account!', recipients=[email])<commit_msg>Adds active attribute to created users<commit_after>import boto3
import re
from flask_mail import Message
from boto3.dynamodb.conditions import Key, Attr
import uuid
import hashlib

def register(jsonData, mailer, dbInstance):
  if validateRegistrationJSON(jsonData):
    email = parseEmail(jsonData)
    hashedPassword = hashPassword(parsePassword(jsonData))
    activationId = addUserAndSendEmail(email, hashedPassword, mailer, dbInstance)
    return activationId
  else:
    return 'Failing Registration Validation'

def hashPassword(password):
  return hashlib.md5( password ).hexdigest()

def validateRegistrationJSON(jsonData):
  '''
  Validate that the JSON object contains
  an email and password attributes
  '''
  if jsonData is not None:
    return 'password' in jsonData.keys() and 'email' in jsonData.keys()
  return False

def parseEmail(jsonData):
  try:
    email = jsonData['email']
    return email
  except Exception:
    return None

def parsePassword(jsonData):
  try:
    password = jsonData['password']
    return password
  except Exception:
    return None

def isEmailValid(email):
  '''
  Validate that thee email is matches the
  format required.
  '''
  emailRegex = re.compile(r"[^@]+@[^@]+\.[^@]+")
  return emailRegex.match(email) is not None

def isPasswordValid(password):
  return len(password) >= 8

def addUserAndSendEmail(email, password, mailer, dbInstance):
  if isEmailValid(email) and isPasswordValid(password) and not isEmailInSystem(email, dbInstance):
    activationId = str(uuid.uuid4())
    dynamodb = dbInstance
    table = dynamodb.Table('users')

    #This will change an existing user with the same email.
    response = table.put_item(
      Item={
        'email': email,
        'password': password,
        'activationId': activationId,
        'active': "F"
      }
    )
    try:
      sendEmail(email, activationId, mailer)
    except:
      print("Unable to send email")

    return activationId
  else:
    return 'none'

def sendEmail(email, activationId, mailer):
  '''
  Create a message and send it from our email to
  the passed in email. The message should contain
  a link built with the activationId
  '''
  #Build Message
  msg = Message('Mentii: Thank You for Creating an Account!', recipients=[email])
{'fraction_non_alphanumeric': 0.09386120996441281, 'fraction_numerical': 0.002224199288256228, 'mean_word_length': 3.9201312910284463, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1046215', 'n_tokens_mistral': 1316, 'n_tokens_neox': 1289, 'n_words': 424}