def add_membership(user,organization): """ Adds a membership object. Required because membership defines the relation between user and organization, and tastypie does not automatically create through relations. """ users = organization.users.all() membership = Membership( user = user, organization = organization, ) if users.count()==0: #If a user is the first one in an organization, make them the administrator. membership.role = UserRoles.administrator membership.save() else: membership.role = UserRoles.student membership.save()
def add_membership(user,organization): """ Adds a membership object. Required because membership defines the relation between user and organization, and tastypie does not automatically create through relations. """ users = organization.users.all() membership_count = Membership.objects.filter(user=user).count() if membership_count>=settings.MEMBERSHIP_LIMIT: error_message = "All users, including user {0} can only have a maximum of 1 organizations. This will hopefully be fixed in a future release.".format(user) log.info(error_message) raise BadRequest(error_message) membership = Membership( user = user, organization = organization, ) if users.count()==0: #If a user is the first one in an organization, make them the administrator. membership.role = UserRoles.administrator membership.save() else: membership.role = UserRoles.student membership.save()