示例#1
0
class FallaResource(resources.ModelResource):
    asignacion_ni = fields.Field(column_name='asignacion ni',
                                 attribute='asignacion_ni',
                                 widget=ForeignKeyWidget(AsignacionNi, 'pk'))
    actividad = fields.Field(column_name='actividad',
                             attribute='actividad',
                             widget=ForeignKeyWidget(Actividad, 'pk'))
    wp = fields.Field(column_name='wp', attribute='wp')
    service_supplier = fields.Field(column_name='service supplier',
                                    attribute='service_supplier')
    estacion = fields.Field(column_name='estacion',
                            attribute='estacion',
                            widget=ForeignKeyWidget(Estacion, 'nombre'))
    banda = fields.Field(column_name='banda', attribute='banda')
    proyecto = fields.Field(column_name='proyecto', attribute='proyecto')
    escenario = fields.Field(column_name='escenario', attribute='escenario')
    ni_ingeniero = fields.Field(column_name='ni ingeniero',
                                attribute='ni_ingeniero',
                                widget=ForeignKeyWidget(
                                    Perfil, 'nombre_completo'))
    concepto = fields.Field(column_name='concepto', attribute='concepto')
    tipo_falla = fields.Field(column_name='tipo falla', attribute='tipo_falla')
    creado = fields.Field(column_name='creado',
                          attribute='creado',
                          widget=DateWidget(format='%d/%m/%Y'))
    actualizado = fields.Field(column_name='actualizado',
                               attribute='actualizado',
                               widget=DateWidget(format='%d/%m/%Y'))

    def save_instance(self, instance, using_transactions=True, dry_run=False):
        creado = instance.creado
        if creado:
            instance.creado = creado
            instance.save()

    def for_delete(self, row, instance):
        return self.fields['subestado'].clean(row)

    class Meta:
        model = Falla
        # exclude = (,)
        export_order = (
            'id',
            'asignacion_ni',
            'actividad',
            'wp',
            'service_supplier',
            'estacion',
            'banda',
            'proyecto',
            'escenario',
            'ni_ingeniero',
            'concepto',
            'tipo_falla',
            'creado',
            'actualizado',
            'estado',
            'subestado',
        )
class NotificacionFallaInstalacionResource(resources.ModelResource):
    asignacion_ni = fields.Field(column_name='asignacion ni',
                                 attribute='asignacion_ni',
                                 widget=ForeignKeyWidget(AsignacionNi, 'pk'))
    actividad = fields.Field(column_name='actividad',
                             attribute='actividad',
                             widget=ForeignKeyWidget(Actividad, 'pk'))
    wp = fields.Field(column_name='wp', attribute='wp')
    service_supplier = fields.Field(column_name='service supplier',
                                    attribute='service_supplier')
    estacion = fields.Field(column_name='estacion',
                            attribute='estacion',
                            widget=ForeignKeyWidget(Estacion, 'nombre'))
    banda = fields.Field(column_name='banda', attribute='banda')
    proyecto = fields.Field(column_name='proyecto', attribute='proyecto')
    escenario = fields.Field(column_name='escenario', attribute='escenario')
    ni_ingeniero = fields.Field(column_name='ni ingeniero',
                                attribute='ni_ingeniero',
                                widget=ForeignKeyWidget(
                                    Perfil, 'nombre_completo'))
    detalle_falla_instalacion = fields.Field(
        column_name='detalle falla instalacion',
        attribute='detalle_falla_instalacion')
    solver = fields.Field(column_name='solver', attribute='solver')
    creado = fields.Field(column_name='creado',
                          attribute='creado',
                          widget=DateWidget(format='%d/%m/%Y'))
    actualizado = fields.Field(column_name='actualizado',
                               attribute='actualizado',
                               widget=DateWidget(format='%d/%m/%Y'))

    def for_delete(self, row, instance):
        return self.fields['subestado'].clean(row)

    class Meta:
        model = NotificacionFallaInstalacion
        # exclude = (,)
        export_order = (
            'id',
            'asignacion_ni',
            'actividad',
            'wp',
            'service_supplier',
            'estacion',
            'banda',
            'proyecto',
            'escenario',
            'ni_ingeniero',
            'detalle_falla_instalacion',
            'solver',
            'creado',
            'actualizado',
            'estado',
            'subestado',
        )
示例#3
0
class ConceptoNpoResource(resources.ModelResource):
    npo_ingeniero = fields.Field(column_name='npo ingeniero',
                                 attribute='npo_ingeniero',
                                 widget=ForeignKeyWidget(
                                     Perfil, 'nombre_completo'))
    estacion = fields.Field(
        column_name='estacion',
        attribute='estacion__nombre',
    )
    actividad = fields.Field(
        column_name='actividad',
        attribute='actividad',
        widget=ForeignKeyWidget(Actividad, 'pk'),
    )
    asignacion_npo = fields.Field(column_name='asignacion npo',
                                  attribute='asignacion_npo',
                                  widget=ForeignKeyWidget(AsignacionNpo, 'pk'))
    wp = fields.Field(
        column_name='wp',
        attribute='wp',
    )
    contenido = fields.Field(
        column_name='contenido',
        attribute='contenido',
    )
    creado = fields.Field(column_name='creado',
                          attribute='creado',
                          widget=DateWidget(format='%d/%m/%Y'))
    actualizado = fields.Field(column_name='actualizado',
                               attribute='actualizado',
                               widget=DateWidget(format='%d/%m/%Y'))

    def for_delete(self, row, instance):
        return self.fields['subestado'].clean(row)

    class Meta:
        model = ConceptoNpo
        exclude = ('imagen')
        export_order = (
            'id',
            'npo_ingeniero',
            'estacion',
            'actividad',
            'asignacion_npo',
            'wp',
            'contenido',
            'estado',
            'subestado',
            'creado',
            'actualizado',
        )
class AlertaResource(resources.ModelResource):
    estacion = fields.Field(
        column_name='estacion',
        attribute='estacion',
        widget=ForeignKeyWidget(Estacion, 'nombre'))
    actividad = fields.Field(
        column_name='actividad',
        attribute='actividad',
        widget=ForeignKeyWidget(Actividad, 'pk'))
    wp = fields.Field(
        column_name='wp',
        attribute='wp',)
    mensaje = fields.Field(
        column_name='mensaje',
        attribute='mensaje',)
    estado_alerta = fields.Field(
        column_name='estado alerta',
        attribute='estado_alerta',)
    tipo_alerta = fields.Field(
        column_name='tipo alerta',
        attribute='tipo_alerta',)
    creado = fields.Field(
        column_name='creado',
        attribute='creado',
        widget=DateWidget(format='%d/%m/%Y'))
    actualizado = fields.Field(
        column_name='actualizado',
        attribute='actualizado',
        widget=DateWidget(format='%d/%m/%Y'))

    def for_delete(self, row, instance):
        return self.fields['subestado'].clean(row)

    class Meta:
        model = Alerta
        # exclude = ('',)
        export_order = (
        'id',
        'estacion',
        'actividad',
        'wp',
        'mensaje',
        'estado_alerta',
        'tipo_alerta',
        'creado',
        'actualizado',
        'estado',
        'subestado',
        )
class ComentarioNiResource(resources.ModelResource):
    ni_ingeniero = fields.Field(column_name='ni ingeniero',
                                attribute='ni_ingeniero',
                                widget=ForeignKeyWidget(
                                    Perfil, 'nombre_completo'))
    incidente_ni = fields.Field(column_name='incidente ni',
                                attribute='incidente_ni',
                                widget=ForeignKeyWidget(IncidenteNi, 'pk'))
    estacion = fields.Field(
        column_name='estacion',
        attribute='estacion__nombre',
    )
    actividad = fields.Field(column_name='actividad',
                             attribute='actividad',
                             widget=ForeignKeyWidget(Actividad, 'pk'))
    wp = fields.Field(
        column_name='wp',
        attribute='wp',
    )
    contenido = fields.Field(
        column_name='contenido',
        attribute='contenido',
    )
    creado = fields.Field(column_name='creado',
                          attribute='creado',
                          widget=DateWidget(format='%d/%m/%Y'))
    actualizado = fields.Field(column_name='actualizado',
                               attribute='actualizado',
                               widget=DateWidget(format='%d/%m/%Y'))

    def for_delete(self, row, instance):
        return self.fields['subestado'].clean(row)

    class Meta:
        model = ComentarioNi
        # exclude = ('')
        export_order = (
            'id',
            'ni_ingeniero',
            'incidente_ni',
            'estacion',
            'actividad',
            'wp',
            'contenido',
            'estado',
            'subestado',
            'creado',
            'actualizado',
        )
示例#6
0
class LzProductsResource(resources.ModelResource):
    """The import_export resource class for model Products"""

    barcode = fields.Field(
        attribute="barcode",
        column_name=u"常易舒编号",
    )
    sample_code = fields.Field(
        attribute="sample_code",
        column_name=u"样本编号",
    )
    risk_state = fields.Field(
        attribute="risk_state",
        column_name=u"风险水平",
    )
    received_date = fields.Field(attribute="received_date",
                                 column_name=u"收样日期",
                                 widget=DateWidget(format='%Y/%m/%d'))
    test_date = fields.Field(attribute="test_date",
                             column_name=u"检测日期",
                             widget=DateWidget(format='%Y/%m/%d'))
    report_date = fields.Field(attribute="report_date",
                               column_name=u"报告日期",
                               widget=DateWidget(format='%Y/%m/%d'))
    batch_code_id = fields.Field(attribute="batch_code_id", column_name="批次号")

    class Meta:
        model = LzProducts
        fields = (
            'sample_code',
            'barcode',
            'risk_state',
            'received_date',
            'test_date',
            'report_date',
            'batch_code_id',
        )
        export_order = (
            'sample_code',
            'barcode',
            'risk_state',
            'received_date',
            'test_date',
            'report_date',
            'batch_code_id',
        )
        import_id_fields = ('barcode', )
        skip_unchanged = True
