Ejemplo n.º 1
0
    def deserialize_one(self, data):
        location_set_id = LocationSet.query.filter_by(
            uuid=data['location_set']).with_entities(LocationSet.id).scalar()

        kwargs = data.copy()
        kwargs.pop('location_set')
        kwargs['location_set_id'] = location_set_id

        return LocationType(**kwargs)
Ejemplo n.º 2
0
    def export_list(self, queryset):
        headers = [
            u'Participant ID', u'Name', u'Partner', u'Role', u'Location ID',
            u'Supervisor ID', u'Gender', u'Email', u'Password',
            u'Phone Primary', u'Phone Secondary #1', u'Phone Secondary #2'
        ]

        if queryset.count():
            location_types = LocationType.objects(
                is_administrative=True, deployment=queryset.first().deployment)
            headers = headers[:5] + \
                map(lambda location_type: location_type.name, location_types) + \
                headers[5:]
            for extra_field in queryset.first(
            ).deployment.participant_extra_fields:
                headers.append(extra_field.label)

        output = StringIO()
        writer = unicodecsv.writer(output, encoding='utf-8')
        writer.writerow([unicode(i) for i in headers])
        yield output.getvalue()
        output.close()

        for participant in queryset:
            # limit to three numbers for export and pad if less than three
            phone_numbers = [i.number for i in participant.phones][:3]
            phone_numbers += [''] * (3 - len(phone_numbers))

            record = [
                participant.participant_id if participant.participant_id
                else '',
                participant.name if participant.name else '',
                participant.partner.name if participant.partner else '',
                participant.role.name if participant.role else '',
                participant.location.code if participant.location else ''] + \
                [participant.location_name_path.get(
                 location_type.name, '')
                 for location_type in location_types] + \
                [participant.supervisor.participant_id if participant.supervisor
                else '',
                participant.gender if participant.gender else '',
                participant.email if participant.email else '',
                participant.password if participant.password else '',
            ]

            record.extend(phone_numbers)

            for extra_field in participant.deployment.participant_extra_fields:
                record.append(getattr(participant, extra_field.name, ''))

            output = StringIO()
            writer = unicodecsv.writer(output, encoding='utf-8')
            writer.writerow([unicode(i) for i in record])
            yield output.getvalue()
            output.close()
Ejemplo n.º 3
0
    def export_list(self, queryset):
        headers = [
            u'Participant ID', u'Name', u'Partner', u'Role',
            u'Location ID', u'Supervisor ID', u'Gender', u'Email', u'Password',
            u'Phone Primary', u'Phone Secondary #1', u'Phone Secondary #2'
        ]

        if queryset.count():
            location_types = LocationType.objects(
                is_administrative=True, deployment=queryset.first().deployment)
            headers = headers[:5] + \
                map(lambda location_type: location_type.name, location_types) + \
                headers[5:]
            for extra_field in queryset.first().deployment.participant_extra_fields:
                headers.append(extra_field.label)

        output = StringIO()
        writer = unicodecsv.writer(output, encoding='utf-8')
        writer.writerow([unicode(i) for i in headers])
        yield output.getvalue()
        output.close()

        for participant in queryset:
            # limit to three numbers for export and pad if less than three
            phone_numbers = [i.number for i in participant.phones][:3]
            phone_numbers += [''] * (3 - len(phone_numbers))

            record = [
                participant.participant_id if participant.participant_id
                else '',
                participant.name if participant.name else '',
                participant.partner.name if participant.partner else '',
                participant.role.name if participant.role else '',
                participant.location.code if participant.location else ''] + \
                [participant.location_name_path.get(
                 location_type.name, '')
                 for location_type in location_types] + \
                [participant.supervisor.participant_id if participant.supervisor
                else '',
                participant.gender if participant.gender else '',
                participant.email if participant.email else '',
                participant.password if participant.password else '',
            ]

            record.extend(phone_numbers)

            for extra_field in participant.deployment.participant_extra_fields:
                record.append(getattr(participant, extra_field.name, ''))

            output = StringIO()
            writer = unicodecsv.writer(output, encoding='utf-8')
            writer.writerow([unicode(i) for i in record])
            yield output.getvalue()
            output.close()
