Total Complexity | 3 |
Total Lines | 35 |
Duplicated Lines | 100 % |
Changes | 0 |
Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | from .model import * |
||
5 | View Code Duplication | class BoxVendor(Base): |
|
|
|||
6 | __tablename__ = 'box_vendors' |
||
7 | |||
8 | id = Column(Integer, primary_key=True, nullable=False) |
||
9 | vendor_id = Column(Integer, ForeignKey("vendors.id"), nullable=False) |
||
10 | box_id = Column(Integer, ForeignKey("boxes.id"), nullable=False) |
||
11 | item_number = Column(String(255), nullable=False) |
||
12 | |||
13 | enabled = Column(Boolean, default=True, nullable=False) |
||
14 | |||
15 | vendor = relationship( |
||
16 | vendor.Vendor, |
||
17 | primaryjoin="and_(BoxVendor.vendor_id==Vendor.id, BoxVendor.enabled==True)", |
||
18 | backref="boxes" |
||
19 | ) |
||
20 | box = relationship( |
||
21 | box.Box, |
||
22 | primaryjoin="and_(BoxVendor.box_id==Box.id, BoxVendor.enabled==True)", |
||
23 | backref="vendors" |
||
24 | ) |
||
25 | |||
26 | |||
27 | def __init__(self, vendor, box, item_number, enabled=True): |
||
28 | self.vendor_id = vendor.id |
||
29 | self.box_id = box.id |
||
30 | self.item_number = item_number |
||
31 | self.enabled = enabled |
||
32 | |||
33 | @classmethod |
||
34 | def from_id(cls, id): |
||
35 | return DBSession.query(cls).filter(cls.id == id).one() |
||
36 | |||
37 | @classmethod |
||
38 | def from_number_fuzzy(cls, number): |
||
39 | return DBSession.query(cls).filter(cls.item_number.like('%{}%'.format(number))).all() |
||
40 |