<commit_before>
from models import Entry
from models import User
from models import Project
from models import Vendor
from models import Objective
from models import EvaluationCriteria
from models import DEFAULT_PROJECT_NAME
from models import project_db_key
from functools import wraps
import json
import time
import datetime
from flask import request, Response, url_for, redirect

def get_project_db_name(rname=DEFAULT_PROJECT_NAME):
    return rname

#Gets evaluation_criteria from db - this needs to implement evaluation_criteria-lifecycle - right now it is a singleton
def get_projects_from_db(userId):
    if userId:
        project_query = Project.query(Project.userIds.IN([userId]))
    else:
        project_query = Project.query()
    return project_query.fetch(100)

#Gets evaluation_criteria from db - this needs to implement evaluation_criteria-lifecycle - right now it is a singleton
def get_project_from_db(projectId):
    project_query = Project.query(Project.projectId == projectId)
    if project_query.count() < 1:
        return None
    else:
        return project_query.fetch(1)[-1]

def get_entry_from_db(projectId, userId):
    entrys_query = Entry.query(Entry.user.identity == userId, Entry.project.projectId == projectId)
    if entrys_query.count() < 1:
        return None
    else:
        return entrys_query.fetch(1)[-1]

def get_entrys_from_given_project_db(projectId):
    entrys_query = Entry.query(Entry.project.projectId == projectId)
    return entrys_query.fetch(100)

def get_entrys_from_given_user_db(userId):
    user = get_user_from_db(userId)
    entrys_query = Entry.query(Entry.user == user)
    return entrys_query.fetch(100)

def get_users_from_db(projectId=None):
    if projectId and projectId != "":
        project = get_project_from_db(projectId)
        if project is not None:
            userIds = project.userIds
            users = []
            for userId in userIds:
                user = get_user_from_db(userId)
                users.append(user)
            return users
    else:
        users_q = User.query(User.type != "Superuser")
        users = users_q.fetch(1000)
        return users

    return None

def get_user_from_db(userId):
    if "@" in userId:
        users_q = User.query(User.email == userId)
    else:
        users_q = User.query(User.identity == userId)
    if users_q.count() < 1:
        return None
    else:
        return users_q.fetch(1)[-1]

def update_users_project(projectId, userIds):
    project = get_project_from_db(projectId)
    project.userIds = userIds
    project.put()
    return project

def update_user(userId, email, type, password, projectIds):
    user = get_user_from_db(userId)
    if user is None:
        project_name = get_project_db_name()
        user = User(parent=project_db_key(project_name))
        user.identity = userId
        user.projectIds = []
    user.email = email
    user.type = type
    user.password = password
    if projectIds:
        for projId in projectIds:
            if projId and projId != "__CREATE__"and projId not in user.projectIds:
                    user.projectIds.append(projId)
                    project = get_project_from_db(projId)
                    if project:
                        project.userIds.append(userId)
                        project.put()
    user.put()
    time.sleep(1)
    return user

def getArrayOfDict(bos):
    # this is not good
    bot = bos[0]
    # this is even worse
    bot = '[' + bot + ']'
    bol = json.loads(bot)
    return bol