示例#7
0
class InvoiceResource(resources.ModelResource):
    id = Field(attribute="id", column_name="INVOICE ID")
    customer_name = Field(attribute="customer_name",
                          column_name="CUSTOMER NAME")
    customer_contact = Field(attribute="customer_contact",
                             column_name="CUSTOMER CONTACT")
    invoice_discount = Field(attribute="invoice_discount",
                             column_name="DISCOUNT")
    total_cost = Field(attribute="total_cost", column_name="TOTAL")
    paid = Field(attribute="paid", column_name="PAID")
    timestamp = Field(attribute="timestamp",
                      column_name="DATE",
                      widget=DateWidget(format='%d/%B/%Y'))

    class Meta:
        model = Invoice
        clean_model_instances = True
        fields = ('id', 'customer_name', 'customer_contact',
                  'invoice_discount', 'total_cost', 'paid', 'timestamp')
        export_order = ('id', 'customer_name', 'customer_contact',
                        'invoice_discount', 'total_cost', 'paid', 'timestamp')

    def dehydrate_paid(self, invoice):
        paid = "No"
        if invoice.paid:
            paid = "Yes"
        return paid

    def get_queryset(self):
        return self._meta.model.objects.order_by('timestamp')
示例#8
0
class SolicitudHWResource(resources.ModelResource):
    ni_ingeniero = fields.Field(column_name='ni ingeniero',
                                attribute='ni_ingeniero',
                                widget=ForeignKeyWidget(
                                    Perfil, 'nombre_completo'))
    asignacion_ni = fields.Field(column_name='asignacion ni',
                                 attribute='asignacion_ni',
                                 widget=ForeignKeyWidget(AsignacionNi, 'pk'))
    estacion = fields.Field(
        column_name='estacion',
        attribute='estacion__nombre',
    )
    actividad = fields.Field(column_name='actividad',
                             attribute='actividad',
                             widget=ForeignKeyWidget(Actividad, 'pk'))
    wp = fields.Field(column_name='wp', attribute='wp', widget=IntegerWidget())
    estado_solicitud = fields.Field(
        column_name='estado solicitud',
        attribute='estado_solicitud',
    )
    creado = fields.Field(column_name='creado',
                          attribute='creado',
                          widget=DateWidget(format='%d/%m/%Y'))
    actualizado = fields.Field(column_name='actualizado',
                               attribute='actualizado',
                               widget=DateWidget(format='%d/%m/%Y'))

    def for_delete(self, row, instance):
        return self.fields['subestado'].clean(row)

    class Meta:
        model = SolicitudHW
        # exclude = ('',)
        export_order = (
            'id',
            'ni_ingeniero',
            'asignacion_ni',
            'estacion',
            'actividad',
            'wp',
            'estado_solicitud',
            'creado',
            'actualizado',
            'estado',
            'subestado',
        )
示例#9
0
class EntryResource(resources.ModelResource):
    """
    Define the resource for counter entry.
    """
    date = Field(column_name='date',
                 attribute='date',
                 widget=DateWidget(format="%d.%m.%Y"))

    class Meta:
        model = Entry
示例#10
0
class EmployeeResource(resources.ModelResource):
    start_date = fields.Field(attribute='start_date',
                              column_name='start_date',
                              widget=DateWidget('%d/%m/%Y'))
    cost_code = fields.Field(column_name='cost_code',
                             attribute='cost_code',
                             widget=ForeignKeyWidget(Location, 'cost_code'))
    department = fields.Field(column_name='department',
                              attribute='department',
                              widget=ForeignKeyWidget(Department,
                                                      'department'))
    position = fields.Field(column_name='position',
                            attribute='position',
                            widget=ForeignKeyWidget(Position, 'position'))
    job = fields.Field(column_name='job',
                       attribute='job',
                       widget=ForeignKeyWidget(TrainingMatrix, 'job'))

    class Meta:
        model = Employee
        exclude = ('id', )
        import_id_fields = ('badge', )
        fields = (
            'badge',
            'name',
            'start_date',
            'end_date',
            'status',
            'cost_code',
            'department',
            'position',
            'job',
            'training',
        )

    def before_save_instance(self, instance, using_transactions, dry_run):
        if instance.job == TrainingMatrix.job:
            return instance

    # def after_save_instance(self, instance, using_transactions, dry_run):
    #     if instance.department == TrainingMatrix.department and instance.position == TrainingMatrix.position:
    #         instance.training = TrainingMatrix.training.all()
    #         return instance

    # def before_save_instance(self, instance, using_transactions, dry_run):
    #    if instance.department == TrainingMatrix.department and instance.position == TrainingMatrix.position:
    #         return instance

    def init_instance(self, row, *args, **kwargs):
        print("I'm working")
        for traininig in TrainingMatrix.objects.all():
            if self.department__department == training.department__department and self.position__position == training.position__position:
                self.training = training.id
                self.training.save()
                return instance
示例#11
0
class MemberResource(resources.ModelResource):
    """A resource class for importing data to the Member class."""

    class Meta:
        import_id_fields = ['id']
        model = Member

    def dehydrate_constituency(self, member):
        """Define export method for constituency taxonomy title."""
        return '%s' % (member.constituency.title)

    name = Field(
        attribute='name',
        column_name='name',
        widget=TextWidget(),
    )
    name_fr = Field(
        attribute='name_fr',
        column_name='name_fr',
        widget=TextWidget(),
    )
    url = Field(
        attribute='url',
        column_name='url',
        widget=TextWidget(),
    )
    date_joined = Field(
        attribute='date_joined',
        column_name='date_joined',
        widget=DateWidget(format='%Y'),
    )
    constituency = Field(
        attribute='constituency',
        column_name='constituency',
        widget=SingleTermWidget(
            Constituency
        ),
    )
    constituency_fr = Field(
        attribute='constituency_fr',
        column_name='constituency_fr',
        widget=SingleTermWidgetFR(
            Constituency,
            'constituency'
        ),
    )
    image = Field(
        attribute='image',
        column_name='image',
        widget=ImageWidget(
            get_image_model()
        ),
    )
class DatoObjetivoResource(resources.ModelResource):
    deportista = fields.Field(column_name='deportista',attribute='deportista',widget=ForeignKeyWidget(Deportista, 'documento'))
    fecha = fields.Field(column_name='fecha',attribute='fecha',widget=DateWidget())

    class Meta:
        model = DatoObjetivo

    def save_instance(self, instance, using_transactions=True, dry_run=False):
        try:
            super(DatoObjetivoResource, self).save_instance(instance, using_transactions, dry_run)
        except IntegrityError:
            pass        
示例#13
0
class RideResource(resources.ModelResource):
    """
    Define the ride resource for import / export.
    """

    date = Field('date', 'date', DateWidget(format="%d.%m.%Y"))
    date_created = Field('date_created', 'date_created', DateWidget(format="%d.%m.%Y"))

    class Meta:
        model = Ride
        export_order = (
            'id', 'date_created', 'date', 'driver__name', 'car__name', 'velos', 'velo_state__name', 'note', 'completed',
            'from_warehouse__name', 'to_warehouse__name', 'created_by', 'request_category__category_name',
            'planned_velos', 'request_comment', 'from_street_nr', 'from_zip_code', 'from_city', 'from_contact_name',
            'from_contact_phone', 'from_comment', 'to_street_nr', 'to_zip_code', 'to_city', 'to_contact_name',
            'to_contact_phone', 'to_comment', 'customer_company', 'customer_salutation', 'customer_firstname',
            'customer_lastname', 'customer_email', 'customer_phone', 'customer_street_nr', 'customer_zip_code',
            'customer_city', 'invoice_same_as_customer', 'charged', 'invoice_purpose', 'price', 'cost_type',
            'invoice_company_name', 'invoice_company_addition', 'invoice_street_nr', 'invoice_zip_code', 'invoice_city',
            'invoice_commissioned', 'spare_parts', 'stocklist', 'distance', 'pickup_time',
        )
        fields = export_order
示例#14
0
class OrderResource(resources.ModelResource):
    # product = Field(
    #     attribute='product',
    #     column_name='product',
    #     widget=ForeignKeyWidget(Product, 'pk')
    # )
    # warehouse = Field(
    #     attribute='warehouse',
    #     column_name='warehouse',
    #     widget=ForeignKeyWidget(Warehouse, 'pk')
    # )
    # customer = Field(
    #     attribute='customer',
    #     column_name='customer',
    #     widget=ForeignKeyWidget(Customer, 'pk')
    # )
    # circuit = Field(
    #     attribute='circuit',
    #     column_name='circuit',
    #     widget=ForeignKeyWidget(Circuit, 'pk')
    # )
    ordered_at = Field(
        attribute='ordered_at',
        column_name='ordered_at',
        widget=DateWidget(),
    )
    ordered_quantity = Field(
        attribute='ordered_quantity',
        column_name='ordered_quantity',
        widget=IntegerWidget(),
    )

    # reference = Field(
    #     attribute='reference',
    #     column_name='order_reference',
    #     widget=DateWidget(),
    # )
    class Meta:
        model = Order
        skip_unchanged = True
        report_skipped = True
        import_id_fields = (
            'circuit',
            'warehouse',
            'product',
            'category',
            'ordered_quantity',
            'ordered_at',
        )
        exclude = ('id', )
示例#15
0
class BoxApplicationsResources(resources.ModelResource):
    """盒子申请信息导入导出"""
    application_id = Field(column_name="申请编号", attribute='id', default=None)
    contract_id = Field(column_name="合同号",
                        attribute='contract_number',
                        widget=ForeignKeyWidget(ContractsInfo,
                                                'contract_number'),
                        default=None)
    amount = Field(column_name="申请数量",
                   attribute='amount',
                   widget=IntegerWidget())
    classification = Field(column_name="申请类别", attribute='classification')
    address_name = Field(column_name="收件人姓名", attribute='address_name')
    address_phone = Field(column_name="收件人号码", attribute='address_phone')
    send_address = Field(column_name="邮寄地址", attribute='send_address')
    box_price = Field(column_name="盒子单价", attribute='box_price', default=None)
    detection_price = Field(column_name="检测单价",
                            attribute='detection_price',
                            default=None)
    use = Field(column_name="用途", attribute='use')
    proposer = Field(column_name="申请人", attribute='proposer', default=None)
    submit_time = Field(column_name="提交时间",
                        attribute='submit_time',
                        widget=DateWidget('%Y-%m-%d'))
    approval_status = Field(column_name="审批状态", attribute='approval_status')
    box_submit_flag = Field(column_name="是否提交", attribute='box_submit_flag')

    class Meta:
        model = BoxApplications
        fields = ('application_id', 'contract_id', 'amount', 'classification',
                  'address_name', 'address_phone', 'send_address', 'box_price',
                  'detection_price', 'use', 'proposer', 'submit_time',
                  'approval_status', 'box_submit_flag')
        export_order = ('application_id', 'contract_id', 'amount',
                        'classification', 'address_name', 'address_phone',
                        'send_address', 'box_price', 'detection_price', 'use',
                        'proposer', 'submit_time', 'approval_status',
                        'box_submit_flag')
        skip_unchanged = True
        import_id_fields = ['application_id']

    def get_export_headers(self):
        export_headers = [
            u'申请编号', u'合同号', u'申请数量', u'申请类别', u'收件人姓名', u'收件人号码', u'邮寄地址',
            u'盒子单价', u'检测单价', u'用途', u'申请人', u'提交时间', u'审批状态', u'是否提交'
        ]
        return export_headers
