ItemTag   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 23
Duplicated Lines 100 %

Importance

Changes 0
Metric Value
dl 23
loc 23
rs 10
c 0
b 0
f 0
wmc 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A from_id() 3 3 1
A __init__() 3 3 1

How to fix   Duplicated Code   

Duplicated Code

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 *
2
from . import tag
3
from . import item
4
5 View Code Duplication
class ItemTag(Base):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
6
    __tablename__ = 'item_tags'
7
8
    id      = Column(Integer, primary_key=True, nullable=False)
9
    tag_id  = Column(Integer, ForeignKey('tags.id'), nullable=False)
10
    item_id = Column(Integer, ForeignKey('items.id'), nullable=False)
11
12
    deleted = Column(Boolean, default=False, nullable=False)
13
14
    tag     = relationship(tag.Tag,
15
                           primaryjoin='and_(ItemTag.tag_id==tag.Tag.id, ItemTag.deleted==False)',
16
                           backref='items')
17
    item    = relationship(item.Item,
18
                           primaryjoin='and_(ItemTag.item_id==item.Item.id, ItemTag.deleted==False)',
19
                           backref='tags')
20
21
    def __init__(self, item, tag):
22
        self.tag_id  = tag.id
23
        self.item_id = item.id
24
25
    @classmethod
26
    def from_id(cls, id):
27
        return DBSession.query(cls).filter(cls.id == id).one()
28
29
@classmethod
30
@limitable_all
31
def __get_tags_with_nobarcode_items(cls):
32
    return DBSession.query(tag.Tag)\
33
            .join(ItemTag)\
34
            .join(item.Item)\
35
            .filter(item.Item.barcode == None)\
36
            .filter(ItemTag.deleted==False)\
37
            .filter(item.Item.enabled==True)\
38
            .filter(tag.Tag.deleted==False)
39
40
tag.Tag.get_tags_with_nobarcode_items = __get_tags_with_nobarcode_items
41
42
@property
43
@limitable_all
44
def __nobarcode_items(self):
45
    return DBSession.query(item.Item)\
46
            .join(ItemTag)\
47
            .join(tag.Tag)\
48
            .filter(ItemTag.tag_id == self.id)\
49
            .filter(item.Item.barcode == None)\
50
            .filter(ItemTag.deleted==False)\
51
            .filter(item.Item.enabled==True)\
52
            .filter(tag.Tag.deleted==False)
53
54
tag.Tag.nobarcode_items = __nobarcode_items
55
56
@classmethod
57
@limitable_all
58
def __get_nobarcode_notag_items(cls):
59
    return DBSession.query(item.Item)\
60
            .filter(item.Item.barcode == None)\
61
            .filter(item.Item.enabled == True)\
62
            .filter(~exists().where(
63
                 and_(ItemTag.item_id == item.Item.id,
64
                      ItemTag.deleted == False)))
65
66
item.Item.get_nobarcode_notag_items = __get_nobarcode_notag_items
67
68