class IsOpenNowSerializer(serializers.Serializer): is_open = serializers.BooleanField() resource_timezone = TimeZoneSerializerField(required=False) resource_time_now = serializers.DateTimeField() matching_opening_hours = TimeElementSerializer(many=True) other_timezone = TimeZoneSerializerField(required=False) other_timezone_time_now = TimezoneRetainingDateTimeField(required=False) matching_opening_hours_in_other_tz = TimeElementSerializer(many=True, required=False) resource = ResourceSerializer()
class SiteSerializer(PrimaryModelSerializer): url = serializers.HyperlinkedIdentityField(view_name='dcim-api:site-detail') status = ChoiceField(choices=SiteStatusChoices, required=False) region = NestedRegionSerializer(required=False, allow_null=True) group = NestedSiteGroupSerializer(required=False, allow_null=True) tenant = NestedTenantSerializer(required=False, allow_null=True) time_zone = TimeZoneSerializerField(required=False) asns = SerializedPKRelatedField( queryset=ASN.objects.all(), serializer=NestedASNSerializer, required=False, many=True ) # Related object counts circuit_count = serializers.IntegerField(read_only=True) device_count = serializers.IntegerField(read_only=True) prefix_count = serializers.IntegerField(read_only=True) rack_count = serializers.IntegerField(read_only=True) virtualmachine_count = serializers.IntegerField(read_only=True) vlan_count = serializers.IntegerField(read_only=True) class Meta: model = Site fields = [ 'id', 'url', 'display', 'name', 'slug', 'status', 'region', 'group', 'tenant', 'facility', 'asn', 'asns', 'time_zone', 'description', 'physical_address', 'shipping_address', 'latitude', 'longitude', 'contact_name', 'contact_phone', 'contact_email', 'comments', 'tags', 'custom_fields', 'created', 'last_updated', 'circuit_count', 'device_count', 'prefix_count', 'rack_count', 'virtualmachine_count', 'vlan_count', ]
class LeaveSerializer(serializers.ModelSerializer): timezone = TimeZoneSerializerField() class Meta: model = Leave fields = [ 'id', 'name', 'desc', 'location', 'type', 'country', 'laws', 'startdate', 'starttime', 'enddate', 'endtime', 'rounds', 'user', 'status', 'is_approved', 'timezone', 'created', 'updated', ]
class ClientSerializer(CustomModelSerializer): """Serialize :model:`rolodex:Client` entries.""" short_name = SerializerMethodField("get_short_name") address = SerializerMethodField("get_address") contacts = ClientContactSerializer( source="clientcontact_set", many=True, exclude=[ "client", ], ) timezone = TimeZoneSerializerField() class Meta: model = Client fields = "__all__" def get_short_name(self, obj): if obj.short_name: return obj.short_name return obj.name def get_address(self, obj): return strip_html(obj.address)
class UserSerializer(serializers.ModelSerializer): timezone = TimeZoneSerializerField(required=False, read_only=True) class Meta: model = User fields = [ 'id', 'email', 'first_name', 'last_name', 'middle_name', 'timezone' ]
class ClientContactSerializer(CustomModelSerializer): """Serialize :model:`rolodex:ClientContact` entries.""" timezone = TimeZoneSerializerField() class Meta: model = ClientContact fields = "__all__"
class ResourceSerializer( TranslationSerializerMixin, EnumSupportSerializerMixin, WritableNestedModelSerializer, ): last_modified_by = UserSerializer(read_only=True) origins = ResourceOriginSerializer(many=True, required=False, allow_null=True) timezone = TimeZoneSerializerField(required=False) class Meta: model = Resource fields = [ "id", "name", "description", "address", "resource_type", "children", "parents", "organization", "origins", "last_modified_by", "extra_data", "is_public", "timezone", ] read_only_fields = ["last_modified_by"] extra_kwargs = {"parents": {"required": False}} def validate(self, attrs): """Validate that the user is a member or admin of at least one of the immediate parent resources organizations""" result = super().validate(attrs) if not self.context.get("request"): return result user = self.context["request"].user if not user.is_superuser and result.get("parents"): users_organizations = user.get_all_organizations() if not any([ parent.organization in users_organizations for parent in result.get("parents") ]): raise ValidationError(detail=_( "Cannot create or edit sub resources of a resource " "in an organisation the user is not part of ")) return result
class UserSerializer(CustomModelSerializer): """Serialize :model:`users:User` entries.""" name = SerializerMethodField("get_name") timezone = TimeZoneSerializerField() class Meta: model = User fields = ["id", "name", "username", "email", "phone", "timezone"] def get_name(self, obj): return obj.get_display_name()
class SiteSerializer(TaggedObjectSerializer, StatusModelSerializerMixin, CustomFieldModelSerializer): url = serializers.HyperlinkedIdentityField( view_name="dcim-api:site-detail") region = NestedRegionSerializer(required=False, allow_null=True) tenant = NestedTenantSerializer(required=False, allow_null=True) time_zone = TimeZoneSerializerField(required=False, allow_null=True) circuit_count = serializers.IntegerField(read_only=True) device_count = serializers.IntegerField(read_only=True) prefix_count = serializers.IntegerField(read_only=True) rack_count = serializers.IntegerField(read_only=True) virtualmachine_count = serializers.IntegerField(read_only=True) vlan_count = serializers.IntegerField(read_only=True) class Meta: model = Site fields = [ "id", "url", "name", "slug", "status", "region", "tenant", "facility", "asn", "time_zone", "description", "physical_address", "shipping_address", "latitude", "longitude", "contact_name", "contact_phone", "contact_email", "comments", "tags", "custom_fields", "created", "last_updated", "circuit_count", "device_count", "prefix_count", "rack_count", "virtualmachine_count", "vlan_count", "computed_fields", ] opt_in_fields = ["computed_fields"]
class ProjectSerializer(CustomModelSerializer): """Serialize :model:`rolodex:Project` entries.""" name = SerializerMethodField("get_name") type = serializers.CharField(source="project_type") start_date = SerializerMethodField("get_start_date") start_month = SerializerMethodField("get_start_month") start_day = SerializerMethodField("get_start_day") start_year = SerializerMethodField("get_start_year") end_date = SerializerMethodField("get_end_date") end_month = SerializerMethodField("get_end_month") end_day = SerializerMethodField("get_end_day") end_year = SerializerMethodField("get_end_year") timezone = TimeZoneSerializerField() class Meta: model = Project exclude = [ "project_type", ] def get_name(self, obj): return str(obj) def get_start_date(self, obj): return dateformat.format(obj.start_date, settings.DATE_FORMAT) def get_end_date(self, obj): return dateformat.format(obj.end_date, settings.DATE_FORMAT) def get_start_month(self, obj): return dateformat.format(obj.start_date, "E") def get_start_day(self, obj): return obj.start_date.day def get_start_year(self, obj): return obj.start_date.year def get_end_month(self, obj): return dateformat.format(obj.end_date, "E") def get_end_day(self, obj): return obj.end_date.day def get_end_year(self, obj): return obj.end_date.year
class ResourceSimpleSerializer(TranslationSerializerMixin, EnumSupportSerializerMixin, serializers.ModelSerializer): timezone = TimeZoneSerializerField() origins = ResourceOriginSerializer(many=True, required=False, allow_null=True) class Meta: model = Resource fields = [ "id", "name", "timezone", "origins", ]
class SiteSerializer(TaggedObjectSerializer, CustomFieldModelSerializer): url = serializers.HyperlinkedIdentityField( view_name='dcim-api:site-detail') status = ChoiceField(choices=SiteStatusChoices, required=False) region = NestedRegionSerializer(required=False, allow_null=True) tenant = NestedTenantSerializer(required=False, allow_null=True) time_zone = TimeZoneSerializerField(required=False) circuit_count = serializers.IntegerField(read_only=True) device_count = serializers.IntegerField(read_only=True) prefix_count = serializers.IntegerField(read_only=True) rack_count = serializers.IntegerField(read_only=True) virtualmachine_count = serializers.IntegerField(read_only=True) vlan_count = serializers.IntegerField(read_only=True) class Meta: model = Site fields = [ 'id', 'url', 'name', 'slug', 'status', 'region', 'tenant', 'facility', 'asn', 'time_zone', 'description', 'physical_address', 'shipping_address', 'latitude', 'longitude', 'contact_name', 'contact_phone', 'contact_email', 'comments', 'tags', 'custom_fields', 'created', 'last_updated', 'circuit_count', 'device_count', 'prefix_count', 'rack_count', 'virtualmachine_count', 'vlan_count', ]
class TimeZoneSerializer(serializers.Serializer): tz = TimeZoneSerializerField()
class ProfileUpdateSerializer(serializers.ModelSerializer): first_name = serializers.CharField(required=False, max_length=50) last_name = serializers.CharField(required=False, max_length=50) middle_name = serializers.CharField(max_length=50, read_only=False, required=False, allow_blank=True) dob = serializers.DateField(read_only=False, required=False, allow_null=True) gender = serializers.CharField(max_length=1, read_only=False, required=False, allow_blank=True) bio = serializers.CharField(read_only=False, max_length=1024, required=False, allow_blank=True) timezone = TimeZoneSerializerField(required=False) phone = serializers.CharField(required=False, allow_blank=True, allow_null=True) userpic = serializers.SerializerMethodField('get_userpic', default="") plan = serializers.SerializerMethodField("get_plan") work_email = serializers.EmailField(allow_null=True, allow_blank=True) use_work_email_incbookings = serializers.BooleanField() use_work_email_outbookings = serializers.BooleanField() show_work_email_in_contact_info = serializers.BooleanField() def get_plan(self, obj): try: plan = obj.user_plans.last() return plan.plan.code except Exception as e: return "DEFAULT" def get_userpic(self, obj): try: image_object = UserImage.objects.get( account=self.context['request'].user, is_deleted=False) return self.context['request'].build_absolute_uri( image_object.image.url) except Exception as e: return "" def get_documents_url(self, obj): try: return self.context.get('request').build_absolute_uri( reverse('userAccount:user-details')) + 'documents/' except Exception as e: return str(e) def get_billing_addresses_url(self, obj): try: return self.context['request'].build_absolute_uri( reverse('userAccount:user-details')) + 'billing_addresses/' except Exception as e: return str(e) def get_phones_url(self, obj): try: return self.context['request'].build_absolute_uri( reverse('userAccount:user-details')) + 'phones/' except Exception as e: return str(e) def get_emails_url(self, obj): try: return self.context['request'].build_absolute_uri( reverse('userAccount:user-details')) + 'emails/' except Exception as e: return str(e) def get_properties_url(self, obj): try: return self.context['request'].build_absolute_uri( reverse('userAccount:user-details')) + 'properties/' except Exception as e: return str(e) class Meta: fields = [ 'id', 'email', 'phone', 'userpic', 'first_name', 'last_name', 'middle_name', 'bio', 'timezone', 'plan', 'work_email', 'use_work_email_incbookings', 'use_work_email_outbookings', 'show_work_email_in_contact_info', 'email_confirmed', 'phone_confirmed', 'client_rating', 'is_banned', 'last_password_update', 'tos_version', 'two_factor_auth', 'is_staff', 'dob', 'gender', 'created_at', 'updated_at', # 'emails_url', ] model = User
class ProfileDetailSerializer(serializers.ModelSerializer): userpic = serializers.SerializerMethodField('get_userpic', default="") timezone = TimeZoneSerializerField() plan = serializers.SerializerMethodField("get_plan") work_email = serializers.EmailField() use_work_email_incbookings = serializers.BooleanField() use_work_email_outbookings = serializers.BooleanField() show_work_email_in_contact_info = serializers.BooleanField() def get_plan(self, obj): try: plan = obj.user_plans.last() return plan.plan.code except Exception as e: return "DEFAULT" def get_userpic(self, obj): try: image_object = UserImage.objects.get( account=self.context['request'].user, is_deleted=False) return self.context['request'].build_absolute_uri( image_object.image.url) except Exception as e: return "" def get_documents_url(self, obj): try: return self.context.get('request').build_absolute_uri( reverse('userAccount:user-details')) + 'documents/' except Exception as e: return str(e) def get_billing_addresses_url(self, obj): try: return self.context['request'].build_absolute_uri( reverse('userAccount:user-details')) + 'billing_addresses/' except Exception as e: return str(e) def get_phones_url(self, obj): try: return self.context['request'].build_absolute_uri( reverse('userAccount:user-details')) + 'phones/' except Exception as e: return str(e) def get_emails_url(self, obj): try: return self.context['request'].build_absolute_uri( reverse('userAccount:user-details')) + 'emails/' except Exception as e: return str(e) def get_properties_url(self, obj): try: return self.context['request'].build_absolute_uri( reverse('userAccount:user-details')) + 'properties/' except Exception as e: return str(e) def to_representation(self, data): representation = super(ProfileDetailSerializer, self).to_representation(data) """ if self.context['request'].GET.get('expand_all'): return representation if not self.context['request'].GET.get('expand_images'): representation.pop("account_images",) if not self.context['request'].GET.get('expand_docs'): representation.pop("documents",) if not self.context['request'].GET.get('expand_phones'): representation.pop("account_phones",) if not self.context['request'].GET.get('expand_addresses'): representation.pop("billing_addresses",) """ return representation class Meta: fields = [ 'id', 'email', 'phone', 'userpic', 'first_name', 'last_name', 'middle_name', 'bio', 'timezone', 'plan', 'work_email', 'use_work_email_incbookings', 'use_work_email_outbookings', 'show_work_email_in_contact_info', 'email_confirmed', 'phone_confirmed', 'client_rating', 'is_banned', 'last_password_update', 'tos_version', 'two_factor_auth', 'is_staff', 'dob', 'gender', 'created_at', 'updated_at', # 'emails_url', ] model = User