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

1import logging 

2 

3from actstream import action 

4from django.contrib.contenttypes.models import ContentType 

5from django.http import Http404, HttpResponse 

6from django.shortcuts import redirect, render 

7from django.utils.translation import gettext as _ 

8 

9from discuss_data.core.helpers import check_perms, check_perms_403 

10from discuss_data.ddcomments.forms import CommentForm 

11from discuss_data.ddcomments.models import Comment 

12from discuss_data.dddatasets.models import DataFile, DataSet, DataSetManagementObject 

13from discuss_data.pages.models import ManualPage 

14 

15logger = logging.getLogger(__name__) 

16 

17 

18def get_dataset_fields_from_form(ds, form): 

19 dataset_fields = list() 

20 for field in form: 

21 try: 

22 qs = getattr(ds, field.name).all() 

23 entries = list() 

24 for entry in qs: 

25 entries.append(entry.name) 

26 entries_str = ", ".join(entries) 

27 dataset_fields.append((field.label, entries_str)) 

28 except AttributeError: 

29 dataset_fields.append((field.label, getattr(ds, field.name))) 

30 return dataset_fields 

31 

32 

33def check_published(ds_uuid): 

34 """ 

35 helper function to check if a dataset has been published 

36 and can thus be accessed openly 

37 """ 

38 try: 

39 ds = DataSet.objects.get(uuid=ds_uuid) 

40 except DataSet.DoesNotExist: 

41 raise Http404("Dataset not found") 

42 if not ds.published: 

43 raise Http404("Dataset not found") 

44 

45 return ds 

46 

47 

48def get_man_page(slug): 

49 """ 

50 helper function for manual pages 

51 """ 

52 

53 try: 

54 return ManualPage.objects.get(slug=slug) 

55 except Exception: 

56 return None 

57 

58 

59def edit_datafile(request, datafile, form): 

60 if form.is_valid(): 

61 datafile.data_file_type = form.cleaned_data["data_file_type"] 

62 uploadedfile = form.cleaned_data["file"] 

63 datafile.file = uploadedfile 

64 try: 

65 datafile.name = uploadedfile._name 

66 datafile.content_type = uploadedfile.content_type 

67 datafile.data_file_size = uploadedfile.size 

68 except Exception as e: 

69 logger.error(e) 

70 

71 datafile.save() 

72 updated = True 

73 form_err = None 

74 else: 

75 updated = False 

76 form_err = form.errors.as_data() 

77 return form, form_err, updated 

78 

79 

80# this is a view function 

81def dataset_comments(request, ds_uuid, view_type): 

82 """ 

83 returns a list of either public or prep comments 

84 of a dataset depending on view_type param ("public"|"prep") 

85 """ 

86 ds = DataSet.objects.get(uuid=ds_uuid) 

87 

88 if view_type != "public": 

89 if not check_perms("edit_dsmo", request.user, ds.dataset_management_object): 

90 response = HttpResponse("no rights to private comments") 

91 response["X-IC-CancelPolling"] = "true" 

92 return response 

93 comments = ds.get_prep_comments_root() 

94 add_url = "dddatasets:prep_edit_comment" 

95 list_url = "dddatasets:prep_comments" 

96 reply_url = "dddatasets:prep_edit_comment_reply" 

97 root_count = comments.count() 

98 all_count = ds.get_prep_comments_all().count() 

99 else: 

100 comments = ds.get_public_comments_root() 

101 add_url = "dddatasets:edit_comment" 

102 list_url = "dddatasets:comments" 

103 reply_url = "dddatasets:edit_comment_reply" 

104 root_count = comments.count() 

105 all_count = ds.get_public_comments_all().count() 

106 # add http header to response to resume polling (which could have been canceled in prep_dataset_edit_comment) 

107 response = render( 

108 request, 

109 "ddcomments/_comments_list.html", 

110 { 

111 "ds": ds, 

112 "comments": comments, 

113 "add_url": add_url, 

114 "list_url": list_url, 

115 "reply_url": reply_url, 

116 "root_count": root_count, 

117 "all_count": all_count, 

118 }, 

119 ) 

120 response["X-IC-ResumePolling"] = "true" 

121 return response 

122 

123 

124# this is a view function 

125def dataset_edit_comment(request, ds_uuid, view_type, co_uuid, reply=None): 

126 """ 

127 view to add, edit or delete public/prep comments 

128 to/from a dataset 

129 """ 

130 

131 if view_type == "public" or view_type == "prep": 

