Total Complexity | 17 |
Total Lines | 166 |
Duplicated Lines | 42.17 % |
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 | """ |
||
2 | :Copyright: 2006-2022 Jochen Kupperschmidt |
||
3 | :License: Revised BSD (see `LICENSE` file for details) |
||
4 | """ |
||
5 | |||
6 | from datetime import datetime |
||
7 | from decimal import Decimal |
||
8 | from typing import Optional |
||
9 | |||
10 | from byceps.services.shop.article import service as article_service |
||
11 | from byceps.services.shop.article.transfer.models import ( |
||
12 | Article, |
||
13 | ArticleNumber, |
||
14 | ArticleType, |
||
15 | ArticleTypeParams, |
||
16 | ) |
||
17 | from byceps.services.shop.order.transfer.order import Orderer |
||
18 | from byceps.services.shop.shop.transfer.models import ShopID |
||
19 | from byceps.services.snippet import service as snippet_service |
||
20 | from byceps.services.snippet.transfer.models import Scope, SnippetID |
||
21 | from byceps.services.ticketing.transfer.models import TicketCategoryID |
||
22 | from byceps.services.user import service as user_service |
||
23 | from byceps.typing import UserID |
||
24 | |||
25 | from tests.helpers import generate_token |
||
26 | |||
27 | |||
28 | def create_shop_fragment( |
||
29 | shop_id: ShopID, creator_id: UserID, name: str, body: str |
||
30 | ) -> SnippetID: |
||
31 | scope = Scope('shop', shop_id) |
||
32 | |||
33 | version, _ = snippet_service.create_fragment(scope, name, creator_id, body) |
||
34 | |||
35 | return version.snippet_id |
||
36 | |||
37 | |||
38 | def create_article( |
||
39 | shop_id: ShopID, |
||
40 | *, |
||
41 | item_number: Optional[ArticleNumber] = None, |
||
42 | type_: ArticleType = ArticleType.other, |
||
43 | type_params: Optional[ArticleTypeParams] = None, |
||
44 | description: Optional[str] = None, |
||
45 | price: Optional[Decimal] = None, |
||
46 | tax_rate: Optional[Decimal] = None, |
||
47 | available_from: Optional[datetime] = None, |
||
48 | available_until: Optional[datetime] = None, |
||
49 | total_quantity: int = 999, |
||
50 | max_quantity_per_order: int = 10, |
||
51 | processing_required: bool = False, |
||
52 | ) -> Article: |
||
53 | if item_number is None: |
||
54 | item_number = ArticleNumber(generate_token()) |
||
55 | |||
56 | if description is None: |
||
57 | description = generate_token() |
||
58 | |||
59 | if price is None: |
||
60 | price = Decimal('24.95') |
||
61 | |||
62 | if tax_rate is None: |
||
63 | tax_rate = Decimal('0.19') |
||
64 | |||
65 | return article_service.create_article( |
||
66 | shop_id, |
||
67 | item_number, |
||
68 | type_, |
||
69 | description, |
||
70 | price, |
||
71 | tax_rate, |
||
72 | total_quantity, |
||
73 | max_quantity_per_order, |
||
74 | processing_required, |
||
75 | type_params=type_params, |
||
76 | ) |
||
77 | |||
78 | |||
79 | View Code Duplication | def create_ticket_article( |
|
1 ignored issue
–
show
|
|||
80 | shop_id: ShopID, |
||
81 | ticket_category_id: TicketCategoryID, |
||
82 | *, |
||
83 | item_number: Optional[ArticleNumber] = None, |
||
84 | description: Optional[str] = None, |
||
85 | price: Optional[Decimal] = None, |
||
86 | tax_rate: Optional[Decimal] = None, |
||
87 | available_from: Optional[datetime] = None, |
||
88 | available_until: Optional[datetime] = None, |
||
89 | total_quantity: int = 999, |
||
90 | max_quantity_per_order: int = 10, |
||
91 | ) -> Article: |
||
92 | if item_number is None: |
||
93 | item_number = ArticleNumber(generate_token()) |
||
94 | |||
95 | if description is None: |
||
96 | description = generate_token() |
||
97 | |||
98 | if price is None: |
||
99 | price = Decimal('24.95') |
||
100 | |||
101 | if tax_rate is None: |
||
102 | tax_rate = Decimal('0.19') |
||
103 | |||
104 | return article_service.create_ticket_article( |
||
105 | shop_id, |
||
106 | item_number, |
||
107 | description, |
||
108 | price, |
||
109 | tax_rate, |
||
110 | total_quantity, |
||
111 | max_quantity_per_order, |
||
112 | ticket_category_id, |
||
113 | ) |
||
114 | |||
115 | |||
116 | View Code Duplication | def create_ticket_bundle_article( |
|
1 ignored issue
–
show
|
|||
117 | shop_id: ShopID, |
||
118 | ticket_category_id: TicketCategoryID, |
||
119 | ticket_quantity: int, |
||
120 | *, |
||
121 | item_number: Optional[ArticleNumber] = None, |
||
122 | description: Optional[str] = None, |
||
123 | price: Optional[Decimal] = None, |
||
124 | tax_rate: Optional[Decimal] = None, |
||
125 | available_from: Optional[datetime] = None, |
||
126 | available_until: Optional[datetime] = None, |
||
127 | total_quantity: int = 999, |
||
128 | max_quantity_per_order: int = 10, |
||
129 | ) -> Article: |
||
130 | if item_number is None: |
||
131 | item_number = ArticleNumber(generate_token()) |
||
132 | |||
133 | if description is None: |
||
134 | description = generate_token() |
||
135 | |||
136 | if price is None: |
||
137 | price = Decimal('24.95') |
||
138 | |||
139 | if tax_rate is None: |
||
140 | tax_rate = Decimal('0.19') |
||
141 | |||
142 | return article_service.create_ticket_bundle_article( |
||
143 | shop_id, |
||
144 | item_number, |
||
145 | description, |
||
146 | price, |
||
147 | tax_rate, |
||
148 | total_quantity, |
||
149 | max_quantity_per_order, |
||
150 | ticket_category_id, |
||
151 | ticket_quantity, |
||
152 | ) |
||
153 | |||
154 | |||
155 | def create_orderer(user_id: UserID) -> Orderer: |
||
156 | detail = user_service.get_detail(user_id) |
||
157 | |||
158 | return Orderer( |
||
159 | user_id, |
||
160 | detail.first_name or 'n/a', |
||
161 | detail.last_name or 'n/a', |
||
162 | detail.country or 'n/a', |
||
163 | detail.zip_code or 'n/a', |
||
164 | detail.city or 'n/a', |
||
165 | detail.street or 'n/a', |
||
166 | ) |
||
167 |