Ejemplo n.º 1
0
def project_create(hash_session):
    logTraffic(CLOUD_URL, endpoint='/private/<hash_session>/project/create')
    if fk.request.method == 'POST':
        access_resp = access_manager.check_cloud(hash_session)
        current_user = access_resp[1]
        if current_user is not None:
            logAccess(CLOUD_URL, 'cloud',
                      '/private/<hash_session>/project/create')
            if fk.request.data:
                data = json.loads(fk.request.data)
                try:
                    name = data.get("name", "")
                    description = data.get("description", "")
                    goals = data.get("goals", "")
                    access = data.get("access", 'public')
                    group = data.get("group", "undefined")
                    tags = data.get("tags", "")
                    environment = data.get("environment", {})
                    query_project = ProjectModel.objects(owner=current_user,
                                                         name=name).first()
                    if query_project is None:
                        project = ProjectModel(created_at=str(
                            datetime.datetime.utcnow()),
                                               owner=current_user,
                                               name=name)
                        project.description = description
                        project.access = access
                        project.goals = goals
                        project.group = group
                        project.tags = [tags]
                        project.save()
                        return cloud_response(201,
                                              'Project successfully created.',
                                              "The project was created.")
                    else:
                        return fk.Response(
                            'A project with this name already exists.',
                            status.HTTP_403_FORBIDDEN)
                except:
                    print(str(traceback.print_exc()))
                    return fk.Response(str(traceback.print_exc()),
                                       status.HTTP_500_INTERNAL_SERVER_ERROR)
            else:
                return fk.Response('No content provided for the creation.',
                                   status.HTTP_204_NO_CONTENT)
        else:
            return fk.Response('Unauthorized action on this project.',
                               status.HTTP_401_UNAUTHORIZED)
    else:
        return fk.Response('Endpoint does not support this HTTP method.',
                           status.HTTP_405_METHOD_NOT_ALLOWED)
Ejemplo n.º 2
0
def download_env(hash_session, env_id):
    logTraffic(CLOUD_URL, endpoint='/private/<hash_session>/env/download/<env_id>')
    if fk.request.method == 'GET':
        access_resp = access_manager.check_cloud(hash_session, ACC_SEC, CNT_SEC)
        current_user = access_resp[1]
        if current_user is None:
            return fk.redirect('{0}:{1}/error/?code=401'.format(VIEW_HOST, VIEW_PORT))
        else:
            logAccess(CLOUD_URL, 'cloud', '/private/<hash_session>/env/download/<env_id>')
            try:
                env = EnvironmentModel.objects.with_id(env_id)
                project = None
                for pro in ProjectModel.objects(owner=current_user):
                    if str(env.id) in pro.history:
                        project = pro
                        break
            except:
                env = None
                project = None
                print(str(traceback.print_exc()))
                return fk.Response(str(traceback.print_exc()), status.HTTP_500_INTERNAL_SERVER_ERROR)
            if env is None or project is None:
                return fk.redirect('{0}:{1}/error/?code=204'.format(VIEW_HOST, VIEW_PORT))
            else:
                prepared = storage_manager.prepare_env(project, env)
                if prepared[0] == None:
                    print("Unable to retrieve a env to download.")
                    return fk.redirect('{0}:{1}/error/?code=204'.format(VIEW_HOST, VIEW_PORT))
                else:
                    return fk.send_file(prepared[0], as_attachment=True, attachment_filename=prepared[1], mimetype='application/zip')
    else:
        return fk.redirect('{0}:{1}/error/?code=405'.format(VIEW_HOST, VIEW_PORT))
Ejemplo n.º 3
0
def project_dashboard(hash_session):
    (traffic, created) = TrafficModel.objects.get_or_create(created_at=str(datetime.datetime.utcnow()), service="cloud", endpoint="/private/dashboard/projects")
    if not created:
        traffic.interactions += 1 
        traffic.save()
        
    if fk.request.method == 'GET':
        current_user = UserModel.objects(session=hash_session).first()
        print fk.request.path
        if current_user is None:
            return fk.redirect('http://0.0.0.0:5000/error-401/?action=dashboard_denied')
        else:
            allowance = current_user.allowed("%s%s"%(fk.request.headers.get('User-Agent'),fk.request.remote_addr))
            print "Allowance: "+allowance
            if allowance == hash_session:
                projects = ProjectModel.objects(owner=current_user).order_by('+created_at')
                summaries = []
                for p in projects:
                    project = {"project":json.loads(p.summary_json())}
                    records = RecordModel.objects(project=p)
                    project["activity"] = {"number":len(records), "records":[{"id":str(record.id), "created":str(record.created_at), "updated":str(record.updated_at), "status":str(record.status)} for record in records]}
                    summaries.append(project)
                return fk.Response(json.dumps({'number':len(summaries), 'projects':summaries}, sort_keys=True, indent=4, separators=(',', ': ')), mimetype='application/json')
            else:
                return fk.redirect('http://0.0.0.0:5000/error-401/?action=dashboard_failed')
    else:
        return fk.redirect('http://0.0.0.0:5000/error-405/')
