Exemplo n.º 1
0
class PartResource(resources.ModelResource):
    part_number = fields.Field(attribute="part_number", column_name='Part number')
    description = fields.Field(attribute="description", column_name='Description')
    supplier_name = fields.Field(attribute="supplier_name", column_name='Supplier')
    variants = fields.Field(attribute="variants", column_name='Variants')
    count = fields.Field(attribute="count", column_name='Count')
    reported_on = fields.Field(attribute="reported_on", column_name='Reported on ( dd/mm/yy )',
                               widget=widgets.DateWidget(format='%d-%b-%y'))
    short_on = fields.Field(attribute="short_on", column_name='Short on ( dd/mm/yy )',
                            widget=widgets.DateWidget(format='%d-%b-%y'))
    shop = fields.Field(attribute="shop", column_name='Shop')
    pmc = fields.Field(attribute="pmc", column_name='PMC')
    team = fields.Field(attribute="team", column_name='Team')
    backlog = fields.Field(attribute="backlog", column_name='Backlog')
    region = fields.Field(attribute="region", column_name='Region')
    unloading_point = fields.Field(attribute="unloading_point", column_name='Unloadingpoint')
    p_q = fields.Field(attribute="p_q", column_name='PQ indiator')
    quantity = fields.Field(attribute="quantity", column_name='Quantity Avl DICV (vehicle#scovered)')
    quantity_expected = fields.Field(attribute="quantity_expected", column_name='Outlook')
    planned_vehicle_qty = fields.Field(attribute="planned_vehicle_qty", column_name='Plan')
    eta_dicv = fields.Field(attribute="eta_dicv", column_name='ETA')
    truck_details = fields.Field(attribute="truck_details", column_name='Truck details')
    shortage_reason = fields.Field(attribute="shortage_reason", column_name='Reason for shortage')
    status = fields.Field(attribute="status", column_name='Status')
    class Meta:
        import_id_fields = ('part_number',)
        model = Part
        widgets = {
            'reported_on': {'format': '%d-%b-%y'},
            'short_on': {'format': '%d-%b-%y'},
        }
Exemplo n.º 2
0
class StockItemResource(InvenTreeResource):
    """Class for managing StockItem data import/export."""

    # Custom managers for ForeignKey fields
    part = Field(attribute='part', widget=widgets.ForeignKeyWidget(Part))

    part_name = Field(attribute='part__full_name', readonly=True)

    supplier_part = Field(attribute='supplier_part', widget=widgets.ForeignKeyWidget(SupplierPart))

    supplier = Field(attribute='supplier_part__supplier__id', readonly=True)

    customer = Field(attribute='customer', widget=widgets.ForeignKeyWidget(Company))

    supplier_name = Field(attribute='supplier_part__supplier__name', readonly=True)

    status_label = Field(attribute='status_label', readonly=True)

    location = Field(attribute='location', widget=widgets.ForeignKeyWidget(StockLocation))

    location_name = Field(attribute='location__name', readonly=True)

    belongs_to = Field(attribute='belongs_to', widget=widgets.ForeignKeyWidget(StockItem))

    build = Field(attribute='build', widget=widgets.ForeignKeyWidget(Build))

    parent = Field(attribute='parent', widget=widgets.ForeignKeyWidget(StockItem))

    sales_order = Field(attribute='sales_order', widget=widgets.ForeignKeyWidget(SalesOrder))

    purchase_order = Field(attribute='purchase_order', widget=widgets.ForeignKeyWidget(PurchaseOrder))

    # Date management
    updated = Field(attribute='updated', widget=widgets.DateWidget())

    stocktake_date = Field(attribute='stocktake_date', widget=widgets.DateWidget())

    def after_import(self, dataset, result, using_transactions, dry_run, **kwargs):
        """Rebuild after import to keep tree intact."""
        super().after_import(dataset, result, using_transactions, dry_run, **kwargs)

        # Rebuild the StockItem tree(s)
        StockItem.objects.rebuild()

    class Meta:
        """Metaclass options."""

        model = StockItem
        skip_unchanged = True
        report_skipped = False
        clean_model_instance = True

        exclude = [
            # Exclude MPTT internal model fields
            'lft', 'rght', 'tree_id', 'level',
            # Exclude internal fields
            'serial_int', 'metadata',
        ]
