class GeneralProjectFormSerializer(serializers.ModelSerializer): em = EMSerializer(read_only=True) xf = XFormSerializer() setting = serializers.SerializerMethodField() responses_count = serializers.SerializerMethodField() class Meta: model = FieldSightXF fields = ('id', 'xf', 'date_created', 'default_submission_status', 'responses_count', 'em', 'is_deployed', 'setting') def get_setting(self, obj): try: return SettingsSerializerProjectGeneralForm(obj.settings).data except: return { "donor_visibility": False, "can_edit": False, "can_delete": False } def get_responses_count(self, obj): is_project = self.context.get('project_id', False) if is_project: return obj.project_form_instances.count() return 0
def get_education_material(self, obj): if not EducationMaterial.objects.filter( fsxf=obj.schedule_forms).exists(): return {} em = EducationMaterial.objects.get(fsxf=obj.schedule_forms) # em = EducationMaterial.objects.first() return EMSerializer(em).data
def get_education_material(self, obj): try: em = obj.schedule_forms.em # em = EducationMaterial.objects.first() return EMSerializer(em).data except: return {}
class GeneralFormSerializer(serializers.ModelSerializer): em = EMSerializer(read_only=True) xf = XFormSerializer() setting = serializers.SerializerMethodField() responses_count = serializers.SerializerMethodField() from_organization = serializers.SerializerMethodField(read_only=True) class Meta: model = FieldSightXF fields = ('id', 'xf', 'date_created', 'default_submission_status', 'responses_count', 'em', 'is_deployed', 'setting', 'site', 'project', 'from_organization') def get_from_organization(self, obj): if obj.organization_form_lib: return True else: return False def get_setting(self, obj): try: return SettingsSerializerGeneralForm(obj.settings).data except: return { "types": [], "regions": [], "donor_visibility": False, "can_edit": False, "can_delete": False } def get_responses_count(self, obj): is_project = self.context.get('project_id', False) if is_project: return obj.project_form_instances.count() # return obj.response_count if hasattr(obj, "response_count") else 0 elif obj.project: site_id = self.context.get('site_id', False) if not site_id: return 0 return obj.project_form_instances.filter(site__id=site_id).count() # project form view from site elif obj.site: return obj.site_form_instances.count()
class FSXFormSerializer(serializers.ModelSerializer): em = EMSerializer(read_only=True) settings = FormSettingsReadOnlySerializer(read_only=True) name = serializers.SerializerMethodField('get_title', read_only=True) descriptionText = serializers.SerializerMethodField('get_description', read_only=True) version = serializers.SerializerMethodField() hash = serializers.SerializerMethodField() downloadUrl = serializers.SerializerMethodField('get_url', read_only=True) formID = serializers.SerializerMethodField('get_form_id', read_only=True) manifestUrl = serializers.SerializerMethodField('get_manifest_url') site_project_id = serializers.SerializerMethodField() last_submission = serializers.SerializerMethodField() site = serializers.SerializerMethodField() class Meta: model = FieldSightXF fields = ('id', 'site', 'project', 'site_project_id', 'downloadUrl', 'manifestUrl', 'name', 'descriptionText', 'formID', 'version', 'hash', 'em', 'settings', 'last_submission') def get_site(self, obj): if obj.site: site = obj.site.id else: site = "" return site def get_version(self, obj): return get_version(obj.xf.xml) @check_obj def get_hash(self, obj): return u"md5:%s" % obj.xf.hash @check_obj def get_title(self, obj): return u"%s" % obj.xf.title @check_obj def get_form_id(self, obj): return u"%s" % obj.xf.id_string @check_obj def get_description(self, obj): return u"%s" % obj.xf.description @check_obj def get_url(self, obj): kwargs = {'pk': obj.pk} request = self.context.get('request') return reverse('forms:download_xform', kwargs=kwargs, request=request) @check_obj def get_manifest_url(self, obj): kwargs = {'pk': obj.xf.pk, 'username': obj.xf.user.username} request = self.context.get('request') return reverse('manifest-url', kwargs=kwargs, request=request) def get_site_project_id(self, obj): if obj.site: return obj.site.project_id return None def get_last_submission(self, obj): if obj.project: last_sub = obj.project_form_instances.order_by('-date').first() if last_sub: return last_sub.date if obj.site: last_sub = obj.site_form_instances.order_by('-date').first() if last_sub: return last_sub.date
class SubStageSerializer(serializers.ModelSerializer): xf = serializers.SerializerMethodField('get_assigned_form', read_only=True) em = EMSerializer(read_only=True) responses_count = serializers.SerializerMethodField() is_deployed = serializers.SerializerMethodField() default_submission_status = serializers.SerializerMethodField() fsxf = serializers.SerializerMethodField() setting = serializers.SerializerMethodField() def get_responses_count(self, obj): try: fsxf = FieldSightXF.objects.get(stage=obj) params = self.context.get('params', {}) if params.get("project_id", False): return fsxf.project_form_instances.count() else: site_id = params.get("site_id") if fsxf.project: return fsxf.project_form_instances.filter( site=site_id).count() else: fsxf.site_form_instances.count() except FieldSightXF.DoesNotExist: return 0 def get_is_deployed(self, obj): try: return obj.stage_forms.is_deployed except: return False def get_default_submission_status(self, obj): try: return obj.stage_forms.default_submission_status except: return 0 def get_assigned_form(self, obj): try: fsxf = obj.stage_forms return XFormSerializer(fsxf.xf).data except: return {} def get_fsxf(self, obj): try: return obj.stage_forms.id except: return None def get_setting(self, obj): try: return SettingsSerializerSubStage(obj.stage_forms.settings).data except: return { "types": obj.tags, "regions": obj.regions, "donor_visibility": False, "can_edit": False, "can_delete": False, } class Meta: model = Stage fields = ('id', 'weight', 'name', 'description', 'order', 'date_created', 'em', 'responses_count', 'xf', 'is_deployed', 'default_submission_status', 'fsxf', 'setting', 'site', 'project') def update(self, instance, validated_data): xf = self.context['request'].data.get('xf') request = self.context['request'] default_submission_status = self.context['request'].data.get( 'default_submission_status', 0) xform = False fsxf = None if xf and xf != '': xform = XForm.objects.get(pk=xf) stage = super(SubStageSerializer, self).update(instance, validated_data) if xform: try: old_form = stage.stage_forms if old_form.xf.id == xform.id: old_form.default_submission_status = \ default_submission_status old_form.save() fsxf = old_form setting = self.context['request'].data.get('setting') if setting: setting.update({"form": fsxf.id}) if not setting.get('id'): settings_serializer = SettingsSerializerSubStage( data=setting) else: settings_serializer = SettingsSerializerSubStage( fsxf.settings, data=setting, partial=True) if settings_serializer.is_valid(): setting_obj = settings_serializer.save( user=request.user) stage.tags = setting_obj.types stage.regions = setting_obj.regions stage.save() return stage else: old_form.is_deleted = True old_form.stage = None old_form.save() fsxf = FieldSightXF.objects.create( xf=xform, site=stage.stage.site, project=stage.stage.project, is_staged=True, stage=stage, default_submission_status=default_submission_status) setting = self.context['request'].data.get('setting') if setting: setting.update({"form": fsxf.id}) if setting.get("id"): del setting['id'] settings_serializer = SettingsSerializerSubStage( data=setting) if settings_serializer.is_valid(): setting_obj = settings_serializer.save( user=request.user) stage.tags = setting_obj.types stage.regions = setting_obj.regions stage.save() return stage except Exception as e: fsxf = FieldSightXF.objects.create( xf=xform, site=stage.stage.site, project=stage.stage.project, is_staged=True, stage=stage, default_submission_status=default_submission_status) setting = self.context['request'].data.get('setting') if setting: setting.update({"form": fsxf.id}) if setting.get("id"): del setting['id'] settings_serializer = SettingsSerializerSubStage( data=setting) if settings_serializer.is_valid(): setting_obj = settings_serializer.save( user=request.user) stage.tags = setting_obj.types stage.regions = setting_obj.regions stage.save() return stage else: #no stages form in edit no form before also pass return stage
def get_education_material(self, obj): try: em = obj.schedule_forms.em return EMSerializer(em).data except Exception: return {}
class SubStageDetailSerializer(serializers.ModelSerializer): stage_forms = FSXFSerializer(read_only=True) em = EMSerializer(read_only=True) responses_count = serializers.SerializerMethodField() has_stage = serializers.SerializerMethodField() has_em = serializers.SerializerMethodField() def get_responses_count(self, obj): try: fsxf = FieldSightXF.objects.get(stage=obj) if fsxf.site is None: return fsxf.project_form_instances.count() else: return fsxf.site_form_instances.count() except FieldSightXF.DoesNotExist: return 0 def get_has_stage(self, obj): try: obj.stage_forms return True except: return False def get_has_em(self, obj): try: obj.em return True except: return False class Meta: model = Stage fields = ('weight', 'name', 'description', 'id', 'order', 'date_created', 'em', 'responses_count', 'stage_forms', 'has_stage', 'has_em') def create(self, validated_data): stage_id = self.context['kwargs'].get('stage_id') tags = self.context['request'].data.get('tags') xf = self.context['request'].data.get('xf', {}) xform = False if xf and xf != '': xf_id = xf.get('id', False) if xf_id: xform = XForm.objects.get(pk=xf_id) stage = super(SubStageDetailSerializer, self).create(validated_data) main_stage = Stage.objects.get(pk=stage_id) if xform: FieldSightXF.objects.create(xf=xform, site=main_stage.site, project=main_stage.project, is_staged=True, stage=stage) stage.stage = main_stage stage.save() # tags return stage def update(self, instance, validated_data): xf = self.context['request'].data.get('xf', {}) xform = False if xf and xf != '': xf_id = xf.get('id', False) if xf_id: xform = XForm.objects.get(pk=xf_id) stage = super(SubStageDetailSerializer, self).update(instance, validated_data) if xform: try: old_form = stage.stage_forms if old_form.xf.id == xform.id: pass else: old_form.deleted = True old_form.stage = None old_form.save() FieldSightXF.objects.create(xf=xform, site=stage.site, project=stage.project, is_staged=True, stage=stage) except: if xform: FieldSightXF.objects.create(xf=xform, site=stage.site, project=stage.project, is_staged=True, stage=stage) # tags return stage
class SubStageDetailSerializer(serializers.ModelSerializer): stage_forms = FSXFSerializer(read_only=True) em = EMSerializer(read_only=True) responses_count = serializers.SerializerMethodField() has_stage = serializers.SerializerMethodField() has_em = serializers.SerializerMethodField() is_deployed = serializers.SerializerMethodField() default_submission_status = serializers.SerializerMethodField() def get_responses_count(self, obj): try: request = self.context.get('request', False) params = {} if request: params = request.query_params site_id = False if params.get("is_project", False): if params.get("is_project") == "0": site_id = params.get("pk", False) fsxf = FieldSightXF.objects.get(stage=obj) if fsxf.site is None: if site_id: return fsxf.project_form_instances.filter( site=site_id).count() return fsxf.project_form_instances.count() else: return fsxf.site_form_instances.count() except FieldSightXF.DoesNotExist: return 0 def get_has_stage(self, obj): try: obj.stage_forms return True except: return False def get_has_em(self, obj): try: obj.em return True except: return False def get_is_deployed(self, obj): try: return obj.stage_forms.is_deployed except: return False def get_default_submission_status(self, obj): try: return obj.stage_forms.default_submission_status except: return 0 class Meta: model = Stage fields = ('weight', 'name', 'description', 'id', 'order', 'date_created', 'em', 'responses_count', 'stage_forms', 'has_stage', 'has_em', 'tags', 'is_deployed', 'default_submission_status') def create(self, validated_data): stage_id = self.context['kwargs'].get('stage_id') default_submission_status = self.context['request'].data.get( 'default_submission_status', 0) xf = self.context['request'].data.get('xf', {}) xform = False if xf and xf != '': xf_id = xf.get('id', False) if xf_id: xform = XForm.objects.get(pk=xf_id) stage = super(SubStageDetailSerializer, self).create(validated_data) main_stage = Stage.objects.get(pk=stage_id) if xform: FieldSightXF.objects.create( xf=xform, site=main_stage.site, project=main_stage.project, is_staged=True, stage=stage, default_submission_status=default_submission_status) stage.stage = main_stage stage.site = main_stage.site stage.project = main_stage.project stage.save() # tags return stage def update(self, instance, validated_data): xf = self.context['request'].data.get('xf', {}) default_submission_status = self.context['request'].data.get( 'default_submission_status', 0) xform = False if xf and xf != '': xf_id = xf.get('id', False) if xf_id: xform = XForm.objects.get(pk=xf_id) stage = super(SubStageDetailSerializer, self).update(instance, validated_data) if xform: try: old_form = stage.stage_forms if old_form.xf.id == xform.id: old_form.default_submission_status = default_submission_status old_form.save() else: old_form.is_deleted = True old_form.stage = None old_form.save() FieldSightXF.objects.create( xf=xform, site=stage.stage.site, project=stage.stage.project, is_staged=True, stage=stage, default_submission_status=default_submission_status) except: if xform: FieldSightXF.objects.create( xf=xform, site=stage.stage.site, project=stage.stage.project, is_staged=True, stage=stage, default_submission_status=default_submission_status) return stage