|
| 1 | +import django.contrib.auth.password_validation |
1 | 2 | import pycountry
|
2 | 3 | import rest_framework.exceptions
|
3 | 4 | import rest_framework.serializers
|
4 | 5 |
|
5 | 6 | import business.constants
|
6 | 7 | import business.models
|
| 8 | +import user.constants |
| 9 | +import user.models |
7 | 10 |
|
8 | 11 |
|
9 | 12 | class BaseLimitOffsetPaginationSerializer(
|
@@ -96,7 +99,7 @@ def validate(self, data):
|
96 | 99 | return data
|
97 | 100 |
|
98 | 101 |
|
99 |
| -class BasePromoSerializer(rest_framework.serializers.ModelSerializer): |
| 102 | +class BaseCompanyPromoSerializer(rest_framework.serializers.ModelSerializer): |
100 | 103 | """
|
101 | 104 | Base serializer for promo, containing validation and representation logic.
|
102 | 105 | """
|
@@ -278,3 +281,140 @@ def to_representation(self, instance):
|
278 | 281 | data.pop('promo_unique', None)
|
279 | 282 |
|
280 | 283 | return data
|
| 284 | + |
| 285 | + |
| 286 | +class OtherFieldSerializer(rest_framework.serializers.Serializer): |
| 287 | + age = rest_framework.serializers.IntegerField( |
| 288 | + required=True, |
| 289 | + min_value=user.constants.AGE_MIN, |
| 290 | + max_value=user.constants.AGE_MAX, |
| 291 | + ) |
| 292 | + country = CountryField(required=True) |
| 293 | + |
| 294 | + |
| 295 | +class BaseUserSerializer(rest_framework.serializers.ModelSerializer): |
| 296 | + password = rest_framework.serializers.CharField( |
| 297 | + write_only=True, |
| 298 | + required=True, |
| 299 | + validators=[django.contrib.auth.password_validation.validate_password], |
| 300 | + max_length=user.constants.PASSWORD_MAX_LENGTH, |
| 301 | + min_length=user.constants.PASSWORD_MIN_LENGTH, |
| 302 | + style={'input_type': 'password'}, |
| 303 | + ) |
| 304 | + name = rest_framework.serializers.CharField( |
| 305 | + required=True, |
| 306 | + min_length=user.constants.NAME_MIN_LENGTH, |
| 307 | + max_length=user.constants.NAME_MAX_LENGTH, |
| 308 | + ) |
| 309 | + surname = rest_framework.serializers.CharField( |
| 310 | + required=True, |
| 311 | + min_length=user.constants.SURNAME_MIN_LENGTH, |
| 312 | + max_length=user.constants.SURNAME_MAX_LENGTH, |
| 313 | + ) |
| 314 | + email = rest_framework.serializers.EmailField( |
| 315 | + required=True, |
| 316 | + min_length=user.constants.EMAIL_MIN_LENGTH, |
| 317 | + max_length=user.constants.EMAIL_MAX_LENGTH, |
| 318 | + ) |
| 319 | + avatar_url = rest_framework.serializers.URLField( |
| 320 | + required=False, |
| 321 | + max_length=user.constants.AVATAR_URL_MAX_LENGTH, |
| 322 | + allow_null=True, |
| 323 | + ) |
| 324 | + other = OtherFieldSerializer(required=True) |
| 325 | + |
| 326 | + class Meta: |
| 327 | + model = user.models.User |
| 328 | + fields = ( |
| 329 | + 'name', |
| 330 | + 'surname', |
| 331 | + 'email', |
| 332 | + 'password', |
| 333 | + 'avatar_url', |
| 334 | + 'other', |
| 335 | + ) |
| 336 | + |
| 337 | + |
| 338 | +class BaseUserPromoSerializer(rest_framework.serializers.ModelSerializer): |
| 339 | + """ |
| 340 | + Base serializer for promos, containing common fields and methods. |
| 341 | + """ |
| 342 | + |
| 343 | + promo_id = rest_framework.serializers.UUIDField(source='id') |
| 344 | + company_id = rest_framework.serializers.UUIDField(source='company.id') |
| 345 | + company_name = rest_framework.serializers.CharField(source='company.name') |
| 346 | + active = rest_framework.serializers.BooleanField(source='is_active') |
| 347 | + like_count = rest_framework.serializers.IntegerField( |
| 348 | + source='get_like_count', |
| 349 | + ) |
| 350 | + comment_count = rest_framework.serializers.IntegerField( |
| 351 | + source='get_comment_count', |
| 352 | + ) |
| 353 | + |
| 354 | + is_liked_by_user = rest_framework.serializers.SerializerMethodField() |
| 355 | + is_activated_by_user = rest_framework.serializers.SerializerMethodField() |
| 356 | + |
| 357 | + class Meta: |
| 358 | + model = business.models.Promo |
| 359 | + fields = ( |
| 360 | + 'promo_id', |
| 361 | + 'company_id', |
| 362 | + 'company_name', |
| 363 | + 'description', |
| 364 | + 'image_url', |
| 365 | + 'active', |
| 366 | + 'is_activated_by_user', |
| 367 | + 'like_count', |
| 368 | + 'comment_count', |
| 369 | + 'is_liked_by_user', |
| 370 | + ) |
| 371 | + read_only_fields = fields |
| 372 | + |
| 373 | + def get_is_liked_by_user(self, obj: business.models.Promo) -> bool: |
| 374 | + """ |
| 375 | + Checks whether the current user has liked this promo. |
| 376 | + """ |
| 377 | + request = self.context.get('request') |
| 378 | + if ( |
| 379 | + request |
| 380 | + and hasattr(request, 'user') |
| 381 | + and request.user.is_authenticated |
| 382 | + ): |
| 383 | + return user.models.PromoLike.objects.filter( |
| 384 | + promo=obj, |
| 385 | + user=request.user, |
| 386 | + ).exists() |
| 387 | + return False |
| 388 | + |
| 389 | + def get_is_activated_by_user(self, obj: business.models.Promo) -> bool: |
| 390 | + """ |
| 391 | + Checks whether the current user has activated this promo code. |
| 392 | + """ |
| 393 | + request = self.context.get('request') |
| 394 | + |
| 395 | + if not ( |
| 396 | + request |
| 397 | + and hasattr(request, 'user') |
| 398 | + and request.user.is_authenticated |
| 399 | + ): |
| 400 | + return False |
| 401 | + |
| 402 | + return user.models.PromoActivationHistory.objects.filter( |
| 403 | + promo=obj, |
| 404 | + user=request.user, |
| 405 | + ).exists() |
| 406 | + |
| 407 | + |
| 408 | +class BaseCommentSerializer(rest_framework.serializers.ModelSerializer): |
| 409 | + """ |
| 410 | + Base serializer for promo comments. |
| 411 | + """ |
| 412 | + |
| 413 | + text = rest_framework.serializers.CharField( |
| 414 | + min_length=user.constants.COMMENT_TEXT_MIN_LENGTH, |
| 415 | + max_length=user.constants.COMMENT_TEXT_MAX_LENGTH, |
| 416 | + ) |
| 417 | + |
| 418 | + class Meta: |
| 419 | + model = user.models.PromoComment |
| 420 | + fields = ('text',) |
0 commit comments