Ejemplo n.º 4
0
def project_records(hash_session, project_name):
    logTraffic(
        CLOUD_URL,
        endpoint='/private/<hash_session>/project/record/<project_name>')
    if fk.request.method == 'GET':
        access_resp = access_manager.check_cloud(hash_session)
        current_user = access_resp[1]
        if current_user is None:
            return fk.redirect('{0}:{1}/error/?code=401'.format(
                VIEW_HOST, VIEW_PORT))
        else:
            logAccess(CLOUD_URL, 'cloud',
                      '/private/<hash_session>/project/record/<project_name>')
            project = ProjectModel.objects(name=project_name).first()
            if project == None or (project != None
                                   and project.owner != current_user
                                   and project.access != 'public'):
                return fk.redirect('{0}:{1}/error/?code=401'.format(
                    VIEW_HOST, VIEW_PORT))
            else:
                return fk.Response(project.activity_json(),
                                   mimetype='application/json')
    else:
        return fk.redirect('{0}:{1}/error/?code=405'.format(
            VIEW_HOST, VIEW_PORT))
Ejemplo n.º 5
0
def env_view(env_id):
    logTraffic(CLOUD_URL, endpoint='/private/env/view/<env_id>')
    hash_session = basicAuthSession(fk.request)
    if fk.request.method == 'GET':
        caccess_resp = access_manager.check_cloud(hash_session, ACC_SEC, CNT_SEC)
        current_user = access_resp[1]
        if current_user is not None:
            try:
                logAccess(CLOUD_URL, 'cloud', '/private/env/view/<env_id>')
                env = EnvironmentModel.objects.with_id(env_id)
                # Make sure user own or used this environment.
                owned = False
                for project in ProjectModel.objects(owner=current_user):
                    if str(env.id) in project.history:
                        owned = True
                        break
                if not owned:
                    env = None
            except:
                env = None
                print(str(traceback.print_exc()))
            if env is None:
                return fk.Response('Unable to find this environment.', status.HTTP_404_NOT_FOUND)
            else:
                return fk.Response(env.to_json(), mimetype='application/json')
        else:
            return fk.Response('Unauthorized action on this environment.', status.HTTP_401_UNAUTHORIZED)
    else:
        return fk.Response('Endpoint does not support this HTTP method.', status.HTTP_405_METHOD_NOT_ALLOWED)
Ejemplo n.º 6
0
def query_basic(words, page, filtr, current_user):
    filtrs = filter2filters(filtr)
    raw = []
    if "user" not in filtrs:
        raw.extend([u for u in UserModel.objects().order_by('+created_at') if all(w in str(u.extended()).lower() for w in words)])
        # _users = UserModel.objects(Q(email__in=words)|Q(email__in=words)|)
        # _users_P = ProfileModel.objects()
        pagination_logs.append("{0} -- query_basic: {1}".format(datetime.datetime.utcnow(), raw))
    if "tool" not in filtrs:
        raw.extend([u for u in ApplicationModel.objects().order_by('+created_at') if all(w in str(u.extended()).lower() for w in words)])
        pagination_logs.append("{0} -- query_basic: {1}".format(datetime.datetime.utcnow(), raw))
    if "project" not in filtrs:
        raw.extend([u for u in ProjectModel.objects().order_by('+created_at') if all(w in str(u.extended()).lower() for w in words) and (u.access == 'public' or current_user and (current_user == u.owner or current_user.group == "admin"))])

        pagination_logs.append("{0} -- query_basic: {1}".format(datetime.datetime.utcnow(), raw))
    if "record" not in filtrs:
        raw.extend([u for u in RecordModel.objects().order_by('+created_at') if all(w in str(u.extended()).lower() for w in words) and (u.access == 'public' or (current_user and u.project) and (current_user == u.project.owner or current_user.group == "admin"))])
        pagination_logs.append("{0} -- query_basic: {1}".format(datetime.datetime.utcnow(), raw))
    if "diff" not in filtrs:
        raw.extend([u for u in DiffModel.objects().order_by('+created_at') if all(w in str(u.extended()).lower() for w in words) and ((u.record_from.access == 'public' and u.record_to.access == 'public') or (current_user and (current_user.group == "admin" or current_user == u.record_from.project.owner or current_user == u.record_to.project.owner)))])
        pagination_logs.append("{0} -- query_basic: {1}".format(datetime.datetime.utcnow(), raw))
    if "env" not in filtrs:
        raw.extend([u for u in EnvironmentModel.objects().order_by('+created_at') if all(w in str(u.extended()).lower() for w in words) and (len(ProjectModel.objects(history=str(u.id))) > 0 and (ProjectModel.objects(history=str(u.id))[0].access == 'public' or current_user and (current_user == ProjectModel.objects(history=str(u.id))[0].owner or current_user.group == "admin")))])
        pagination_logs.append("{0} -- query_basic: {1}".format(datetime.datetime.utcnow(), raw))
    return raw2dict(raw, page)
