Add support for targeting notifications to organizations and remove the password_required notification for new orbs

This commit is contained in:
Joseph Schorr 2014-03-12 19:00:24 -04:00
parent 578add3b9e
commit 525ef8d14f
8 changed files with 61 additions and 16 deletions

View file

@ -278,7 +278,7 @@ class NotificationKind(BaseModel):
class Notification(BaseModel):
uuid = CharField(default=uuid_generator, index=True)
kind = ForeignKeyField(NotificationKind, index=True)
notification_user = ForeignKeyField(User, index=True)
target = ForeignKeyField(User, index=True)
metadata_json = TextField(default='{}')
created = DateTimeField(default=datetime.now, index=True)

View file

@ -59,7 +59,7 @@ class InvalidBuildTriggerException(DataModelException):
pass
def create_user(username, password, email):
def create_user(username, password, email, is_organization=False):
if not validate_email(email):
raise InvalidEmailAddressException('Invalid email address: %s' % email)
if not validate_username(username):
@ -96,7 +96,7 @@ def create_user(username, password, email):
# If the password is None, then add a notification for the user to change
# their password ASAP.
if not pw_hash:
if not pw_hash and not is_organization:
create_notification('password_required', new_user)
return new_user
@ -107,7 +107,7 @@ def create_user(username, password, email):
def create_organization(name, email, creating_user):
try:
# Create the org
new_org = create_user(name, None, email)
new_org = create_user(name, None, email, is_organization=True)
new_org.organization = True
new_org.save()
@ -1546,24 +1546,44 @@ def list_trigger_builds(namespace_name, repository_name, trigger_uuid,
.where(RepositoryBuildTrigger.uuid == trigger_uuid))
def create_notification(kind, user, metadata={}):
def create_notification(kind, target, metadata={}):
kind_ref = NotificationKind.get(name=kind)
notification = Notification.create(kind=kind_ref, notification_user=user,
notification = Notification.create(kind=kind_ref, target=target,
metadata_json=json.dumps(metadata))
return notification
def list_notifications(user, kind=None):
Org = User.alias()
AdminTeam = Team.alias()
AdminTeamMember = TeamMember.alias()
AdminUser = User.alias()
query = (Notification.select()
.join(User)
.where(Notification.notification_user == user))
.join(User)
.switch(Notification)
.join(Org, JOIN_LEFT_OUTER, on=(Org.id == Notification.target))
.join(AdminTeam, JOIN_LEFT_OUTER, on=(Org.id ==
AdminTeam.organization))
.join(TeamRole, JOIN_LEFT_OUTER, on=(AdminTeam.role == TeamRole.id))
.switch(AdminTeam)
.join(AdminTeamMember, JOIN_LEFT_OUTER, on=(AdminTeam.id ==
AdminTeamMember.team))
.join(AdminUser, JOIN_LEFT_OUTER, on=(AdminTeamMember.user ==
AdminUser.id)))
where_clause = ((Notification.target == user) |
((AdminUser.id == user) &
(TeamRole.name == 'admin')))
if kind:
query = query.join(NotificationKind).where(NotificationKind.name == kind)
where_clause = where_clause & (NotificationKind.name == kind)
return query.order_by(Notification.created).desc()
return query.where(where_clause).order_by(Notification.created).desc()
def delete_notifications_by_kind(user, kind):
def delete_notifications_by_kind(target, kind):
kind_ref = NotificationKind.get(name=kind)
Notification.delete().where(Notification.notification_user == user, Notification.kind == kind_ref).execute()
Notification.delete().where(Notification.target == target,
Notification.kind == kind_ref).execute()