Beispiel #1
0
def sync(self, client=conn, resource=s3, dist="", bucket=os.environ['BUCKET_NAME'], token=''):
    count = 0
    existing_items = 0
    new_items = 0
    paginator = client.get_paginator('list_objects')
    for result in paginator.paginate(Bucket=bucket, PaginationConfig={ 'PageSize': 500, 'StartingToken': token }):
        keys = []
        for s3_object in result.get('Contents'):
            count += 1
            if count % 1000 == 0:
                print(count)
            s3_key = s3_object['Key']
            keys.append(s3_key)
        with db.atomic():
            (existing, new) = build_directory_tree(keys)
            existing_items += existing
            new_items += new
        self.update_state(state='PROGRESS', meta={
            'existingObjects': existing_items,
            'newObjects': new_items,
            'total': count
        })
    
    self.update_state(state='SUCCESS', meta={
        'existingObjects': existing_items,
        'newObjects': new_items,
        'total': count
    })
def to_database(available_files):
    with DATABASE.atomic():
        for idx in range(0, len(available_files), 100):
            try:
                GetStarted.insert_many(available_files[idx:idx+100]).execute()
            except IntegrityError:
                # print(available_files[idx])
                pass
Beispiel #3
0
def register():
    """User register route"""
    payload = request.get_json()
    print(payload)
    payload['email'] = payload['email'].lower()
    payload['username'] = payload['username'].lower()

    try:

        # encrypting password before creating it
        payload['password'] = generate_password_hash(payload['password'])

        with DATABASE.atomic():

            # creating new user
            new_user = models.User.create(username=payload['username'],
                                          email=payload['email'],
                                          password=payload['password'])
        login_user(new_user)
        new_user_dict = model_to_dict(new_user)
        new_user_dict.pop('password')

        return jsonify(
            data=new_user_dict,
            message='Sucessfully created new user with username: {}'.format(
                new_user_dict['username']),
            status=200), 200

    except IntegrityError as e:

        print('ERROR ARGUMENTS')
        print(e.args[0])

        # using the peewee.integrity error to check if the email/username is taken

        if e.args[0] == 'UNIQUE constraint failed: user.email':

            return jsonify(
                data={},
                message='There is already a user registered with that email.',
                status=401), 401

        elif e.args[0] == 'UNIQUE constraint failed: user.username':

            return jsonify(
                data={},
                message=
                'There is already a user registered with that username.',
                status=401), 401
        else:
            print('ERROR')
            return e
Beispiel #4
0
    def get(self):
        args = request.headers.get('userInfo')
        aws_keys = json.loads(args)

        instances = get_instances(aws_keys['aws_access_key_id'], aws_keys['aws_secret_access_key'])
        for i in instances:
            instance_id = i.get('instance_id')
            availability_zone = i.get('availability_zone')
            instance_state = i.get('instance_state')
            try:
                with DATABASE.atomic():
                    Instance.create(instance_id=instance_id)

            except IntegrityError:
                pass
        return jsonify({"data": instances})
def update_project(project_id):
    new_project = request.get_json()
    try:
        project = Project.get(Project.id == project_id)
    except models.DoesNotExist:
        return jsonify({ 'message': 'Project not found' }), 400
    if new_project['id'] != project.id:
        return jsonify({ 'message': 'Cannot update project at this url' }), 400
    try:
        if new_project['markerOptions']:
            marker_options = new_project['markerOptions']
            for marker_name in marker_options:
                try:
                    marker_option = MarkerOption.get(MarkerOption.name == marker_name)
                    ProjectMarkerOption.get(ProjectMarkerOption.project == project.id,
                                            ProjectMarkerOption.marker_option == marker_option.id)
                except models.DoesNotExist:
                    ProjectMarkerOption.create(project = project, marker_option = marker_option)
        if new_project['users']:
            users = new_project['users']
            for user in users:
                try:
                    project_user = ProjectUser.get(ProjectUser.app_user == user['id'],
                                                    ProjectUser.project == project)
                except models.DoesNotExist:
                    ProjectUser.create(app_user = user['id'], project = project)
        if new_project['stacks']:
            stacks = new_project['stacks']
            with db.atomic():
                for stack in stacks:
                    try:
                        project_stack = ProjectStack.get(ProjectStack.stack == stack['id'],
                                                        ProjectStack.project == project)
                    except models.DoesNotExist:
                        project_stack = ProjectStack.create(project=project, stack = stack['id'])
                        probe_setting = stack['probeSettings']
                        probe_position = json.loads(probe_setting['position'])
                        ProbeSetting.create(x_offset=int(probe_position['xOffset']),
                                            y_offset=int(probe_position['yOffset']),
                                            project_stack=project_stack,
                                            label=probe_position['label']
                                        )
        message = "Project updated"
    except Exception as ex:
        print ex
    project = Project.get(Project.id == project_id)
    return jsonify({ 'message': 'Project updated', 'project': project.deep_serialized() }), 200