示例#16
0
class PaymentResource(resources.ModelResource):
    id = Field(column_name="编号", attribute='id', default=None)
    payment_number = Field(column_name="到账编号",
                           attribute='payment_number',
                           default=None)
    contract_number = Field(column_name="合同号",
                            attribute='contract_number',
                            default=None)
    receive_value = Field(column_name="到账金额",
                          attribute='receive_value',
                          default=None)
    wait_invoices = Field(column_name="待开票额",
                          attribute='wait_invoices',
                          default=None)
    receive_date = Field(
        column_name="到账时间",
        attribute='receive_date',
        widget=DateWidget(format='%Y-%m-%d'),
        default=None,
    )
    record_number = Field(
        column_name="业务流编号",
        attribute='record_number',
    )

    class Meta:
        model = Payment
        fields = ('id', 'payment_number', 'contract_number', 'receive_value',
                  'wait_invoices', 'receive_date', 'record_number')
        export_order = (
            'id',
            'payment_number',
            'contract_number',
            'receive_value',
            'wait_invoices',
            'receive_date',
        )
        skip_unchanged = True
        import_id_fields = ['id']

    def dehydrate_contract_number(self, payment):
        contract_number = payment.record_number.contract_number.contract_number
        return contract_number

    def get_export_headers(self):
        export_headers = ["编号", "到款编号", "合同号", "到账金额", "待开票额", "到账时间", '业务流编号']
        return export_headers
示例#17
0
class IntentionSource(resources.ModelResource):
    """意向池导入导出resources"""

    id = Field(
        column_name="编号", attribute='id', default=None
    )
    salesman = Field(
        column_name="销售", attribute='salesman', default=None
    )
    intention_client = Field(
        column_name="意向名称", attribute='intention_client'
    )
    contact_name = Field(
        column_name="联系人姓名", attribute='contact_name'
    )
    contact_number = Field(
        column_name="电话(或微信)", attribute='contact_number'
    )
    intention_progress = Field(
        column_name="意向进展", attribute='follow_situations'
    )
    remark = Field(
        column_name="备注", attribute='remark'
    )
    fill_date = Field(
        column_name="填写日期", attribute='fill_date', default=None,
        widget=DateWidget(format='%Y-%m-%d')
    )

    class Meta:
        model = Intentions
        fields = (
            'id', 'salesman', 'intention_client', 'contact_name',
            'contact_number', 'intention_progress', 'remark', 'fill_date'
        )
        export_order = fields
        skip_unchanged = True
        import_id_fields = ['id']

    def get_export_headers(self):
        export_headers = [u'编号', u'销售', u'意向客户', u'合同号', u'电话(或微信)',
                          u'意向进展', u'备注', u'填写日期']
        return export_headers
示例#18
0
class CustomerResource(resources.ModelResource):

    country = fields.Field(column_name='country',
                           attribute='country',
                           widget=CountryWidget(Country, field='name'))

    industry = fields.Field(column_name='industry',
                            attribute='industry',
                            widget=ForeignKeyWidget(Industry, field='title'))

    services = fields.Field(column_name='services',
                            attribute='services',
                            widget=ServicesWidget(ServiceType,
                                                  field='title',
                                                  separator="/"))

    owner = fields.Field(column_name='owner',
                         attribute='owner',
                         widget=ForeignKeyWidget(Employee, 'name'))

    sales_rep = fields.Field(column_name='sales_rep',
                             attribute='sales_rep',
                             widget=ForeignKeyWidget(Employee, 'name'))

    lead_date = fields.Field(column_name='lead_date',
                             attribute='lead_date',
                             widget=DateWidget(format="%d-%m-%Y"))

    class Meta:
        model = Lead
        skip_unchanged = True
        report_skipped = True

        import_id_fields = ('title', )
        fields = ('title', 'contact_name', 'email', 'phone', 'city', 'state',
                  'country', 'requirement', 'industry', 'services',
                  'lead_date', 'lead_source', 'latest_lead_status',
                  'sales_rep', 'owner')
        export_order = ('title', 'contact_name', 'email', 'phone', 'city',
                        'state', 'country', 'requirement', 'industry',
                        'services', 'lead_date', 'lead_source',
                        'latest_lead_status', 'sales_rep', 'owner')
示例#19
0
class MemberResource(resources.ModelResource):
    """A resource class for importing data to the Member class."""
    class Meta:
        import_id_fields = ['id']
        model = Member

    name = Field(
        attribute='name',
        column_name='name',
        widget=TextWidget(),
    )
    name_fr = Field(
        attribute='name_fr',
        column_name='name_fr',
        widget=TextWidget(),
    )
    url = Field(
        attribute='url',
        column_name='url',
        widget=TextWidget(),
    )
    date_joined = Field(
        attribute='date_joined',
        column_name='date_joined',
        widget=DateWidget(format='%Y'),
    )
    constituency = Field(
        attribute='constituency',
        column_name='constituency',
        widget=SingleTermWidget(Constituency),
    )
    constituency_fr = Field(
        attribute='constituency_fr',
        column_name='constituency_fr',
        widget=SingleTermWidgetFR(Constituency, 'constituency'),
    )
    image = Field(
        attribute='image',
        column_name='image',
        widget=ImageWidget(get_image_model()),
    )
示例#20
0
class TechsupportResources(resources.ModelResource):
    """
    确认收货时更新盒子内容
    """
    # id = Field(
    #     column_name='id', attribute='id', default=None
    # )
    parent = Field(
        column_name="合作方", attribute="parent",
        widget=ForeignKeyWidget(Partners, "name"), default=None)
    send_date = Field(
        column_name='邮寄日期', attribute='send_date',
        widget=DateWidget(format='%Y-%m-%d'), default=None
    )
    made_date = Field(
        column_name='生产日期', attribute='made_date',
        widget=DateWidget(format='%Y-%m-%d'), default=None
    )
    sale_man = Field(
        column_name="业务员", attribute="sale_man",
        widget=ForeignKeyWidget(BmsUser, "username"), default=None)
    receive_date = Field(
        column_name='收样日期', attribute='receive_date',
        widget=DateWidget(format='%Y-%m-%d'), default=None
    )
    sampling_date = Field(
        column_name='采样日期', attribute='sampling_date',
        widget=DateWidget(format='%Y-%m-%d'), default=None
    )
    report_end_date = Field(
        column_name='报告截止', attribute='report_end_date',
        widget=DateWidget(format='%Y-%m-%d'), default=None
    )
    project_source = Field(
        column_name='检测项目来源', attribute='project_source', default=None
    )
    send_number = Field(
        column_name='快递单号', attribute='send_number', default=None
    )
    index_number = Field(
        column_name='样品编号', attribute='index_number', default=None
    )
    report_date = Field(
        column_name='报告出具日期', attribute='report_date',
        widget=DateWidget(format='%Y-%m-%d'), default=None
    )
    pe_number = Field(
        column_name='体检号', attribute='pe_number', default=None
    )
    barcode = Field(
        column_name='条形码', attribute='barcode', default=None
    )
    name = Field(
        column_name='姓名', attribute='name', default=None
    )
    gender = Field(
        column_name='性别', attribute='gender', default=None
    )
    age = Field(
        column_name='年龄', attribute='age', default=None
    )
    contact = Field(
        column_name='联系方式', attribute='contact', default=None
    )
    id_number = Field(
        column_name='身份证号码', attribute='id_number',
    )
    email = Field(
        column_name='提取-实验备注', attribute='email', default=None
    )
    occupation = Field(
        column_name='职业', attribute='occupation', default=None
    )
    bmi = Field(
        column_name='BMI', attribute='bmi', default=None
    )
    height = Field(
        column_name='身高(m)', attribute='height', default=None
    )
    weight = Field(
        column_name='体重(Kg)', attribute='weight', default=None
    )
    note = Field(
        column_name='备注', attribute='note', default=None
    )
    hemoglobin = Field(
        column_name='血红蛋白', attribute='hemoglobin', default=None
    )
    results = Field(
        column_name='综合结果', attribute='results', default=None
    )
    detection_state = Field(
        column_name='检测状态', attribute='detection_state', default=None
    )
    register_date = Field(
        column_name='登记日期', attribute='register_date',
        widget=DateWidget(format='%Y-%m-%d'), default=None
    )
    colonoscopy_result = Field(
        column_name='肠镜结果', attribute='colonoscopy_result', default=None
    )
    smoking = Field(
        column_name='抽烟', attribute='smoking', default=None
    )
    drinking = Field(
        column_name='喝酒', attribute='drinking', default=None
    )
    colonoscopy = Field(
        column_name='肠镜', attribute='colonoscopy', default=None
    )
    cancer = Field(
        column_name='癌症、息肉史', attribute='cancer', default=None
    )
    direct_bowel_cancer = Field(
        column_name='直系肠癌史', attribute='direct_bowel_cancer', default=None
    )
    lower_digestive = Field(
        column_name='下消化道不适症状', attribute='lower_digestive',
        default=None
    )
    other_medical_history = Field(
        column_name='以下其他病史', attribute='other_medical_history',
        default=None
    )
    other_chronic_diseases = Field(
        column_name='其他慢性病', attribute='other_chronic_diseases',
    )
    questionnaire_note = Field(
        column_name='调查问卷备注', attribute='questionnaire_note',
        default=None
    )
    bd_number = Field(
        column_name="盒子发货编号", attribute="bd_number_id"
    )

    class Meta:
        model = Techsupport
        skip_unchanged = True
        import_id_fields = ('barcode',)
        fields = (
            "parent", "send_date", "made_date", "sale_man",
            'receive_date', 'sampling_date', "report_end_date",
            "project_source",
            "send_number", 'index_number', "report_date",
            "pe_number", "barcode", 'name', "gender", "age",
            'contact',
            "id_number", 'email', "occupation",
            "bmi", "height", "weight",
            "note", 'hemoglobin', "results",
            "detection_state", "register_date", "colonoscopy_result",
            "smoking", 'drinking', "colonoscopy", "cancer",
            "direct_bowel_cancer", "lower_digestive", "other_medical_history",
            "other_chronic_diseases",
            'questionnaire_note', "bd_number")
        export_order = (
            "parent", "send_date", "made_date", "sale_man",
            'receive_date', 'sampling_date', "report_end_date",
            "project_source",
            "send_number", 'index_number', "report_date",
            "pe_number", "barcode", 'name', "gender", "age",
            'contact',
            "id_number", 'email', "occupation",
            "bmi", "height", "weight",
            "note", 'hemoglobin', "results",
            "detection_state", "register_date", "colonoscopy_result",
            "smoking", 'drinking', "colonoscopy", "cancer",
            "direct_bowel_cancer", "lower_digestive", "other_medical_history",
            "other_chronic_diseases",
            'questionnaire_note', "bd_number")

    # def get_export_headers(self):
    #     return ["收样日期", "采样日期", "报告截止",
    #             "检测项目来源", "快递单号", "样品编号", "报告出具日期",
    #             "体检号", "条形码", "姓名",
    #             "性别", "年龄", "联系方式", "身份证号码",
    #             "邮箱", "职业", "BMI", "身高(m)",
    #             "体重(Kg)", "备注",
    #             "血红蛋白", "综合结果",
    #             "检测状态", "登记日期", "肠镜结果", "抽烟",
    #             "喝酒", "肠镜", "癌症、息肉史",
    #             "直系肠癌史", "下消化道不适症状", "以下其他病史", "其他慢性病",
    #             "调查问卷备注"
    #             ]

    def get_or_init_instance(self, instance_loader, row):
        instance = self.get_instance(instance_loader, row)
        if instance:
            if instance.status < 1:
                instance.status = 1
            return (instance, False)
        else:
            return (self.init_instance(row), True)