Ejemplo n.º 7
0
 def tearDown(self):
     # Delete all documents
     # Stop mongodb instance.
     for traffic in TrafficModel.objects():
       traffic.delete()
     for stat in StatModel.objects():
       stat.delete()
     for bundle in BundleModel.objects():
       bundle.delete()
     for version in VersionModel.objects():
       version.delete()
     for user in UserModel.objects():
       user.delete()
     for file in FileModel.objects():
       file.delete()
     for profile in ProfileModel.objects():
       profile.delete()
     for message in MessageModel.objects():
       message.delete()
     for project in ProjectModel.objects():
       project.delete()
     for comment in CommentModel.objects():
       comment.delete()
     for application in ApplicationModel.objects():
       application.delete()
     for access in AccessModel.objects():
       access.delete()
     for environment in EnvironmentModel.objects():
       environment.delete()
     for record in RecordModel.objects():
       record.delete()
     for record in RecordBodyModel.objects():
       record.delete()
     for diff in DiffModel.objects():
       diff.delete()
Ejemplo n.º 8
0
def env_remove(env_id):
    logTraffic(CLOUD_URL, endpoint='/private/env/remove/<env_id>')
    hash_session = basicAuthSession(fk.request)     
    if fk.request.method in ['GET', 'DELETE']:
        access_resp = access_manager.check_cloud(hash_session, ACC_SEC, CNT_SEC)
        current_user = access_resp[1]
        if current_user is None:
            return fk.Response('Unauthorized action on this environment.', status.HTTP_401_UNAUTHORIZED)
        else:
            try:
                logAccess(CLOUD_URL, 'cloud', '/private/env/remove/<env_id>')
                env = EnvironmentModel.objects.with_id(env_id)
            except:
                print(str(traceback.print_exc()))
            if env is None:
                return fk.Response('Unable to find this environment.', status.HTTP_404_NOT_FOUND)
            else:
                # result = storage_manager.delete_env_files(env)
                # if result:
                # implement project history en removal: project.history.append(str(env.id))
                
                count = 0
                for project in ProjectModel.objects(owner=current_user):
                    try:
                        project.history.remove(str(env_id))
                        project.save()
                        count = count + 1
                    except:
                        pass
                if count > 0:
                    env.delete()
                return cloud_response(200, 'Deletion succeeded', 'The environment %s was succesfully deleted.'%env_id)
    else:
       return fk.Response('Endpoint does not support this HTTP method.', status.HTTP_405_METHOD_NOT_ALLOWED)
Ejemplo n.º 9
0
def project_records(hash_session, project_name):
    (traffic, created) = TrafficModel.objects.get_or_create(created_at=str(datetime.datetime.utcnow()), service="cloud", endpoint="/private/project/record/<project_id>")
    if not created:
        traffic.interactions += 1 
        traffic.save()
        
    if fk.request.method == 'GET':
        current_user = UserModel.objects(session=hash_session).first()
        print fk.request.path
        if current_user is None:
            return fk.redirect('http://0.0.0.0:5000/?action=records_denied')
        else:
            allowance = current_user.allowed("%s%s"%(fk.request.headers.get('User-Agent'),fk.request.remote_addr))
            print "Allowance: "+allowance
            if allowance == hash_session:
                project = ProjectModel.objects(name=project_name).first()
                # project = ProjectModel.objects(name=project_name, owner=current_user).first_or_404()
                if project ==  None or (project != None and project.owner != current_user and project.access != 'public'):
                    return fk.redirect('http://0.0.0.0:5000/?action=records_failed')
                else:
                    return fk.Response(project.activity_json(), mimetype='application/json')
            else:
                return fk.redirect('http://0.0.0.0:5000/?action=records_failed')
    else:
        return fk.redirect('http://0.0.0.0:5000/error-405/')