Exemplo n.º 3
0
class StockItemResource(ModelResource):
    """ Class for managing StockItem data import/export """

    # Custom manaegrs for ForeignKey fields
    part = Field(attribute='part', widget=widgets.ForeignKeyWidget(Part))

    part_name = Field(attribute='part__full_ame', readonly=True)

    supplier_part = Field(attribute='supplier_part',
                          widget=widgets.ForeignKeyWidget(SupplierPart))

    supplier = Field(attribute='supplier_part__supplier__id', readonly=True)

    supplier_name = Field(attribute='supplier_part__supplier__name',
                          readonly=True)

    status_label = Field(attribute='status_label', readonly=True)

    location = Field(attribute='location',
                     widget=widgets.ForeignKeyWidget(StockLocation))

    location_name = Field(attribute='location__name', readonly=True)

    belongs_to = Field(attribute='belongs_to',
                       widget=widgets.ForeignKeyWidget(StockItem))

    build = Field(attribute='build', widget=widgets.ForeignKeyWidget(Build))

    sales_order = Field(attribute='sales_order',
                        widget=widgets.ForeignKeyWidget(SalesOrder))

    build_order = Field(attribute='build_order',
                        widget=widgets.ForeignKeyWidget(Build))

    purchase_order = Field(attribute='purchase_order',
                           widget=widgets.ForeignKeyWidget(PurchaseOrder))

    # Date management
    updated = Field(attribute='updated', widget=widgets.DateWidget())

    stocktake_date = Field(attribute='stocktake_date',
                           widget=widgets.DateWidget())

    def after_import(self, dataset, result, using_transactions, dry_run,
                     **kwargs):

        super().after_import(dataset, result, using_transactions, dry_run,
                             **kwargs)

        # Rebuild the StockItem tree(s)
        StockItem.objects.rebuild()

    class Meta:
        model = StockItem
        skip_unchanged = True
        report_skipped = False
        clean_model_instance = True
Exemplo n.º 4
0
class DemandeResource(resources.ModelResource):
    frais_date = fields.Field(attribute='frais_date',
                              column_name='Date',
                              widget=widgets.DateWidget())
    num_releve = fields.Field(attribute='num_releve',
                              column_name="Numéro d'édition")
    chauffeur = fields.Field(attribute='chauffeur', column_name='Chauffeur')
    code_activite = fields.Field(attribute='code_activite',
                                 column_name='Code frais')
    montant_ttc = fields.Field(attribute='montant_ttc',
                               column_name='Total',
                               widget=widgets.DecimalWidget())

    class Meta:
        model = Demande
        fields = ('frais_date', 'num_releve', 'chauffeur', 'code_activite',
                  'montant_ttc')
        export_order = ('frais_date', 'num_releve', 'chauffeur',
                        'code_activite', 'montant_ttc')

    def dehydrate_user(self, Demande):
        return '%s' % (Demande.user.username)

    def dehydrate_chauffeur(self, Demande):
        return '%s' % (Demande.chauffeur.code_matricule)
Exemplo n.º 5
0
 def after_import_row(self, row, row_result, **kwargs):
     super().after_import_row(row, row_result, **kwargs)
     if row_result.import_type == results.RowResult.IMPORT_TYPE_NEW:
         date_hired = widgets.DateWidget().clean(row.get('date_hired'))
         if date_hired is not None and date_hired <= datetime.date.today():
             instance = self._meta.model.objects.get(
                 pk=row_result.object_id)
             instance.payee.start_contract(date_hired)
