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

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

# -*- coding: utf-8 -*- 

# Copyright (C) 2018-2019 by the Free Software Foundation, Inc. 

# 

# This file is part of Postorius. 

# 

# Postorius is free software: you can redistribute it and/or modify it under 

# the terms of the GNU General Public License as published by the Free 

# Software Foundation, either version 3 of the License, or (at your option) 

# any later version. 

# 

# Postorius is distributed in the hope that it will be useful, but WITHOUT 

# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 

# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 

# more details. 

# 

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

# Postorius. If not, see <http://www.gnu.org/licenses/>. 

# 

 

from django.conf import settings 

from django.core.exceptions import MultipleObjectsReturned 

from django.db import IntegrityError 

from django.http import Http404, HttpResponse, HttpResponseBadRequest 

from django.shortcuts import redirect 

from django.urls import reverse_lazy 

from django.views.decorators.http import require_safe 

from django.views.generic import CreateView, DeleteView, ListView, UpdateView 

 

from postorius.auth.mixins import DomainOwnerMixin, ListOwnerMixin 

from postorius.models import Domain, EmailTemplate, List 

 

 

class ListContextMixin: 

 

def get_context_data(self, *args, **kwargs): 

context = super().get_context_data(*args, **kwargs) 

context['list'] = List.objects.get_or_404( 

fqdn_listname=self.kwargs['list_id']) 

return context 

 

 

class ListTemplateIndexView(ListOwnerMixin, ListContextMixin, ListView): 

 

model = EmailTemplate 

template_name = 'postorius/lists/template_list.html' 

 

def get_queryset(self): 

return EmailTemplate.objects.filter( 

identifier=self.kwargs['list_id']) 

 

 

class ListTemplateCreateView(ListOwnerMixin, ListContextMixin, CreateView): 

 

template_name = 'postorius/lists/template_add.html' 

model = EmailTemplate 

fields = ['name', 'data'] 

 

def get_success_url(self): 

return reverse_lazy( 

'list_template_list', args=(self.kwargs['list_id'],)) 

 

def form_valid(self, form): 

formdata = form.cleaned_data 

formdata['identifier'] = self.kwargs['list_id'] 

formdata['context'] = 'list' 

email_template = EmailTemplate(**formdata) 

# Try to save the model. Some of the unique constraints that we have 

# depend on the mailing_list attribute added above. So, even though 

# we check for form validity, the save can fail. 

try: 

email_template.save() 

except IntegrityError as e: 

form.add_error('name', str(e)) 

form.add_error('name', 

'You already have this template set. ' 

'Use edit instead of creating a new one.') 

return self.form_invalid(form) 

return redirect(self.get_success_url()) 

 

 

class ListTemplateUpdateView(ListOwnerMixin, ListContextMixin, UpdateView): 

 

template_name = 'postorius/lists/template_update.html' 

model = EmailTemplate 

fields = ['data'] 

 

def get_success_url(self): 

return reverse_lazy( 

'list_template_list', args=(self.kwargs['list_id'],)) 

 

 

class ListTemplateDeleteView(ListOwnerMixin, ListContextMixin, DeleteView): 

 

template_name = 'postorius/lists/template_delete.html' 

model = EmailTemplate 

 

def get_success_url(self): 

return reverse_lazy( 

'list_template_list', args=(self.kwargs['list_id'],)) 

 

 

class DomainContextMixin: 

 

def get_context_data(self, *args, **kwargs): 

context = super().get_context_data(*args, **kwargs) 

context['domain'] = Domain.objects.get_or_404( 

mail_host=self.kwargs['domain']) 

return context 

 

 

class DomainTemplateIndexView(DomainOwnerMixin, DomainContextMixin, ListView): 

 

model = EmailTemplate 

template_name = 'postorius/domain/template_index.html' 

header = 'Index' 

 

def get_queryset(self): 

return EmailTemplate.objects.filter(identifier=self.kwargs['domain']) 

 

 

class DomainTemplateCreateView( 

DomainOwnerMixin, DomainContextMixin, CreateView): 

 

model = EmailTemplate 

template_name = 'postorius/domain/template_add.html' 

fields = ['name', 'data'] 

header = 'New Template' 

 

def get_success_url(self): 

return reverse_lazy('domain_template_list', 

args=(self.kwargs['domain'],)) 

 

def form_valid(self, form): 

formdata = form.cleaned_data 

formdata['identifier'] = self.kwargs['domain'] 

formdata['context'] = 'domain' 

template = EmailTemplate(**formdata) 

# Try to save the model. Some of the unique constraints that we have 

# depend on the mailing_list attribute added above. So, even though we 

# check for form validity, the save can fail. 

try: 

template.save() 

except IntegrityError as e: 

form.add_error('name', str(e)) 

form.add_error('name', 

'You already have this template set.' 

'Use edit instead of creating a new one.') 

return self.form_invalid(form) 

return redirect(self.get_success_url()) 

 

 

class DomainTemplateUpdateView( 

DomainOwnerMixin, DomainContextMixin, UpdateView): 

 

model = EmailTemplate 

template_name = 'postorius/domain/template_add.html' 

fields = ['data'] 

header = 'Edit Template' 

 

def get_success_url(self): 

return reverse_lazy('domain_template_list', 

args=(self.kwargs['domain'],)) 

 

 

class DomainTemplateDeleteView( 

DomainOwnerMixin, DomainContextMixin, DeleteView): 

 

template_name = 'postorius/domain/template_delete.html' 

model = EmailTemplate 

 

def get_success_url(self): 

return reverse_lazy('domain_template_list', 

args=(self.kwargs['domain'],)) 

 

 

@require_safe 

def get_template_data(request, context, identifier, name): 

# At this point, the request should be authenticated and it's method should 

# be GET. We just need to find the correct template and return it's 

# content, if it exists, return a 404 otherwise. 

if context not in ('list', 'domain'): 

return HttpResponseBadRequest( 

'context should be either "list" or "domain"') 

data = dict(name=name, identifier=identifier, context=context) 

# Depending on the context, populate the right identifier. 

try: 

template = EmailTemplate.objects.get(**data) 

except EmailTemplate.DoesNotExist: 

raise Http404('Template is not defined.') 

except MultipleObjectsReturned: 

raise HttpResponseBadRequest('Multiple Templates exist') 

 

content_type = 'text/plain' 

if settings.DEFAULT_CHARSET: 

content_type += '; charset=' + settings.DEFAULT_CHARSET 

return HttpResponse(template.data, 

content_type=content_type)