Exemple #1
0
def create_language(request):  # tested & in docs
    try:
        variables = {'auth': request.authenticated_userid}

        req = request.json_body
        try:
            parent_client_id = req['parent_client_id']
            parent_object_id = req['parent_object_id']
        except:
            parent_client_id = None
            parent_object_id = None
        translation_gist_client_id = req['translation_gist_client_id']
        translation_gist_object_id = req['translation_gist_object_id']
        client = DBSession.query(Client).filter_by(id=variables['auth']).first()
        object_id = req.get('object_id', None)
        if not client:
            raise KeyError("Invalid client id (not registered on server). Try to logout and then login.",
                           variables['auth'])
        user = DBSession.query(User).filter_by(id=client.user_id).first()
        if not user:
            raise CommonException("This client id is orphaned. Try to logout and then login once more.")

        parent = None
        if parent_client_id and parent_object_id:
            parent = DBSession.query(Language).filter_by(client_id=parent_client_id, object_id=parent_object_id).first()
        language = Language(client_id=variables['auth'],
                            object_id=object_id,
                                translation_gist_client_id=translation_gist_client_id,
                                translation_gist_object_id=translation_gist_object_id)
        DBSession.add(language)
        if parent:
            language.parent = parent
        DBSession.flush()
        basegroups = []
        basegroups += [DBSession.query(BaseGroup).filter_by(name="Can edit languages").first()]
        basegroups += [DBSession.query(BaseGroup).filter_by(name="Can delete languages").first()]
        if not object_id:
            groups = []
            for base in basegroups:
                group = Group(subject_client_id=language.client_id, subject_object_id=language.object_id, parent=base)
                groups += [group]
            for group in groups:
                add_user_to_group(user, group)
        request.response.status = HTTPOk.code
        return {'object_id': language.object_id,
                'client_id': language.client_id}
    except KeyError as e:
        request.response.status = HTTPBadRequest.code
        return {'error': str(e)}

    except IntegrityError as e:
        request.response.status = HTTPInternalServerError.code
        return {'error': str(e)}

    except CommonException as e:
        request.response.status = HTTPConflict.code
        return {'error': str(e)}
Exemple #2
0
def create_translationgist(request):
    try:
        variables = {'auth': request.authenticated_userid}

        req = request.json_body
        object_id = req.get('object_id', None)
        type = req['type']
        client = DBSession.query(Client).filter_by(
            id=variables['auth']).first()
        if not client:
            raise KeyError(
                "Invalid client id (not registered on server). Try to logout and then login.",
                variables['auth'])
        user = DBSession.query(User).filter_by(id=client.user_id).first()
        if not user:
            raise CommonException(
                "This client id is orphaned. Try to logout and then login once more."
            )
        translationgist = TranslationGist(client_id=variables['auth'],
                                          object_id=object_id,
                                          type=type)
        DBSession.add(translationgist)
        DBSession.flush()
        basegroups = []
        basegroups += [
            DBSession.query(BaseGroup).filter_by(
                name="Can delete translationgist").first()
        ]
        if not object_id:
            groups = []
            for base in basegroups:
                group = Group(subject_client_id=translationgist.client_id,
                              subject_object_id=translationgist.object_id,
                              parent=base)
                groups += [group]
            for group in groups:
                add_user_to_group(user, group)
        request.response.status = HTTPOk.code
        return {
            'object_id': translationgist.object_id,
            'client_id': translationgist.client_id
        }
    except KeyError as e:
        request.response.status = HTTPBadRequest.code
        return {'error': str(e)}

    except IntegrityError as e:
        request.response.status = HTTPInternalServerError.code
        return {'error': str(e)}

    except CommonException as e:
        request.response.status = HTTPConflict.code
        return {'error': str(e)}
