def form_valid(self, form, *args, **kwargs): obj = form.save(commit=False) obj.site_id = int(form.data.get("site_id")) obj.status = obj.STATUS_PENDING obj.save() app_log.log_request(self.request, actions.SUBMIT_COMMENT, obj=obj) return self.render_to_response(self.get_context_data(form=form))
def form_valid(self, form, *args, **kwargs): obj = form.save(commit=False) obj.site_id = int(form.data.get("site_id")) obj.credits = form.data.get("your_name", "") obj.name = form.data.get("description", "") obj.status = obj.STATUS_PENDING obj.save() app_log.log_request(self.request, actions.SUBMIT_PHOTO, obj=obj) return self.render_to_response(self.get_context_data(form=form))
def post(self, request, *args, **kwargs): try: comment_id = int(request.REQUEST.get("comment_id", -1)) comment = GeologicalSiteComment.objects.get(pk=comment_id) comment.num_likes += 1 comment.save() app_log.log_request(self.request, actions.LIKE_COMMENT, obj=comment) return http.HttpResponse("OK") except (GeologicalSiteComment.DoesNotExist, ValueError) as e: pass return http.HttpResponseBadRequest("FAIL")
def api_issue(): log_request(request, 'api-issue') # prepare constants headers = {'Content-Type': 'application/json; charset=utf-8'} bad_payload = { 'success': False, 'num': None, 'url': None, } bad_request_response = (dump_to_json(bad_payload), 400, headers) # get and validate user's data valid, why = is_issue_request_valid(request) if not valid: log_request(request, 'api-issue-errors', why) return bad_request_response user_data = request.get_json() user_data = escape_sel_context(user_data) # get template exp_keys = {'owner', 'repo', 'title', 'body', 'labels', 'assignees'} issue_tmpl = get_config('issue_template', exp_keys) # specify template as_is_keys = {'owner', 'repo', 'labels', 'assignees'} issue = {k: issue_tmpl[k] for k in as_is_keys} issue['title'] = issue_tmpl['title'].format_map(user_data) issue['body'] = '' line_prefix = '' for line in issue_tmpl['body']: if isinstance(line, dict): line_prefix = line['line_prefix'] else: line = line.format_map(user_data) line = ('\n' + line_prefix).join(line.split('\n')) issue['body'] += line_prefix + line issue['body'] = issue['body'].rstrip('\n') # create issue try: num, url = gitHub.create_issue(**issue) payload = { 'success': True, 'num': num, 'url': url, } except: why = 'The exception raised in api_issue:\n' + format_exc() app.logger.warning(why) log_request(request, 'api-issue-errors', why) return bad_request_response return (dump_to_json(payload), 200, headers)
def github_gate(): log_request(request, 'api-github-gate') return 'ok'