Coverage for .tox / coverage / lib / python3.11 / site-packages / sideshow / db / model / customers.py: 100%

29 statements  

« prev     ^ index     » next       coverage.py v7.13.0, created at 2025-12-15 17:10 -0600

1# -*- coding: utf-8; -*- 

2################################################################################ 

3# 

4# Sideshow -- Case/Special Order Tracker 

5# Copyright © 2024-2025 Lance Edgar 

6# 

7# This file is part of Sideshow. 

8# 

9# Sideshow is free software: you can redistribute it and/or modify it 

10# under the terms of the GNU General Public License as published by 

11# the Free Software Foundation, either version 3 of the License, or 

12# (at your option) any later version. 

13# 

14# Sideshow is distributed in the hope that it will be useful, but 

15# WITHOUT ANY WARRANTY; without even the implied warranty of 

16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 

17# General Public License for more details. 

18# 

19# You should have received a copy of the GNU General Public License 

20# along with Sideshow. If not, see <http://www.gnu.org/licenses/>. 

21# 

22################################################################################ 

23""" 

24Data models for Customers 

25""" 

26 

27import sqlalchemy as sa 

28from sqlalchemy import orm 

29 

30from wuttjamaican.db import model 

31from wuttjamaican.util import make_utc 

32 

33from sideshow.enum import PendingCustomerStatus 

34 

35 

36class CustomerMixin: # pylint: disable=too-few-public-methods 

37 """ 

38 Base class for customer tables. This has shared columns, used by e.g.: 

39 

40 * :class:`LocalCustomer` 

41 * :class:`PendingCustomer` 

42 """ 

43 

44 full_name = sa.Column( 

45 sa.String(length=100), 

46 nullable=True, 

47 doc=""" 

48 Full display name for the customer account. 

49 """, 

50 ) 

51 

52 first_name = sa.Column( 

53 sa.String(length=50), 

54 nullable=True, 

55 doc=""" 

56 First name of the customer. 

57 """, 

58 ) 

59 

60 last_name = sa.Column( 

61 sa.String(length=50), 

62 nullable=True, 

63 doc=""" 

64 Last name of the customer. 

65 """, 

66 ) 

67 

68 phone_number = sa.Column( 

69 sa.String(length=20), 

70 nullable=True, 

71 doc=""" 

72 Phone number for the customer. 

73 """, 

74 ) 

75 

76 email_address = sa.Column( 

77 sa.String(length=255), 

78 nullable=True, 

79 doc=""" 

80 Email address for the customer. 

81 """, 

82 ) 

83 

84 def __str__(self): 

85 return self.full_name or "" 

86 

87 

88class LocalCustomer( # pylint: disable=too-few-public-methods 

89 CustomerMixin, model.Base 

90): 

91 """ 

92 This table contains the :term:`local customer` records. 

93 

94 Sideshow will do customer lookups against this table by default, 

95 unless it's configured to use :term:`external customers <external 

96 customer>` instead. 

97 

98 Also by default, when a :term:`new order batch` with a 

99 :term:`pending customer` is executed, a new record is added to 

100 this local customers table, for lookup next time. 

101 """ 

102 

103 __tablename__ = "sideshow_customer_local" 

104 

105 uuid = model.uuid_column() 

106 

107 external_id = sa.Column( 

108 sa.String(length=20), 

109 nullable=True, 

110 doc=""" 

111 ID of the proper customer account associated with this record, if 

112 applicable. 

113 """, 

114 ) 

115 

116 orders = orm.relationship( 

117 "Order", 

118 order_by="Order.order_id.desc()", 

119 back_populates="local_customer", 

120 cascade_backrefs=False, 

121 doc=""" 

122 List of :class:`~sideshow.db.model.orders.Order` records 

123 associated with this customer. 

124 """, 

125 ) 

126 

127 new_order_batches = orm.relationship( 

128 "NewOrderBatch", 

129 order_by="NewOrderBatch.id.desc()", 

130 back_populates="local_customer", 

131 cascade_backrefs=False, 

132 doc=""" 

133 List of 

134 :class:`~sideshow.db.model.batch.neworder.NewOrderBatch` 

135 records associated with this customer. 

136 """, 

137 ) 

138 

139 

140class PendingCustomer( # pylint: disable=too-few-public-methods 

141 CustomerMixin, model.Base 

142): 

143 """ 

144 This table contains the :term:`pending customer` records, used 

145 when creating an :term:`order` for new/unknown customer. 

146 

147 Sideshow will automatically create and (hopefully) delete these 

148 records as needed. 

149 

150 By default, when a :term:`new order batch` with a pending customer 

151 is executed, a new record is added to the :term:`local customers 

152 <local customer>` table, for lookup next time. 

153 """ 

154 

155 __tablename__ = "sideshow_customer_pending" 

156 

157 uuid = model.uuid_column() 

158 

159 customer_id = sa.Column( 

160 sa.String(length=20), 

161 nullable=True, 

162 doc=""" 

163 ID of the proper customer account associated with this record, if 

164 applicable. 

165 """, 

166 ) 

167 

168 status = sa.Column( 

169 sa.Enum(PendingCustomerStatus), 

170 nullable=False, 

171 doc=""" 

172 Status code for the customer record. 

173 """, 

174 ) 

175 

176 created = sa.Column( 

177 sa.DateTime(), 

178 nullable=False, 

179 default=make_utc, 

180 doc=""" 

181 Timestamp when the customer record was created. 

182 """, 

183 ) 

184 

185 created_by_uuid = model.uuid_fk_column("user.uuid", nullable=False) 

186 created_by = orm.relationship( 

187 model.User, 

188 cascade_backrefs=False, 

189 doc=""" 

190 Reference to the 

191 :class:`~wuttjamaican:wuttjamaican.db.model.auth.User` who 

192 created the customer record. 

193 """, 

194 ) 

195 

196 orders = orm.relationship( 

197 "Order", 

198 order_by="Order.order_id.desc()", 

199 cascade_backrefs=False, 

200 back_populates="pending_customer", 

201 doc=""" 

202 List of :class:`~sideshow.db.model.orders.Order` records 

203 associated with this customer. 

204 """, 

205 ) 

206 

207 new_order_batches = orm.relationship( 

208 "NewOrderBatch", 

209 order_by="NewOrderBatch.id.desc()", 

210 cascade_backrefs=False, 

211 back_populates="pending_customer", 

212 doc=""" 

213 List of 

214 :class:`~sideshow.db.model.batch.neworder.NewOrderBatch` 

215 records associated with this customer. 

216 """, 

217 )