Skip to content

Commit

Permalink
refactoring related_name for org users (aka members)
Browse files Browse the repository at this point in the history
  • Loading branch information
snyaggarwal committed Aug 27, 2020
1 parent cae3321 commit 36a6679
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 15 deletions.
6 changes: 1 addition & 5 deletions core/orgs/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,6 @@ class Meta:
def org(self):
return self.mnemonic

@property
def members(self):
return self.users

@property
def num_members(self):
return self.members.count()
Expand All @@ -43,7 +39,7 @@ def get_url_kwarg():

@classmethod
def get_by_username(cls, username):
return cls.objects.filter(users__username=username)
return cls.objects.filter(members__username=username)

@classmethod
def get_public(cls):
Expand Down
19 changes: 19 additions & 0 deletions core/users/migrations/0003_auto_20200827_1240.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Generated by Django 3.0.9 on 2020-08-27 12:40

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('orgs', '0002_auto_20200720_1450'),
('users', '0002_auto_20200821_1342'),
]

operations = [
migrations.AlterField(
model_name='userprofile',
name='organizations',
field=models.ManyToManyField(related_name='members', to='orgs.Organization'),
),
]
12 changes: 3 additions & 9 deletions core/users/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class Meta:
swappable = 'AUTH_USER_MODEL'

OBJECT_TYPE = USER_OBJECT_TYPE
organizations = models.ManyToManyField('orgs.Organization', related_name='users')
organizations = models.ManyToManyField('orgs.Organization', related_name='members')
company = models.TextField(null=True, blank=True)
location = models.TextField(null=True, blank=True)
preferred_locale = models.TextField(null=True, blank=True)
Expand Down Expand Up @@ -69,14 +69,8 @@ def __str__(self):
return str(self.mnemonic)

def is_admin_for(self, concept_container):
parent = concept_container.parent

if parent == self:
return True
if self.organizations.filter(id=parent.id).exists():
return True

return False
parent_id = concept_container.parent_id
return parent_id == self.id or self.organizations.filter(id=parent_id).exists()


admin.site.register(UserProfile)
2 changes: 1 addition & 1 deletion core/users/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def get(self, request, *args, **kwargs):
if not self.can_view(organization):
return Response(status=status.HTTP_403_FORBIDDEN)

self.queryset = organization.users.all()
self.queryset = organization.members.all()
return self.list(request, *args, **kwargs)

def post(self, request, *args, **kwargs):
Expand Down

0 comments on commit 36a6679

Please sign in to comment.