class SearcherNotificationSerializer(serializers.ModelSerializer): offer = OfferSerializer() discount = DiscountSerializer() class Meta: model = SearcherNotification exclude = ['searcher']
class EventSerializer(serializers.ModelSerializer): name = serializers.CharField(max_length=50) city = serializers.CharField(max_length=50) venue = serializers.CharField(max_length=50) genre = serializers.CharField(max_length=20) date = serializers.DateTimeField() description = serializers.CharField(max_length=255) offers = OfferSerializer(many=True, required=False) class Meta: model = Event fields = ('name', 'city', 'venue', 'genre', 'date', 'description', 'offers') def create(self, validated_data): return Event.objects.create(**validated_data) def update(self, instance, validated_data): instance.name = validated_data.get('name', instance.name) instance.city = validated_data.get('city', instance.city) instance.venue = validated_data.get('venue', instance.venue) instance.genre = validated_data.get('genre', instance.genre) instance.date = validated_data.get('date', instance.date) instance.description = validated_data.get('description', instance.description) instance.save() return instance
class AdvertiserNotificationSerializer(serializers.ModelSerializer): offer = OfferSerializer() discount = DiscountSerializer() story = StorySerializer() class Meta: model = AdvertiserNotification exclude = ['advertiser']
class ConversationSerializer(serializers.HyperlinkedModelSerializer): """ Conversation seralizer. Used to obtain basic conversation fields. """ interlocutor_1 = UserSerializer(read_only=True) interlocutor_2 = UserSerializer(read_only=True) offer = OfferSerializer(read_only=True) class Meta: model = Conversation fields = [ 'id', 'timestamp', 'interlocutor_1', 'interlocutor_2', 'offer', 'messages' ] depth = 1
def save(self, offers_data: dict, client_instance: object): instance_offer_history = HistoryOfferModel.objects.create( client=client_instance) for offers in offers_data: serial = OfferSerializer(data=offers) try: serial.is_valid() serial.save(history_offers=instance_offer_history) except Exception as e: logging.error(f'In OffersListView view: {e}'.encode('utf-8'))
def search_offers_by_client(self, client_data, client_modifiable=True): self.query_db_client = ClientService.filter_client( self, client_data=client_data) if self.query_db_client.exists(): self.query_db = self.filter_history_offer(client_data, True) try: if self.query_db.exists(): serial = OfferSerializer(self.query_db, many=True) self.serialized = serial.data return self else: return None except Exception as e: return None else: return None
def search_offers(self, client_data, client_modifiable=True): self.instance_client = ClientService.instance_client( self, client=client_data) self.query_db = self.filter_history_offer(client_data, False) if self.query_db.exists(): serial = OfferSerializer(self.query_db, many=True) self.serialized = serial.data return self else: new_offers_list = requests.post( 'https://fb254f08-fd54-4731-b4d7-90149503e6c5.mock.pstmn.io/offers', json=client_data) new_offers_list_decoded = json.loads(new_offers_list.text) offers = new_offers_list_decoded['offers'] self.save(offers_data=offers, client_instance=self.instance_client) return self.search_offers(client_data=client_data, client_modifiable=False)
class OrderSerializer(serializers.ModelSerializer): products = TransactionSerializer(many=True) delivery_date = serializers.DateTimeField(source='offer.delivery_date') total = serializers.SerializerMethodField() offer = OfferSerializer(read_only=True) class Meta: model = Order fields = ( 'id', 'user_id', 'offer_id', 'order_date', 'products', 'delivery_date', 'total', 'offer', ) @staticmethod def get_total(obj): return obj.total