Coverage for /home/runner/work/viur-core/viur-core/viur/src/viur/core/modules/formmailer.py: 0%

43 statements  

« prev     ^ index     » next       coverage.py v7.6.3, created at 2024-10-16 22:16 +0000

1from viur.core.skeleton import RelSkel 

2from viur.core import errors, utils, securitykey, email 

3from viur.core.decorators import * 

4from viur.core.bones import BaseBone 

5from viur.core.module import Module 

6 

7 

8class MailSkel(RelSkel): 

9 changedate = None # Changedates won't apply here 

10 

11 

12class Formmailer(Module): 

13 mailTemplate = None 

14 

15 @exposed 

16 @skey(allow_empty=True) 

17 def index(self, *args, **kwargs): 

18 if not self.canUse(): 

19 raise errors.Forbidden() # Unauthorized 

20 

21 skel = self.mailSkel() 

22 

23 if len(kwargs) == 0: 

24 return self.render.add(skel=skel, failed=False) 

25 

26 if not skel.fromClient(kwargs): 

27 return self.render.add(skel=skel, failed=True) 

28 

29 # Allow bones to perform outstanding "magic" operations before sending the mail 

30 for key, _bone in skel.items(): 

31 if isinstance(_bone, BaseBone): 

32 _bone.performMagic(skel, key, isAdd=True) 

33 

34 # Get recipients 

35 rcpts = self.getRcpts(skel) 

36 

37 # Get additional options for sendEMail 

38 opts = self.getOptions(skel) 

39 if not isinstance(opts, dict): 

40 opts = {} 

41 

42 # Send the email! 

43 email.sendEMail(dests=rcpts, tpl=self.mailTemplate, skel=skel, **opts) 

44 self.onAdded(skel) 

45 

46 return self.render.addSuccess(skel) 

47 

48 @exposed 

49 def add(self, *args, **kwargs): 

50 return self.index(*args, **kwargs) 

51 

52 def canUse(self) -> bool: 

53 return False 

54 

55 def mailSkel(self): 

56 raise NotImplementedError("You must implement the \"mailSkel\" function!") 

57 

58 def getRcpts(self, skel): 

59 raise NotImplementedError("You must implement the \"getRcpts\" function!") 

60 

61 def getOptions(self, skel): 

62 return None 

63 

64 def onAdded(self, skel): 

65 pass 

66 

67 

68Formmailer.html = True