Coverage for api/models/oauth.py: 100%

18 statements  

« prev     ^ index     » next       coverage.py v7.15.2, created at 2026-10-07 06:14 +0000

1from django.contrib.postgres.fields import ArrayField 

2from django.db import models 

3 

4from oauth2_provider.models import AbstractApplication 

5 

6from api.constants.restricted_features import RestrictedFeature 

7 

8 

9class OAuth2Registration(models.Model): 

10 """Information about API key applicants.""" 

11 

12 name = models.CharField( 

13 max_length=150, 

14 unique=True, 

15 help_text="A unique human-readable name for your application or " 

16 "project requiring access to the Openverse API.", 

17 ) 

18 description = models.CharField( 

19 max_length=10000, 

20 help_text="A description of what you are trying to achieve with your " 

21 "project using the API. Please provide as much detail as " 

22 "possible!", 

23 ) 

24 email = models.EmailField( 

25 help_text="A valid email that we can reach you at if we have any " 

26 "questions about your use case or data consumption." 

27 ) 

28 

29 

30class ThrottledApplication(AbstractApplication): 

31 """An OAuth2 application with adjustable rate limits.""" 

32 

33 RATE_LIMIT_MODELS = [ 

34 ("standard", "standard"), # Default rate limit for all API keys. 

35 ("enhanced", "enhanced"), # Rate limits for "super" keys, granted on a 

36 # case-by-case basis. 

37 ("exempt", "exempt"), # Rate limits used for internal infrastructure to 

38 # by-pass rate limiting entirely. 

39 ] 

40 rate_limit_model = models.CharField( 

41 max_length=20, choices=RATE_LIMIT_MODELS, default="standard" 

42 ) 

43 verified = models.BooleanField(default=False) 

44 revoked = models.BooleanField(default=False) 

45 

46 privileges = ArrayField( 

47 models.CharField( 

48 choices=RestrictedFeature.choices, 

49 ), 

50 default=list, 

51 help_text=( 

52 "Restricted features to which this application has been granted " 

53 "privileged access" 

54 ), 

55 ) 

56 

57 

58class OAuth2Verification(models.Model): 

59 """ 

60 An email verification code sent by noreply-catalog. 

61 

62 After verification occurs, the entry should be deleted. 

63 """ 

64 

65 associated_application = models.ForeignKey( 

66 ThrottledApplication, on_delete=models.CASCADE 

67 ) 

68 email = models.EmailField() 

69 code = models.CharField(max_length=256, db_index=True)