class PrivateMessageSerializer(ModelSerializer): from_user = UserProfileSerializer(read_only=True) user = UserProfileSerializer(read_only=True) class Meta: model = PrivateMessage fields = '__all__'
class EventSerializer(ModelSerializer): id = serializers.ReadOnlyField() creator = UserProfileSerializer(read_only=True) categories = CategorySerializer(read_only=True, many=True) members = UserProfileSerializer(read_only=True, many=True) geo_point = GeoPointSerializer() class Meta: model = Event fields = ('id', 'creator', 'created', 'description', 'categories', 'members', 'geo_point', 'date', 'time') def create(self, validated_data): geo_point_validated = validated_data.pop('geo_point') geo_point = GeoPoint.objects.create(**geo_point_validated) event = Event.objects.create(geo_point=geo_point, **validated_data) return event def update(self, instance, validated_data): instance.description = validated_data.pop('description') instance.date = validated_data.pop('date') instance.time = validated_data.get('time', instance.time) instance.save() geo_point = instance.geo_point geo_point.latitude = validated_data.get('geo_point').get('latitude') geo_point.longitude = validated_data.get('geo_point').get('longitude') geo_point.address = validated_data.get('geo_point').get('address') geo_point.save() return instance
class ComplaintSerializer(ModelSerializer): suspected = UserProfileSerializer(read_only=True) supplier = UserProfileSerializer(read_only=True) class Meta: model = Complaint fields = '__all__'
class PrivateMessageSerializer(ModelSerializer): from_user = UserProfileSerializer(read_only=True) user = UserProfileSerializer(read_only=True) ticket = serializers.IntegerField(source='ticket.id', read_only=True) class Meta: model = PrivateMessage fields = '__all__'
class RequestSerializer(ModelSerializer): to_user = UserProfileSerializer(read_only=True) event = serializers.IntegerField(source='event.id', read_only=True) title = serializers.CharField(source='event.description', read_only=True) from_user = UserProfileSerializer(read_only=True) class Meta: model = Request fields = '__all__' extra_kwargs = {'decision': {'required': False}}
def send_event_response_request(event_request): fields = ('id', 'first_name', 'last_name', 'photo') from_user = UserProfileSerializer( UserProfile.objects.get(email=event_request.from_user), fields=fields).data to_user = UserProfileSerializer( UserProfile.objects.get(email=event_request.to_user), fields=fields).data _send_realtime_event_to_user(to_user_ids=[event_request.from_user.id], realtime_event=RequestEvent( id=event_request.id, event=event_request.event.id, title=event_request.event.description, from_user=from_user, to_user=to_user, decision=event_request.decision, created=event_request.created, ))
class ChatSerializer(serializers.Serializer): content_type = serializers.CharField() content_id = serializers.IntegerField() title = serializers.CharField() from_user = UserProfileSerializer(read_only=True) last_message = serializers.CharField() last_message_id = serializers.IntegerField() last_seen_message_id = serializers.IntegerField() last_message_created = serializers.CharField() last_message_from_user_name = serializers.CharField()
class EventSerializer(ModelSerializer): id = serializers.ReadOnlyField() creator = UserProfileSerializer(read_only=True) categories = SubCategorySerializer(many=True, read_only=True, required=False) members = UserProfileSerializer(read_only=True, many=True) geo_point = GeoPointSerializer() class Meta: model = Event fields = '__all__' def create(self, validated_data): geo_point_validated = validated_data.pop('geo_point') geo_point = GeoPoint.objects.create(**geo_point_validated) categories = validated_data.pop('categories') event = Event.objects.create(geo_point=geo_point, **validated_data) if categories is not None: set_event_categories(categories, event) return event def update(self, instance, validated_data): instance.description = validated_data.get('description', instance.description) instance.date = validated_data.get('date', instance.date) instance.time = validated_data.get('time', instance.time) instance.ended = validated_data.get('ended', instance.ended) instance.save() if validated_data.get('categories'): set_event_categories(validated_data.get('categories'), instance) if validated_data.get('geo_point'): set_geo_point(instance.geo_point, validated_data) return instance
class MessageSerializer(ModelSerializer): from_user = UserProfileSerializer(read_only=True) chat = EventSerializer(read_only=True) class Meta: model = Message fields = '__all__' def to_representation(self, obj): message = super(MessageSerializer, self).to_representation(obj) message.pop('event') return message
def send_message(message, members_ids, chat): fields = ('id', 'first_name', 'last_name', 'photo') from_user = UserProfileSerializer( UserProfile.objects.get(email=message.from_user), fields=fields).data _send_realtime_event_to_user(to_user_ids=members_ids, realtime_event=MessageEvent( from_user=from_user, chat=chat, id=message.id, text=message.text, created=message.created, event=message.event.id, is_systemic=message.is_systemic, ))
class LastSeenMessageSerializer(ModelSerializer): user = UserProfileSerializer(read_only=True) message = MessageSerializer(read_only=True) def create(self, validated_data): db_row = LastSeenMessage.objects.filter( chat_id=validated_data.get('chat_id'), user=validated_data.get('user')) if db_row: db_row.update(message_id=validated_data.get('message_id')) last_seen_message = db_row[0] else: last_seen_message = LastSeenMessage.objects.create( **validated_data) return last_seen_message class Meta: model = LastSeenMessage fields = '__all__'
class TicketSerializer(ModelSerializer): id = serializers.ReadOnlyField() creator = UserProfileSerializer(read_only=True) categories = SubCategorySerializer(many=True, read_only=True, required=False) geo_point = GeoPointSerializer() class Meta: model = Ticket fields = '__all__' def create(self, validated_data): geo_point_validated = validated_data.pop('geo_point') geo_point = GeoPoint.objects.create(**geo_point_validated) categories = validated_data.pop('categories') ticket = Ticket.objects.create(geo_point=geo_point, **validated_data) if categories is not None: set_ticket_categories(categories, ticket) return ticket def update(self, instance, validated_data): # instance.description = validated_data.pop('description') instance.price = validated_data.get('price', instance.price) instance.name = validated_data.get('name', instance.name) instance.date = validated_data.pop('date') instance.time = validated_data.get('time', instance.time) instance.save() categories = validated_data.pop('categories') if categories is not None: set_ticket_categories(categories, instance) set_geo_point(instance.geo_point, validated_data) return instance