Exemplo n.º 6
0
class StockItemResource(ModelResource):
    """ Class for managing StockItem data import/export """

    # Custom manaegrs for ForeignKey fields
    part = Field(attribute='part', widget=widgets.ForeignKeyWidget(Part))

    part_name = Field(attribute='part__full_ame', readonly=True)

    supplier_part = Field(attribute='supplier_part',
                          widget=widgets.ForeignKeyWidget(SupplierPart))

    supplier = Field(attribute='supplier_part__supplier__id', readonly=True)

    supplier_name = Field(attribute='supplier_part__supplier__name',
                          readonly=True)

    status_label = Field(attribute='status_label', readonly=True)

    location = Field(attribute='location',
                     widget=widgets.ForeignKeyWidget(StockLocation))

    location_name = Field(attribute='location__name', readonly=True)

    belongs_to = Field(attribute='belongs_to',
                       widget=widgets.ForeignKeyWidget(StockItem))

    customer = Field(attribute='customer',
                     widget=widgets.ForeignKeyWidget(Company))

    build = Field(attribute='build', widget=widgets.ForeignKeyWidget(Build))

    purchase_order = Field(attribute='purchase_order',
                           widget=widgets.ForeignKeyWidget(PurchaseOrder))

    # Date management
    updated = Field(attribute='updated', widget=widgets.DateWidget())

    stocktake_date = Field(attribute='stocktake_date',
                           widget=widgets.DateWidget())

    class Meta:
        model = StockItem
        skip_unchanged = True
        report_skipped = False
        clean_model_instance = True
Exemplo n.º 7
0
class MoResource(resources.ModelResource):
  number = Field(attribute='number', column_name='mo')
  status = Field(attribute='status', column_name='status_mo')
  schedule_id = Field(attribute='schedule_id', column_name='schedule', widget=widgets.ForeignKeyWidget(Schedule, 'number'))
  sku_id = Field(attribute='sku_id', column_name='sku', widget=widgets.ForeignKeyWidget(Sku, 'name'))
  workcenter_id = Field(attribute='workcenter_id', column_name="workcenter", widget=widgets.ForeignKeyWidget(WorkCenter, 'name'))
  entry_date = Field(attribute='entry_date', column_name='entry_date', widget=widgets.DateWidget())
  finish_date = Field(attribute='finish_date', column_name='finish_date', widget=widgets.DateWidget())
  order_qty = Field(attribute='order_qty', column_name='order_qty')
  received_qty = Field(attribute='received_qty', column_name='received_qty')
  project = Field(attribute='project', column_name='project')
  
  class Meta:
    model = Mo
    skip_unchanged = True
    report_skipped = True
  
  def save_instance(self, instance, using_transactions=True, dry_run=False):
    try:
      super().save_instance(instance, using_transactions, dry_run)
    except IntegrityError:
      pass
Exemplo n.º 8
0
class TimeSeriesResource(resources.ModelResource):
    valuation_date = fields.Field(
        column_name='valuation_date', attribute='valuation_date',
        widget=widgets.DateWidget(format='%d/%m/%Y'))

    def before_import(self, dataset, *args, **kwargs):
        if 'id' not in dataset.headers:
            dataset.insert_col(0, lambda row: "", header='id')
        if dataset.headers:
            dataset.headers = [str(header).lower().strip()
                               for header in dataset.headers]

    class Meta:
        model = TimeSeries
        # skip_unchanged = True
        # report_skipped = True
        fields = ['id', 'valuation_date', 'underlying', 'mid']
        import_id_fields = ['id', ]
Exemplo n.º 9
0
class FeeRequestResource(resources.ModelResource):
    date = fields.Field(attribute='date', column_name='Date', widget=widgets.DateWidget())
    id = fields.Field(attribute='id', column_name="Numéro d'édition")
    driver = fields.Field(attribute='driver', column_name='Chauffeur')
    activity = fields.Field(attribute='activity', column_name='Code frais')
    

    class Meta:
        model = FeeRequest
        fields = ('date', 'id', 'driver', 'activity')
        export_order = ('date', 'id', 'driver', 'activity')
        
         
    def dehydrate_user(self, FeeRequest):
        return '%s' % (FeeRequest.user.username)


    def dehydrate_driver(self, FeeRequest):
        return '%s' % (FeeRequest.driver.code_matricule)