Ejemplo n.º 10
0
def public_projects(user_id):
    logTraffic(endpoint='/public/projects')
    if fk.request.method == 'GET':
        projects = ProjectModel.objects()
        projects_dict = {'total_projects':len(projects), 'projects':[]}
        for project in projects:
            if project.access == 'public':
                projects_dict['projects'].append(project.extended())
        return api_response(200, 'Projects list', projects_dict)
    else:
        return api_response(405, 'Method not allowed', 'This endpoint supports only a GET method.')
Ejemplo n.º 11
0
def project_create():
    logTraffic(CLOUD_URL, endpoint='/private/project/create')
    hash_session = basicAuthSession(fk.request)
    if fk.request.method == 'POST':
        access_resp = access_manager.check_cloud(hash_session, ACC_SEC, CNT_SEC)
        current_user = access_resp[1]
        if current_user is not None:
            logAccess(fk, access_resp[1], CLOUD_URL, 'cloud', '/private/project/create')
            if current_user.quota >= current_user.max_quota*1024*1024*1024:
                return fk.Response('You have exceeded your allowed maximum quota.', status.HTTP_401_UNAUTHORIZED)
            if fk.request.data:
                data = json.loads(fk.request.data)
                try:
                    name = data.get("name", "")
                    description = data.get("description", "")
                    goals = data.get("goals", "")
                    access = data.get("access", 'public')
                    group = data.get("group", "undefined")
                    tags = data.get("tags", "")
                    environment = data.get("environment", {})
                    query_project = ProjectModel.objects(owner=current_user, name=name).first()
                    if query_project is None:
                        project = ProjectModel(created_at=str(datetime.datetime.utcnow()), owner=current_user, name=name)
                        project.description = description
                        project.access = access
                        project.goals = goals
                        project.group = group
                        project.tags = [tags]
                        project.save()
                        return cloud_response(201, 'Project successfully created.', project.info())
                    else:
                        return fk.Response('A project with this name already exists.', status.HTTP_403_FORBIDDEN)
                except:
                    print(str(traceback.print_exc()))
                    return fk.Response('Failure to process. Contact admin if it persists.', status.HTTP_500_INTERNAL_SERVER_ERROR)
            else:
                return fk.Response('No content provided for the creation.', status.HTTP_204_NO_CONTENT)
        else:
            return fk.Response('Unauthorized action on this project.', status.HTTP_401_UNAUTHORIZED)
    else:
        return fk.Response('Endpoint does not support this HTTP method.', status.HTTP_405_METHOD_NOT_ALLOWED)
Ejemplo n.º 12
0
def public_user_projects(user_id):
    logTraffic(endpoint='/public/user/projects')
    if fk.request.method == 'GET':
    	user = UserModel.objects.with_id(user_id)
    	if user == None:
    		return api_response(404, 'No user found', 'We could not fetch the user with id:%s.'%user_id)
        projects = ProjectModel.objects(owner=user)
        projects_dict = {'total_projects':len(projects), 'projects':[]}
        for project in projects:
            projects_dict['projects'].append(project.extended())
        return api_response(200, 'Projects list', projects_dict)
    else:
        return api_response(405, 'Method not allowed', 'This endpoint supports only a GET method.')
Ejemplo n.º 13
0
def project_records(project_name):
    logTraffic(CLOUD_URL, endpoint='/private/project/record/<project_name>')   
    hash_session = basicAuthSession(fk.request)
    if fk.request.method == 'GET':
        access_resp = access_manager.check_cloud(hash_session, ACC_SEC, CNT_SEC)
        current_user = access_resp[1]
        if current_user is None:
            return fk.redirect('{0}:{1}/error/?code=401'.format(VIEW_HOST, VIEW_PORT))
        else:
            logAccess(CLOUD_URL, 'cloud', '/private/project/record/<project_name>')
            project = ProjectModel.objects(name=project_name).first()
            if project ==  None or (project != None and project.owner != current_user and project.access != 'public' and current_user.group != "admin"):
                return fk.redirect('{0}:{1}/error/?code=401'.format(VIEW_HOST, VIEW_PORT))
            else:
                return fk.Response(project.activity_json(), mimetype='application/json')
    else:
        return fk.redirect('{0}:{1}/error/?code=405'.format(VIEW_HOST, VIEW_PORT))