Exemple #3
0
def create_translationatom(request):
    try:
        variables = {'auth': request.authenticated_userid}

        req = request.json_body
        parent_client_id = req['parent_client_id']
        parent_object_id = req['parent_object_id']
        locale_id = req['locale_id']
        content = req['content']
        object_id = req.get('object_id', None)
        client = DBSession.query(Client).filter_by(id=variables['auth']).first()
        if not client:
            raise KeyError("Invalid client id (not registered on server). Try to logout and then login.",
                           variables['auth'])
        user = DBSession.query(User).filter_by(id=client.user_id).first()
        if not user:
            raise CommonException("This client id is orphaned. Try to logout and then login once more.")
        parent = DBSession.query(TranslationGist).filter_by(client_id=parent_client_id, object_id=parent_object_id).first()
        if not parent.marked_for_deletion:
            translationatom = TranslationAtom(client_id=variables['auth'],
                                              object_id=object_id,
                                              parent=parent,
                                              locale_id=locale_id,
                                              content=content)
            DBSession.add(translationatom)
            DBSession.flush()
            if not object_id:
                basegroups = []
                basegroups += [DBSession.query(BaseGroup).filter_by(name="Can edit translationatom").first()]
                if not object_id:
                    groups = []
                    for base in basegroups:
                        group = Group(subject_client_id=translationatom.client_id, subject_object_id=translationatom.object_id, parent=base)
                        groups += [group]
                    for group in groups:
                        add_user_to_group(user, group)
            request.response.status = HTTPOk.code
            return {'object_id': translationatom.object_id,
                    'client_id': translationatom.client_id}
        else:
            raise KeyError("TranslationGist deleted")
    except KeyError as e:
        request.response.status = HTTPBadRequest.code
        return {'error': str(e)}

    except IntegrityError as e:
        request.response.status = HTTPInternalServerError.code
        return {'error': str(e)}

    except CommonException as e:
        request.response.status = HTTPConflict.code
        return {'error': str(e)}
Exemple #4
0
def create_organization(request):  # TODO: test
    try:

        variables = {'auth': request.authenticated_userid}
        req = request.json_body
        name = req['name']
        about = req['about']
        client = DBSession.query(Client).filter_by(
            id=variables['auth']).first()
        if not client:
            raise KeyError(
                "Invalid client id (not registered on server). Try to logout and then login."
            )
        user = DBSession.query(User).filter_by(id=client.user_id).first()
        if not user:
            raise CommonException(
                "This client id is orphaned. Try to logout and then login once more."
            )

        organization = Organization(name=name, about=about)
        if user not in organization.users:
            organization.users.append(user)
        DBSession.add(organization)
        DBSession.flush()
        bases = DBSession.query(BaseGroup).filter_by(subject='organization')
        for base in bases:
            group = Group(parent=base, subject_object_id=organization.id)
            add_user_to_group(user, group)
            DBSession.add(group)
        request.response.status = HTTPOk.code
        return {'organization_id': organization.id}
    except KeyError as e:
        request.response.status = HTTPBadRequest.code
        return {'error': str(e)}

    except IntegrityError as e:
        request.response.status = HTTPInternalServerError.code
        return {'error': str(e)}

    except CommonException as e:
        request.response.status = HTTPConflict.code
        return {'error': str(e)}
Exemple #5
0
def create_language(request):  # tested & in docs
    try:
        variables = {'auth': request.authenticated_userid}

        req = request.json_body
        try:
            parent_client_id = req['parent_client_id']
            parent_object_id = req['parent_object_id']
        except:
            parent_client_id = None
            parent_object_id = None
        translation_gist_client_id = req['translation_gist_client_id']
        translation_gist_object_id = req['translation_gist_object_id']
        client = DBSession.query(Client).filter_by(
            id=variables['auth']).first()
        object_id = req.get('object_id', None)
        if not client:
            raise KeyError(
                "Invalid client id (not registered on server). Try to logout and then login.",
                variables['auth'])
        user = DBSession.query(User).filter_by(id=client.user_id).first()
        if not user:
            raise CommonException(
                "This client id is orphaned. Try to logout and then login once more."
            )

        parent = None
        if parent_client_id and parent_object_id:
            parent = DBSession.query(Language).filter_by(
                client_id=parent_client_id,
                object_id=parent_object_id).first()
        language = Language(
            client_id=variables['auth'],
            object_id=object_id,
            translation_gist_client_id=translation_gist_client_id,
            translation_gist_object_id=translation_gist_object_id)
        DBSession.add(language)
        if parent:
            language.parent = parent
        DBSession.flush()
        basegroups = []
        basegroups += [
            DBSession.query(BaseGroup).filter_by(
                name="Can edit languages").first()
        ]
        basegroups += [
            DBSession.query(BaseGroup).filter_by(
                name="Can delete languages").first()
        ]
        if not object_id:
            groups = []
            for base in basegroups:
                group = Group(subject_client_id=language.client_id,
                              subject_object_id=language.object_id,
                              parent=base)
                groups += [group]
            for group in groups:
                add_user_to_group(user, group)
        request.response.status = HTTPOk.code
        return {
            'object_id': language.object_id,
            'client_id': language.client_id
        }
    except KeyError as e:
        request.response.status = HTTPBadRequest.code
        return {'error': str(e)}

    except IntegrityError as e:
        request.response.status = HTTPInternalServerError.code
        return {'error': str(e)}

    except CommonException as e:
        request.response.status = HTTPConflict.code
        return {'error': str(e)}