Exemplo n.º 10
0
class StudentResource(resources.ModelResource):
    enrollment_id = fields.Field(column_name='Enrollment ID', attribute="enrollment_id")
    enrollment_type = fields.Field(column_name="Enrollment Type", attribute='enrollment_type', widget=widgets.ForeignKeyWidget(EnrollmentType))
    course_enrollment_mode = fields.Field(column_name="Enrollment Mode", attribute="course_enrollment_mode", widget=widgets.ForeignKeyWidget(CourseEnrollmentMode))
    student_name = fields.Field(column_name='Name', attribute="student_name")
    registered_for = fields.Field(column_name='UG/PG', attribute="registered_for", widget=widgets.ForeignKeyWidget(ProgramType))
    course_enrolled = fields.Field(column_name='Course', attribute="course_enrolled", widget=widgets.ForeignKeyWidget(Course))
    course_specialization = fields.Field(column_name='Specialization', attribute="course_specialization", widget=widgets.ForeignKeyWidget(CourseSpecialization))
    batch = fields.Field(column_name='Batch', attribute="batch", widget=widgets.IntegerWidget())
    father_name = fields.Field(column_name='Father Name', attribute="father_name")
    mother_name = fields.Field(column_name='Mother Name', attribute="mother_name")
    dob = fields.Field(column_name='DOB', attribute="dob", widget=widgets.DateWidget())
    address = fields.Field(column_name='Address', attribute="address")
    study_center = fields.Field(column_name='Study Center', attribute="study_center", widget=widgets.ForeignKeyWidget(StudyCenter))

    class Meta:
        model = Student
        import_id_fields = ('enrollment_id',)

    def before_save_instance(self, instance, dry_run):
        print 'before save instance'
Exemplo n.º 11
0
 def setUp(self):
     self.date = date(1868, 8, 13)
     self.widget = widgets.DateWidget('%d.%m.%Y')
Exemplo n.º 12
0
class ProjectResource(resources.ModelResource):
    name = fields.Field(column_name='Name', attribute='name')
    type = fields.Field(column_name='Type', attribute='type')
    start_date = fields.Field(column_name='Start Date', attribute='start_date', widget=widgets.DateWidget(format='%d/%m/%Y'))
    status = fields.Field(column_name='Status', attribute='get_status')
    region__name = fields.Field(column_name='Region', attribute='region__name')
    district__name = fields.Field(column_name='District', attribute='district__name')
    town = fields.Field(column_name='Town', attribute='town')
    duration = fields.Field(column_name='Duration (Yrs)', attribute='duration')
    authority__name = fields.Field(column_name='Authority', attribute='authority__name')
    consultant__name = fields.Field(column_name='Consultant', attribute='consultant__name')
    quantity_demanded = fields.Field(column_name="Qty Demanded (Tons)", attribute='quantity_demanded')
    quantity_supplied = fields.Field(column_name="Qty Supplied (Tons)", attribute='quantity_supplied')
    longitude = fields.Field(column_name="Longitude", attribute="longitude")
    latitude = fields.Field(column_name="Latitude", attribute="latitude")
    remarks = fields.Field(column_name="Remarks", attribute="remarks")

    def __init__(self, user):
        self.user = user

    class Meta:
        model = models.Project
        fields = ['name', 'type', 'start_date', 'status', 'region__name', 'district__name', 'town', 'duration', 'authority__name', 'consultant__name', 'quantity_demanded', 'quantity_supplied', 'latitude', 'longitude', 'remarks']
        export_order = fields

    def get_queryset(self):
        return models.Project.objects.all().order_by('-date_created')
Exemplo n.º 13
0
 def test_default_format(self):
     self.widget = widgets.DateWidget()
     self.assertEqual(("%Y-%m-%d", ), self.widget.formats)
Exemplo n.º 14
0
 def test_clean_raises_ValueError(self):
     self.widget = widgets.DateWidget('x')
     with self.assertRaisesRegex(ValueError, "Enter a valid date."):
         self.widget.clean('2021-05-01')
