Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update pop up org name #7

Merged
merged 1 commit into from
Mar 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 28 additions & 5 deletions core/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,8 @@ def create(self, validated_data):


class CoreUserUpdateOrganizationSerializer(serializers.ModelSerializer):
""" Let's user update his organization_name,and email. Also this assigns permissions to users """
""" Let's user update his organization_name,and email from the one time pop-up screen.
Also this assigns permissions to users """

email = serializers.CharField(required=False)
organization_name = serializers.CharField(required=False)
Expand All @@ -299,12 +300,33 @@ def update(self, instance, validated_data):
instance.email = validated_data.get('email', instance.email)
if instance.email is not None:
instance.save()
is_new_org = Organization.objects.filter(name=organization_name)

organization, is_new_org = Organization.objects.get_or_create(name=organization_name)
# check whether org_name is "default"
if organization_name == 'default':
default_org = Organization.objects.filter(name='Default Organization').first()
instance.organization = default_org
instance.save()
# now attach the user role as USER to default organization
default_org_user = CoreGroup.objects.filter(organization__name='Default Organization',
is_org_level=True,
permissions=PERMISSIONS_VIEW_ONLY).first()
instance.core_groups.add(default_org_user)

# remove any other group permissions he is not added
for single_group in instance.core_groups.all():
default_org_groups = CoreGroup.objects.filter(organization__name='Default Organization',
is_org_level=True,
permissions=PERMISSIONS_VIEW_ONLY)
if single_group not in default_org_groups:
instance.core_groups.remove(single_group)

# check whether an old organization
elif is_new_org.exists():

# if an already existing user in an org add him as user
if not is_new_org:
organization = Organization.objects.get(name=organization_name)
instance.organization = organization
instance.is_active = False
instance.save()
# now attach the user role as USER
org_user = CoreGroup.objects.filter(organization__name=organization_name,
Expand All @@ -322,8 +344,9 @@ def update(self, instance, validated_data):
instance.core_groups.remove(single_group)

# if the current user is the first user
else:
elif not is_new_org.exists():
# first update the org name for that user
organization = Organization.objects.create(name=organization_name)
instance.organization = organization
instance.save()
# now attach the user role as ADMIN
Expand Down
18 changes: 17 additions & 1 deletion core/views/organization.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import django_filters
from rest_framework import viewsets
from rest_framework.response import Response

from rest_framework.decorators import action
from core.models import Organization
from core.serializers import OrganizationSerializer
from core.permissions import IsOrgMember
Expand Down Expand Up @@ -47,3 +47,19 @@ def list(self, request, *args, **kwargs):
permission_classes = (IsOrgMember,)
queryset = Organization.objects.all()
serializer_class = OrganizationSerializer

# /organization/names/
# send only the names
@action(detail=False, methods=['get'], name='Fetch Already existing Organization', url_path='names')
def fetch_existing_orgs(self, request, pk=None, *args, **kwargs):
"""
Fetch Already existing Organizations in Buildly Core,
Any logged in user can access this
"""
# all orgs in Buildly Core
queryset = Organization.objects.all()
names = list()
for record in queryset:
names.append(record.name)

return Response(names)