class TranslationSerializer(TranslatableModelSerializer): translations = TranslatedFieldsField(shared_model=Translation, read_only=False) class Meta: model = Translation fields = ('id', 'title', 'translations')
class ShippingMethodSerializer(ServiceBaseSerializer): translations = TranslatedFieldsField(shared_model=ShippingMethod, required=False) class Meta: model = ShippingMethod fields = ("__all__")
class RatingCategorySerializer(TranslatableModelSerializer): url = serializers.HyperlinkedIdentityField(view_name='category-rud') translations = TranslatedFieldsField(shared_model=RatingCategory) class Meta: model = RatingCategory fields = ('id', 'url', 'identifier', 'counts_for_average', 'translations')
class SupportSerializers(TranslatableModelSerializer): industry = StringRelatedField(many=True) type_project = StringRelatedField(many=True) translations = TranslatedFieldsField(shared_model=SupportTranslate) class Meta: model = SupportTranslate fields = '__all__'
class ProjectSerializer(TranslatableModelSerializer): industry = StringRelatedField() translations = TranslatedFieldsField(shared_model=ProjectTranslate) class Meta: model = ProjectTranslate fields = [ 'image', 'industry', 'sum', 'start', 'finish', 'id', 'help', 'translations' ]
class CountryAutoSharedModelTranslatedSerializer(TranslatableModelSerializer): """ A serializer with both translated fields and translation model deduced. """ translations = TranslatedFieldsField() class Meta: model = Country fields = ('pk', 'country_code', 'translations')
class CountryTranslatedSerializer(TranslatableModelSerializer): """ A serializer with translated fields deduced and translation model explicitly declared. """ translations = TranslatedFieldsField(shared_model=Country) class Meta: model = Country fields = ('pk', 'country_code', 'translations')
class ChoiceSerializer(TranslatableModelSerializer): _uid = serializers.UUIDField(label='ID', read_only=True) translations = TranslatedFieldsField(shared_model=Choice) class Meta: model = Choice fields = ( '_uid', 'question', 'is_correct', 'translations', )
class PostSerializer(TranslatableModelSerializer): translations = TranslatedFieldsField(shared_model=Post) # translations = PostTranslationSerializer(many=True,) class Meta: model = Post fields = ( 'id', 'slug', 'title', 'body', 'translations', 'language_code', )
class SmartPhoneModelSerializer(TranslatableModelSerializerMixin, ProductSerializer): multilingual = TranslatedFieldsField() operating_system = ValueRelatedField(model=OperatingSystem) variants = SmartphoneVariantSerializer(many=True) class Meta(ProductSerializer.Meta): model = SmartPhoneModel def create(self, validated_data): variants = validated_data.pop('variants') product = super(SmartPhoneModelSerializer, self).create(validated_data) for variant in variants: SmartPhoneVariant.objects.create(product=product, **variant) return product
class CountryExplicitTranslatedSerializer(TranslatableModelSerializer): """ A serializer with explicit translation serializer. """ class CountryTranslatedFieldsSerializer(serializers.ModelSerializer): class Meta: model = Country._parler_meta.root_model fields = ("name", ) # Skip url for the hell of it trans = TranslatedFieldsField( serializer_class=CountryTranslatedFieldsSerializer, source="translations") class Meta: model = Country fields = ('pk', 'country_code', 'trans')
class QuestionSerializer(TranslatableModelSerializer): _uid = serializers.UUIDField(label='ID', read_only=True) translations = TranslatedFieldsField(shared_model=Question) text = serializers.SerializerMethodField() result = serializers.SerializerMethodField(read_only=True) url = serializers.HyperlinkedIdentityField( read_only=True, view_name='assessment-api:retrieve_question', lookup_field='pk', lookup_url_kwarg='uuid', ) class Meta: model = Question fields = ( '_uid', 'survey', 'result', 'of_type', 'translations', 'url', 'text', ) def get_result(self, obj): try: user = self.context.get('request').user except Exception: return None if user.id is None: return None try: result = Result.objects.get(survey=obj.survey, user=user) except Result.DoesNotExist: return None else: return str(result.pk) def get_text(self, obj): try: return obj.question except ObjectDoesNotExist: return _("Translation does not exist for your current language.")
class TranslatedModelSerializer(TranslatableModelSerializer): translations = TranslatedFieldsField() def to_representation(self, obj): ret = super(TranslatedModelSerializer, self).to_representation(obj) if obj is None: return ret return self.translated_fields_to_representation(obj, ret) def translated_fields_to_representation(self, obj, ret): translated_fields = {} for lang_key, trans_dict in ret.pop("translations", {}).items(): for field_name, translation in trans_dict.items(): if field_name not in translated_fields: translated_fields[field_name] = {lang_key: translation} else: translated_fields[field_name].update({lang_key: translation}) ret.update(translated_fields) return ret
class SmartCardSerializer(TranslatableModelSerializerMixin, ProductSerializer): multilingual = TranslatedFieldsField() class Meta(ProductSerializer.Meta): model = SmartCard
class ContactSerializer(TranslatableModelSerializer): translations = TranslatedFieldsField(shared_model=ContactTranslate) class Meta: model = ContactTranslate fields = '__all__'
class CountryTranslatedSerializer(TranslatableModelSerializer): translations = TranslatedFieldsField(shared_model=Country) class Meta: model = Country fields = ('pk', 'country_code', 'translations')
class NewsSerializers(TranslatableModelSerializer): translations = TranslatedFieldsField(shared_model=NewsTranslate) class Meta: model = NewsTranslate fields = '__all__'
class SurveySerializer(TranslatableModelSerializer): _uid = serializers.UUIDField(label='ID', read_only=True) translations = TranslatedFieldsField(shared_model=Survey) questions = QuestionSerializer(many=True, read_only=True) is_admin = serializers.SerializerMethodField() in_users = serializers.SerializerMethodField() result = serializers.SerializerMethodField(read_only=True) name = serializers.SerializerMethodField(read_only=True) description = serializers.SerializerMethodField(read_only=True) class Meta: model = Survey fields = ( '_uid', 'is_active', 'is_private', 'start_date_time', 'end_date_time', 'translations', 'questions', 'is_admin', 'in_users', 'result', 'name', 'description', ) def get_result(self, obj): """ When an authenticated user request survey details, it should include if the survey has been started, completed, etc. """ try: user = self.context.get('request').user except Exception: return {"status": "error", "result_id": None} if user.id is None: return {"status": "anonymous", "result_id": None} try: result = Result.objects.get(survey=obj, user=user) except Result.DoesNotExist: return {"status": "unstarted", "result_id": None} if result.answers.count() == obj.questions.count(): return {"status": "complete", "result_id": str(result.pk)} else: return {"status": "incomplete", "result_id": str(result.pk)} def get_is_admin(self, obj): """ The application should never directly report who the admin is. This method reports if the current authenticated user is the admin. """ try: user = self.context.get('request').user except Exception: # raise serializers.ValidationError('Could not access request.user') return False if user == obj.admin: return True else: return False def get_in_users(self, obj): """ The application should never directly report the entire user group. This method reports if the current authenticated user is users. """ try: user = self.context.get('request').user except Exception: # raise serializers.ValidationError('Could not access request.user') return False if user in obj.users.all(): return True else: return False def get_name(self, obj): try: return obj.name except ObjectDoesNotExist: return str(obj.pk) def get_description(self, obj): try: return obj.description except ObjectDoesNotExist: return str(obj.pk)