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

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

import json 

import logging 

 

from django.contrib import messages 

from django.contrib.auth.mixins import LoginRequiredMixin 

from django.core.exceptions import PermissionDenied 

from django.db import transaction 

from django.http import HttpResponse 

from django.shortcuts import render, redirect 

from django.urls import reverse 

 

from common.models import List, Fanfic, FanficList 

from common.utils import CustomError 

from common.views import BaseView 

from notifier.models import Notification 

 

logger = logging.getLogger(__name__) 

 

 

class ListsView(LoginRequiredMixin, BaseView): 

def post(self, request, list_id=None): 

if request.is_ajax() is False or list_id is None: 

raise PermissionDenied() 

 

success = False 

message = "There was an unexpected error trying to process this " \ 

"action." 

try: 

with transaction.atomic(): 

act_user = request.user 

list_user = List.objects.filter(user=act_user, id=list_id) 

if list_user.exists() is False: 

raise CustomError("This list does not exist anymore.") 

 

list_user = list_user.first() 

 

fanfic_id = int(request.POST.get('fanficId')) 

mark_to_join = request.POST.get('join') 

mark_to_join = True if mark_to_join == 'true' else False 

 

fanfic = Fanfic.objects.filter(id=fanfic_id) 

if fanfic.exists() is False: 

raise CustomError("This fanfic does not exist anymore.") 

 

fanfic = fanfic.first() 

 

fl = FanficList.objects.filter(list=list_user, fanfic=fanfic) 

if fl.exists() is True and mark_to_join is True: 

# already added 

success = True 

message = "This fanfic has been already added to this " \ 

"list." 

list_name = list_user.name 

list_id_checked = list_user.id 

elif fl.exists() is True and mark_to_join is False: 

# the user wants to remove the fanfic from this list 

fl.delete() 

success = True 

message = "This fanfic is not on your lists anymore." 

 

list_name = None 

list_id_checked = None 

elif fl.exists() is False and mark_to_join is False: 

# the user wants to remove the fanfic from this list, 

# but it has been already deleted 

success = True 

message = "This fanfic has been previously removed from " \ 

"this list." 

 

f = FanficList.objects.filter(list__user=act_user, 

fanfic=fanfic) 

if f.exists(): 

f = f.first() 

list_name = f.name 

list_id_checked = f.id 

else: 

list_name = None 

list_id_checked = None 

elif fl.exists() is False and mark_to_join is True: 

# the user wants to add the fanfic to a list, delete the 

# old one if there is 

FanficList.objects.filter(fanfic=fanfic, 

list__user=act_user).delete() 

FanficList.objects.create(fanfic=fanfic, list=list_user) 

success = True 

message = "The fanfic has been added to the list." 

list_name = list_user.name 

list_id_checked = list_user.id 

 

# send notification 

Notification.objects.create( 

subject=fanfic, verb="has started following", 

target=act_user, 

link=fanfic.get_url(), 

in_top_bar=False, in_feed=True, reverse=True) 

 

success = True 

except CustomError as e: 

logger.error("Error trying to add/remove fanfic from list: {" 

"}".format(e)) 

message = e 

except Exception as e: 

message = "There was an unexpected error trying to process this " \ 

"action." 

logger.error("Error trying to add/remove fanfic from list: {" 

"}".format(e)) 

 

data = { 

"success": success, 

"message": message, 

"list_name": list_name, 

"list_id_checked": list_id_checked 

} 

return HttpResponse(json.dumps(data), content_type='text/plain', 

status=200) 

 

def get(self, request): 

""" Manage user's lists """ 

user_lists = List.objects.filter(user=request.user) 

return render(request, 'user_lists.html', {'user_lists': user_lists}) 

 

def put(self, request): 

""" Update/create lists' names """ 

new_ones = False 

try: 

with transaction.atomic(): 

list_created = False 

list_changed = False 

if "new" in request.POST: 

# new lists 

new_lists_names = request.POST.getlist("new") 

if len(new_lists_names) >= 1: 

for name in new_lists_names: 

name_clean = name.lstrip().rstrip() 

if name_clean != "": 

if len(name_clean) > 12: 

messages.error(request, 

"Some of the lists " 

"could not be " 

"updated. The max " 

"length of the name " 

"is 12 characteres " 

"long.") 

else: 

user_list = List.objects.filter( 

user=request.user, 

name=name_clean) 

if user_list.exists() is True: 

messages.error(request, 

"Some of the lists " 

"could not be " 

"created. There cannot" 

" be two lists with " 

"the " 

"same name.") 

else: 

List.objects.create(user=request.user, 

name=name_clean) 

list_created = True 

 

if "fieldChange" in request.POST: 

changed_lists_ids = request.POST.get("fieldChange") 

if len(changed_lists_ids) >= 1: 

changed_lists_ids = changed_lists_ids.split(",") 

for changed_list_id in changed_lists_ids: 

id_list = int(changed_list_id) 

list_obj = List.objects.filter(user=request.user, 

id=id_list) 

if list_obj.exists() is True: 

list_obj = list_obj.first() 

new_name = request.POST.get( 

"list" + str(id_list)).lstrip().rstrip() 

if len(new_name) > 12 or new_name == "": 

messages.error(request, 

"Some of the lists " 

"could not be " 

"updated. The max " 

"length of the name " 

"is 12 characteres " 

"long.") 

else: 

list_obj.name = new_name 

list_obj.save() 

list_changed = True 

 

if list_changed is True: 

messages.success(request, 

"The lists have been successfully " 

"updated.") 

elif list_created is True: 

messages.success(request, 

"The lists have been successfully " 

"created.") 

 

except CustomError as e: 

logger.error(e) 

messages.error(request, e) 

except Exception as e: 

msg = ("There was an error trying to update the user's " 

"lists.") 

logger.error(msg + ": " + str(e)) 

messages.error(request, msg) 

 

return redirect(reverse('lists:manage_lists')) 

 

def delete(self, request): 

""" Delete lists """ 

try: 

with transaction.atomic(): 

delete_lists_ids = request.POST.getlist("dellist") 

 

for delete_list_id in delete_lists_ids: 

id_list = int(delete_list_id) 

list_obj = List.objects.filter(user=request.user, 

id=id_list) 

if list_obj.exists() is True: 

list_obj = list_obj.first() 

list_obj.delete() 

messages.success(request, "The lists have been successfully " 

"delete.") 

 

except CustomError as e: 

logger.error(e) 

messages.error(request, e) 

except Exception: 

msg = ("There was an error trying to delete the user's " 

"lists.") 

logger.error(msg) 

messages.error(request, msg) 

 

return redirect(reverse('lists:manage_lists'))