|
9 | 9 | import rest_framework_simplejwt.token_blacklist.models as tb_models |
10 | 10 | import rest_framework_simplejwt.tokens |
11 | 11 |
|
| 12 | +import business.constants |
12 | 13 | import business.models |
13 | 14 | import user.constants |
14 | 15 | import user.models |
@@ -247,6 +248,124 @@ def to_representation(self, instance): |
247 | 248 | return data |
248 | 249 |
|
249 | 250 |
|
| 251 | +class UserFeedQuerySerializer(rest_framework.serializers.Serializer): |
| 252 | + """ |
| 253 | + Serializer for validating query parameters of promo feed requests. |
| 254 | + """ |
| 255 | + |
| 256 | + limit = rest_framework.serializers.CharField( |
| 257 | + required=False, |
| 258 | + allow_blank=True, |
| 259 | + ) |
| 260 | + offset = rest_framework.serializers.CharField( |
| 261 | + required=False, |
| 262 | + allow_blank=True, |
| 263 | + ) |
| 264 | + category = rest_framework.serializers.CharField( |
| 265 | + min_length=business.constants.TARGET_CATEGORY_MIN_LENGTH, |
| 266 | + max_length=business.constants.TARGET_CATEGORY_MAX_LENGTH, |
| 267 | + required=False, |
| 268 | + allow_blank=True, |
| 269 | + ) |
| 270 | + active = rest_framework.serializers.BooleanField( |
| 271 | + required=False, |
| 272 | + allow_null=True, |
| 273 | + ) |
| 274 | + |
| 275 | + _allowed_params = None |
| 276 | + |
| 277 | + def get_allowed_params(self): |
| 278 | + if self._allowed_params is None: |
| 279 | + self._allowed_params = set(self.fields.keys()) |
| 280 | + return self._allowed_params |
| 281 | + |
| 282 | + def validate(self, attrs): |
| 283 | + query_params = self.initial_data |
| 284 | + allowed_params = self.get_allowed_params() |
| 285 | + |
| 286 | + unexpected_params = set(query_params.keys()) - allowed_params |
| 287 | + if unexpected_params: |
| 288 | + raise rest_framework.exceptions.ValidationError('Invalid params.') |
| 289 | + |
| 290 | + field_errors = {} |
| 291 | + |
| 292 | + attrs = self._validate_int_field('limit', attrs, field_errors) |
| 293 | + attrs = self._validate_int_field('offset', attrs, field_errors) |
| 294 | + |
| 295 | + if field_errors: |
| 296 | + raise rest_framework.exceptions.ValidationError(field_errors) |
| 297 | + |
| 298 | + return attrs |
| 299 | + |
| 300 | + def _validate_int_field(self, field_name, attrs, field_errors): |
| 301 | + value_str = self.initial_data.get(field_name) |
| 302 | + if value_str is None: |
| 303 | + return attrs |
| 304 | + |
| 305 | + if value_str == '': |
| 306 | + raise rest_framework.exceptions.ValidationError( |
| 307 | + f'Invalid {field_name} format.', |
| 308 | + ) |
| 309 | + |
| 310 | + try: |
| 311 | + value_int = int(value_str) |
| 312 | + if value_int < 0: |
| 313 | + raise rest_framework.exceptions.ValidationError( |
| 314 | + f'{field_name.capitalize()} cannot be negative.', |
| 315 | + ) |
| 316 | + attrs[field_name] = value_int |
| 317 | + except (ValueError, TypeError): |
| 318 | + raise rest_framework.exceptions.ValidationError( |
| 319 | + f'Invalid {field_name} format.', |
| 320 | + ) |
| 321 | + |
| 322 | + return attrs |
| 323 | + |
| 324 | + |
| 325 | +class PromoFeedSerializer(rest_framework.serializers.ModelSerializer): |
| 326 | + promo_id = rest_framework.serializers.UUIDField(source='id') |
| 327 | + company_id = rest_framework.serializers.UUIDField(source='company.id') |
| 328 | + company_name = rest_framework.serializers.CharField(source='company.name') |
| 329 | + active = rest_framework.serializers.BooleanField(source='is_active') |
| 330 | + is_activated_by_user = rest_framework.serializers.SerializerMethodField() |
| 331 | + is_liked_by_user = rest_framework.serializers.SerializerMethodField() |
| 332 | + like_count = rest_framework.serializers.SerializerMethodField() |
| 333 | + comment_count = rest_framework.serializers.SerializerMethodField() |
| 334 | + |
| 335 | + class Meta: |
| 336 | + model = business.models.Promo |
| 337 | + fields = [ |
| 338 | + 'promo_id', |
| 339 | + 'company_id', |
| 340 | + 'company_name', |
| 341 | + 'description', |
| 342 | + 'image_url', |
| 343 | + 'active', |
| 344 | + 'is_activated_by_user', |
| 345 | + 'like_count', |
| 346 | + 'is_liked_by_user', |
| 347 | + 'comment_count', |
| 348 | + ] |
| 349 | + |
| 350 | + read_only_fields = fields |
| 351 | + |
| 352 | + def get_is_activated_by_user(self, obj) -> bool: |
| 353 | + # TODO: |
| 354 | + return False |
| 355 | + |
| 356 | + def get_like_count(self, obj) -> int: |
| 357 | + # TODO: |
| 358 | + return 0 |
| 359 | + |
| 360 | + def get_is_liked_by_user(self, obj) -> bool: |
| 361 | + # TODO: |
| 362 | + return False |
| 363 | + |
| 364 | + def get_comment_count(self, obj) -> int: |
| 365 | + # TODO: |
| 366 | + return 0 |
| 367 | + |
| 368 | + |
250 | 369 | class UserPromoDetailSerializer(rest_framework.serializers.ModelSerializer): |
251 | 370 | """ |
252 | 371 | Serializer for detailed promo-code information |
|
0 commit comments