Ejemplo n.º 4
0
    def export_list(self, queryset, deployment):
        if queryset.count() < 1:
            yield
        else:
            submission = queryset.first()
            sample_kwargs = {}
            if hasattr(g, 'event'):
                sample_kwargs['event'] = g.event
            if hasattr(g, 'deployment'):
                sample_kwargs['deployment'] = g.deployment
            samples = Sample.objects(**sample_kwargs)
            form = submission.form
            fields = [
                field.name for group in form.groups for field in group.fields
            ]
            location_types = LocationType.objects(is_administrative=True,
                                                  deployment=deployment)

            sample_headers = list(samples.scalar('name'))

            if submission.submission_type == 'O':
                ds_headers = [
                    'Participant ID', 'Name', 'DB Phone', 'Recent Phone'] + \
                    map(lambda location_type: location_type.name,
                        location_types)
                if form.form_type == 'INCIDENT':
                    ds_headers += [
                        'Location', 'Location Code', 'PS Code', 'RV'
                    ] + fields + \
                        ['Timestamp', 'Status', 'Description']
                else:
                    ds_headers += [
                        'Location', 'Location Code', 'PS Code', 'RV'
                    ] + fields + ['Timestamp']
                    ds_headers.extend(sample_headers)
                    ds_headers.append('Comment')
            else:
                ds_headers = [
                    'Participant ID', 'Name', 'DB Phone', 'Recent Phone'] + \
                    map(lambda location_type: location_type.name,
                        location_types)
                ds_headers += [
                    'Location', 'Location Code', 'PS Code', 'RV'] + \
                    fields + ['Timestamp']

                ds_headers.extend(sample_headers)
                ds_headers.extend(map(lambda f: '%s-CONFIDENCE' % f, fields))

            output = StringIO()
            writer = unicodecsv.writer(output, encoding='utf-8')
            writer.writerow([unicode(i) for i in ds_headers])
            yield output.getvalue()
            output.close()

            for submission in queryset:
                if submission.submission_type == 'O':
                    record = [
                        getattr(submission.contributor, 'participant_id', '')
                        if getattr(
                            submission.contributor, 'participant_id', '')
                        else '',
                        getattr(submission.contributor, 'name', '')
                        if getattr(submission.contributor, 'name', '') else '',
                        getattr(submission.contributor, 'phone', '')
                        if getattr(
                            submission.contributor, 'phone', '') else '',
                        recent_phone(submission.contributor)] + \
                        [submission.location_name_path.get(
                         location_type.name, '')
                         for location_type in location_types] + \
                        [getattr(submission.location, 'name', '')
                         if submission.location else '',
                         getattr(submission.location, 'code', '')
                         if submission.location else '',
                         getattr(submission.location, 'political_code', '') or ''
                         if submission.location else '',
                         getattr(submission.location, 'registered_voters', '')
                         if submission.location else ''] + \
                        [export_field(form, submission, f) for f in fields]
                    record += \
                        [submission.updated.strftime('%Y-%m-%d %H:%M:%S'),
                         getattr(submission, 'status', '')
                         if getattr(submission, 'status', '') else '',
                         getattr(submission, 'description', '')
                         if getattr(submission, 'description', '') else ''] \
                        if form.form_type == 'INCIDENT' else \
                        ([submission.updated.strftime('%Y-%m-%d %H:%M:%S')] +
                            [1 if sample in submission.location.samples else 0
                                for sample in samples] +
                            [submission.comments.first().comment.replace(
                                '\n', '')
                             if submission.comments.first() else ''])
                else:
                    sib = submission.siblings.first()
                    record = [
                        getattr(sib.contributor, 'participant_id', '') or ''
                        if sib and hasattr(sib, 'contributor') else '',
                        getattr(sib.contributor, 'name', '') or ''
                        if sib and hasattr(sib, 'contributor') else '',
                        getattr(sib.contributor, 'phone', '') or ''
                        if sib and hasattr(sib, 'contributor') else '',
                        recent_phone(sib.contributor)
                        if hasattr(sib, 'contributor')
                        else '' if sib else ''] + \
                        [submission.location_name_path.get(
                            location_type.name, '')
                         for location_type in location_types] + \
                        [getattr(submission.location, 'name', '')
                         if submission.location else '',
                         getattr(submission.location, 'code', '')
                         if submission.location else '',
                         getattr(submission.location, 'political_code', '') or ''
                         if submission.location else '',
                         getattr(submission.location, 'registered_voters', '')
                         if submission.location else ''] + \
                        [export_field(form, submission, f) for f in fields] + \
                        [submission.updated.strftime('%Y-%m-%d %H:%M:%S')] + \
                        [1 if hasattr(submission.location, 'samples') and
                         sample in submission.location.samples else 0
                         for sample in samples] + \
                        [submission.confidence.get(field, '') or ''
                         for field in fields]

                output = StringIO()
                writer = unicodecsv.writer(output, encoding='utf-8')
                writer.writerow([unicode(i) for i in record])
                yield output.getvalue()
                output.close()
