class ProjectSerializer(DynamicFieldsModelSerializer): typology = serializers.ReadOnlyField(source='get_typology') completed = serializers.ReadOnlyField(source='get_completed_perc') shared_companies = serializers.ReadOnlyField(source='get_shared_companies') task_companies = profile_serializers.CompanySerializer( source='get_shared_companies_qs', many=True) messages_count = serializers.ReadOnlyField(source='get_messages_count') days_for_gantt = serializers.SerializerMethodField( source='get_days_for_gantt') project_owner = profile_serializers.CompanySerializer( source='get_project_owner') company = profile_serializers.CompanySerializer() referent = profile_serializers.ProfileSerializer() profiles = profile_serializers.ProfileSerializer(many=True) creator = profile_serializers.UserSerializer() shared_project = ProjectGenericSerializer() class Meta: model = models.Project fields = '__all__' def get_field_names(self, *args, **kwargs): view = self.get_view if view: if 'month' in view.kwargs: self.month = view.kwargs['month'] if 'year' in view.kwargs: self.year = view.kwargs['year'] return view.project_response_include_fields return super(ProjectSerializer, self).get_field_names(*args, **kwargs) def get_days_for_gantt(self, obj): days = [] date_from, date_to = get_first_last_dates_of_month_and_year( self.month, self.year) start_date, end_date = (date_from, date_to) if obj.date_start.month == date_from.month: start_date = obj.date_start if obj.date_end.month == date_from.month: end_date = obj.date_end for dt in daterange(start_date, end_date): days.append(dt.day) return days
class MessageSerializer(DynamicFieldsModelSerializer): talk = TalkSerializer() sender = profile_serializers.ProfileSerializer() class Meta: model = models.Message fields = '__all__' def get_field_names(self, *args, **kwargs): view = self.get_view if view: return view.message_response_include_fields return super(MessageSerializer, self).get_field_names(*args, **kwargs)
class CertificationSerializer(DynamicFieldsModelSerializer): contact = profile_serializers.ProfileSerializer() owner = profile_serializers.CompanySerializer() class Meta: model = models.Certification fields = '__all__' def get_field_names(self, *args, **kwargs): view = self.get_view if view: return view.certification_response_include_fields return super(CertificationSerializer, self).get_field_names(*args, **kwargs)
class TeamSerializer(DynamicFieldsModelSerializer): role = serializers.ReadOnlyField(source="get_role") project = ProjectSerializer() profile = profile_serializers.ProfileSerializer() class Meta: model = models.Team fields = '__all__' def get_field_names(self, *args, **kwargs): view = self.get_view if view: return view.team_response_include_fields return super(TeamSerializer, self).get_field_names(*args, **kwargs)
class NotificationRecipientSerializer(DynamicFieldsModelSerializer): notification = NotifySerializer() recipient = profile_serializers.ProfileSerializer() class Meta: model = models.NotificationRecipient fields = '__all__' def get_field_names(self, *args, **kwargs): view = self.get_view if view: return view.notification_recipient_response_include_fields return super(NotificationRecipientSerializer, self).get_field_names(*args, **kwargs)
class BomSerializer(DynamicFieldsModelSerializer): owner = profile_serializers.CompanySerializer() contact = profile_serializers.ProfileSerializer() selected_companies = profile_serializers.CompanySerializer(many=True) bom_rows = BomRowSerializer(many=True) project = project_serializers.ProjectSerializer() class Meta: model = models.Bom fields = '__all__' def get_field_names(self, *args, **kwargs): view = self.get_view if view: return view.bom_response_include_fields return super(BomSerializer, self).get_field_names(*args, **kwargs)
class TaskSerializer(DynamicFieldsModelSerializer): project = ProjectSerializer() assigned_company = profile_serializers.CompanySerializer() workers = profile_serializers.ProfileSerializer(many=True) share_status = serializers.ReadOnlyField(source="get_share_status") shared_task = TaskGenericSerializer() class Meta: model = models.Task fields = '__all__' def get_field_names(self, *args, **kwargs): view = self.get_view if view: return view.task_response_include_fields return super(TaskSerializer, self).get_field_names(*args, **kwargs)
class NotifySerializer(DynamicFieldsModelSerializer): sender = profile_serializers.ProfileSerializer() content_type = serializers.SerializerMethodField(source='get_content_type') class Meta: model = models.Notify fields = '__all__' def get_field_names(self, *args, **kwargs): view = self.get_view if view: return view.notification_response_include_fields return super(NotifySerializer, self).get_field_names(*args, **kwargs) def get_content_type(self, obj): return obj.content_type.name
class QuotationSerializer(DynamicFieldsModelSerializer): bom = BomSerializer() owner = profile_serializers.CompanySerializer() contact = profile_serializers.ProfileSerializer() quotation_rows = QuotationRowSerializer(many=True) project = project_serializers.ProjectSerializer() bom_rows_id_list = serializers.SerializerMethodField() is_valid = serializers.BooleanField(source='is_valid_quotation') class Meta: model = models.Quotation fields = '__all__' def get_field_names(self, *args, **kwargs): view = self.get_view if view: return view.quotation_response_include_fields return super(QuotationSerializer, self).get_field_names(*args, **kwargs) def get_bom_rows_id_list(self, obj): return obj.get_bom_rows_id_list()
class OfferSerializer(DynamicFieldsModelSerializer): contact = profile_serializers.ProfileSerializer() owner = profile_serializers.CompanySerializer() product = product_serializers.ProductSerializer() unit = product_serializers.UnitSerializer() followers = serializers.SerializerMethodField(read_only=True) buyers = serializers.SerializerMethodField(read_only=True) class Meta: model = models.Offer fields = '__all__' def get_field_names(self, *args, **kwargs): view = self.get_view if view: return view.offer_response_include_fields return super(OfferSerializer, self).get_field_names(*args, **kwargs) def get_followers(self, obj): return [row.id for row in obj.followers.all()] def get_buyers(self, obj): return [row.id for row in obj.buyers.all()]
class TaskActivitySerializer(DynamicFieldsModelSerializer): task = TaskSerializer() profile = profile_serializers.ProfileSerializer() days_for_gantt = serializers.SerializerMethodField( source='get_days_for_gantt') class Meta: model = models.Activity fields = '__all__' def get_field_names(self, *args, **kwargs): view = self.get_view if view: if 'month' in view.kwargs: self.month = view.kwargs['month'] if 'year' in view.kwargs: self.year = view.kwargs['year'] return view.activity_response_include_fields return super(TaskActivitySerializer, self).get_field_names(*args, **kwargs) def get_days_for_gantt(self, obj): days = [] date_from, date_to = get_first_last_dates_of_month_and_year( self.month, self.year) start_date, end_date = (date_from, date_to) if obj.datetime_start.month == date_from.month: start_date = obj.datetime_start if obj.datetime_end.month == date_from.month: end_date = obj.datetime_end if isinstance(start_date, datetime.datetime): start_date = start_date.date() if isinstance(end_date, datetime.datetime): end_date = end_date.date() for dt in daterange(start_date, end_date): days.append(dt.day) return days