def update_project(projectId, department, group, description, defaultPassword, userIds, vendorIds, due_date, bos):
    project_name = get_project_db_name()
    project = get_project_from_db(projectId)
    if project is None:
        project = Project(parent=project_db_key(project_name))
        project.projectId = projectId
        project.objectiveIds = []
    project.department = department
    project.description = description
    project.group = group
    project.defaultPassword = <PASSWORD>
    project.userIds = userIds
    project.vendorIds = vendorIds
    for ui in userIds:
        user = get_user_from_db(ui)
        if user and projectId not in user.projectIds:
            user.projectIds.append(projectId)
            user.put()
    for vi in vendorIds:
        vendor = get_user_from_db(vi)
        if vendor and projectId not in vendor.projectIds:
            vendor.projectIds.append(projectId)
            vendor.put()
    if due_date is None or due_date == "":
        project.due_date = datetime.datetime.now()
    else:
        project.due_date = datetime.datetime.strptime(due_date.split(" ")[0], "%Y-%m-%d")
    bol = getArrayOfDict(bos)
    if len(project.objectiveIds) > 0:
        nnbos = []
        for bo in bol:
            nnbos.append(bo["objectiveId"])
        for pbo in project.objectiveIds:
            if pbo not in nnbos:
                delete_objective_from_db(projectId, pbo)
    for bo in bol:
        #print bo["objectiveId"] + ", " + bo["description"] + ", " + bo["weight"]
        boid = bo["objectiveId"]
        nbo = get_objective_from_db(projectId, boid)
        if nbo is None:
            nbo = Objective(parent=project_db_key(project_name))
            nbo.objectiveId = boid
            nbo.projectId = projectId
        nbo.description = bo["description"]
        nbo.weight = int(bo["weight"])
        nbo.evaluation_criteriaIds = []
        for ec in bo["evaluation_criteria"]:
            ecid = ec["evaluation_criteriaId"]
            nec = get_evaluation_criteria_from_db(projectId, boid, ecid)
            #print "\t" + projectId + ", " + ec["evaluation_criteriaId"] + ", " + ec["evaluation_criterion"] + "\n\t" + str(nec)
            if nec is None:
                nec = EvaluationCriteria(parent=project_db_key(project_name))
                nec.evaluation_criterionId = ecid
                nec.objectiveId = boid
                nec.projectId = projectId
            nec.evaluation_criterion = ec["evaluation_criterion"]
            nec.put()
            nbo.evaluation_criteriaIds.append(ecid)
        nbo.put()
        project.objectiveIds.append(nbo.objectiveId)
    project.put()
    return project

