Example #1
0
    def save(entity):

        from services import UserService, CompanyService

        if entity.user is None:
            raise ValueError("User is required")

        if entity.company is None:
            raise ValueError("Company is required")

        user = UserService.UserInstance.get(entity.user.urlsafe())
        if user is None:
            raise ValueError("User not found")

        company = CompanyService.CompanyInstance.get(entity.company.urlsafe())
        if company is None:
            raise ValueError("Company not found")

        if entity.key is None:

            #It validates if association already exists
            v = UserXCompany.get_by_company_and_user(entity.company,
                                                     entity.user)
            if v is not None:
                raise ValueError("Association already exists")

            entity = UserXCompany.save(entity)
        else:
            current = UserXCompany.get(entity.key.urlsafe())
            if current is not None:
                current.user = entity.user
                current.company = entity.company
                entity = UserXCompany.save(entity)
            else:
                raise ValueError("UserXCompany does not exists")
        return entity
Example #2
0
 def get_by_company_and_user(company, user):
     return UserXCompany.get_by_company_and_user(company, user)