Ejemplo n.º 14
0
def env_edit(env_id):
    logTraffic(CLOUD_URL, endpoint='/private/env/edit/<env_id>')
    hash_session = basicAuthSession(fk.request)
    if fk.request.method == 'POST':
        access_resp = access_manager.check_cloud(hash_session, ACC_SEC, CNT_SEC)
        current_user = access_resp[1]
        if current_user is None:
            return fk.Response('Unauthorized action on this environment.', status.HTTP_401_UNAUTHORIZED)
        else:
            logAccess(fk, access_resp[1], CLOUD_URL, 'cloud', '/private/env/edit/<env_id>')
            try:
                env = EnvironmentModel.objects.with_id(env_id)
                owned = False
                for project in ProjectModel.objects(owner=current_user):
                    if str(env.id) in project.history:
                        owned = True
                        break
                if not owned:
                    env = None
            except:
                print(str(traceback.print_exc()))
            if env is None:
                return fk.Response('Unable to find this environment.', status.HTTP_404_NOT_FOUND)
            else:
                if fk.request.data:
                    data = json.loads(fk.request.data)
                    try:
                        group = data.get("group", env.group)
                        system = data.get("system", env.system)
                        env.group = group
                        env.system = system
                        env.save()
                        return fk.Response('Environment edited', status.HTTP_200_OK)
                    except:
                        print(str(traceback.print_exc()))
                        return fk.Response('Failure to process. Contact admin if it persists.', status.HTTP_500_INTERNAL_SERVER_ERROR)
                else:
                    return fk.Response('No content provided for the update.', status.HTTP_204_NO_CONTENT)
    else:
        return fk.Response('Endpoint does not support this HTTP method.', status.HTTP_405_METHOD_NOT_ALLOWED)
Ejemplo n.º 15
0
def env_edit(env_id):
    logTraffic(CLOUD_URL, endpoint='/private/env/edit/<env_id>')
    hash_session = basicAuthSession(fk.request)
    if fk.request.method == 'POST':
        access_resp = access_manager.check_cloud(hash_session, ACC_SEC, CNT_SEC)
        current_user = access_resp[1]
        if current_user is None:
            return fk.Response('Unauthorized action on this environment.', status.HTTP_401_UNAUTHORIZED)
        else:
            try:
                env = EnvironmentModel.objects.with_id(env_id)
                owned = False
                for project in ProjectModel.objects(owner=current_user):
                    if str(env.id) in project.history:
                        owned = True
                        break
                if not owned:
                    env = None
            except:
                print(str(traceback.print_exc()))
            if env is None:
                return fk.Response('Unable to find this environment.', status.HTTP_404_NOT_FOUND)
            else:
                if fk.request.data:
                    data = json.loads(fk.request.data)
                    try:
                        group = data.get("group", env.group)
                        system = data.get("system", env.system)
                        env.group = group
                        env.system = system
                        env.save()
                        return fk.Response('Environment edited', status.HTTP_200_OK)
                    except:
                        print(str(traceback.print_exc()))
                        return fk.Response(str(traceback.print_exc()), status.HTTP_500_INTERNAL_SERVER_ERROR)
                else:
                    return fk.Response('No content provided for the update.', status.HTTP_204_NO_CONTENT)
    else:
        return fk.Response('Endpoint does not support this HTTP method.', status.HTTP_405_METHOD_NOT_ALLOWED)
Ejemplo n.º 16
0
def user_home():
    (traffic, created) = TrafficModel.objects.get_or_create(created_at=str(datetime.datetime.utcnow()), service="cloud", endpoint="/public/user/home")
    if not created:
        traffic.interactions += 1 
        traffic.save()
        
    if fk.request.method == 'GET':
        users = UserModel.objects()
        projects = ProjectModel.objects()
        records = RecordModel.objects()
        environments = EnvironmentModel.objects()
        print fk.request.path

        users_stat = {"number":len(users)}
        users_stat["history"] = [json.loads(stat.to_json()) for stat in StatModel.objects(category="user")]

        projects_stat = {"number":len(projects)}
        projects_stat["history"] = [json.loads(stat.to_json()) for stat in StatModel.objects(category="project")]

        storage_stat = {}
        storage_stat["history"] = [json.loads(stat.to_json()) for stat in StatModel.objects(category="storage")]
        amount = 0
        for user in users:
            try:
                amount += user.quota
            except:
                amount += 0

        storage_stat["size"] = size(amount)

        records_stat = {"number":len(records)}
        records_stat["history"] = [json.loads(stat.to_json()) for stat in StatModel.objects(category="record")]

        return fk.Response(json.dumps({'users':users_stat, 'projects':projects_stat, 'records':records_stat, 'storage':storage_stat}, sort_keys=True, indent=4, separators=(',', ': ')), mimetype='application/json')
    else:
        return fk.make_response('Method not allowed.', status.HTTP_405_METHOD_NOT_ALLOWED)
