22import threading
33
44from django .db import models
5+ from django .db .models import Q
56from django .conf import settings
67from django .core .exceptions import ImproperlyConfigured
78from django .utils import timezone
@@ -244,6 +245,16 @@ class Meta:
244245 def __str__ (self ):
245246
246247 return f'{ self .name } '
248+
249+ def find_users_by_email_pattern (self , only_new = False ):
250+
251+ if self .email_address_pattern :
252+ matching_users = User .objects .filter (email__iregex = self .email_address_pattern )
253+ if only_new :
254+ matching_users = matching_users .exclude (useradditionalinfo__company = self )
255+ return matching_users if matching_users .exists () else None
256+
257+ return None
247258
248259
249260class UserAdditionalInfo (AuditedBaseModel ):
@@ -261,7 +272,14 @@ class UserAdditionalInfo(AuditedBaseModel):
261272 null = True ,
262273 blank = True ,
263274 help_text = 'Whether this user belongs to an Authoring Tool vendor (optional)'
264- )
275+ )
276+
277+ is_vendor_self_declared = models .BooleanField (
278+ null = True ,
279+ blank = True ,
280+ verbose_name = ("is vendor (self declared)" ),
281+ help_text = 'Whether this user has self-declared an affiliation with an Authoring Tool vendor (optional)'
282+ )
265283
266284 company = models .ForeignKey (
267285 Company ,
@@ -277,6 +295,19 @@ class Meta:
277295 verbose_name = "User Additional Info"
278296 verbose_name_plural = "User Additional Info"
279297
298+ def find_company_by_email_pattern (self ):
299+
300+ if self .email :
301+
302+ companies = Company .objects .filter (email_address_pattern__isnull = False )
303+ if companies .exists ():
304+ for company in companies :
305+ user = User .objects .filter (id = self .id , email__iregex = company .email_address_pattern ).first ()
306+ if user :
307+ return company
308+
309+ return None
310+
280311
281312class AuthoringTool (TimestampedBaseModel ):
282313 """
@@ -319,7 +350,17 @@ class Meta:
319350 verbose_name_plural = "Authoring Tools"
320351
321352 constraints = [
322- models .UniqueConstraint (fields = ['name' , 'version' ], name = 'unique_name_version' )
353+ # Postgres supports NULLS DISTINCT, but not all DB's do (Sqlite does not!) - hence workaround using two constraints
354+ # models.UniqueConstraint(fields=['name', 'version', 'company_id'], name='unique_name_version_company', nulls_distinct=False)
355+ models .UniqueConstraint (
356+ name = 'unique_name_version_company_id' ,
357+ fields = ['name' , 'version' , 'company_id' ]
358+ ),
359+ models .UniqueConstraint (
360+ name = 'unique_name_version_company_id_null' ,
361+ fields = ['name' , 'version' ],
362+ condition = Q (company_id__isnull = True )
363+ )
323364 ]
324365
325366 def __str__ (self ):
0 commit comments