示例#21
0
class ArticleResource(resources.ModelResource):
    import_id = fields.Field(attribute='import_id',
                             column_name='No.',
                             default=None)
    editor_id = fields.Field(attribute='editor_id',
                             column_name='ID người nhập',
                             default=None)
    date_start = fields.Field(attribute='date_start',
                              column_name='Ngày bắt đầu',
                              default=None,
                              widget=DateWidget())
    date_end = fields.Field(attribute='date_end',
                            column_name='Ngày kết thúc',
                            default=None,
                            widget=DateWidget())
    time_start = fields.Field(attribute='time_start',
                              column_name='Giờ bắt đầu',
                              default=None,
                              widget=CustomTimeWidget(format='%H:%M:%S'))
    time_end = fields.Field(attribute='time_end',
                            column_name='Giờ kết thúc',
                            default=None,
                            widget=CustomTimeWidget(format='%H:%M:%S'))
    title = fields.Field(attribute='title',
                         column_name='Tiêu đề',
                         default=None)
    province = fields.Field(attribute='province',
                            column_name='Tỉnh thành',
                            default=None,
                            widget=FullNameForeignKeyWidget(Province, 'name'))
    district = fields.Field(attribute='district',
                            column_name='Quận Huyện',
                            default=None,
                            widget=FullNameForeignKeyWidget(District, 'name'))
    latitude = fields.Field(attribute='latitude',
                            column_name='Vĩ độ / latitude',
                            default=None)
    longitude = fields.Field(attribute='longitude',
                             column_name='Kinh độ / longitude',
                             default=None)
    category = fields.Field(attribute='category',
                            column_name='Thể loại',
                            default=None,
                            widget=ForeignKeyWidget(Category, 'name'))
    tags = fields.Field(attribute='tags',
                        column_name='Tag bài viết',
                        default=None)
    targets = fields.Field(attribute='targets',
                           column_name='Đối tượng',
                           default=None,
                           widget=MultiSelectFieldWidget())
    content = fields.Field(attribute='content',
                           column_name='Nội dung',
                           default=None)
    extra_info = fields.Field(attribute='extra_info',
                              column_name='Thông tin thêm',
                              default=None)
    website = fields.Field(attribute='website',
                           column_name='Website',
                           default=None)
    phone_contact = fields.Field(attribute='phone_contact',
                                 column_name='Số điện thoại liên hệ',
                                 default=None)
    place_name = fields.Field(attribute='place_name',
                              column_name='Địa điểm tổ chức',
                              default=None)
    region = fields.Field(attribute='region',
                          column_name='Khu vực',
                          default=None)
    information = fields.Field(attribute='information',
                               column_name='Thông tin khác',
                               default=None)
    address_detail = fields.Field(attribute='address_detail',
                                  column_name='Chi tiết địa chỉ',
                                  default=None)

    class Meta:
        model = Article
        # do not create new duplicate records, check unique with title and place_name
        import_id_field = ['title']
        import_id_fields = ['title', 'place_name']
        skip_unchanged = True
        report_skipped = True
        dry_run = True
        fields = (
            'category',  #
            'tags',
            'targets',  #
            'title',
            'content',
            'extra_info',
            'phone_contact',
            'website',
            'place_name',
            'region',
            'province',
            'district',
            'date_start',
            'time_start',
            'date_end',
            'time_end',
            'latitude',
            'longitude',
            'information',
            'address_detail',
            # gia ve
        )

    def skip_row(self, instance, original):
        if original == instance:
            return True
        if not self._meta.skip_unchanged:
            return False
        for field in self.get_fields():
            try:
                if list(field.get_value(instance).all()) != list(
                        field.get_value(original).all()):
                    return True
            except AttributeError:
                if field.get_value(instance) != field.get_value(original):
                    return False
        return True

    def after_import(self, queryset, data, *args, **kwargs):
        created_by = kwargs['user'].id
        rows = data.rows
        errors = data.totals['error']
        if errors == 0:
            for obj in rows:
                try:
                    article = Article.objects.get(id=obj.object_id)
                    if article.longitude and article.latitude:
                        fields = to_hash_fields(lon=article.longitude,
                                                lat=article.latitude)
                        for attr, value in fields.items():
                            setattr(article, attr, value)
                    article.created_by = created_by
                    article.save()
                except AttributeError:
                    pass
示例#22
0
class DeliveryResource(resources.ModelResource):
    # product = Field(
    #     attribute='product',
    #     column_name='product',
    #     widget=ForeignKeyWidget(Product, 'pk')
    # )
    # warehouse = Field(
    #     attribute='warehouse',
    #     column_name='warehouse',
    #     widget=ForeignKeyWidget(Warehouse, 'pk')
    # )
    # customer = Field(
    #     attribute='customer',
    #     column_name='customer',
    #     widget=ForeignKeyWidget(Customer, 'pk')
    # )
    # circuit = Field(
    #     attribute='circuit',
    #     column_name='circuit',
    #     widget=ForeignKeyWidget(Circuit, 'pk')
    # )
    delivered_at = Field(
        attribute='delivered_at',
        column_name='delivered_at',
        widget=DateWidget(),
    )
    delivered_quantity = Field(
        attribute='delivered_quantity',
        column_name='delivered_quantity',
        widget=IntegerWidget(),
    )

    # reference = Field(
    #     attribute='reference',
    #     column_name='order_reference',
    #     widget=DateWidget(),
    # )
    class Meta:
        model = Delivery
        skip_unchanged = True
        report_skipped = True
        import_id_fields = (
            'circuit',
            'warehouse',
            'product',
            'category',
            'delivered_quantity',
            'delivered_at',
        )
        exclude = ('id', )

        # use_transactions = True
        # use_bulk = True
        # skip_diff = True
        # chunk_size = 20

    # def get_queryset(self):
    #     ''' Select related object data with the query '''
    #     return super().get_queryset().select_related('circuit', 'warehouse', 'product', 'customer')  # .all()

    def before_import_row(self, row, **kwargs):
        # Clean numeric fields
        if not str(row.get('delivered_quantity')).isnumeric():
            row['delivered_quantity'] = None

        # # Get or create nested models
        # (product_model, _created) = Product.objects.get_or_create(
        #     reference=row.get('product'),
        #     # name=row.get('product'),
        # )
        # row['product'] = product_model.id

        # (warehouse_model, _created) = Warehouse.objects.get_or_create(
        #     reference=row.get('warehouse')
        # )
        # row['warehouse'] = warehouse_model.id

        # (circuit_model, _created) = Circuit.objects.get_or_create(
        #     reference=row.get('circuit'),
        # )
        # row['circuit'] = circuit_model.id
        row['delivered_at'] = datetime.datetime.strptime(
            row['delivered_at'], "%m/%d/%Y").date()