Ejemplo n.º 17
0
def fetchDependencies(name, obj):
    deps = {}
    if name == "user":
        profiles = [el for el in ProfileModel.objects(user=obj)]
        deps["profile"] = profiles
        files = [el for el in FileModel.objects(owner=obj)]
        deps["file"] = files
        projects = [el for el in ProjectModel.objects(owner=obj)]
        deps["project"] = projects
        tools = [el for el in ApplicationModel.objects(developer=obj)]
        deps["tool"] = tools
    elif name == "version":
        envs = [el for el in EnvironmentModel.objects(version=obj)]
        deps["env"] = envs
    elif name == "record":
        diffs_from = [el for el in DiffModel.objects(record_from=obj)]
        deps["diff"] = diffs_from
        diffs_to = [el for el in DiffModel.objects(record_to=obj)]
        for rec in diffs_to:
            if rec not in deps["diff"]:
                deps["diff"].append(rec)
    elif name == "project":
        records = [el for el in RecordModel.objects(project=obj)]
        deps["record"] = records
    elif name == "file":
        projects = [
            pr for pr in ProjectModel.objects() if str(obj.id) in pr.resources
        ]
        logo_projects = [el for el in ProjectModel.objects(logo=obj)]
        for pr in logo_projects:
            if pr not in projects:
                projects.append(pr)
        deps["project"] = projects
        records = [
            rec for rec in RecordModel.objects()
            if str(obj.id) in rec.resources
        ]
        deps["record"] = records
        tools = [el for el in ApplicationModel.objects(logo=obj)]
        deps["tool"] = tools
        envs = [
            env for env in EnvironmentModel.objects()
            if str(obj.id) in env.resources
        ]
        deps["env"] = envs
        diffs = [
            dff for dff in DiffModel.objects() if str(obj.id) in dff.resources
        ]
        deps["diff"] = diffs
        profiles = [el for el in ProfileModel.objects(picture=obj)]
        deps["profile"] = profiles
    elif name == "env":
        records = [el for el in RecordModel.objects(environment=obj)]
        deps["record"] = records
        projects = [
            pr for pr in ProjectModel.objects() if str(obj.id) in pr.history
        ]
        deps["project"] = projects
    elif name == "bundle":
        envs = [el for el in EnvironmentModel.objects(bundle=obj)]
        deps["env"] = envs

    return deps
Ejemplo n.º 18
0
def fetchDependencies(name, obj, offset, leftover, filtrs):
    deps = {}
    size = 0
    if name == "user":
        # profiles, size, offset, leftover = paginate(ProfileModel.objects(user=obj), offset, leftover, size)
        # deps["profile"] = profiles
        if "file" not in filtrs:
            files, size, offset, leftover = paginate(FileModel.objects(owner=obj), offset, leftover, size)
            deps["file"] = files
        if "project" not in filtrs:
            projects, size, offset, leftover = paginate(ProjectModel.objects(owner=obj), offset, leftover, size)
            deps["project"] = projects
        if "tool" not in filtrs:
            tools, size, offset, leftover = paginate(ApplicationModel.objects(developer=obj), offset, leftover, size)
            deps["tool"] = tools
    # elif name == "version":
    #     if "env" not in filtrs:
    #         envs, size, offset, leftover = paginate(EnvironmentModel.objects(version=obj), offset, leftover, size)
    #         deps["env"] = envs
    elif name == "record":
        if "diff" not in filtrs:
            diffs_from, size, offset, leftover = paginate(DiffModel.objects(record_from=obj), offset, leftover, size)
            deps["diff"] = diffs_from
            diffs_to = [el for el in DiffModel.objects(record_to=obj)]
            diffs_more = []
            for rec in diffs_to:
                if rec not in deps["diff"]:
                    diffs_more.append(rec)
            diffs_more, size, offset, leftover = paginate(diffs_more, offset, leftover, size)
            deps["diff"].extend(diffs_more)
    elif name == "project":
        if "record" not in filtrs:
            records, size, offset, leftover = paginate(RecordModel.objects(project=obj), offset, leftover, size)
            deps["record"] = records
    # elif name == "file":
    #     projects = [pr for pr in ProjectModel.objects() if str(obj.id) in pr.resources]
    #     logo_projects = [el for el in ProjectModel.objects(logo=obj)]
    #     for pr in logo_projects:
    #         if pr not in projects:
    #             projects.append(pr)
    #     projects, size, offset, leftover = paginate(projects, offset, leftover, size)
    #     deps["project"] = projects

    #     records = [rec for rec in RecordModel.objects() if str(obj.id) in rec.resources]
    #     records, size, offset, leftover = paginate(records, offset, leftover, size)
    #     deps["record"] = records

    #     tools, size, offset, leftover = paginate(ApplicationModel.objects(logo=obj), offset, leftover, size)
    #     deps["tool"] = tools

    #     envs = [env for env in EnvironmentModel.objects() if str(obj.id) in env.resources]
    #     envs, size, offset, leftover = paginate(envs, offset, leftover, size)
    #     deps["env"] = envs

    #     diffs = [dff for dff in DiffModel.objects() if str(obj.id) in dff.resources]
    #     diffs, size, offset, leftover = paginate(diffs, offset, leftover, size)
    #     deps["diff"] = diffs

    #     profiles, size, offset, leftover = paginate(ProfileModel.objects(picture=obj), offset, leftover, size)
    #     deps["profile"] = profiles
    elif name == "env":
        if "record" not in filtrs:
            records, size, offset, leftover = paginate(RecordModel.objects(environment=obj), offset, leftover, size)
            deps["record"] = records

        if "project" not in filtrs:
            projects = [pr for pr in ProjectModel.objects if str(obj.id) in pr.history]
            projects, size, offset, leftover = paginate(projects, offset, leftover, size)
            deps["project"] = projects
    # elif name == "bundle":
    #     envs, size, offset, leftover = paginate(EnvironmentModel.objects(bundle=obj), offset, leftover, size)
    #     deps["env"] = envs
    pagination_logs.append("{0} -- fetchDependencies: {1}, {2}, {3}, {4}".format(datetime.datetime.utcnow(), size, offset, leftover))
    return deps, size, offset, leftover
