| 27 | |
| 28 | |
| 29 | class Item(metaclass=ItemMetaclass): |
| 30 | __unique_key__: List = [] |
| 31 | __pipelines__: List = None |
| 32 | |
| 33 | def __init__(self, **kwargs): |
| 34 | self.__dict__ = kwargs |
| 35 | |
| 36 | def __repr__(self): |
| 37 | return "<{}: {}>".format(self.item_name, tools.dumps_json(self.to_dict)) |
| 38 | |
| 39 | def __getitem__(self, key): |
| 40 | return self.__dict__[key] |
| 41 | |
| 42 | def __setitem__(self, key, value): |
| 43 | self.__dict__[key] = value |
| 44 | |
| 45 | def update(self, *args, **kwargs): |
| 46 | """ |
| 47 | 更新字段,与字典使用方法一致 |
| 48 | """ |
| 49 | self.__dict__.update(*args, **kwargs) |
| 50 | |
| 51 | def update_strict(self, *args, **kwargs): |
| 52 | """ |
| 53 | 更新严格更新,只更新item中有的字段 |
| 54 | """ |
| 55 | for key, value in dict(*args, **kwargs).items(): |
| 56 | if key in self.__dict__: |
| 57 | self.__dict__[key] = value |
| 58 | |
| 59 | def pre_to_db(self): |
| 60 | """ |
| 61 | 入库前的处理 |
| 62 | """ |
| 63 | pass |
| 64 | |
| 65 | @property |
| 66 | def to_dict(self): |
| 67 | propertys = {} |
| 68 | for key, value in self.__dict__.items(): |
| 69 | if key not in ( |
| 70 | "__name__", |
| 71 | "__table_name__", |
| 72 | "__name_underline__", |
| 73 | "__update_key__", |
| 74 | "__unique_key__", |
| 75 | "__pipelines__", |
| 76 | ): |
| 77 | if key.startswith(f"_{self.__class__.__name__}"): |
| 78 | key = key.replace(f"_{self.__class__.__name__}", "") |
| 79 | propertys[key] = value |
| 80 | |
| 81 | return propertys |
| 82 | |
| 83 | def to_sql(self, auto_update=False, update_columns=()): |
| 84 | return tools.make_insert_sql( |
| 85 | self.table_name, self.to_dict, auto_update, update_columns |
| 86 | ) |
no outgoing calls