Skip to content

Commit

Permalink
Remove Team.domain. Fixes #192. Reverses #108.
Browse files Browse the repository at this point in the history
  • Loading branch information
jace committed Aug 7, 2017
1 parent 66de394 commit 44d995e
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 85 deletions.
31 changes: 0 additions & 31 deletions lastuser_core/models/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,10 +171,6 @@ def add_email(self, email, primary=False, type=None, private=False):
emailob.primary = False
useremail = UserEmail(user=self, email=email, primary=primary, type=type, private=private)
useremail = failsafe_add(db.session, useremail, user=self, email=email)
with db.session.no_autoflush:
for team in Team.query.filter_by(domain=useremail.domain):
if self not in team.users:
team.users.append(self)
return useremail

def del_email(self, email):
Expand Down Expand Up @@ -460,29 +456,6 @@ def make_teams(self):
if self.members is None:
self.members = Team(title=_(u"Members"), org=self)

@property
def domain(self):
if self.members:
return self.members.domain

@domain.setter
def domain(self, value):
from ..signals import user_data_changed, team_data_changed # Import here as we can't import at top-level
if not value:
value = None
if not self.members:
self.make_teams()
if value and value != self.members.domain:
# Look for team members based on domain, but only if the domain value was
# changed
with db.session.no_autoflush:
for useremail in UserEmail.query.filter_by(domain=value).join(User):
if useremail.user not in self.members.users:
self.members.users.append(useremail.user)
user_data_changed.send(useremail.user, changes=['team-membership'])
team_data_changed.send(self.members, user=None, changes=['membership']) # The team /edit view sends 'edit'
self.members.domain = value

@hybrid_property
def name(self):
return self._name
Expand Down Expand Up @@ -595,10 +568,6 @@ class Team(UuidMixin, BaseMixin, db.Model):
users = db.relationship(User, secondary='team_membership', lazy='dynamic',
backref='teams') # No cascades here! Cascades will delete users

#: Email domain for this team. Any users with a matching email address
#: will be auto-added to this team
domain = db.Column(db.Unicode(253), nullable=True, index=True)

#: Client id that created this team
client_id = db.Column(None, db.ForeignKey('client.id',
use_alter=True, name='team_client_id_fkey'), nullable=True)
Expand Down
8 changes: 3 additions & 5 deletions lastuser_oauth/views/resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ def get_userinfo(user, client, scope=[], session=None, get_permissions=True):
userinfo['phone'] = unicode(user.phone)
if '*' in scope or 'organizations' in scope or 'organizations/*' in scope:
userinfo['organizations'] = {
'owner': [{'userid': org.buid, 'buid': org.buid, 'uuid': org.uuid, 'name': org.name, 'title': org.title, 'domain': org.domain} for org in user.organizations_owned()],
'member': [{'userid': org.buid, 'buid': org.buid, 'uuid': org.uuid, 'name': org.name, 'title': org.title, 'domain': org.domain} for org in user.organizations_memberof()],
'all': [{'userid': org.buid, 'buid': org.buid, 'uuid': org.uuid, 'name': org.name, 'title': org.title, 'domain': org.domain} for org in user.organizations()],
'owner': [{'userid': org.buid, 'buid': org.buid, 'uuid': org.uuid, 'name': org.name, 'title': org.title} for org in user.organizations_owned()],
'member': [{'userid': org.buid, 'buid': org.buid, 'uuid': org.uuid, 'name': org.name, 'title': org.title} for org in user.organizations_memberof()],
'all': [{'userid': org.buid, 'buid': org.buid, 'uuid': org.uuid, 'name': org.name, 'title': org.title} for org in user.organizations()],
}

if '*' in scope or 'organizations' in scope or 'teams' in scope or 'organizations/*' in scope or 'teams/*' in scope:
Expand All @@ -54,7 +54,6 @@ def get_userinfo(user, client, scope=[], session=None, get_permissions=True):
'title': team.title,
'org': team.org.buid,
'org_uuid': team.org.uuid,
'domain': team.domain,
'owners': team == team.org.owners,
'members': team == team.org.members,
'member': True}
Expand All @@ -70,7 +69,6 @@ def get_userinfo(user, client, scope=[], session=None, get_permissions=True):
'title': team.title,
'org': team.org.buid,
'org_uuid': team.org.uuid,
'domain': team.domain,
'owners': team == team.org.owners,
'members': team == team.org.members,
'member': False}
Expand Down
3 changes: 0 additions & 3 deletions lastuser_ui/forms/org.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@ class OrganizationForm(forms.Form):
name = forms.AnnotatedTextField(__("Username"), validators=[forms.validators.DataRequired()],
prefix=u"https://hasgeek.com/…",
widget_attrs={'autocorrect': 'none', 'autocapitalize': 'none'})
domain = forms.RadioField(__("Domain"),
description=__(u"Users with an email address at this domain will automatically become members of this organization"),
validators=[forms.validators.Optional()])

def validate_name(self, field):
if not valid_username(field.data):
Expand Down
17 changes: 1 addition & 16 deletions lastuser_ui/views/org.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
# -*- coding: utf-8 -*-

