Пример #1
0
    def output(self, key, obj):
        date = fields.get_value(self.date, obj) if self.date else None
        time = fields.get_value(self.time, obj)

        if time == __date_time_null_value__:
            return ""

        if not date:
            return self.format_time(time)

        date_time = date + time
        return self.format(date_time)
Пример #2
0
        def output(self, key, obj):
            value = _fields.get_value(key if self.attribute is None else self.attribute, obj)

            # For all fields, when its value was null (None), return null directly,
            #  instead of return its default value (eg. int type's default value was 0)
            # Because sometimes the client **needs** to know, was a field of the model empty, to decide its behavior.
            return None if value is None else self.format(value)
Пример #3
0
    def output(self, key, obj):
        # Copied from upstream NestedField
        value = base_fields.get_value(
            key if self.attribute is None else self.attribute, obj)
        if value is None:
            if self.allow_null:
                return None
            elif self.default is not None:
                return self.default

        # Handle mappings
        if not hasattr(value, '__class__'):
            raise ValueError('Polymorph field only accept class instances')

        candidates = [
            fields for cls, fields in iteritems(self.mapping)
            if isinstance(value, cls)
        ]

        if len(candidates) <= 0:
            raise ValueError('Unknown class: ' + value.__class__.__name__)
        elif len(candidates) > 1:
            raise ValueError('Unable to determine a candidate for: ' +
                             value.__class__.__name__)
        else:
            return base_fields.marshal(value, candidates[0].resolved)
Пример #4
0
 def output(self, key, obj):
     value = get_value(key if self.attribute is None else self.attribute, obj)
     if value is None:
         return self.default
     else:
         value = geojson.loads(value)            
     return self.format(value)
Пример #5
0
        def output(self, key, obj):
            value = _fields.get_value(
                key if self.attribute is None else self.attribute, obj)

            # For all fields, when its value was null (None), return null directly,
            #  instead of return its default value (eg. int type's default value was 0)
            # Because sometimes the client **needs** to know, was a field of the model empty, to decide its behavior.
            return None if value is None else self.format(value)
Пример #6
0
 def output(self, key, obj):
     if not obj or not obj.HasField(key):
         value = None
     else:
         value = fields.get_value(key if self.attribute is None else self.attribute, obj)
     if self.allow_null and value is None:
         return None
     return fields.marshal(value, self.nested, self.display_null)
Пример #7
0
    def output(self, key, obj):
        value = get_value(key if self.attribute is None else self.attribute, obj)
        if value is None:
            if self.allow_null:
                return None
            elif self.allow_empty:
                return {}

        return marshal(value, self.nested)
Пример #8
0
    def output(self, key, obj):
        tz = get_timezone()

        value = fields.get_value(
            key if self.attribute is None else self.attribute, obj)

        if value is None:
            return self.default

        return self.format(value, tz)
Пример #9
0
    def output(self, key, obj):
        value = get_value(key if self.attribute is None else self.attribute,
                          obj)
        if value is None:
            if self.allow_null:
                return None
            elif self.allow_empty:
                return {}

        return marshal(value, self.nested)
Пример #10
0
    def output(self, key, obj):
        attribute = self.attribute or key
        if not obj.HasField(attribute):
            return self.default

        value = fields.get_value(attribute, obj)

        if value is None:
            return self.default

        return self.format(value)
Пример #11
0
    def output(self, key, data):
        value = fields.get_value(key if self.attribute is None else self.attribute, data)
        # we cannot really test for external dict behavior
        if fields.is_indexable_but_not_string(value) and not isinstance(value, dict):
            # Convert all instances in typed list to container type
            return [self.container.output(idx, val) for idx, val
                    in enumerate(value)]

        if value is None:
            return self.default

        return [marshal(value, self.container.nested)]
Пример #12
0
    def output(self, key, obj):
        val = fields.get_value(
            key if self.attribute is None else self.attribute, obj)

        lines = []
        for l in val.lines:
            lines.append([[c.lon, c.lat] for c in l.coordinates])

        response = {
            "type": "MultiLineString",
            "coordinates": lines,
        }
        return response
Пример #13
0
    def output(self, key, data):
        value = fields.get_value(
            key if self.attribute is None else self.attribute, data)
        # we cannot really test for external dict behavior
        if fields.is_indexable_but_not_string(value) and not isinstance(
                value, dict):
            # Convert all instances in typed list to container type
            return [
                self.container.output(idx, val)
                for idx, val in enumerate(value)
            ]

        if value is None:
            return self.default

        return [marshal(value, self.container.nested)]
