class PTokenAnnotationResource(resources.ModelResource): token_indices = fields.Field(attribute='token_indices', widget=widgets.CharWidget()) obj_head = fields.Field(attribute='obj_head', widget=widgets.CharWidget()) gov_head = fields.Field(attribute='gov_head', widget=widgets.CharWidget()) gov_obj_syntax = fields.Field(attribute='gov_obj_syntax', widget=widgets.CharWidget()) adp_pos = fields.Field(attribute='adp_pos', widget=widgets.CharWidget()) gov_pos = fields.Field(attribute='gov_pos', widget=widgets.CharWidget()) obj_pos = fields.Field(attribute='obj_pos', widget=widgets.CharWidget()) gov_supersense = fields.Field(attribute='gov_supersense', widget=widgets.CharWidget()) obj_supersense = fields.Field(attribute='obj_supersense', widget=widgets.CharWidget()) is_gold = fields.Field(attribute='is_gold', widget=widgets.BooleanWidget()) annotator_cluster = fields.Field(attribute='annotator_cluster', widget=widgets.CharWidget()) is_transitive = fields.Field(attribute='is_transitive', widget=widgets.BooleanWidget()) gov_head_index = fields.Field(attribute='gov_head_index', widget=widgets.IntegerWidget()) obj_head_index = fields.Field(attribute='obj_head_index', widget=widgets.IntegerWidget()) is_typo = fields.Field(attribute='is_typo', widget=widgets.BooleanWidget()) is_abbr = fields.Field(attribute='is_abbr', widget=widgets.BooleanWidget()) mwe_subtokens = fields.Field(attribute='mwe_subtokens', widget=widgets.CharWidget()) main_subtoken_indices = fields.Field(attribute='main_subtoken_indices', widget=widgets.CharWidget()) main_subtoken_string = fields.Field(attribute='main_subtoken_string', widget=widgets.CharWidget()) obj_case = fields.Field( column_name='obj_case', attribute='obj_case', widget=ObjCaseWidget()) adposition = fields.Field( column_name='adposition_id', attribute='adposition', widget=ForeignKeyWidget(ms.Adposition)) construal = fields.Field( column_name='construal_id', attribute='construal', widget=ForeignKeyWidget(ms.Construal)) sentence = fields.Field( column_name='sent_id', attribute='sentence', widget=SentenceForeignKeyWidget(ms.CorpusSentence, 'sent_id')) usage = fields.Field( column_name='usage_id', attribute='usage', widget=ForeignKeyWidget(ms.Usage)) class Meta: model = ms.PTokenAnnotation import_id_fields = ('sentence', 'token_indices') fields = ('token_indices', 'adposition', 'construal', 'usage', 'sentence', 'obj_case', 'obj_head', 'gov_head', 'gov_obj_syntax', 'adp_pos', 'gov_pos', 'obj_pos', 'gov_supersense', 'obj_supersense', 'is_gold', 'annotator_cluster', 'is_transitive', 'gov_head_index', 'obj_head_index', 'is_typo', 'is_abbr', 'mwe_subtokens', 'main_subtoken_indices', 'main_subtoken_string')
class GroupResource(resources.ModelResource): description = fields.Field(attribute='groupprofile__description', widget=widgets.PostSaveWidget(widgets.CharWidget()), readonly=False) priority = fields.Field(attribute='groupprofile__priority', widget=widgets.PostSaveWidget(widgets.IntegerWidget()), readonly=False) can_sms_notifications = fields.Field(attribute='groupprofile__can_sms_notifications', widget=widgets.PostSaveWidget(widgets.BooleanWidget()), readonly=False) user_set = fields.Field(attribute='user_set', widget=widgets.ManyToManyWidget(User, field='username', separator=',')) class Meta: model = Group fields = ('id', 'name', 'description', 'priority', 'can_sms_notifications', 'user_set') export_order = ('id', 'name', 'description', 'priority', 'can_sms_notifications', 'user_set') # save userprofile related fields def after_post_save_instance(self, instance, row, using_transactions, dry_run): instance.groupprofile.save()
class B(BookResource): delete = fields.Field(widget=widgets.BooleanWidget()) def for_delete(self, row, instance): return self.fields['delete'].clean(row) def before_delete_instance(self, instance, dry_run): super(B, self).before_delete_instance(instance, dry_run) if dry_run: self.before_delete_instance_dry_run = True else: self.before_delete_instance_dry_run = False def delete_instance(self, instance, using_transactions=True, dry_run=False): super(B, self).delete_instance(instance, using_transactions, dry_run) if dry_run: self.delete_instance_dry_run = True else: self.delete_instance_dry_run = False def after_delete_instance(self, instance, dry_run): super(B, self).after_delete_instance(instance, dry_run) if dry_run: self.after_delete_instance_dry_run = True else: self.after_delete_instance_dry_run = False
class CorpusSentenceResource(resources.ModelResource): corpus = fields.Field( column_name='corpus_name', attribute='corpus', widget=CorpusForeignKeyWidget(ms.Corpus, 'name')) language = fields.Field( column_name='language_name', attribute='language', widget=ForeignKeyWidget(ms.Language, 'name')) sent_id = fields.Field(attribute='sent_id', widget=widgets.CharWidget()) orthography = fields.Field(attribute='orthography', widget=widgets.CharWidget()) is_parallel = fields.Field(attribute='is_parallel', widget=widgets.BooleanWidget()) doc_id = fields.Field(attribute='doc_id', widget=widgets.CharWidget()) text = fields.Field(attribute='text', widget=widgets.CharWidget()) tokens = fields.Field(attribute='tokens', widget=widgets.CharWidget()) word_gloss = fields.Field(attribute='word_gloss', widget=widgets.CharWidget()) sent_gloss = fields.Field(attribute='sent_gloss', widget=widgets.CharWidget()) note = fields.Field(attribute='note', widget=widgets.CharWidget()) mwe_markup = fields.Field(attribute='mwe_markup', widget=widgets.CharWidget()) class Meta: model = ms.CorpusSentence import_id_fields = ('sent_id',) fields = ('corpus', 'sent_id', 'language', 'orthography', 'is_parallel', 'doc_id', 'text', 'tokens', 'word_gloss', 'sent_gloss', 'note', 'mwe_markup')
class DocumentResources(resources.ModelResource): # date_expired = Field(attribute='date_expired', column_name='date_testing') # myfield = Field(column_name='myfield') # full_title = Field() delete = fields.Field(widget=widgets.BooleanWidget()) def for_delete(self, row, instance): return self.fields['delete'].clean(row) class Meta: model = Document fields = ('id__title__text__user__target__date_expired') @receiver(post_import, dispatch_uid='balabala...') def _post_import(model, **kwargs): pass @receiver(post_export, dispatch_uid='balabala...') def _post_export(model, **kwargs): pass '''widjets = { 'date': {'format': '%d.%m.%Y'} }''' # skip_unchanged = True # report_skipped = False # import_id_fields = ('isbn',) # exclude = ('imported') # export_order = ('id', 'user', 'date', 'text', 'title' ) '''def dehydrate_full_title(self, document):
class DetailResource(BaseImportExportResource): entered_into_lims = fields.Field( attribute='entered_into_lims', widget=widgets.BooleanWidget(), ) external_data = fields.Field( attribute='external_data', widget=widgets.BooleanWidget(), ) def import_data(self, dataset, **kwargs): """Overridden from import_action to lock table then call super().import_data()""" print( f'DEBUG: in DetailResource.{funcname()}, about to do table locking super' ) tables_need_some_lockin = [models.JAXIdDetail] return super().import_data(dataset, table_locks=tables_need_some_lockin, **kwargs) def before_import(self, dataset, using_transactions=True, dry_run=False, **kwargs): print( 'DEBUG: in DetailResource.before_import, about to call outer check_rows_before_the_import' ) id_prefix = 'J' id_field_name = 'jaxid' id_model = JAXIdDetail check_rows_before_the_import(dataset, id_prefix=id_prefix, id_field_name=id_field_name, id_model=id_model, **kwargs) class Meta: model = JAXIdDetail import_id_fields = ('jaxid', ) fields = model.all_field_names() export_order = fields all_fields = (fields, ) import_format = None
class UserImportResource(UserResource): reset_password = fields.Field(column_name='reset_password', widget=widgets.BooleanWidget(), readonly=False) def __init__(self): super().__init__() self.is_new = False self.request = None self.reset_password = False class Meta: model = User fields = ('id', 'username', 'first_name', 'last_name', 'email', 'mobile_number', 'is_staff', 'is_dispatcher', 'is_active', 'reset_password') export_order = ('id', 'username', 'first_name', 'last_name', 'email', 'mobile_number', 'is_staff', 'is_dispatcher', 'is_active', 'reset_password') def before_import(self, dataset, using_transactions, dry_run, **kwargs): self.request = kwargs.get('request', None) def before_save_instance(self, instance, using_transactions, dry_run): self.is_new = not instance.id def after_post_save_instance(self, instance, row, using_transactions, dry_run): # save userprofile related fields super().after_post_save_instance(instance, row, using_transactions, dry_run) # reset_password self.reset_password = self.fields['reset_password'].clean(row) # is new? if self.reset_password is None: self.reset_password = self.is_new if self.reset_password and not dry_run: # reset password PasswordReset(instance.email, self.request).send_reset() def after_import_row(self, row, row_result, **kwargs): logger.info('after_import_row') row_result.diff[8] = '{}'.format(1 if self.reset_password else 0) logger.info(row) logger.info(row_result.__dict__)
class UserResource(resources.ModelResource): is_dispatcher = fields.Field(attribute='userprofile__is_dispatcher', widget=widgets.PostSaveWidget(widgets.BooleanWidget()), readonly=False) mobile_number = fields.Field(attribute='userprofile__mobile_number', widget=widgets.PostSaveWidget(widgets.CharWidget()), readonly=False) class Meta: model = User fields = ('id', 'username', 'first_name', 'last_name', 'email', 'mobile_number', 'is_staff', 'is_dispatcher', 'is_active') export_order = ('id', 'username', 'first_name', 'last_name', 'email', 'mobile_number', 'is_staff', 'is_dispatcher', 'is_active') # save userprofile related fields def after_post_save_instance(self, instance, row, using_transactions, dry_run): instance.userprofile.save()
class CorporateOrganizationResource(resources.ModelResource): id = fields.Field(attribute="id") name = fields.Field(attribute="name", column_name="Name of Organization - Req.") partner = fields.Field(attribute="partner - Req.", widget=widgets.BooleanWidget(), column_name="Is Partner") partner_type = fields.Field(attribute="partner_type", column_name="Partner Type") class Meta: fields = ('id', 'name', 'partner', 'partner_type') skip_unchanged = True model = CorporateOrganization
class BuildResource(InvenTreeResource): """Class for managing import/export of Build data.""" # For some reason, we need to specify the fields individually for this ModelResource, # but we don't for other ones. # TODO: 2022-05-12 - Need to investigate why this is the case! id = Field(attribute='pk') reference = Field(attribute='reference') title = Field(attribute='title') part = Field(attribute='part', widget=widgets.ForeignKeyWidget(part.models.Part)) part_name = Field(attribute='part__full_name', readonly=True) overdue = Field(attribute='is_overdue', readonly=True, widget=widgets.BooleanWidget()) completed = Field(attribute='completed', readonly=True) quantity = Field(attribute='quantity') status = Field(attribute='status') batch = Field(attribute='batch') notes = Field(attribute='notes') class Meta: """Metaclass options""" models = Build skip_unchanged = True report_skipped = False clean_model_instances = True exclude = [ 'lft', 'rght', 'tree_id', 'level', 'metadata', ]
class PurchaseOrderResource(InvenTreeResource): """Class for managing import / export of PurchaseOrder data.""" # Add number of line items line_items = Field(attribute='line_count', widget=widgets.IntegerWidget(), readonly=True) # Is this order overdue? overdue = Field(attribute='is_overdue', widget=widgets.BooleanWidget(), readonly=True) class Meta: """Metaclass""" model = PurchaseOrder skip_unchanged = True clean_model_instances = True exclude = [ 'metadata', ]
class OfferResource(CustomModelResource): name = fields.Field( column_name='name', attribute='name', default=None, widget=widgets.CharWidget(), ) price = fields.Field( column_name='price', attribute='price', default=0, widget=widgets.DecimalWidget(), ) product = fields.Field( column_name='product', attribute='product', widget=widgets.ForeignKeyWidget(Product, field='name'), ) is_active = fields.Field(column_name='is_active', attribute='is_active', default=1, widget=widgets.BooleanWidget()) attributes = fields.Field(column_name='attributes', attribute='attributes', default={}, widget=widgets.CharWidget()) class Meta: model = Offer fields = ('name', 'product', 'price', 'is_active', 'attributes') export_order = ('name', 'product', 'attributes', 'is_active', 'price') import_id_fields = ('name', )
class AdpositionRevisionResource(import_export.resources.ModelResource): name = fields.Field(column_name='adposition_name', attribute='name', widget=widgets.CharWidget()) lang = fields.Field( column_name='language_name', attribute='lang', widget=ForeignKeyWidget(ms.Language, 'name')) morphtype = fields.Field(attribute='morphtype', widget=MorphTypeWidget()) transitivity = fields.Field(attribute='transitivity', widget=TransitivityWidget()) obj_cases = fields.Field(column_name='obj_case', attribute='obj_cases', widget=ObjCasesWidget()) is_pp_idiom = fields.Field(column_name='is_pp_idiom', attribute='is_pp_idiom', widget=widgets.BooleanWidget()) def skip_row(self, instance, original): m = instance if ms.Adposition.objects.filter(current_revision__metadatarevision__adpositionrevision__lang__name=m.lang.name, current_revision__metadatarevision__adpositionrevision__name=m.name): return True return False # handle revision creation def save_instance(self, instance, using_transactions=True, dry_run=False): try: ex_article = ms.Adposition.objects.get(current_revision__metadatarevision__adpositionrevision__lang__name='English', current_revision__metadatarevision__adpositionrevision__name='at').article except AttributeError: raise Exception("Xposition Import: Please create Adposition 'at' to use as a model!") m = instance lang_article = ms.Language.objects.get(name=m.lang.name).article if ms.Adposition.objects.filter(current_revision__metadatarevision__adpositionrevision__lang__name=m.lang.name, current_revision__metadatarevision__adpositionrevision__name=m.name): # thep = ms.Adposition.objects.get(current_revision__metadatarevision__adpositionrevision__lang__name=m.lang.name, # current_revision__metadatarevision__adpositionrevision__name=m.name) # thep.newRevision(ADMIN_REQUEST, # commit=True, # name=m.name, # lang=m.lang, # morphtype=m.morphtype, # transitivity=m.transitivity, # obj_cases=m.obj_cases, # is_pp_idiom=m.is_pp_idiom) return # code taken from wiki/plugins/metadata/forms.py newarticle = ArticleMetadataFormFunctions(ADMIN_REQUEST).newArticle_without_category(name=m.name, ex_article=ex_article, parent=lang_article.urlpath_set.all()[0], slug=m.name) # associate the article with the SupersenseRevision m.article = newarticle # create the Supersense, add the article, category, and revision p = ms.Adposition() p.article = newarticle p.add_revision(m, ADMIN_REQUEST, article_revision=newarticle.current_revision, save=True) # cannot delay saving the new adposition revision m.save() p.save() class Meta: model = ms.AdpositionRevision import_id_fields = ('name', 'lang',) fields = ('name', 'lang', 'morphtype', 'transitivity', 'obj_cases', 'is_pp_idiom') instance_loader_class = AdpositionRevisionInstanceLoader
class PersonResource(ModelResource): class Meta: model = Person fields = ( 'id', 'inner_id', 'reg_number', 'phone', 'email', 'reg_number', 'title', 'front_title', 'fullname', 'back_title', 'gender', 'religion', 'nation', 'place_of_birth', 'date_of_birth', 'last_education_level', 'last_education_institution', 'last_education_name', 'year_graduate', 'created_at', 'is_employee_applicant') # id = Field(attribute='id', column_name='id', widget=UUIDWidget()) inner_id = Field(attribute='inner_id', column_name='inner_id', readonly=True, widget=widgets.CharWidget()) pid = Field(attribute='pid', column_name='pid', widget=widgets.CharWidget()) reg_number = Field(attribute='reg_number', column_name='reg_number', readonly=True, widget=widgets.IntegerWidget()) phone = Field(attribute='phone2', column_name='phone2', widget=widgets.CharWidget()) email = Field(attribute='email', column_name='email', widget=widgets.CharWidget()) title = Field(attribute='title', column_name='title', widget=widgets.CharWidget()) front_title = Field(attribute='front_title', column_name='front_title', widget=widgets.CharWidget()) fullname = Field(attribute='fullname', column_name='fullname', widget=widgets.CharWidget()) back_title = Field(attribute='back_title', column_name='back_title', widget=widgets.CharWidget()) gender = Field(attribute='gender', column_name='gender', widget=widgets.CharWidget()) religion = Field(attribute='religion', column_name='religion', widget=widgets.CharWidget()) nation = Field(attribute='nation', column_name='nation', widget=widgets.CharWidget()) place_of_birth = Field(attribute='place_of_birth', column_name='place_of_birth', widget=widgets.CharWidget()) # date_of_birth = Field(attribute='date_of_birth', column_name='date_of_birth', widget=ExcelDateWidget(date_format='%d/%m/%Y')) last_education_level = Field(attribute='last_education_level', column_name='last_education_level',widget=widgets.CharWidget()) last_education_institution = Field(attribute='last_education_institution', column_name='last_education_institution', widget=widgets.CharWidget()) last_education_name = Field(attribute='last_education_name', column_name='last_education_name',widget=widgets.CharWidget()) year_graduate = Field(attribute='year_graduate', column_name='year_graduate', widget=widgets.CharWidget()) # created_at = fields.Field(attribute='created_at', column_name='created_at',widget=ExcelDateWidget(date_format='%d/%m/%Y %H:%M')) is_employee_applicant = fields.Field(attribute='is_employee_applicant',column_name='is_employee_applicant',widget=widgets.BooleanWidget()) is_teacher_applicant = fields.Field(attribute='is_teacher_applicant',column_name='is_teacher_applicant',widget=widgets.BooleanWidget()) is_matriculant = fields.Field(attribute='is_matriculant',column_name='is_matriculant',widget=widgets.BooleanWidget())
class ProductResource(CustomModelResource): # id = fields.Field(default=generate_Jid(prefix='J'), # readonly=True, # widget=widgets.CharWidget(), # ) name = fields.Field( column_name='name', attribute='name', default=None, widget=widgets.CharWidget(), ) # price = fields.Field(column_name='price', attribute='price', # default=0, # widget=widgets.DecimalWidget(), # ) description = fields.Field( column_name='description', attribute='description', default=None, widget=widgets.CharWidget(), ) # producer = fields.Field(column_name='producer', attribute='producer', # default=None, # widget=widgets.CharWidget(), # ) category = fields.Field( column_name='category', attribute='category', default=None, widget=widgets.ForeignKeyWidget(ProductCategory, field='name'), ) producer = fields.Field( column_name='producer', attribute='producer', default=None, widget=widgets.ForeignKeyWidget(Producer, field='name'), ) attributes = fields.Field( column_name='attributes', attribute='attributes', default=None, widget=CustomManyToManyWidget(ProductAttribute, field="name"), ) is_active = fields.Field(column_name='is_active', attribute='is_active', default=1, widget=widgets.BooleanWidget()) discount_policy = fields.Field(column_name='discount_policy', attribute='discount_policy', default={}, widget=widgets.CharWidget()) # delete = fields.Field(column_name='delete', attribute='delete', # default=0, # widget=widgets.BooleanWidget()) # def for_delete(self, row, instance): # return self.fields['delete'].clean(row) class Meta: model = Product fields = ('id', 'name', 'description', 'producer', 'category', 'is_active', 'attributes', 'discount_policy') export_order = ('id', 'name', 'producer', 'is_active', 'category', 'attributes', 'description', 'discount_policy') # import_id_fields = ('name',) def dehydrate_str_choices(self, obj): if obj.id: return obj.str_choices()
def setUp(self): self.widget = widgets.BooleanWidget()
class B(BookResource): delete = fields.Field(widget=widgets.BooleanWidget()) def for_delete(self, row, instance): return self.fields['delete'].clean(row)
def setUp(self): self.widget = widgets.PostSaveWidget(widgets.BooleanWidget())