import --> org multiple locations on create/edit/list & serialzier
This commit is contained in:
@@ -30,7 +30,7 @@ from apps.authentication.models import (
|
|||||||
Organization,
|
Organization,
|
||||||
OrganizationType,
|
OrganizationType,
|
||||||
BankAccountInformation,
|
BankAccountInformation,
|
||||||
BlacklistedAccessToken
|
BlacklistedAccessToken, OrganizationLocationInfo
|
||||||
)
|
)
|
||||||
from apps.authentication.tools import get_token_jti
|
from apps.authentication.tools import get_token_jti
|
||||||
from apps.authorization.api.v1 import api as authorize_view
|
from apps.authorization.api.v1 import api as authorize_view
|
||||||
@@ -261,14 +261,21 @@ class OrganizationViewSet(BaseViewSet, ModelViewSet, DynamicSearchMixin):
|
|||||||
def list(self, request, *args, **kwargs):
|
def list(self, request, *args, **kwargs):
|
||||||
""" all organization """
|
""" all organization """
|
||||||
org = get_organization_by_user(request.user)
|
org = get_organization_by_user(request.user)
|
||||||
|
param = self.request.query_params # noqa
|
||||||
|
|
||||||
queryset = self.get_queryset(
|
queryset = self.get_queryset(
|
||||||
visibility_by_org_scope=True
|
visibility_by_org_scope=True
|
||||||
) if org.free_visibility_by_scope else self.get_queryset()
|
) if org.free_visibility_by_scope else self.get_queryset()
|
||||||
|
|
||||||
query = self.filter_query(queryset)
|
# filter by organization type
|
||||||
|
if 'org_type' in param.keys():
|
||||||
|
queryset = queryset.filter(type__id=param.get('org_type', 0))
|
||||||
|
|
||||||
page = self.paginate_queryset(query.order_by('-create_date')) # paginate queryset
|
# filter on search
|
||||||
|
if 'search' in param.keys():
|
||||||
|
queryset = self.filter_query(queryset)
|
||||||
|
|
||||||
|
page = self.paginate_queryset(queryset.order_by('-create_date')) # paginate queryset
|
||||||
|
|
||||||
if page is not None: # noqa
|
if page is not None: # noqa
|
||||||
serializer = self.serializer_class(page, many=True)
|
serializer = self.serializer_class(page, many=True)
|
||||||
@@ -284,6 +291,18 @@ class OrganizationViewSet(BaseViewSet, ModelViewSet, DynamicSearchMixin):
|
|||||||
if serializer.is_valid():
|
if serializer.is_valid():
|
||||||
organization = serializer.save()
|
organization = serializer.save()
|
||||||
|
|
||||||
|
if 'addresses' in request.data.keys():
|
||||||
|
# import multiple addresses with postal_code to orgs
|
||||||
|
|
||||||
|
address_obj_list = []
|
||||||
|
for addr in request.data['addresses']:
|
||||||
|
addr.update({'org': organization.id})
|
||||||
|
address_obj_list.append(
|
||||||
|
OrganizationLocationInfo(**addr)
|
||||||
|
)
|
||||||
|
|
||||||
|
OrganizationLocationInfo.objects.bulk_create(*address_obj_list)
|
||||||
|
|
||||||
if 'user_relations' in request.data.keys():
|
if 'user_relations' in request.data.keys():
|
||||||
user_relations = CustomOperations().custom_create( # create user relations
|
user_relations = CustomOperations().custom_create( # create user relations
|
||||||
request=request,
|
request=request,
|
||||||
@@ -315,6 +334,20 @@ class OrganizationViewSet(BaseViewSet, ModelViewSet, DynamicSearchMixin):
|
|||||||
serializer.is_valid(raise_exception=True)
|
serializer.is_valid(raise_exception=True)
|
||||||
organization = serializer.save()
|
organization = serializer.save()
|
||||||
|
|
||||||
|
if 'addresses' in request.data.keys():
|
||||||
|
# import multiple addresses with postal_code to orgs
|
||||||
|
|
||||||
|
organization.locations.delete() # remove ex locations
|
||||||
|
# create new locations
|
||||||
|
address_obj_list = []
|
||||||
|
for addr in request.data['addresses']:
|
||||||
|
addr.update({'org': organization.id})
|
||||||
|
address_obj_list.append(
|
||||||
|
OrganizationLocationInfo(**addr)
|
||||||
|
)
|
||||||
|
|
||||||
|
OrganizationLocationInfo.objects.bulk_create(*address_obj_list)
|
||||||
|
|
||||||
if 'user_relations' in request.data.keys():
|
if 'user_relations' in request.data.keys():
|
||||||
user_relations = CustomOperations().custom_update( # update user relations
|
user_relations = CustomOperations().custom_update( # update user relations
|
||||||
request=request,
|
request=request,
|
||||||
|
|||||||
@@ -363,6 +363,13 @@ class OrganizationSerializer(serializers.ModelSerializer):
|
|||||||
'name': city.name,
|
'name': city.name,
|
||||||
} for city in instance.service_area.all()
|
} for city in instance.service_area.all()
|
||||||
]
|
]
|
||||||
|
|
||||||
|
representation['addresses'] = [
|
||||||
|
{
|
||||||
|
"postal_code": addr.postal_code,
|
||||||
|
"address": addr.address
|
||||||
|
} for addr in instance.locations.all()
|
||||||
|
]
|
||||||
return representation
|
return representation
|
||||||
|
|
||||||
def update(self, instance, validated_data):
|
def update(self, instance, validated_data):
|
||||||
|
|||||||
@@ -167,6 +167,23 @@ class Organization(BaseModel):
|
|||||||
super(Organization, self).save(*args, **kwargs)
|
super(Organization, self).save(*args, **kwargs)
|
||||||
|
|
||||||
|
|
||||||
|
class OrganizationLocationInfo(BaseModel):
|
||||||
|
org = models.ForeignKey(
|
||||||
|
Organization,
|
||||||
|
on_delete=models.CASCADE,
|
||||||
|
related_name='locations',
|
||||||
|
null=True
|
||||||
|
)
|
||||||
|
postal_code = models.CharField(max_length=150, null=True, blank=True)
|
||||||
|
address = models.TextField(max_length=2000, null=True, blank=True)
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return f'{self.org.name}-{self.postal_code}-{self.address}'
|
||||||
|
|
||||||
|
def save(self, *args, **kwargs):
|
||||||
|
super(OrganizationLocationInfo, self).save(*args, **kwargs)
|
||||||
|
|
||||||
|
|
||||||
class OrganizationStats(BaseModel):
|
class OrganizationStats(BaseModel):
|
||||||
organization = models.OneToOneField(
|
organization = models.OneToOneField(
|
||||||
Organization,
|
Organization,
|
||||||
|
|||||||
Reference in New Issue
Block a user