Пример #14
0
    def output(self, key, obj):
        # Copied from upstream NestedField
        value = base_fields.get_value(key if self.attribute is None else self.attribute, obj)
        if value is None:
            if self.allow_null:
                return None
            elif self.default is not None:
                return self.default

        # Handle mappings
        if not hasattr(value, '__class__'):
            raise ValueError('Polymorph field only accept class instances')

        candidates = [fields for cls, fields in iteritems(self.mapping) if isinstance(value, cls)]

        if len(candidates) <= 0:
            raise ValueError('Unknown class: ' + value.__class__.__name__)
        elif len(candidates) > 1:
            raise ValueError('Unable to determine a candidate for: ' + value.__class__.__name__)
        else:
            return base_fields.marshal(value, candidates[0].resolved)
Пример #15
0
 def output(self, key, obj):
     k = key if self.attribute is None else self.attribute
     if not obj or not obj.HasField(k):
         return None
     else:
         return fields.get_value(k, obj)
Пример #16
0
        def output(self, key, obj):
            value = _fields.get_value(key if self.attribute is None else self.attribute, obj)

            # 对于所有字段,碰到 null(None) 值时,都直接返回 null,而不是返回一个默认值(如: int => 0)
            # 若不这样做,客户端没法确定一个字段(例如值为 0 的 int 字段)到底是赋的值就是0还是没赋值
            return None if value is None else self.format(value)
Пример #17
0
    def get(self):
        args = self.get_parser.parse_args()

        # NPS score filters
        nps_score_start = nps_score_end = None
        if args['nps_score_start'] and args['nps_score_end']:
            nps_score_start = args['nps_score_start']
            nps_score_end = args['nps_score_end']

        # add time based filters
        start_date = args['start_date']
        end_date = args['end_date']

        # list of instance IDs associated with given form IDs
        form = form_id_exists(args['form_id'])
        instances = []
        instances_ = InstanceModel.objects.filter(form=form)
        instances.extend(instances_)

        # get the feedbacks
        feedbacks_, all_start_date, all_end_date = FeedbackModel.get_timeline(
                        merchant = g.user.merchant,
                        nps_score_start = nps_score_start,
                        nps_score_end = nps_score_end,
                        start_date = start_date,
                        end_date = end_date,
                        instances = instances
                    )

        # contructing list of dicts for CSVs
        form_fields = dict([(str(i.id), i.text) for i in form.fields]) # dict of titles of form fields
        feedbacks = [] # this list will contain dicts of all feedbacks for csv
        for f in feedbacks_:
            # all feedback details excluding the responses
            f_ = {

                # general feedback details
                'Feedback Text': get_value('feedback_text', f),
                'Feedback Score': get_value('nps_score', f),
                
                # date & time info
                'Date': get_value('received_at', f).strftime('%d-%m-%Y'),
                'Time': get_value('received_at', f).strftime('%H:%M'),

                # customer details
                'Customer Name': get_value('customer.name', f),
                'Customer Mobile': get_value('customer.mobile', f),
                'Customer E-mail': get_value('customer.email', f),
                
                # form and instance details
                'Form Name': get_value('form_instance.form.name', f),
                'Instance Name': get_value('form_instance.name', f),
                'Instance Location': get_value('form_instance.location', f),

            }
            # adding the details of the responses of the feedback
            responses = {}
            for k,v in form_fields.items():
                responses[v] = f.field_responses.get(k)
            f_.update(responses)

            # appending to the list of all feedbacks
            feedbacks.append(f_)

        # keys for the CSV file
        keys = [
            'Date',
            'Time',
            'Customer Name',
            'Customer Mobile',
            'Customer E-mail',
            'Form Name',
            'Instance Name',
            'Instance Location',
            'Feedback Text',
            'Feedback Score'
        ]
        # adding the title of the questions of the form as CSV columns
        for title in form_fields.values():
            keys.append(title)
        # writing to a StringIO object
        f = StringIO.StringIO()
        dict_writer = csv.DictWriter(f, keys)
        dict_writer.writer.writerow(keys)
        dict_writer.writerows(feedbacks)

        # uploading to S3
        headers = {'Content-Disposition': 'attachment; filename=timeline-export.csv'}
        key_name = 'timeline-export-{0}.csv'.format(g.user.merchant.id)
        url = upload_s3(f, key_name, 'application/octet-stream', 'ingage-csv-exports', headers)

        return {'csv_url': url}