Ejemplo n.º 19
0
def user_dashboard(hash_session):
    (traffic, created) = TrafficModel.objects.get_or_create(created_at=str(datetime.datetime.utcnow()), service="cloud", endpoint="/private/user/dashboard/<hash_session>")
    if not created:
        traffic.interactions += 1 
        traffic.save()
        
    if fk.request.method == 'GET':
        user_model = UserModel.objects(session=hash_session).first()
        print fk.request.path
        if user_model is None:
            return fk.redirect('http://0.0.0.0:5000/?action=logout_denied')
        else:
            profile_model = ProfileModel.objects(user=user_model).first()
            # print "Connected_at: %s"%str(user_model.connected_at)
            allowance = user_model.allowed("%s%s"%(fk.request.headers.get('User-Agent'),fk.request.remote_addr))
            print "Allowance: "+allowance
            # print "Connected_at: %s"%str(user_model.connected_at)
            if allowance == hash_session:
                dashboard = {}
                projects = ProjectModel.objects(owner=user_model)
                if profile_model is not None:
                    dashboard["profile"] = {'fname':profile_model.fname, 'lname':profile_model.lname, 'organisation':profile_model.organisation, 'about':profile_model.about, 'picture':profile_model.picture}
                dashboard["records_total"] = 0
                dashboard["projects_total"] = len(projects)
                dashboard["records_total"] = 0
                dashboard["environments_total"] = 0
                dashboard["projects"] = []
                for project in projects:
                    project_dash = {"name":project.name, "records":{"January":{"number":0, "size":0}, "February":{"number":0, "size":0}, "March":{"number":0, "size":0}, "April":{"number":0, "size":0}, "May":{"number":0, "size":0}, "June":{"number":0, "size":0}, "July":{"number":0, "size":0}, "August":{"number":0, "size":0}, "September":{"number":0, "size":0}, "October":{"number":0, "size":0}, "November":{"number":0, "size":0}, "December":{"number":0, "size":0}}}
                    records = RecordModel.objects(project=project)
                    dashboard["records_total"] += len(records)
                    for record in records:
                        environment = record.environment
                        size = 0
                        try:
                            size = environment.bundle["size"]
                        except:
                            size = 0

                        dashboard["environments_total"] += size

                        month = str(record.created_at).split("-")[1]
                        if month == "01":
                            project_dash["records"]["January"]["number"] += 1
                            project_dash["records"]["January"]["size"] += size
                        if month == "02":
                            project_dash["records"]["February"]["number"] += 1
                            project_dash["records"]["February"]["size"] += size
                        if month == "03":
                            project_dash["records"]["March"]["number"] += 1
                            project_dash["records"]["March"]["size"] += size
                        if month == "04":
                            project_dash["records"]["April"]["number"] += 1
                            project_dash["records"]["April"]["size"] += size
                        if month == "05":
                            project_dash["records"]["May"]["number"] += 1
                            project_dash["records"]["May"]["size"] += size
                        if month == "06":
                            project_dash["records"]["June"]["number"] += 1
                            project_dash["records"]["June"]["size"] += size
                        if month == "07":
                            project_dash["records"]["July"]["number"] += 1
                            project_dash["records"]["July"]["size"] += size
                        if month == "08":
                            project_dash["records"]["August"]["number"] += 1
                            project_dash["records"]["August"]["size"] += size
                        if month == "09":
                            project_dash["records"]["September"]["number"] += 1
                            project_dash["records"]["September"]["size"] += size
                        if month == "10":
                            project_dash["records"]["October"]["number"] += 1
                            project_dash["records"]["October"]["size"] += size
                        if month == "11":
                            project_dash["records"]["November"]["number"] += 1
                            project_dash["records"]["November"]["size"] += size
                        if month == "12":
                            project_dash["records"]["December"]["number"] += 1
                            project_dash["records"]["December"]["size"] += size

                    dashboard["projects"].append(project_dash)

                return fk.Response(json.dumps(dashboard, sort_keys=True, indent=4, separators=(',', ': ')), mimetype='application/json')
            else:
                return fk.redirect('http://0.0.0.0:5000/?action=dashboard_failed')
    else:
        return fk.make_response('Method not allowed.', status.HTTP_405_METHOD_NOT_ALLOWED)
