You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

101 lines
3.3 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  1. import csv
  2. from io import TextIOWrapper
  3. from django.urls import reverse
  4. from django.http import HttpResponse, HttpResponseRedirect
  5. from django.shortcuts import get_object_or_404
  6. from django.views import View
  7. from django.views.generic import TemplateView, CreateView
  8. from django.views.generic.list import ListView
  9. from django.contrib.auth.mixins import LoginRequiredMixin
  10. from .permissions import SuperUserMixin
  11. from .forms import ProjectForm
  12. from .models import Document, Project
  13. class IndexView(TemplateView):
  14. template_name = 'index.html'
  15. class ProjectView(LoginRequiredMixin, TemplateView):
  16. def get_template_names(self):
  17. project = get_object_or_404(Project, pk=self.kwargs['project_id'])
  18. return [project.get_template_name()]
  19. class ProjectsView(LoginRequiredMixin, CreateView):
  20. form_class = ProjectForm
  21. template_name = 'projects.html'
  22. class DatasetView(SuperUserMixin, LoginRequiredMixin, ListView):
  23. template_name = 'admin/dataset.html'
  24. paginate_by = 5
  25. def get_queryset(self):
  26. project = get_object_or_404(Project, pk=self.kwargs['project_id'])
  27. return project.documents.all()
  28. class LabelView(SuperUserMixin, LoginRequiredMixin, TemplateView):
  29. template_name = 'admin/label.html'
  30. class StatsView(SuperUserMixin, LoginRequiredMixin, TemplateView):
  31. template_name = 'admin/stats.html'
  32. class GuidelineView(SuperUserMixin, LoginRequiredMixin, TemplateView):
  33. template_name = 'admin/guideline.html'
  34. class DataUpload(SuperUserMixin, LoginRequiredMixin, TemplateView):
  35. template_name = 'admin/dataset_upload.html'
  36. def post(self, request, *args, **kwargs):
  37. project = get_object_or_404(Project, pk=kwargs.get('project_id'))
  38. try:
  39. form_data = TextIOWrapper(request.FILES['csv_file'].file, encoding='utf-8')
  40. if project.is_type_of(Project.SEQUENCE_LABELING):
  41. Document.objects.bulk_create([Document(
  42. text=line.strip(),
  43. project=project) for line in form_data])
  44. else:
  45. reader = csv.reader(form_data)
  46. Document.objects.bulk_create([Document(
  47. text=line[0].strip(),
  48. project=project) for line in reader])
  49. return HttpResponseRedirect(reverse('dataset', args=[project.id]))
  50. except:
  51. return HttpResponseRedirect(reverse('upload', args=[project.id]))
  52. class DataDownload(SuperUserMixin, LoginRequiredMixin, View):
  53. def get(self, request, *args, **kwargs):
  54. project_id = self.kwargs['project_id']
  55. project = get_object_or_404(Project, pk=project_id)
  56. docs = project.get_documents(is_null=False).distinct()
  57. filename = '_'.join(project.name.lower().split())
  58. response = HttpResponse(content_type='text/csv')
  59. response['Content-Disposition'] = 'attachment; filename="{}.csv"'.format(filename)
  60. writer = csv.writer(response)
  61. for d in docs:
  62. writer.writerows(d.make_dataset())
  63. return response
  64. class DemoTextClassification(TemplateView):
  65. template_name = 'demo/demo_text_classification.html'
  66. class DemoNamedEntityRecognition(TemplateView):
  67. template_name = 'demo/demo_named_entity.html'
  68. class DemoTranslation(TemplateView):
  69. template_name = 'demo/demo_translation.html'