示例#23
0
class InvoicesResources(resources.ModelResource):
    """发票详细信息rsources"""

    id = Field(column_name='编号', attribute='id', default=None)
    invoice_id = Field(column_name="发票编号", attribute='invoice_id')
    salesman = Field(
        column_name="业务员",
        attribute='salesman',
    )
    contract_number = Field(column_name="合同号",
                            attribute='contract_number',
                            default=None)
    invoice_type = Field(column_name="开票类型", attribute='invoice_type')
    invoice_issuing = Field(
        column_name="开票单位",
        attribute='invoice_issuing',
        readonly=True,
    )
    invoice_title = Field(column_name="抬头", attribute='invoice_title')
    tariff_item = Field(column_name="税号", attribute='tariff_item')
    send_address = Field(column_name="对方地址", attribute='send_address')
    address_phone = Field(column_name="号码", attribute='address_phone')
    opening_bank = Field(column_name="开户行", attribute='opening_bank')
    bank_account_number = Field(column_name="账号",
                                attribute='bank_account_number')
    invoice_value = Field(column_name="开票金额",
                          attribute='invoice_value',
                          default=None)
    invoice_content = Field(column_name="发票内容", attribute='invoice_content')
    remark = Field(column_name="备注", attribute='remark')
    apply_name = Field(column_name="申请人", attribute='apply_name', default=None)
    receive_value = Field(column_name="到账金额",
                          attribute='receive_value',
                          default=None)
    receive_date = Field(
        column_name="到账时间",
        attribute='receive_date',
        default=None,
        widget=DateWidget(format='%Y-%m-%d'),
    )
    invoice_number = Field(column_name="发票号码", attribute='invoice_number')
    billing_date = Field(
        column_name="开票日期",
        attribute='billing_date',
        widget=DateWidget(format='%Y-%m-%d'),
        default=None,
    )
    invoice_send_date = Field(
        column_name="寄出日期",
        attribute='invoice_send_date',
        widget=DateWidget(format='%Y-%m-%d'),
        default=None,
    )
    tracking_number = Field(column_name="快递单号", attribute='tracking_number')
    tax_rate = Field(column_name="税率", attribute='tax_rate')
    fill_name = Field(column_name="填写人", attribute='fill_name', default=None)
    send_flag = Field(column_name="是否提交", attribute='send_flag')
    record_number = Field(column_name="业务流编号", attribute='record_number')

    class Meta:
        model = Invoices
        fields = (
            'id',
            'invoice_id',
            'salesman',
            'contract_number',
            'invoice_type',
            'invoice_issuing',
            'invoice_title',
            'tariff_item',
            'send_address',
            'address_phone',
            'opening_bank',
            'bank_account_number',
            'invoice_value',
            'remark',
            'invoice_content',
            'apply_name',
            'receive_value',
            'receive_date',
            'invoice_number',
            'billing_date',
            'invoice_send_date',
            'tracking_number',
            'tax_rate',
            'fill_name',
            'send_flag',
            'record_number',
        )
        export_order = fields
        skip_unchanged = True
        import_id_fields = ['id']

    def get_export_headers(self):
        export_headers = [
            u'编号', u'发票编号', u'业务员', u'合同号', u'开票类型', u'开票单位', u'抬头', u'税号',
            u'对方地址', u'号码', u'开户行', u'账号', u'开票金额', u'备注', u'发票内容', u'申请人',
            u'到账金额', u'到账时间', u'发票号码', u'开票日期', u'寄出时间', u'快递单号', u'税率',
            u'填写人', u'是否提交', u'业务流编号'
        ]
        return export_headers

    def dehydrate_contract_number(self, invoices):
        contract_number = invoices.record_number.contract_number.contract_number
        return contract_number

    def dehydrate_invoice_issuing(self, invoices):
        issuing_entities = {
            'shry': '上海锐翌',
            'hzth': '杭州拓宏',
            'hzry': '杭州锐翌',
            'sdry': '山东锐翌'
        }
        return issuing_entities[invoices.invoice_issuing]
示例#24
0
class ExtractionResource(GenericResource):
    sample_kind = Field(attribute="sample_kind_name", column_name='Extraction Type')
    volume_used = Field(attribute='volume_used', column_name='Volume Used (uL)', widget=DecimalWidget())
    # parent sample container
    sample_container = Field(column_name='Container Barcode')
    sample_container_coordinates = Field(column_name='Location Coord')
    # Computed fields
    container = Field(attribute='container', column_name='Nucleic Acid Container Barcode',
                      widget=ForeignKeyWidget(Container, field='barcode'))
    # Non-attribute fields
    location = Field(attribute='location', column_name='Nucleic Acid Location Barcode',
                     widget=ForeignKeyWidget(Container, field='barcode'))
    location_coordinates = Field(attribute='context_sensitive_coordinates', column_name='Nucleic Acid Location Coord')
    volume_history = Field(attribute='volume_history', widget=JSONWidget())
    concentration = Field(attribute='concentration', column_name='Conc. (ng/uL)', widget=DecimalWidget())
    source_depleted = Field(attribute='source_depleted', column_name='Source Depleted')
    # individual = Field(attribute='individual', widget=ForeignKeyWidget(Individual, field='name'))
    extracted_from = Field(attribute='extracted_from', widget=ForeignKeyWidget(Sample, field='name'))
    creation_date = Field(attribute='creation_date', column_name='Extraction Date', widget=DateWidget())
    comment = Field(attribute='comment', column_name='Comment')

    class Meta:
        model = Sample
        import_id_fields = ()
        fields = (
            'sample_kind',
            'volume_used',
            'concentration',
            'source_depleted',
            'comment',
        )
        excluded = (
            'container',
            'individual',
            'extracted_from',
            'volume_history',
        )
        export_order = (
            'sample_kind',
            'volume_used',
            'sample_container',
            'sample_container_coordinates',
            'container',
            'location',
            'location_coordinates',
            'volume_history',
            'concentration',
            'source_depleted',
            'creation_date',
            'comment',
        )

    def before_import(self, dataset, using_transactions, dry_run, **kwargs):
        skip_rows(dataset, 7)  # Skip preamble

    def import_field(self, field, obj, data, is_m2m=False):
        # More!! ugly hacks

        if field.attribute in ('source_depleted', 'context_sensitive_coordinates'):
            # Computed field, skip importing it.
            return

        if field.attribute == "sample_kind_name":
            obj.sample_kind = SampleKind.objects.get(name=data["Extraction Type"])

        if field.attribute == 'volume_history':
            # We store volume as a JSON object of historical values, so this
            # needs to be initialized in a custom way. In this case we are
            # initializing the volume history of the EXTRACTED sample, so the
            # actual history entry is of the "normal" type (UPDATE).
            vol = blank_str_to_none(data.get("Volume (uL)"))  # "" -> None for CSVs
            obj.volume_history = [create_volume_history(
                VolumeHistoryUpdateType.UPDATE,
                str(float_to_decimal(vol)) if vol is not None else ""
            )]
            return

        if field.attribute == 'extracted_from':
            obj.extracted_from = Sample.objects.get(
                container__barcode=get_normalized_str(data, "Container Barcode"),
                coordinates=get_normalized_str(data, "Location Coord"),
            )
            # Cast the "Source Depleted" cell to a Python Boolean value and
            # update the original sample if needed. This is the act of the
            # extracted sample depleting the original in the process of its
            # creation.
            obj.extracted_from.depleted = (obj.extracted_from.depleted or
                                           check_truth_like(get_normalized_str(data, "Source Depleted")))
            return

        if field.attribute == 'container':
            # Per Alex: We can make new tube racks (8x12) automatically if
            # needed for extractions, using the inputted barcode for the new
            # object.

            shared_parent_info = dict(
                barcode=get_normalized_str(data, "Nucleic Acid Location Barcode"),
                # TODO: Currently can only extract into tube racks 8x12
                #  - otherwise this logic will fall apart
                kind=CONTAINER_SPEC_TUBE_RACK_8X12.container_kind_id
            )

            try:
                parent = Container.objects.get(**shared_parent_info)
            except Container.DoesNotExist:
                parent = Container.objects.create(
                    **shared_parent_info,
                    # Below is creation-specific data
                    # Leave coordinates blank if creating
                    # Per Alex: Container name = container barcode if we
                    #           auto-generate the container
                    name=shared_parent_info["barcode"],
                    comment=f"Automatically generated via extraction template import on "
                            f"{datetime.utcnow().isoformat()}Z"
                )

            # Per Alex: We can make new tubes if needed for extractions

            # Information that can be used to either retrieve or create a new
            # tube container. It is of type tube specifically because, as
            # mentioned above, extractions currently only occur into 8x12 tube
            # racks.
            shared_container_info = dict(
                barcode=get_normalized_str(data, "Nucleic Acid Container Barcode"),
                # TODO: Currently can only extract into tubes
                #  - otherwise this logic will fall apart
                kind=CONTAINER_SPEC_TUBE.container_kind_id,
                location=parent,
                coordinates=get_normalized_str(data, "Nucleic Acid Location Coord"),
            )

            try:
                obj.container = Container.objects.get(**shared_container_info)
            except Container.DoesNotExist:
                obj.container = Container.objects.create(
                    **shared_container_info,
                    # Below is creation-specific data
                    # Per Alex: Container name = container barcode if we
                    #           auto-generate the container
                    name=shared_container_info["barcode"],
                    comment=f"Automatically generated via extraction template import on "
                            f"{datetime.utcnow().isoformat()}Z"
                )

            return

        if field.attribute == "volume_used":
            vu = blank_str_to_none(data.get("Volume Used (uL)"))  # "" -> None for CSVs
            data["Volume Used (uL)"] = float_to_decimal(vu) if vu is not None else None

        elif field.attribute == "concentration":
            conc = blank_str_to_none(data.get("Conc. (ng/uL)"))  # "" -> None for CSVs
            data["Conc. (ng/uL)"] = float_to_decimal(conc) if conc is not None else None

        super().import_field(field, obj, data, is_m2m)

    def before_save_instance(self, instance, using_transactions, dry_run):
        instance.name = instance.extracted_from.name
        instance.alias = instance.extracted_from.alias
        instance.collection_site = instance.extracted_from.collection_site
        instance.experimental_group = instance.extracted_from.experimental_group
        instance.individual = instance.extracted_from.individual
        instance.tissue_source = Sample.BIOSPECIMEN_TYPE_TO_TISSUE_SOURCE.get(
            instance.extracted_from.sample_kind.name, "")

        super().before_save_instance(instance, using_transactions, dry_run)

    def after_save_instance(self, instance, using_transactions, dry_run):
        # Update volume and depletion status of original sample, thus recording
        # that the volume was reduced by an extraction process, including an ID
        # to refer back to the extracted sample.
        instance.extracted_from.volume_history.append(create_volume_history(
            VolumeHistoryUpdateType.EXTRACTION,
            instance.extracted_from.volume - instance.volume_used,
            instance.id
        ))

        instance.extracted_from.update_comment = f"Extracted sample (imported from template) consumed " \
                                                 f"{instance.volume_used} µL."

        instance.extracted_from.save()

        super().after_save_instance(instance, using_transactions, dry_run)
        reversion.set_comment("Imported extracted samples from template.")