def get_objective_from_db(projectId, objectiveId):
    objectives_query = Objective.query(Objective.objectiveId == objectiveId,<commit_msg>Temp hold<commit_after>
from models import Entry
from models import User
from models import Project
from models import Vendor
from models import Objective
from models import EvaluationCriteria
from models import DEFAULT_PROJECT_NAME
from models import project_db_key
from functools import wraps
import json
import time
import datetime
from flask import request, Response, url_for, redirect

def get_project_db_name(rname=DEFAULT_PROJECT_NAME):
    return rname

#Gets evaluation_criteria from db - this needs to implement evaluation_criteria-lifecycle - right now it is a singleton
def get_projects_from_db(userId):
    if userId:
        project_query = Project.query(Project.userIds.IN([userId]))
    else:
        project_query = Project.query()
    return project_query.fetch(100)

#Gets evaluation_criteria from db - this needs to implement evaluation_criteria-lifecycle - right now it is a singleton
def get_project_from_db(projectId):
    project_query = Project.query(Project.projectId == projectId)
    if project_query.count() < 1:
        return None
    else:
        return project_query.fetch(1)[-1]

def get_entry_from_db(projectId, userId):
    entrys_query = Entry.query(Entry.user.identity == userId, Entry.project.projectId == projectId)
    if entrys_query.count() < 1:
        return None
    else:
        return entrys_query.fetch(1)[-1]

def get_entrys_from_given_project_db(projectId):
    entrys_query = Entry.query(Entry.project.projectId == projectId)
    return entrys_query.fetch(100)

def get_entrys_from_given_user_db(userId):
    user = get_user_from_db(userId)
    entrys_query = Entry.query(Entry.user == user)
    return entrys_query.fetch(100)

def get_users_from_db(projectId=None):
    if projectId and projectId != "":
        project = get_project_from_db(projectId)
        if project is not None:
            userIds = project.userIds
            users = []
            for userId in userIds:
                user = get_user_from_db(userId)
                users.append(user)
            return users
    else:
        users_q = User.query(User.type != "Superuser")
        users = users_q.fetch(1000)
        return users

    return None

def get_user_from_db(userId):
    if "@" in userId:
        users_q = User.query(User.email == userId)
    else:
        users_q = User.query(User.identity == userId)
    if users_q.count() < 1:
        return None
    else:
        return users_q.fetch(1)[-1]

def update_users_project(projectId, userIds):
    project = get_project_from_db(projectId)
    project.userIds = userIds
    project.put()
    return project

def update_user(userId, email, type, password, projectIds):
    user = get_user_from_db(userId)
    if user is None:
        project_name = get_project_db_name()
        user = User(parent=project_db_key(project_name))
        user.identity = userId
        user.projectIds = []
    user.email = email
    user.type = type
    user.password = password
    if projectIds:
        for projId in projectIds:
            if projId and projId != "__CREATE__"and projId not in user.projectIds:
                    user.projectIds.append(projId)
                    project = get_project_from_db(projId)
                    if project:
                        project.userIds.append(userId)
                        project.put()
    user.put()
    time.sleep(1)
    return user

def getArrayOfDict(bos):
    # this is not good
    bot = bos[0]
    # this is even worse
    bot = '[' + bot + ']'
    bol = json.loads(bot)
    return bol

def update_project(projectId, department, group, description, defaultPassword, userIds, vendorIds, due_date, bos):
    project_name = get_project_db_name()
    project = get_project_from_db(projectId)
    if project is None:
        project = Project(parent=project_db_key(project_name))
        project.projectId = projectId
        project.objectiveIds = []
    project.department = department
    project.description = description
    project.group = group
    project.defaultPassword = <PASSWORD>
    project.userIds = userIds
    project.vendorIds = vendorIds
    for ui in userIds:
        user = get_user_from_db(ui)
        if user and projectId not in user.projectIds:
            user.projectIds.append(projectId)
            user.put()
    for vi in vendorIds:
        vendor = get_user_from_db(vi)
        if vendor and projectId not in vendor.projectIds:
            vendor.projectIds.append(projectId)
            vendor.put()
    if due_date is None or due_date == "":
        project.due_date = datetime.datetime.now()
    else:
        project.due_date = datetime.datetime.strptime(due_date.split(" ")[0], "%Y-%m-%d")
    bol = getArrayOfDict(bos)
    if len(project.objectiveIds) > 0:
        nnbos = []
        for bo in bol:
            nnbos.append(bo["objectiveId"])
        for pbo in project.objectiveIds:
            if pbo not in nnbos:
                print "deleting " + pbo
                print project.objectiveIds
    #            delete_objective_from_db(projectId, pbo)

    for bo in bol:
        #print bo["objectiveId"] + ", " + bo["description"] + ", " + bo["weight"]
        boid = bo["objectiveId"]
        nbo = get_objective_from_db(projectId, boid)
        if nbo is None:
            nbo = Objective(parent=project_db_key(project_name))
            nbo.objectiveId = boid
            nbo.projectId = projectId
        nbo.description = bo["description"]
        nbo.weight = int(bo["weight"])
        nbo.evaluation_criteriaIds = []
        for ec in bo["evaluation_criteria"]:
            ecid = ec["evaluation_criteriaId"]
            nec = get_evaluation_criteria_from_db(projectId, boid, ecid)
            #print "\t" + projectId + ", " + ec["evaluation_criteriaId"] + ", " + ec["evaluation_criterion"] + "\n\t" + str(nec)
            if nec is None:
                nec = EvaluationCriteria(parent=project_db_key(project_name))
                nec.evaluation_criterionId = ecid
                nec.objectiveId = boid
                nec.projectId = projectId
            nec.evaluation_criterion = ec["evaluation_criterion"]
            nec.put()
            nbo.evaluation_criteriaIds.append(ecid)
        nbo.put()
        project.objectiveIds.append(nbo.objectiveId)
    project.put()
    return project

def get_objective_from_db(projectId, objectiveId):
    objectives_query = Objective.query(Objective.objectiveId == objectiveId,
{'fraction_non_alphanumeric': 0.0900681978521596, 'fraction_numerical': 0.004076193462412793, 'mean_word_length': 2.6399429386590585, 'pattern_counts': {'":': 4, '<': 11, '<?xml version=': 0, '>': 7, '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': '6008047', 'n_tokens_mistral': 3903, 'n_tokens_neox': 3836, 'n_words': 897}