Exemplo n.º 15
0
class ItemResource(resources.ModelResource):
    client_name = fields.Field(attribute='device__client__name',
                               column_name='Hospital name')
    manufacturer_name = fields.Field(
        attribute='device__product__manufacturer__name',
        column_name='Manufacturer name')
    hospital_number = fields.Field(attribute='device__hospital_number',
                                   column_name='Hospital part #',
                                   widget=widgets.CharWidget())
    model_number = fields.Field(attribute='device__product__model_number',
                                column_name='Manufacturer part #',
                                widget=widgets.CharWidget())
    cost_type = fields.Field(attribute='cost_type', column_name='Cost type')
    serial_number = fields.Field(attribute='serial_number',
                                 column_name='Serial number',
                                 widget=widgets.CharWidget())
    lot_number = fields.Field(attribute='lot_number',
                              column_name='Lot number',
                              widget=widgets.CharWidget())
    purchased_date = fields.Field(attribute='purchased_date',
                                  column_name='Purchased date',
                                  widget=widgets.DateWidget())
    expired_date = fields.Field(attribute='expired_date',
                                column_name='Expiry date',
                                widget=widgets.DateWidget())
    bulk_discount_percent = fields.Field(attribute='bulk_discount_percent',
                                         column_name='Bulk discount percent',
                                         widget=widgets.NumberWidget())
    bulk_discount_value = fields.Field(attribute='bulk_discount_value',
                                       column_name='Bulk discount value',
                                       widget=widgets.NumberWidget())
    discount_order = fields.Field(attribute='discount_order',
                                  column_name='Discount order',
                                  widget=widgets.NumberWidget())
    discount_start_date = fields.Field(attribute='discount_start_date',
                                       column_name='Discount start date',
                                       widget=widgets.DateWidget())
    discount_end_date = fields.Field(attribute='discount_end_date',
                                     column_name='Discount end date',
                                     widget=widgets.DateWidget())

    COST_TYPES_DICT = dict((name, value) for value, name in COST_TYPES)

    class Meta:
        model = Item
        fields = ('id', 'client_name', 'manufacturer_name', 'hospital_number',
                  'model_number', 'serial_number', 'lot_number',
                  'purchased_date', 'expired_date', 'cost_type',
                  'bulk_discount_percent', 'bulk_discount_value',
                  'discount_order', 'discount_start_date', 'discount_end_date')
        export_order = ('id', 'client_name', 'manufacturer_name',
                        'hospital_number', 'model_number', 'serial_number',
                        'lot_number', 'purchased_date', 'expired_date',
                        'cost_type', 'bulk_discount_percent',
                        'bulk_discount_value', 'discount_order',
                        'discount_start_date', 'discount_end_date')

    @staticmethod
    def _build_bulk_discount(item):
        return (item.id and item.bulk_discount) or Discount()

    def dehydrate_cost_type(self, item):
        return item.get_cost_type_display()

    def dehydrate_bulk_discount_percent(self, item):
        return self._build_bulk_discount(item).percent

    def dehydrate_bulk_discount_value(self, item):
        return self._build_bulk_discount(item).value

    def dehydrate_discount_order(self, item):
        return self._build_bulk_discount(item).order

    def dehydrate_discount_start_date(self, item):
        return self._build_bulk_discount(item).start_date

    def dehydrate_discount_end_date(self, item):
        return self._build_bulk_discount(item).end_date

    def get_field_value(self, field_name, row, default=None):
        return row.get(self.fields.get(field_name).column_name) or default

    def before_import_row(self, row, **kwargs):
        field = 'cost_type'
        row[self.fields[field].column_name] = self.COST_TYPES_DICT.get(
            self.get_field_value(field, row), UNIT_COST)

    def init_instance(self, row=None):
        instance = super().init_instance(row)
        client = Client.objects.get(
            name=self.get_field_value('client_name', row))
        product = Product.objects.get(
            model_number=self.get_field_value('model_number', row))
        instance.device = Device.objects.get_or_create(client=client,
                                                       product=product)[0]
        return instance

    def get_or_init_instance(self, instance_loader, row):
        instance, created = super().get_or_init_instance(instance_loader, row)

        hospital_number = self.get_field_value('hospital_number', row)
        if hospital_number and (instance.device.hospital_number !=
                                hospital_number):
            device = instance.device
            device.hospital_number = hospital_number
            device.save()

        bulk_discount_percent = self.get_field_value('bulk_discount_percent',
                                                     row,
                                                     default=0)
        bulk_discount_value = self.get_field_value('bulk_discount_value',
                                                   row,
                                                   default=0)
        if bulk_discount_percent or bulk_discount_value:
            device = instance.device
            client_price = ClientPrice.objects.get_or_create(
                client=device.client, product=device.product)[0]
            bulk_discount = client_price.discount_set.get_or_create(
                name='Bulk',
                cost_type=self.get_field_value('cost_type', row),
                order=self.get_field_value('discount_order', row),
                start_date=self.get_field_value('discount_start_date',
                                                row,
                                                default=None),
                end_date=self.get_field_value('discount_end_date',
                                              row,
                                              default=None),
                apply_type=PRE_DOCTOR_ORDER,
                discount_type=PERCENT_DISCOUNT
                if bulk_discount_percent > 0 else VALUE_DISCOUNT,
                percent=bulk_discount_percent,
                value=bulk_discount_value,
            )[0]
            instance.build_bulk_discount(bulk_discount)

        return instance, created

    def after_save_instance(self, instance, using_transactions, dry_run):
        another_cost_type_orphan_discounts = instance.discounts.exclude(
            cost_type=instance.cost_type).all()
        instance.discounts.remove(*another_cost_type_orphan_discounts)
        instance.save_bulk_discount()
