1-
21from django .contrib .auth import get_user_model
32from django .core .exceptions import ValidationError as ModelValidationError
43from profanity_check import predict
@@ -126,6 +125,7 @@ def get_longitude(self, obj):
126125 return float (approx_lon )
127126 return None
128127
128+
129129# Unified serializer for all listing types (Items and Sublets); used for CRUD operations
130130class ListingSerializer (ListingTypeMixin , ModelSerializer ):
131131 LISTING_TYPE_CONFIG = {
@@ -176,6 +176,7 @@ class Meta:
176176 "negotiable" ,
177177 "created_at" ,
178178 "expires_at" ,
179+ "status" ,
179180 "images" ,
180181 "listing_type" ,
181182 "additional_data" ,
@@ -188,6 +189,7 @@ class Meta:
188189 "buyers" ,
189190 "images" ,
190191 "favorites" ,
192+ "status" ,
191193 ]
192194
193195 def validate (self , attrs ):
@@ -254,12 +256,19 @@ def create(self, validated_data):
254256 raise ValidationError ({"listing_type" : f"Must be one of: { valid_types } " })
255257
256258 try :
257- return create_method (validated_data , additional_data )
259+ instance = create_method (validated_data , additional_data )
258260 except ModelValidationError as e :
259261 raise ValidationError (
260262 e .message_dict if hasattr (e , "message_dict" ) else e .messages
261263 ) from e
262264
265+ from django .db import transaction
266+
267+ from market .tasks import moderate_listing_task
268+
269+ transaction .on_commit (lambda : moderate_listing_task .delay (instance .id ))
270+ return instance
271+
263272 def _create_item (self , validated_data , additional_data ):
264273 category_name = additional_data .get ("category" )
265274 category = Category .objects .filter (name = category_name ).first ()
@@ -291,7 +300,6 @@ def _create_sublet(self, validated_data, additional_data):
291300 latitude = additional_data .get ("latitude" )
292301 longitude = additional_data .get ("longitude" )
293302
294-
295303 if latitude is not None :
296304 latitude = float (latitude )
297305 if longitude is not None :
@@ -314,6 +322,8 @@ def _create_sublet(self, validated_data, additional_data):
314322 return sublet
315323
316324 def update (self , instance , validated_data ):
325+ old_title = instance .title
326+ old_description = instance .description
317327 listing_type = self .initial_data .get ("listing_type" )
318328 additional_data = self .initial_data .get ("additional_data" , {})
319329
@@ -336,6 +346,20 @@ def update(self, instance, validated_data):
336346 self ._update_sublet (instance , additional_data )
337347
338348 instance .save ()
349+
350+ # TODO: needs to also validate images when implemented
351+ content_changed = (
352+ instance .title != old_title or instance .description != old_description
353+ )
354+ if content_changed :
355+ from django .db import transaction
356+
357+ from market .tasks import moderate_listing_task
358+
359+ instance .status = Listing .Status .PENDING
360+ instance .save (update_fields = ["status" ])
361+ transaction .on_commit (lambda : moderate_listing_task .delay (instance .id ))
362+
339363 return instance
340364
341365 except ModelValidationError as e :
@@ -395,6 +419,7 @@ class Meta:
395419 "price" ,
396420 "negotiable" ,
397421 "expires_at" ,
422+ "status" ,
398423 "images" ,
399424 "favorite_count" ,
400425 "listing_type" ,
@@ -434,6 +459,7 @@ class Meta:
434459 "title" ,
435460 "price" ,
436461 "expires_at" ,
462+ "status" ,
437463 "images" ,
438464 "favorite_count" ,
439465 "listing_type" ,
0 commit comments