132 pass 

133 else: 

134 raise Exception("no view type") 

135 err = None 

136 ds = DataSet.objects.get(uuid=ds_uuid) 

137 if view_type == "public": 

138 add_url = "dddatasets:edit_comment" 

139 list_url = "dddatasets:comments" 

140 reply_url = "dddatasets:edit_comment_reply" 

141 else: 

142 add_url = "dddatasets:prep_edit_comment" 

143 list_url = "dddatasets:prep_comments" 

144 reply_url = "dddatasets:prep_edit_comment_reply" 

145 

146 if view_type != "public": 

147 check_perms_403("edit_dsmo", request.user, ds.dataset_management_object) 

148 parent = None 

149 

150 # DELETE: delete not the whole Comment object, but mark the text as "deleted" 

151 if request.method == "DELETE": 

152 co_uuid = request.DELETE["objectid"] 

153 comment = Comment.objects.get(uuid=co_uuid) 

154 check_perms_403("delete_comment", request.user, comment) 

155 comment.set_delete() 

156 comment.save() 

157 

158 if view_type == "public": 

159 return dataset_comments(request, ds.uuid, "public") 

160 else: 

161 return dataset_comments(request, ds.uuid, "prep") 

162 

163 if request.method == "POST": 

164 if co_uuid and not reply: 

165 # Edit 

166 comment = Comment.objects.get(uuid=co_uuid) 

167 check_perms_403("edit_comment", request.user, comment) 

168 form = CommentForm(request.POST, instance=comment) 

169 if form.is_valid(): 

170 comment = form.save() 

171 if view_type == "public": 

172 return dataset_comments(request, ds.uuid, "public") 

173 else: 

174 return dataset_comments(request, ds.uuid, "prep") 

175 else: 

176 err = form.errors.as_data() 

177 else: 

178 # Add new and reply 

179 comment = Comment() 

180 form = CommentForm(request.POST, instance=comment) 

181 if form.is_valid(): 

182 comment.content_type = ContentType.objects.get_for_model(ds) 

183 comment.object_id = ds.id 

184 comment.owner = request.user 

185 if view_type == "public": 

186 comment.comment_type = comment.PUBLIC 

187 act_public = True 

188 else: 

189 comment.comment_type = comment.PRIVATE 

190 act_public = False 

191 if reply: 

192 comment.parent = Comment.objects.get(uuid=co_uuid) 

193 comment = form.save() 

194 # send action 

195 action.send( 

196 request.user, 

197 verb="added comment", 

198 target=comment, 

199 action_object=ds, 

200 public=act_public, 

201 ) 

202 return render( 

203 request, 

204 "ddcomments/_comment_block_load.html", 

205 {"ds": ds, "add_url": add_url, "list_url": list_url}, 

206 ) 

207 else: 

208 err = form.errors.as_data() 

209 else: 

210 # Get 

211 if co_uuid and not reply: 

212 # Edit form 

213 comment = Comment.objects.get(uuid=co_uuid) 

214 check_perms_403("edit_comment", request.user, comment) 

215 form = CommentForm(instance=comment) 

216 # add http header to cancel polling while existing comment is being edited (resume header added in prep_dataset_comments() called by POST) 

217 response = render( 

218 request, 

219 "ddcomments/_comment_add.html", 

220 { 

221 "ds": ds, 

222 "comment": comment, 

223 "comment_form": form, 

224 "err": err, 

225 "edit": True, 

226 "add_url": add_url, 

227 "list_url": list_url, 

228 "reply_url": reply_url, 

229 "form": form, 

230 "ictarget": "#comments-list", 

231 }, 

232 ) 

233 response["X-IC-CancelPolling"] = "true" 

234 return response 

235 elif reply: 

236 # Reply form 

237 parent = Comment.objects.get(uuid=co_uuid) 

238 form = CommentForm() 

239 else: 

240 # Add form 

241 form = CommentForm() 

242 # updated = False 

243 # form_err = None 

244 response = render( 

245 request, 

246 "ddcomments/_comment_add.html", 

247 { 

248 "ds": ds, 

249 "parent": parent, 

250 "comment_form": form, 

251 "err": err, 

252 "add_url": add_url, 

253 "list_url": list_url, 

254 "reply_url": reply_url, 

255 "form": form, 

256 "reply": reply, 

257 "ictarget": "#discuss", 

258 }, 

259 ) 

260 if reply: 

261 response["X-IC-CancelPolling"] = "true" 

262 return response