Exemplo n.º 16
0
class ProductResource(resources.ModelResource):

    pdid = fields.Field(
        column_name='PdID',  # column name on file
        attribute='pdid',
        widget=CustomDecimalWidget())  # attribute on table
    name = fields.Field(
        column_name='Product',  # column name on file
        attribute='name',
    )  # attribute on table
    descr1 = fields.Field(
        column_name='Descr1',  # column name on file
        attribute='descr1',
    )  # attribute on table
    descr2 = fields.Field(
        column_name='Descr2',  # column name on file
        attribute='descr2',
    )  # attribute on table
    detail = fields.Field(
        column_name='Detail',  # column name on file
        attribute='detail',
    )  # attribute on table
    remarks = fields.Field(
        column_name='Remarks',  # column name on file
        attribute='remarks',
    )  # attribute on table
    status_id = fields.Field(column_name='ProductStatus',
                             attribute='status',
                             widget=widgets.ForeignKeyWidget(
                                 ProductStatus, 'name'))
    price = fields.Field(column_name='Price',
                         attribute='price',
                         widget=widgets.DecimalWidget())
    date = fields.Field(column_name='TIMESTAMP',
                        attribute='date',
                        widget=widgets.DateWidget('%Y.%m.%d'))
    Handlager = fields.Field(
        column_name='Handlager',
        attribute='handlager',
    )
    lang_id = fields.Field(
        column_name='lang_id',
        attribute='lang_id',
    )
    weight = fields.Field(
        column_name='Weigth',
        attribute='weight',
    )
    ptype = fields.Field(
        column_name='PType',
        attribute='ptype',
    )
    HarmonizedCode = fields.Field(
        column_name='HarmonizedCode',
        attribute='harmonizedcode',
    )
    eccn = fields.Field(
        column_name='ECCN',
        attribute='eccn',
    )
    lkz = fields.Field(
        column_name='LKZ',
        attribute='lkz',
    )
    ag = fields.Field(
        column_name='AG',
        attribute='ag',
    )
    imageurl = fields.Field(
        column_name='imageURL',
        attribute='imageurl',
    )

    class Meta:
        model = Product
        fields = (
            'pdid',
            'name',
            'descr1',
            'descr2',
            'detail',
            'remarks',
            'status_id',
            'price',
            'date',
            'Handlager',
            'lang_id',
            'weight',
            'ptype',
            'HarmonizedCode',
            'eccn',
            'lkz',
            'ag',
            'imageurl',
        )
        list_display = (
            'name',
            'descr1',
        )
        import_id_fields = ('pdid', )
        skip_unchanged = True  # skip unchanged records
        report_skipped = True  # whether skipped records appear in the import Result object
        exclude = ('id', )