示例#1
0
 def exists(cls, field, value):
     session = Database().Session
     return session.query(exists().where(getattr(cls, field) == value))\
         .scalar()
示例#2
0
 def get_user_list(cls, filters=None):
     session = Database().Session
     return session.query(cls).all()
示例#3
0
 def username_exists(cls, username):
     session = Database().Session
     return session.query(exists().where(cls.username == username)).scalar()
示例#4
0
 def get_by_username(cls, username):
     session = Database().Session
     return session.query(cls).filter_by(username=username).one()
示例#5
0
 def get_by_email(cls, email):
     session = Database().Session
     return session.query(cls).filter_by(email=email).one()
示例#6
0
 def tearDownClass(cls):
     Database().close()
示例#7
0
 def tearDownClass(cls):
     Database().drop_all()
示例#8
0
def load(fixtures, drop_all=False):
    # TODO we are not sure the DB is connected here....

    if drop_all:
        Database().drop_all()

    log.info("Create Organizations and Users")
    for org in fixtures.get("organizations", {}):

        # create organization
        organization = db.Organization(**{k:org[k] for k in ["name", "domain",\
            "address1", "address2", "zipcode", "country", "public_key"]})
        organization.save()
        log.debug(f"processed organization={organization.name}")

        # create users
        for usr in org.get("users", {}):
            user = db.User(**usr)
            user.set_password(usr.get("password"))
            user.organization_id = organization.id
            user.save()
            log.debug(f"processed user={user.username}")

    log.info("Create collaborations")
    for col in fixtures.get("collaborations", {}):

        # create collaboration
        collaboration = db.Collaboration(name=col.get("name"))
        log.debug(f"processed collaboration={collaboration.name}")

        # append organizations to the collaboration

        for participant in col.get("participants", {}):

            if isinstance(participant, str):
                org_name = participant
                node_api_key = str(uuid.uuid1())
            elif isinstance(participant, dict):
                org_name = participant.get("name")
                node_api_key = participant.get("api-key", str(uuid.uuid1()))

            organization = db.Organization.get_by_name(org_name)
            collaboration.organizations.append(organization)
            log.debug(f"added {org_name} to the collaboration")

            node = db.Node(
                organization=organization,
                collaboration=collaboration,
                name=f"{organization.name} - {collaboration.name} Node",
                api_key=node_api_key)
            node.save()
            log.debug(f"added node {node.name} to {collaboration.name}")

        collaboration.save()

        # append dummy tasks to the collaboration
        log.debug("Processing Task Assignments")
        for image in col.get("tasks", {}):
            initiator = collaboration.organizations[0]
            task = db.Task(name=f"Example task",
                           image=image,
                           collaboration=collaboration,
                           run_id=db.Task.next_run_id(),
                           initiator=initiator)

            for organization in collaboration.organizations:
                result = db.Result(task=task,
                                   input="something",
                                   organization=organization)
                result.save()

            task.save()
            log.debug(f"Processed task {task.name}")
示例#9
0
 def exists(cls, organization_id, collaboration_id):
     session = Database().Session
     return session.query(cls).filter_by(
         organization_id=organization_id,
         collaboration_id=collaboration_id
     ).scalar()
示例#10
0
 def get_by_name(cls, name):
     session = Database().Session
     try:
         return session.query(cls).filter_by(name=name).first()
     except NoResultFound:
         return None