def create_user_project_settings(project, user, homepage=Section.timeline): """ Given a project and user, create notification policy for it. """ model_cls = apps.get_model("settings", "UserProjectSettings") try: return model_cls.objects.create(project=project, user=user, homepage=homepage) except IntegrityError as e: raise exc.IntegrityError( _("Notify exists for specified user and project")) from e
def accept_invitation_by_existing_user(token: str, user_id: int): user_model = get_user_model() user = user_model.objects.get(id=user_id) membership = get_membership_by_token(token) try: membership.user = user membership.save(update_fields=["user"]) except IntegrityError: raise exc.IntegrityError( _("This user is already a member of the project.")) return user
def set_notify_policy_level(notify_policy, notify_level, live=False): """ Set notification level for specified policy. """ if notify_level not in [e.value for e in NotifyLevel]: raise exc.IntegrityError(_("Invalid value for notify level")) if live: notify_policy.live_notify_level = notify_level else: notify_policy.notify_level = notify_level notify_policy.save()
def create_notify_policy(project, user, level=NotifyLevel.involved, live_level=NotifyLevel.involved): """ Given a project and user, create notification policy for it. """ model_cls = apps.get_model("notifications", "NotifyPolicy") try: return model_cls.objects.create(project=project, user=user, notify_level=level, live_notify_level=live_level) except IntegrityError as e: raise exc.IntegrityError( _("Notify exists for specified user and project")) from e
def create_notify_policy_if_not_exists(project, user, level=NotifyLevel.involved, live_level=NotifyLevel.involved, web_level=True): """ Given a project and user, create notification policy for it. """ model_cls = apps.get_model("notifications", "NotifyPolicy") try: result = model_cls.objects.get_or_create(project=project, user=user, defaults={ "notify_level": level, "live_notify_level": live_level, "web_notify_level": web_level }) return result[0] except IntegrityError as e: raise exc.IntegrityError( _("Notify exists for specified user and project")) from e