示例#25
0
class ContractInfoResources(resources.ModelResource):
    """合同信息导入导出resources"""

    contract_id = Field(column_name="编号", attribute='id', default=None)
    contract_code = Field(column_name="合同编码", attribute='contract_code')
    contract_number = Field(column_name="合同号", attribute='contract_number')
    client = Field(column_name="客户",
                   attribute='client',
                   default=None,
                   widget=ForeignKeyWidget(Partners, 'name'))
    box_price = Field(column_name="盒子单价", attribute='box_price', default=None)
    detection_price = Field(column_name="检测单价",
                            attribute='detection_price',
                            default=None)
    full_set_price = Field(column_name="全套价格",
                           attribute='full_set_price',
                           default=None)
    contract_money = Field(column_name="合同金额",
                           attribute='contract_money',
                           default=None)
    # count_invoice_value = Field(
    #     column_name="已开票额", attribute='count_invoice_value', default=None
    # )
    receive_invoice_value = Field(column_name="已到账额",
                                  attribute='receive_invoice_value',
                                  default=None)
    send_date = Field(column_name="寄出时间",
                      attribute='send_date',
                      default=None,
                      widget=DateWidget(format='%Y-%m-%d'))
    tracking_number = Field(column_name="邮寄单号", attribute='tracking_number')
    send_back_date = Field(column_name="寄回时间",
                           attribute='send_back_date',
                           default=None,
                           widget=DateWidget(format='%Y-%m-%d'))
    contract_type = Field(column_name="合同类型", attribute='contract_type')
    start_date = Field(column_name="起始时间",
                       attribute='start_date',
                       default=None,
                       widget=DateWidget(format='%Y-%m-%d'))
    end_date = Field(column_name="截止时间",
                     attribute='end_date',
                     default=None,
                     widget=DateWidget(format='%Y-%m-%d'))
    remark = Field(column_name="备注", attribute='remark')
    staff_name = Field(column_name="业务员",
                       attribute='staff_name__username',
                       default=None)

    class Meta:
        model = ContractsInfo
        fields = (
            'contract_id',
            'contract_code',
            'contract_number',
            'client',
            'box_price',
            'detection_price',
            'full_set_price',
            'contract_money',
            'receive_invoice_value',
            'send_date',
            'tracking_number',
            'send_back_date',
            'contract_type',
            'start_date',
            'end_date',
            'remark',
            'staff_name',
        )
        export_order = (
            'contract_id',
            'contract_code',
            'contract_number',
            'client',
            'box_price',
            'detection_price',
            'full_set_price',
            'contract_money',
            'receive_invoice_value',
            'send_date',
            'tracking_number',
            'send_back_date',
            'contract_type',
            'start_date',
            'end_date',
            'remark',
            'staff_name',
        )
        import_id_fields = ['contract_id']
        skip_unchanged = True

    def dehydrate_full_set_price(self, contractinfo):
        """计算全套价格"""
        if contractinfo.box_price is not None and contractinfo.detection_price \
                is not None:
            full_set_price = contractinfo.box_price + contractinfo.detection_price
        else:
            full_set_price = None
        return full_set_price

    def dehydrate_receive_invoice_value(self, contractinfo):
        """获取已到账总金额"""
        receive_value = 0
        bussiness_record = BusinessRecord.objects.filter(
            contract_number=contractinfo.id)
        if len(bussiness_record):
            for record_data in bussiness_record:
                payment_data = record_data.payment_set.all()
                pay = payment_data.aggregate(value=Sum('receive_value'))
                pay_amount = pay.get('value', 0)
                receive_value += pay_amount
        return receive_value
示例#26
0
class ActividadResource(resources.ModelResource):
    wp = fields.Field(column_name='wp', attribute='wp', widget=IntegerWidget())
    id_notificacion_noc = fields.Field(column_name='id notificacion noc',
                                       attribute='id_notificacion_noc',
                                       widget=IntegerWidget())
    service_supplier = fields.Field(
        column_name='service supplier',
        attribute='service_supplier',
    )
    field_manager = fields.Field(
        column_name='field manager',
        attribute='field_manager',
    )
    valor_wp_eur = fields.Field(
        column_name='valor wp eur',
        attribute='valor_wp_eur',
    )
    estacion = fields.Field(
        column_name='estacion',
        attribute='estacion',
        widget=ForeignKeyWidget(Estacion, 'nombre'),
    )
    regional = fields.Field(
        column_name='regional',
        attribute='estacion__regional',
    )
    ciudad = fields.Field(
        column_name='ciudad',
        attribute='estacion__ciudad',
    )
    responsable = fields.Field(
        column_name='responsable',
        attribute='estacion__responsable',
    )
    prioridad = fields.Field(
        column_name='prioridad',
        attribute='estacion__prioridad',
    )
    tipo_trabajo = fields.Field(
        column_name='tipo trabajo',
        attribute='tipo_trabajo',
    )
    fecha_ingreso_onair = fields.Field(column_name='fecha ingreso onair',
                                       attribute='fecha_ingreso_onair',
                                       widget=DateWidget(format='%d/%m/%Y'))
    realtifinish = fields.Field(column_name='realtifinish',
                                attribute='realtifinish',
                                widget=DateWidget(format='%d/%m/%Y'))
    fecha_integracion = fields.Field(column_name='fecha integracion',
                                     attribute='fecha_integracion',
                                     widget=DateWidget(format='%d/%m/%Y'))
    grupo_gap = fields.Field(
        column_name='grupo gap',
        attribute='grupo_gap',
    )
    fecha_estado_noc = fields.Field(column_name='fecha estado noc',
                                    attribute='fecha_estado_noc',
                                    widget=DateWidget(format='%d/%m/%Y'))
    estado_noc = fields.Field(
        column_name='estado noc',
        attribute='estado_noc',
    )
    subestado_noc = fields.Field(
        column_name='subestado noc',
        attribute='subestado_noc',
    )
    impacto_degradacion = fields.Field(
        column_name='impacto degradacion',
        attribute='impacto_degradacion',
    )
    fecha_fc_visita = fields.Field(column_name='fecha fc visita',
                                   attribute='fecha_fc_visita',
                                   widget=DateWidget(format='%d/%m/%Y'))

    # asignaciones npo
    npo_ingeniero = fields.Field(
        column_name='npo ingeniero',
        attribute='npo_ingeniero',
    )
    npo_estado_asignacion = fields.Field(
        column_name='npo estado asignacion',
        attribute='npo_estado_asignacion',
    )
    npo_concepto = fields.Field(
        column_name='npo concepto',
        attribute='npo_concepto',
    )
    npo_tipo_intervencion = fields.Field(
        column_name='npo tipo intervencion',
        attribute='npo_tipo_intervencion',
    )
    npo_fecha_asignacion = fields.Field(column_name='npo fecha asignacion',
                                        attribute='npo_fecha_asignacion',
                                        widget=DateWidget(format='%d/%m/%Y'))
    npo_fm_supervisor = fields.Field(
        column_name='npo fm supervisor',
        attribute='npo_fm_supervisor',
    )

    # asignaciones ni
    ni_ingeniero = fields.Field(
        column_name='ni ingeniero',
        attribute='ni_ingeniero',
    )
    ni_estado_asignacion = fields.Field(
        column_name='ni estado asignacion',
        attribute='ni_estado_asignacion',
    )
    ni_origen_falla = fields.Field(
        column_name='ni origen falla',
        attribute='ni_origen_falla',
    )
    ni_solver = fields.Field(
        column_name='ni solver',
        attribute='ni_solver',
    )
    ni_concepto = fields.Field(
        column_name='ni concepto',
        attribute='ni_concepto',
    )
    ni_estado_solicitud_hw = fields.Field(
        column_name='ni estado solicitud hw',
        attribute='ni_estado_solicitud_hw',
    )
    ni_tipo_intervencion = fields.Field(
        column_name='ni tipo intervencion',
        attribute='ni_tipo_intervencion',
    )
    ni_fecha_asignacion = fields.Field(column_name='ni fecha asignacion',
                                       attribute='ni_fecha_asignacion',
                                       widget=DateWidget(format='%d/%m/%Y'))
    ni_fm_supervisor = fields.Field(
        column_name='ni fm supervisor',
        attribute='ni_fm_supervisor',
    )
    clasificacion_previa = fields.Field(
        column_name='clasificacion previa',
        attribute='clasificacion_previa',
    )

    # asignaciones npo y asignaciones ni
    estado_ultimo = fields.Field(
        column_name='estado ultimo',
        attribute='estado_ultimo',
    )
    estado_unico = fields.Field(
        column_name='estado unico',
        attribute='estado_unico',
    )

    def for_delete(self, row, instance):
        return self.fields['subestado'].clean(row)

    class Meta:
        model = Actividad
        exclude = (
            'creado',
            'actualizado',
        )
        export_order = (
            # actividad
            'id',
            'wp',
            'id_notificacion_noc',
            'agrupador',
            'service_supplier',
            'field_manager',
            'valor_wp_eur',
            'estacion',
            'regional',
            'ciudad',
            'responsable',
            'prioridad',
            'banda',
            'proyecto',
            'escenario',
            'tipo_trabajo',
            'fecha_ingreso_onair',
            'realtifinish',
            'fecha_integracion',
            'grupo_gap',
            'fecha_estado_noc',
            'estado_noc',
            'subestado_noc',
            'impacto_degradacion',
            'fecha_fc_visita',
            # asignaciones npo
            'npo_ingeniero',
            'npo_estado_asignacion',
            'npo_concepto',
            'npo_tipo_intervencion',
            'npo_fecha_asignacion',
            'npo_fm_supervisor',
            # asignaciones ni
            'ni_ingeniero',
            'ni_estado_asignacion',
            'ni_origen_falla',
            'ni_solver',
            'ni_concepto',
            'ni_estado_solicitud_hw',
            'ni_tipo_intervencion',
            'ni_fecha_asignacion',
            'ni_fm_supervisor',
            'clasificacion_previa',
            # asignaciones npo y asignaciones ni
            'estado_ultimo',
            'estado_unico',
            # actividad
            'estado',
            'subestado',
        )