Ejemplo n.º 5
0
    def export_list(self, queryset, deployment):
        if queryset.count() < 1:
            yield
        else:
            submission = queryset.first()
            sample_kwargs = {}
            if hasattr(g, 'event'):
                sample_kwargs['event'] = g.event
            if hasattr(g, 'deployment'):
                sample_kwargs['deployment'] = g.deployment
            samples = Sample.objects(**sample_kwargs)
            form = submission.form
            fields = [
                field.name for group in form.groups for field in group.fields]
            location_types = LocationType.objects(
                is_administrative=True, deployment=deployment)

            sample_headers = list(samples.scalar('name'))

            if submission.submission_type == 'O':
                ds_headers = [
                    'Participant ID', 'Name', 'DB Phone', 'Recent Phone'] + \
                    map(lambda location_type: location_type.name,
                        location_types)
                if form.form_type == 'INCIDENT':
                    ds_headers += [
                        'Location', 'Location Code', 'PS Code', 'RV'
                    ] + fields + \
                        ['Timestamp', 'Status', 'Description']
                else:
                    ds_headers += [
                        'Location', 'Location Code', 'PS Code', 'RV'
                    ] + fields + ['Timestamp']
                    ds_headers.extend(sample_headers)
                    ds_headers.append('Comment')
            else:
                ds_headers = [
                    'Participant ID', 'Name', 'DB Phone', 'Recent Phone'] + \
                    map(lambda location_type: location_type.name,
                        location_types)
                ds_headers += [
                    'Location', 'Location Code', 'PS Code', 'RV'] + \
                    fields + ['Timestamp']

                ds_headers.extend(sample_headers)
                ds_headers.extend(map(lambda f: '%s-CONFIDENCE' % f, fields))

            output = StringIO()
            writer = unicodecsv.writer(output, encoding='utf-8')
            writer.writerow([unicode(i) for i in ds_headers])
            yield output.getvalue()
            output.close()

            for submission in queryset:
                if submission.submission_type == 'O':
                    record = [
                        getattr(submission.contributor, 'participant_id', '')
                        if getattr(
                            submission.contributor, 'participant_id', '')
                        else '',
                        getattr(submission.contributor, 'name', '')
                        if getattr(submission.contributor, 'name', '') else '',
                        getattr(submission.contributor, 'phone', '')
                        if getattr(
                            submission.contributor, 'phone', '') else '',
                        recent_phone(submission.contributor)] + \
                        [submission.location_name_path.get(
                         location_type.name, '')
                         for location_type in location_types] + \
                        [getattr(submission.location, 'name', '')
                         if submission.location else '',
                         getattr(submission.location, 'code', '')
                         if submission.location else '',
                         getattr(submission.location, 'political_code', '') or ''
                         if submission.location else '',
                         getattr(submission.location, 'registered_voters', '')
                         if submission.location else ''] + \
                        [export_field(form, submission, f) for f in fields]
                    record += \
                        [submission.updated.strftime('%Y-%m-%d %H:%M:%S'),
                         getattr(submission, 'status', '')
                         if getattr(submission, 'status', '') else '',
                         getattr(submission, 'description', '')
                         if getattr(submission, 'description', '') else ''] \
                        if form.form_type == 'INCIDENT' else \
                        ([submission.updated.strftime('%Y-%m-%d %H:%M:%S')] +
                            [1 if sample in submission.location.samples else 0
                                for sample in samples] +
                            [submission.comments.first().comment.replace(
                                '\n', '')
                             if submission.comments.first() else ''])
                else:
                    sib = submission.siblings.first()
                    record = [
                        getattr(sib.contributor, 'participant_id', '') or ''
                        if sib and hasattr(sib, 'contributor') else '',
                        getattr(sib.contributor, 'name', '') or ''
                        if sib and hasattr(sib, 'contributor') else '',
                        getattr(sib.contributor, 'phone', '') or ''
                        if sib and hasattr(sib, 'contributor') else '',
                        recent_phone(sib.contributor)
                        if hasattr(sib, 'contributor')
                        else '' if sib else ''] + \
                        [submission.location_name_path.get(
                            location_type.name, '')
                         for location_type in location_types] + \
                        [getattr(submission.location, 'name', '')
                         if submission.location else '',
                         getattr(submission.location, 'code', '')
                         if submission.location else '',
                         getattr(submission.location, 'political_code', '') or ''
                         if submission.location else '',
                         getattr(submission.location, 'registered_voters', '')
                         if submission.location else ''] + \
                        [export_field(form, submission, f) for f in fields] + \
                        [submission.updated.strftime('%Y-%m-%d %H:%M:%S')] + \
                        [1 if hasattr(submission.location, 'samples') and
                         sample in submission.location.samples else 0
                         for sample in samples] + \
                        [submission.confidence.get(field, '') or ''
                         for field in fields]

                output = StringIO()
                writer = unicodecsv.writer(output, encoding='utf-8')
                writer.writerow([unicode(i) for i in record])
                yield output.getvalue()
                output.close()