Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1# (c) 2018-2020 

2# MPIB <https://www.mpib-berlin.mpg.de/>, 

3# MPI-CBS <https://www.cbs.mpg.de/>, 

4# MPIP <http://www.psych.mpg.de/> 

5# 

6# This file is part of Castellum. 

7# 

8# Castellum is free software; you can redistribute it and/or modify it 

9# under the terms of the GNU Affero General Public License as published 

10# by the Free Software Foundation; either version 3 of the License, or 

11# (at your option) any later version. 

12# 

13# Castellum is distributed in the hope that it will be useful, but 

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

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

16# Affero General Public License for more details. 

17# 

18# You should have received a copy of the GNU Affero General Public 

19# License along with Castellum. If not, see 

20# <http://www.gnu.org/licenses/>. 

21 

22from django.db.utils import IntegrityError 

23 

24from castellum.subjects.models import Subject 

25 

26from .models import Domain 

27from .models import Pseudonym 

28 

29 

30def attempt(fn, attempts=10): 

31 try: 

32 return fn() 

33 except IntegrityError: 

34 if attempts <= 0: 

35 raise 

36 return attempt(fn, attempts=attempts - 1) 

37 

38 

39def create_domain(bits=40): 

40 domain = attempt(lambda: Domain.objects.create(bits=bits)) 

41 return domain.key 

42 

43 

44def delete_domain(domain_key): 

45 domain = Domain.objects.get(key=domain_key) 

46 domain.delete() 

47 

48 

49def get_subject(domain_key, pseudonym): 

50 return Subject.objects.get(pseudonym__domain__key=domain_key, pseudonym__pseudonym=pseudonym) 

51 

52 

53def get_pseudonym(subject, target_domain_key): 

54 target_domain = Domain.objects.get(key=target_domain_key) 

55 target, __ = attempt( 

56 lambda: Pseudonym.objects.get_or_create(subject=subject, domain=target_domain) 

57 ) 

58 return target.pseudonym 

59 

60 

61def delete_pseudonym(domain_key, pseudonym): 

62 p = Pseudonym.objects.get(domain__key=domain_key, pseudonym=pseudonym) 

63 p.subject = None 

64 p.save()