def edit_comment(self, request, pk): obj = self.get_object() history_entry_id = request.QUERY_PARAMS.get("id", None) history_entry = ( services.get_history_queryset_by_model_instance(obj).filter( id=history_entry_id).first()) if history_entry is None: return response.NotFound() obj = services.get_instance_from_key(history_entry.key) comment = request.DATA.get("comment", None) self.check_permissions(request, "edit_comment", history_entry) if history_entry is None: return response.NotFound() if comment is None: return response.BadRequest({"error": _("comment is required")}) if history_entry.delete_comment_date or history_entry.delete_comment_user: return response.BadRequest( {"error": _("deleted comments can't be edited")}) # comment_versions can be None if there are no historic versions of the comment comment_versions = history_entry.comment_versions or [] comment_versions.append({ "date": history_entry.created_at, "comment": history_entry.comment, "comment_html": history_entry.comment_html, "user": { "id": request.user.pk, }, }) new_mentions = self._get_new_mentions(obj, history_entry.comment, comment) history_entry.edit_comment_date = timezone.now() history_entry.comment = comment history_entry.comment_html = mdrender(obj.project, comment) history_entry.comment_versions = comment_versions history_entry.save() if new_mentions: signal_mentions.send( sender=self.__class__, user=self.request.user, obj=obj, mentions=new_mentions, ) return response.Ok()
def comment_versions(self, request, pk): obj = self.get_object() history_entry_id = request.QUERY_PARAMS.get('id', None) history_entry = services.get_history_queryset_by_model_instance(obj).filter(id=history_entry_id).first() if history_entry is None: return response.NotFound() self.check_permissions(request, 'comment_versions', history_entry) if history_entry is None: return response.NotFound() history_entry.attach_user_info_to_comment_versions() return response.Ok(history_entry.comment_versions)
def exception_handler(exc): """ Returns the response that should be used for any given exception. By default we handle the REST framework `APIException`, and also Django's builtin `Http404` and `PermissionDenied` exceptions. Any unhandled exceptions may return `None`, which will cause a 500 error to be raised. """ if isinstance(exc, exceptions.APIException): headers = {} if getattr(exc, "auth_header", None): headers["WWW-Authenticate"] = exc.auth_header if getattr(exc, "wait", None): headers["X-Throttle-Wait-Seconds"] = "%d" % exc.wait detail = format_exception(exc) return response.Response(detail, status=exc.status_code, headers=headers) elif isinstance(exc, Http404): return response.NotFound({'_error_message': str(exc)}) elif isinstance(exc, DjangoPermissionDenied): return response.Forbidden({"_error_message": str(exc)}) # Note: Unhandled exceptions will raise a 500 error. return None
def undelete_comment(self, request, pk): obj = self.get_object() history_entry_id = request.QUERY_PARAMS.get('id', None) history_entry = services.get_history_queryset_by_model_instance(obj).filter(id=history_entry_id).first() if history_entry is None: return response.NotFound() self.check_permissions(request, 'undelete_comment', history_entry) if history_entry is None: return response.NotFound() if not history_entry.delete_comment_date and not history_entry.delete_comment_user: return response.BadRequest({"error": _("Comment not deleted")}) history_entry.delete_comment_date = None history_entry.delete_comment_user = None history_entry.save() return response.Ok()
def delete_comment(self, request, pk): obj = self.get_object() history_entry_id = request.QUERY_PARAMS.get('id', None) history_entry = services.get_history_queryset_by_model_instance(obj).filter(id=history_entry_id).first() if history_entry is None: return response.NotFound() self.check_permissions(request, 'delete_comment', history_entry) if history_entry is None: return response.NotFound() if history_entry.delete_comment_date or history_entry.delete_comment_user: return response.BadRequest({"error": _("Comment already deleted")}) history_entry.delete_comment_date = timezone.now() history_entry.delete_comment_user = {"pk": request.user.pk, "name": request.user.get_full_name()} history_entry.save() return response.Ok()
def csv(self, request): uuid = request.QUERY_PARAMS.get("uuid", None) if uuid is None: return response.NotFound() project = get_object_or_404(Project, userstories_csv_uuid=uuid) queryset = project.user_stories.all().order_by('ref') data = services.userstories_to_csv(project, queryset) csv_response = HttpResponse(data.getvalue(), content_type='application/csv; charset=utf-8') csv_response['Content-Disposition'] = 'attachment; filename="userstories.csv"' return csv_response
def retrieve(self, request, *args, **kwargs): pk = kwargs.get("pk", None) resource_id = kwargs.get("resource_id", None) resource = get_object_or_404(self.resource_model, pk=resource_id) self.check_permissions(request, 'retrieve', resource) try: self.object = resource.get_watchers().get(pk=pk) except ObjectDoesNotExist: # or User.DoesNotExist return response.NotFound() serializer = self.get_serializer(self.object) return response.Ok(serializer.data)
def list(self, request): return response.NotFound()
def csv(self, request): get_data = {} status_id = [] # PDF or Excel doc_type = request.QUERY_PARAMS.get('doc_type', None) uuid = request.QUERY_PARAMS.get("uuid", None) start_date = request.QUERY_PARAMS.get('start_date', None) end_date = request.QUERY_PARAMS.get('end_date', None) type = request.QUERY_PARAMS.get('type', None) status = request.QUERY_PARAMS.get('status', None) asset = request.QUERY_PARAMS.get('asset_cat', None) performance = request.QUERY_PARAMS.get('performance_cat', None) photo = request.QUERY_PARAMS.get('photo', None) name = request.QUERY_PARAMS.get('type_name', None) accident_report_type = request.QUERY_PARAMS.get( 'accident_report_type', None) if status: status = status.split(',') if uuid is None: return response.NotFound() project = get_object_or_404(Project, issues_csv_uuid=uuid) if asset and performance: if status: queryset = project.issues.filter(issue_category=asset, issue_subcategory=performance, type__name=type, status__id__in=status, created_date__date__range=[ start_date, end_date ]).order_by('-created_date') else: if type == "Investigation": queryset = project.issues.filter( asset_name=asset, test_name=performance, type__name=type, created_date__date__range=[start_date, end_date ]).order_by('-created_date') else: queryset = project.issues.filter( issue_category=asset, issue_subcategory=performance, type__name=type, created_date__date__range=[start_date, end_date ]).order_by('-created_date') elif asset and not performance: if status: queryset = project.issues.filter(issue_category=asset, type__name=type, status__id__in=status, created_date__date__range=[ start_date, end_date ]).order_by('-created_date') else: if type == "Investigation": queryset = project.issues.filter( asset_name=asset, type__name=type, created_date__date__range=[start_date, end_date ]).order_by('-created_date') else: queryset = project.issues.filter( issue_category=asset, type__name=type, created_date__date__range=[start_date, end_date ]).order_by('-created_date') else: queryset = project.issues.filter( type__name=type, created_date__date__range=[start_date, end_date]).order_by('-created_date') data = write_excel.write_excel(self, request, project, queryset, type, status, start_date, end_date, asset, performance, photo, doc_type, name, accident_report_type) if doc_type == "excel": csv_response = HttpResponse( save_virtual_workbook(data), content_type='application/vnd.ms-excel; charset=utf-8') csv_response[ 'Content-Disposition'] = 'attachment; filename="issues.xlsx"' return csv_response if doc_type == "pdf": html = HTML(string=data) html.write_pdf( target='/tmp/mypdf.pdf', stylesheets=[CSS(settings.STATIC_ROOT + '/main.css')], presentational_hints=True) # print("=============================") # print(html) fs = FileSystemStorage('/tmp') with fs.open('mypdf.pdf') as pdf: response = HttpResponse(pdf, content_type='application/pdf') response[ 'Content-Disposition'] = 'attachment; filename="mypdf.pdf"' return response