示例#27
0
class MasterIncidentResource(resources.ModelResource):
    Date = fields.Field(column_name='Date',
                        attribute='Date',
                        widget=DateWidget(format='%m-%d-%Y'))
    Time_of_Incident = fields.Field(column_name='Time_Inc',
                                    attribute='Time_of_Incident',
                                    default=time())
    province = fields.Field(column_name='Province',
                            attribute='Province',
                            widget=ForeignKeyWidget(Province, 'name'))
    district = fields.Field(column_name='District',
                            attribute='District',
                            widget=DistrictForeignKey(District, 'name'))
    City_Village = fields.Field(column_name='City_Vill',
                                attribute='City_Village')
    Police_District = fields.Field(column_name='Police_Dist',
                                   attribute='Police_District')
    incident_type = fields.Field(column_name='Inc_Type',
                                 attribute='Incident_Type',
                                 widget=ForeignKeyWidget(IncidentType, 'name'))
    incident_subtype = fields.Field(column_name='Inc_Subtype',
                                    attribute='Incident_Subtype',
                                    widget=IncidentSubTypeForeignKey(
                                        IncidentSubtype, 'name'))
    Description_of_Incident = fields.Field(column_name='Inc_Desc',
                                           attribute='Description_of_Incident')
    initiator = fields.Field(column_name='Initiator',
                             attribute='Initiator',
                             widget=ForeignKeyWidget(Organization, 'code'))
    target = fields.Field(column_name='Target',
                          attribute='Target',
                          widget=ForeignKeyWidget(Organization, 'code'))

    class Meta:
        model = MasterIncident
        export_order = fields = (
            'Single_ID',
            'Date',
            'Time_of_Incident',
            'province',
            'district',
            'City_Village',
            'Area',
            'Police_District',
            'incident_type',
            'incident_subtype',
            'Description_of_Incident',
            'HPA',
            'initiator',
            'target',
            'IGCHO',
            'Kill_Natl',
            'Kill_Intl',
            'Kill_ANSF',
            'Kill_IM',
            'Kill_ALP_PGM',
            'Kill_AOG',
            'Kill_ISKP',
            'Inj_Natl',
            'Inj_Intl',
            'Inj_ANSF',
            'Inj_IM',
            'Inj_ALP_PGM',
            'Inj_AOG',
            'Inj_ISKP',
            'Abd_Natl',
            'Abd_Intl',
            'Abd_ANSF',
            'Abd_IM',
            'Abd_ALP_PGM',
            'Latitude',
            'Longitude',
            'PRMO',
            'UNDSS',
            'INSO',
        )
        exclude = (
            'id',
            'created_at',
            'updated_at',
        )
        clean_model_instances = True
        import_id_fields = ('Single_ID', )

    def before_import_row(self, row, **kwargs):
        single_id = row.get('Single_ID')
        province = row.get('Province')
        district = row.get('District')
        date = row.get('Date')
        incident_type = row.get('Inc_Type')
        incident_subtype = row.get('Inc_Subtype')
        initiator = row.get('Initiator')
        target = row.get('Target')
        timeofincident = row.get('Time_Inc')
        hpa = row.get('HPA')
        prmo = str.lower('no' if row.get('PRMO') is None else row.get('PRMO'))
        undss = str.lower(
            'no' if row.get('UNDSS') is None else row.get('UNDSS'))
        inso = str.lower('no' if row.get('INSO') is None else row.get('INSO'))
        igcho = str.lower(
            'no' if row.get('IGCHO') is None else row.get('IGCHO'))

        if prmo == 'yes':
            row['PRMO'] = 1
        else:
            row['PRMO'] = 0

        if undss == 'yes':
            row['UNDSS'] = 1
        else:
            row['UNDSS'] = 0

        if inso == 'yes':
            row['INSO'] = 1
        else:
            row['INSO'] = 0

        if igcho == 'yes':
            row['IGCHO'] = 1
        else:
            row['IGCHO'] = 0

        if single_id == 'null' or single_id == None:
            raise ValidationError("Single ID cannot be null")

        if date is not None or date != 'null':
            if isinstance(date, datetime):
                value = date.date().strftime('%m-%d-%Y')
                try:
                    datetime.strptime(str(value), '%m-%d-%Y')
                    row['Date'] = date.date().strftime('%m-%d-%Y')
                except (ValueError, TypeError):
                    raise ValidationError(
                        "Incorrect data format, should be MM-DD-YYYY")
            else:
                raise ValidationError(
                    "Incorrect data format, Date should be in date format")
        else:
            raise ValidationError("Date cannot be blank")

        if timeofincident is not None or timeofincident != 'null':
            try:
                if isinstance(timeofincident, (
                        datetime,
                        time,
                )):
                    row['Time_Inc'] = timeofincident.strftime('%H:%M:%S')
                else:
                    row['Time_Inc'] = timeofincident
            except ValueError:
                raise ValidationError(
                    "Incorrect time format, should be hh:mm:ss")
        else:
            raise ValidationError('Time of Incident cannot be blank')

        if hpa is None:
            row['HPA'] = 'no'
        elif hpa.lower() == 'yes' or hpa.lower() == 'no':
            row['HPA'] = hpa.lower()
        else:
            raise ValidationError('HPA value must be "yes" or "no"')

        prov = Province.objects.filter(name=province)
        if not bool(prov):
            raise ValidationError('Province name %s cannot be found' %
                                  province)

        dist = District.objects.filter(name=district,
                                       province__name__contains=province)
        if not bool(dist):
            raise ValidationError(
                'District name %s cannot be found in %s Province' %
                (district, province))

        itype = IncidentType.objects.filter(name=incident_type)
        if not bool(itype):
            raise ValidationError('Incident Type name %s cannot be found' %
                                  incident_type)

        if incident_subtype is not None:
            istype = IncidentSubtype.objects.filter(
                name=incident_subtype,
                incidenttype__name__contains=incident_type)
            if not bool(istype):
                raise ValidationError(
                    'Incident Sub Type name %s cannot be found in %s Incident Type'
                    % (incident_subtype, incident_type))

        init = Organization.objects.filter(code=initiator)
        if not bool(init):
            raise ValidationError('Initiator name %s cannot be found' % init)

        target = Organization.objects.filter(code=target)
        if not bool(target):
            raise ValidationError('Target name %s cannot be found' % target)
示例#28
0
文件: resources.py 项目: d120/kifplan
class KiffelResource(resources.ModelResource):
    """
    importer mapping for Kiffel CSV
    Important: make sure to convert the export file to **comma**-separated
    CSV (,). Semicolons will not work.
    """

    hashtag = fields.Field(attribute='id', column_name='#')
    vorname = fields.Field(attribute='vorname', column_name='Vorname')
    nachname = fields.Field(attribute='nachname', column_name='Nachname')
    email = fields.Field(attribute='email', column_name='E-Mail')
    nickname = fields.Field(attribute='nickname',
                            column_name='Namensschildername')
    student = fields.Field(attribute='student',
                           column_name='Student',
                           widget=JaNeinBooleanWidget,
                           default=False)
    hochschule = fields.Field(attribute='hochschule',
                              column_name='Hochschule/Ort/Verein')
    kommentar_public = fields.Field(attribute='kommentar_public',
                                    column_name='Öffentlicher Kommentar')
    kommentar_orga = fields.Field(attribute='kommentar_orga',
                                  column_name='Kommentar für Orgas')
    anreise_geplant = fields.Field(attribute='anreise_geplant',
                                   column_name='Anreise',
                                   widget=DateWidget(format='%d.%m.%Y'))
    abreise_geplant = fields.Field(attribute='abreise_geplant',
                                   column_name='Abreise',
                                   widget=DateWidget(format='%d.%m.%Y'))
    ernaehrungsgewohnheit = fields.Field(attribute='ernaehrungsgewohnheit',
                                         column_name='Ernährungsgewohnheit')
    lebensmittelunvertraeglichkeiten = fields.Field(
        attribute='lebensmittelunvertraeglichkeiten',
        column_name='Lebensmittelunverträglichkeiten etc.')
    volljaehrig = fields.Field(attribute='volljaehrig',
                               column_name='Über 18',
                               widget=JaNeinBooleanWidget,
                               default=False)
    eigener_schlafplatz = fields.Field(attribute='eigener_schlafplatz',
                                       column_name='Habe eigenen Schlafplatz',
                                       widget=JaNeinBooleanWidget,
                                       default=False)
    tshirt_groesse = fields.Field(attribute='tshirt_groesse',
                                  column_name='T-Shirt-Größe')
    nickname_auf_tshirt = fields.Field(
        attribute='nickname_auf_tshirt',
        column_name='Namensschildername auf T-Shirt (+5€)',
        widget=JaNeinBooleanWidget,
        default=False)
    kapuzenjacke_groesse = fields.Field(attribute='kapuzenjacke_groesse',
                                        column_name='Kapuzenjacke (25€)')
    nickname_auf_kapuzenjacke = fields.Field(
        attribute='nickname_auf_kapuzenjacke',
        column_name='Namensschildername auf Kapuzenjacke (+5€)',
        widget=JaNeinBooleanWidget,
        default=False)
    weitere_tshirts = fields.Field(
        attribute='weitere_tshirts',
        column_name='zusätzliche T-Shirts (in gleicher Größe)')
    interesse_theater = fields.Field(attribute='interesse_theater',
                                     column_name='Interesse an Theaterbesuch',
                                     widget=JaNeinBooleanWidget,
                                     default=False)
    interesse_esoc = fields.Field(attribute='interesse_esoc',
                                  column_name='Interesse an ESOC Führung',
                                  widget=JaNeinBooleanWidget,
                                  default=False)
    anmeldung_angelegt = fields.Field(
        attribute='anmeldung_angelegt',
        column_name='Angelegt',
        widget=DateWidget(format='%Y-%m-%d %H:%M:%S %z'))
    anmeldung_aktualisiert = fields.Field(
        attribute='anmeldung_aktualisiert',
        column_name='Aktualisiert',
        widget=DateWidget(format='%Y-%m-%d %H:%M:%S %z'))

    def before_save_instance(self, instance, dry_run):
        instance.status = 'angemeldet'
        instance.ist_orga = False
        instance.kdv_id = EAN8.get_random()
        while Person.objects.filter(kdv_id=instance.kdv_id).count() > 0:
            instance.kdv_id = EAN8.get_random()

    class Meta:
        model = Person
        exclude = ('datum_bezahlt', 'datum_tshirt_erhalten',
                   'datum_teilnahmebestaetigung_erhalten', 'kommentar',
                   'engel_handle', 'twitter_handle')
        import_id_fields = ('hashtag', )