from flask import g, current_app, render_template, url_for, abort, redirect, request, Markup
from flask import g, current_app, render_template, url_for, abort, redirect, request
from baseframe import _
from baseframe.forms import render_form, render_redirect, render_delete_sqla
from baseframe.staticdata import webmail_domains
from coaster.views import load_model, load_models

from lastuser_core.models import db, Organization, Team
Expand All @@ -13,18 +12,6 @@
from ..forms.org import OrganizationForm, TeamForm


def user_org_domains(user, org=None):
domains = [email.domain for email in user.emails if email.domain not in webmail_domains]
choices = [(d, d) for d in domains]
if org and org.domain and org.domain not in domains:
choices.insert(0, (org.domain, Markup(_("{domain} <em>(Current setting)</em>")).format(domain=org.domain)))
if not domains:
choices.insert(0, (u'', Markup(_("<em>(You do not have a verified non-webmail email address yet)</em>"))))
else:
choices.insert(0, (u'', Markup(_("<em>(No domain associated with this organization)</em>"))))
return choices


# --- Routes: Organizations ---------------------------------------------------

@lastuser_ui.route('/organizations')
Expand All @@ -37,7 +24,6 @@ def org_list():
@requires_login
def org_new():
form = OrganizationForm()
form.domain.choices = user_org_domains(g.user)
form.name.description = current_app.config.get('ORG_NAME_REASON')
form.title.description = current_app.config.get('ORG_TITLE_REASON')
if form.validate_on_submit():
Expand Down Expand Up @@ -66,7 +52,6 @@ def org_info(org):
@load_model(Organization, {'name': 'name'}, 'org', permission='edit')
def org_edit(org):
form = OrganizationForm(obj=org)
form.domain.choices = user_org_domains(g.user, org)
form.name.description = current_app.config.get('ORG_NAME_REASON')
form.title.description = current_app.config.get('ORG_TITLE_REASON')
if form.validate_on_submit():
Expand Down
26 changes: 26 additions & 0 deletions migrations/versions/07f975f81f03_remove_team_domain.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
"""Remove team domain
Revision ID: 07f975f81f03
Revises: 4e206c5ddabd
Create Date: 2017-08-04 15:12:11.992856
"""
from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = '07f975f81f03'
down_revision = '4e206c5ddabd'
branch_labels = None
depends_on = None


def upgrade():
op.drop_index('ix_team_domain', table_name='team')
op.drop_column('team', 'domain')


def downgrade():
op.add_column('team', sa.Column('domain', sa.VARCHAR(length=253), autoincrement=False, nullable=True))
op.create_index('ix_team_domain', 'team', ['domain'], unique=False)
2 changes: 1 addition & 1 deletion tests/unit/lastuser_core/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def make_fixtures(self):
db.session.add(client)
self.client = client

dachshunds = Team(title=u"Dachshunds", org=batdog, domain=u'keepballin.ca')
dachshunds = Team(title=u"Dachshunds", org=batdog)
db.session.add(dachshunds)
self.dachshunds = dachshunds

Expand Down
28 changes: 0 additions & 28 deletions tests/unit/lastuser_core/test_model_user_Organization.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,34 +167,6 @@ def test_organization_available_permissions(self):
self.assertIsInstance(org_with_permissions, list)
self.assertItemsEqual(org_with_permissions, [netizens])

def test_organization_domain(self):
"""
Test for retrieving team members in a Organization based on domain
"""

# scenario 1: domain valid is None
allegiant = models.Organization(name=u'allegiant', title=u'Allegiant')
self.assertEqual(allegiant.domain, None)

# scenario 2: no teams in organzation
allegiant_domain = u'allegiants.com'
allegiant.domain = allegiant_domain
self.assertEqual(allegiant.domain, allegiant_domain)

# scenario 3: members dont have same domain as domain value
beatrice = models.User(username=u'beatrice', fullname=u'Beatrice Prior', email=u'[email protected]')
tobias = models.User(username=u'tobias', fullname=u'Tobias Eaton', email=u'[email protected]')
erudite = models.Organization(name=u'erudite', title=u'Erudite')
erudite.owners.users.append(beatrice)
erudite.members.users.append(tobias)
db.session.add(beatrice)
db.session.add(tobias)
db.session.add(erudite)
db.session.commit()
erudite.domain = u'erudites.com'
self.assertEqual(erudite.domain, u'erudites.com')
self.assertItemsEqual(erudite.teams, [erudite.owners, erudite.members])

def test_organization_name(self):
"""
Test for retrieving Organization's name
Expand Down
1 change: 0 additions & 1 deletion tests/unit/lastuser_core/test_model_user_User.py
Original file line number Diff line number Diff line change
Expand Up @@ -470,4 +470,3 @@ def test_user_add_email(self):
db.session.add(gustav)
db.session.commit()
self.assertEqual(gustav_result.email, gustav_email)
self.assertEqual(gustav.teams[0], self.fixtures.dachshunds)

0 comments on commit 44d995e

Please sign in to comment.