Exemple #6
0
def edit_organization(request):  # TODO: test
    try:
        response = dict()
        organization_id = request.matchdict.get('organization_id')
        organization = DBSession.query(Organization).filter_by(
            id=organization_id).first()

        variables = {'auth': request.authenticated_userid}
        client = DBSession.query(Client).filter_by(
            id=variables['auth']).first()
        if not client:
            raise KeyError(
                "Invalid client id (not registered on server). Try to logout and then login."
            )
        creator = DBSession.query(User).filter_by(id=client.user_id).first()
        if not creator:
            raise CommonException(
                "This client id is orphaned. Try to logout and then login once more."
            )
        if organization:
            if not organization.marked_for_deletion:
                req = request.json_body
                if 'add_users' in req:
                    for user_id in req['add_users']:
                        user = DBSession.query(User).filter_by(
                            id=user_id).first()
                        if user not in organization.users:
                            if not user in organization.users:
                                organization.users.append(user)
                            bases = DBSession.query(BaseGroup).filter_by(
                                subject='organization')
                            for base in bases:
                                group = DBSession.query(Group).filter_by(
                                    base_group_id=base.id,
                                    subject_object_id=organization.id).first()
                                add_user_to_group(user, group)
                if 'delete_users' in req:
                    for user_id in req['delete_users']:
                        if user_id == creator.id:
                            raise CommonException(
                                "You shouldn't delete yourself")
                        user = DBSession.query(User).filter_by(
                            id=user_id).first()
                        if user in organization.users:
                            organization.users.remove(user)
                            bases = DBSession.query(BaseGroup).filter_by(
                                subject='organization')
                            for base in bases:
                                group = DBSession.query(Group).filter_by(
                                    base_group_id=base.id,
                                    subject_object_id=organization.id).first()
                                group.users.remove(user)
                if 'name' in req:
                    organization.name = req['name']
                if 'about' in req:
                    organization.about = req['about']
                request.response.status = HTTPOk.code
                return response

        request.response.status = HTTPNotFound.code
        return {'error': str("No such organization in the system")}
    except KeyError as e:
        request.response.status = HTTPBadRequest.code
        return {'error': str(e)}
Exemple #7
0
def signup_post(request):  # tested
    try:
        req = request.json_body
        login = req['login']
        name = req['name']
        email = req['email']
        password = req['password']

        day = req.get('day')
        month = req.get('month')
        year = req.get('year')
        if day is None or month is None or year is None:
            request.response.status = HTTPBadRequest.code
            return {'Error': "day, month or year of the birth is missing"}
        # birthday = datetime.datetime.strptime(day + month + year, "%d%m%Y").date()
        try:
            day = int(day)
            month = int(month)
            year = int(year)
            birthday = datetime.date(year, month, day)
        except ValueError:
            request.response.status = HTTPBadRequest.code
            return {'Error': "Invalid birthday"}

        if DBSession.query(User).filter_by(login=login).first():
            raise CommonException(
                "The user with this login is already registered")
        if DBSession.query(Email).filter_by(email=email).first():
            raise CommonException(
                "The user with this email is already registered")
        new_user = User(login=login,
                        name=name,
                        created_at=datetime.datetime.utcnow(),
                        intl_name=login,
                        birthday=birthday,
                        is_active=True)
        pwd = Passhash(password=password)
        email = Email(email=email)
        new_user.password = pwd
        new_user.email = email
        DBSession.add(new_user)
        basegroups = []
        basegroups += [
            DBSession.query(BaseGroup).filter_by(
                name="Can create dictionaries").first()
        ]
        basegroups += [
            DBSession.query(BaseGroup).filter_by(
                name="Can create languages").first()
        ]
        basegroups += [
            DBSession.query(BaseGroup).filter_by(
                name="Can create organizations").first()
        ]
        basegroups += [
            DBSession.query(BaseGroup).filter_by(
                name="Can create translation strings").first()
        ]
        groups = []
        for base in basegroups:
            groups += [
                DBSession.query(Group).filter_by(
                    subject_override=True, base_group_id=base.id).first()
            ]
        for group in groups:
            add_user_to_group(new_user, group)
        DBSession.flush()
        return {}

    except KeyError as e:
        request.response.status = HTTPBadRequest.code
        return {'status': request.response.status, 'error': str(e)}

    except CommonException as e:
        request.response.status = HTTPConflict.code
        return {'status': request.response.status, 'error': str(e)}

    except ValueError as e:
        request.response.status = HTTPConflict.code
        return {'status': request.response.status, 'error': str(e)}