示例#29
0
class SampleResource(GenericResource):
    # Simple model fields
    sample_kind = Field(attribute='sample_kind_name', column_name='Sample Kind')
    name = Field(attribute='name', column_name='Sample Name')
    alias = Field(attribute='alias', column_name='Alias')
    experimental_group = Field(attribute='experimental_group', column_name='Experimental Group', widget=JSONWidget())
    collection_site = Field(attribute='collection_site', column_name='Collection Site')
    tissue_source = Field(attribute='tissue_source', column_name='Tissue Source')
    concentration = Field(attribute='concentration', column_name='Conc. (ng/uL)', widget=DecimalWidget())
    depleted = Field(attribute='depleted', column_name='Source Depleted')
    creation_date = Field(attribute='reception_date', column_name='Reception Date', widget=DateWidget())
    phenotype = Field(attribute='phenotype', column_name='Phenotype')
    comment = Field(attribute='comment', column_name='Comment')

    # Computed fields to include in export / display on import

    container_barcode = Field(attribute='container_barcode', column_name='Container Barcode')
    container_kind = Field(attribute='container_kind', column_name='Container Kind')
    container_name = Field(attribute='container_name', column_name='Container Name')
    container_location = Field(attribute='container_location', column_name='Location Barcode')

    # Oddly enough, Location Coord is contextual - when Container Kind is one with coordinates, this
    # specifies the sample's location within the container itself. Otherwise, it specifies the location of
    # the container within the parent container. TODO: Ideally this should be tweaked
    context_sensitive_coordinates = Field(attribute='context_sensitive_coordinates', column_name='Location Coord')

    individual_name = Field(attribute='individual_name', column_name='Individual Name')
    sex = Field(attribute='individual_sex', column_name='Sex')
    taxon = Field(attribute='individual_taxon', column_name='Taxon')
    cohort = Field(attribute='individual_cohort', column_name='Cohort')
    pedigree = Field(attribute='individual_pedigree', column_name='Pedigree')
    mother_name = Field(attribute='individual_mother', column_name='Mother ID')
    father_name = Field(attribute='individual_father', column_name='Father ID')

    volume = Field(attribute='volume', column_name='Volume (uL)', widget=DecimalWidget())

    COMPUTED_FIELDS = frozenset((
        "volume",
        "individual_id",
        "individual_name",
        "individual_sex",
        "individual_taxon",
        "individual_cohort",
        "individual_pedigree",
        "individual_mother",
        "individual_father",
        "container_barcode",
        "container_kind",
        "container_name",
        "container_location",
        "context_sensitive_coordinates",
    ))

    class Meta:
        model = Sample
        import_id_fields = ("container__barcode", "context_sensitive_coordinates")
        fields = (
            "sample_kind",
            "name",
            "alias",
            "concentration",
            "collection_site",
            "container__barcode",
        )
        excluded = ("volume_history", "individual", "depleted", "container")
        export_order = (
            "sample_kind",
            "name",
            "alias",
            "cohort",
            "experimental_group",
            "taxon",
            "container_kind",
            "container_name",
            "container_barcode",
            "container_location",
            "context_sensitive_coordinates",
            "individual_name",
            "sex",
            "pedigree",
            "mother_name",
            "father_name",
            "volume",
            "concentration",
            "collection_site",
            "tissue_source",
            "creation_date",
            "phenotype",
            "comment",
        )

    def before_import(self, dataset, using_transactions, dry_run, **kwargs):
        skip_rows(dataset, 6)

    def import_obj(self, obj, data, dry_run):
        super().import_obj(obj, data, dry_run)

        # Sample import can optionally create new individuals in the system;
        # or re-use existing ones. Along with the individual associated with
        # the sample, if mother/father IDs are specified, corresponding records
        # can be created by the system.

        taxon = normalize_scientific_name(get_normalized_str(data, "Taxon"))
        pedigree = str_cast_and_normalize(get_normalized_str(data, "Pedigree"))
        cohort = str_cast_and_normalize(get_normalized_str(data, "Cohort"))

        mother = None
        father = None

        if data["Mother ID"]:
            mother, _ = Individual.objects.get_or_create(
                name=get_normalized_str(data, "Mother ID"),
                sex=Individual.SEX_FEMALE,
                taxon=taxon,  # Mother has same taxon as offspring
                **({"pedigree": pedigree} if pedigree else {}),  # Mother has same pedigree as offspring
                **({"cohort": cohort} if cohort else {}),  # Mother has same cohort as offspring TODO: Confirm
            )

        if data["Father ID"]:
            father, _ = Individual.objects.get_or_create(
                name=get_normalized_str(data, "Father ID"),
                sex=Individual.SEX_MALE,
                taxon=taxon,  # Father has same taxon as offspring
                **({"pedigree": pedigree} if pedigree else {}),  # Father has same pedigree as offspring
                **({"cohort": cohort} if cohort else {}),  # Father has same cohort as offspring TODO: Confirm
            )

        # TODO: This should throw a nicer warning if the individual already exists
        # TODO: Warn if the individual exists but pedigree/cohort is different
        individual, individual_created = Individual.objects.get_or_create(
            name=get_normalized_str(data, "Individual ID"),
            sex=get_normalized_str(data, "Sex", default=Individual.SEX_UNKNOWN),
            taxon=taxon,
            **({"pedigree": pedigree} if pedigree else {}),
            **({"cohort": cohort} if cohort else {}),
            **({"mother": mother} if mother else {}),
            **({"father": father} if father else {}),
        )
        obj.individual = individual

        # If we're doing a dry run (i.e. uploading for confirmation) and we're
        # re-using an individual, create a warning for the front end; it's
        # quite possible that this is a mistake.
        # TODO: API-generalized method for doing this (currently not possible for React front-end)

        if dry_run and not individual_created:
            request = CrequestMiddleware.get_request()
            messages.warning(request, f"Row {data['#']}: Using existing individual '{individual}' instead of creating "
                                      f"a new one")

        vol = blank_str_to_none(data.get("Volume (uL)"))  # "" -> None for CSVs

        # We store volume as a JSON object of historical values, so this needs to be initialized in a custom way.
        obj.volume_history = [create_volume_history(
            VolumeHistoryUpdateType.UPDATE,
            str(float_to_decimal(vol)) if vol is not None else ""
        )]

    def import_field(self, field, obj, data, is_m2m=False):
        # Ugly hacks lie below

        normalized_container_kind = get_normalized_str(data, "Container Kind").lower()

        if field.attribute == "sample_kind_name":
            obj.sample_kind = SampleKind.objects.get(name=data["Sample Kind"])

        elif field.attribute == "container_barcode" and normalized_container_kind in SAMPLE_CONTAINER_KINDS:
            # Oddly enough, Location Coord is contextual - when Container Kind
            # is one with coordinates, this specifies the sample's location
            # within the container itself. Otherwise, it specifies the location
            # of the container within the parent container.
            # TODO: Ideally this should be tweaked

            location_barcode = get_normalized_str(data, "Location Barcode")

            try:
                container_parent = Container.objects.get(barcode=location_barcode)
            except Container.DoesNotExist:
                if location_barcode:
                    # If a parent container barcode was specified, raise a
                    # better error message detailing what went wrong.
                    # Otherwise, we assume it was left blank on purpose.
                    raise Container.DoesNotExist(f"Container with barcode {location_barcode} does not exist")
                container_parent = None

            container_data = dict(
                kind=normalized_container_kind,
                name=get_normalized_str(data, "Container Name"),
                barcode=get_normalized_str(data, "Container Barcode"),
                **(dict(location=container_parent) if container_parent else dict(location__isnull=True)),
            )

            normalized_coords = get_normalized_str(data, "Location Coord")

            if normalized_container_kind in SAMPLE_CONTAINER_KINDS_WITH_COORDS:
                # Case where container itself has a coordinate system; in this
                # case the SAMPLE gets the coordinates (e.g. with a plate.)
                obj.coordinates = normalized_coords
            else:
                # Case where the container gets coordinates within the parent
                # (e.g. a tube in a rack).
                container_data["coordinates"] = normalized_coords

            # If needed, create a sample-holding container to store the current
            # sample; or retrieve an existing one with the correct barcode.
            # This will throw an error if the kind specified mismatches with an
            # existing barcode record in the database, which serves as an
            # ad-hoc additional validation step.

            container, _ = Container.objects.get_or_create(**container_data)
            obj.container = container

            return

        elif field.attribute == "experimental_group":
            # Experimental group is stored as a JSON array, so parse out what's
            # going on by splitting the string value into potentially multiple
            # values. If any value is blank, skip it, since it was either the
            # "null" value ("" -> []) or an accidental trailing comma/similar.
            data["Experimental Group"] = json.dumps([
                g.strip()
                for g in RE_SEPARATOR.split(get_normalized_str(data, "Experimental Group"))
                if g.strip()
            ])

        elif field.attribute == "comment":
            # Normalize None comments to empty strings
            data["Comment"] = get_normalized_str(data, "Comment")

        elif field.attribute == "alias":
            # if numeric value entered as alias make sure it's a string
            data["Alias"] = get_normalized_str(data, "Alias")

        elif field.attribute in self.COMPUTED_FIELDS:
            # Ignore importing this, since it's a computed property.
            return

        super().import_field(field, obj, data, is_m2m)

    def before_save_instance(self, instance, using_transactions, dry_run):
        # TODO: Don't think this is needed
        instance.individual.save()
        super().before_save_instance(instance, using_transactions, dry_run)

    def after_save_instance(self, instance, using_transactions, dry_run):
        super().after_save_instance(instance, using_transactions, dry_run)
        reversion.set_comment("Imported samples from template.")
示例#30
0
class ForecastResource(resources.ModelResource):
    ''' Data import class '''
    # product = Field(
    #     attribute='product',
    #     column_name='product',
    #     widget=ForeignKeyWidget(Product, 'pk')
    # )
    # warehouse = Field(
    #     attribute='warehouse',
    #     column_name='warehouse',
    #     widget=ForeignKeyWidget(Warehouse, 'pk')
    # )
    # customer = Field(
    #     attribute='customer',
    #     column_name='customer',
    #     widget=ForeignKeyWidget(Customer, 'pk')
    # )
    # circuit = Field(
    #     attribute='circuit',
    #     column_name='circuit',
    #     widget=ForeignKeyWidget(Circuit, 'pk')
    # )
    forecast_date = Field(
        attribute='forecast_date',
        column_name='forecast_date',
        widget=DateWidget(),
    )
    version = Field(
        attribute='version',
        column_name='version',
        widget=ForeignKeyWidget(Version, 'pk'),
    )
    forecasted_quantity = Field(
        attribute='forecasted_quantity',
        column_name='forecasted_quantity',
        # widget=IntegerCleanWidget(),
        widget=IntegerWidget(),
    )

    # product_cost = Field(
    #     column_name='product_cost',
    #     widget=DecimalWidget(),
    # )
    # product_type = Field(
    #     column_name='product_type',
    # )
    # product_name = Field(
    #     column_name='product_name',
    # )

    class Meta:
        model = Forecast
        # instance_loader_class = CachedInstanceLoader
        # fields = ('id', 'product', 'forecast_date', 'forecasted_quantity',)
        exclude = (
            'status',
            'created_at',
            'updated_at',
        )
        use_transactions = True
        use_bulk = True
        batch_size = 1000

        skip_diff = True
        skip_unchanged = False