Ejemplo n.º 20
0
def public_search():
    (traffic, created) = TrafficModel.objects.get_or_create(created_at=str(datetime.datetime.utcnow()), service="cloud", endpoint="/public/dashboard/search")
    if not created:
        traffic.interactions += 1 
        traffic.save()
        
    if fk.request.method == 'GET':
        if fk.request.args:
            query = fk.request.args.get("query").split(" ") #single word for now.
            users = []
            for user in UserModel.objects():
                profile = ProfileModel.objects(user=user)
                where = []
                if query in user.email:
                    where.append("email")
                if query in profile.fname:
                    where.append("fname")
                if query in profile.lname:
                    where.append("lname")
                if query in profile.organisation:
                    where.append("organisation")
                if query in profile.about:
                    where.append("about")
                if len(where) != 0:
                    users.append({"id":str(user.id), "email":user.email, "fname":profile.fname, "lname":profile.lname, "organisation":profile.organisation, "about":profile.about})
            projects = []
            records = []
            for project in ProjectModel.objects():
                if project.access == 'public':
                    where_project = []
                    if query in project.name:
                        where_project.append("name")
                    if query in project.goals:
                        where_project.append("goals")
                    if query in project.description:
                        where_project.append("description")
                    if query in project.group:
                        where_project.append("group")

                    if len(where_project) != 0:
                        projects.append({"user":str(project.owner.id), "id":str(project.id), "name":project.name, "created":str(project.created_at), "duration":str(project.duration)})
                    
                    for record in RecordModel.objects(project=project):
                        if record.access == 'public':
                            body = record.body
                            where_record = []
                            
                            if query in record.label:
                                where_record.append("label")
                            if query in str(json.dumps(record.system)):
                                where_record.append("system")
                            if query in str(json.dumps(record.program)):
                                where_record.append("program")
                            if query in str(json.dumps(record.inputs)):
                                where_record.append("inputs")
                            if query in str(json.dumps(record.outputs)):
                                where_record.append("outputs")
                            if query in str(json.dumps(record.dependencies)):
                                where_record.append("dependencies")
                            if query in record.status:
                                where_record.append("status")
                            if query in str(json.dumps(body.data)):
                                where_record.append("data")

                            if len(where_record) != 0:
                                records.append({"user":str(record.project.owner.id), "project":str(record.project.id), "id":str(record.id), "label":record.label, "created":str(record.created_at), "status":record.status})

            diffs = []
            for diff in DiffModel.objects():
                if diff.record_from.access == 'public' and diff.record_to.access == 'public':
                    where = []
                    if query in str(json.dumps(diff.diff)):
                        where.append("diff")
                    if query in diff.proposition:
                        where.append("proposition")
                    if query in diff.status:
                        where.append("status")
                    if query in str(json.dumps(diff.comments)):
                        where.append("comments")

                    if len(where) != 0:
                        diffs.append({"id":str(diff.id), "from":str(diff.record_from.id), "to":str(diff.record_to.id), "sender":str(diff.sender.id), "targeted":str(diff.targeted.id), "proposition":diff.proposition, "status":diff.status})
                
            return fk.Response(json.dumps({'users':{'count':len(users), 'result':users}, 'projects':{'count':len(projects), 'result':projects}, 'records':{'count':len(records), 'result':records}, 'diffs':{'count':len(diffs), 'result':diffs}}, sort_keys=True, indent=4, separators=(',', ': ')), mimetype='application/json')
        else:
            return fk.redirect('http://0.0.0.0:5000/error-400/')
    else:
        return fk.redirect('http://0.0.0.0:5000/error-405/')