views.py 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. # django imports
  2. from django.shortcuts import render
  3. from django.http import HttpResponse
  4. from django.conf import settings
  5. from django.contrib.auth.decorators import login_required
  6. from django.http import Http404
  7. # main imports
  8. import os
  9. import json
  10. from . import config as cfg
  11. def list_files(request):
  12. # get param
  13. # TODO : implement view which list all expe links file
  14. experiment_path = cfg.expe_data_folder
  15. files = sorted(os.listdir(experiment_path))
  16. data = {
  17. 'folder': files
  18. }
  19. return render(request, 'links/files.html', data)
  20. def user_links(request):
  21. filename = request.GET.get('filename')
  22. if filename is None:
  23. # send 404 error
  24. raise Http404("Page does not exist")
  25. filepath = os.path.join(cfg.expe_data_folder, filename)
  26. if not os.path.exists(filepath):
  27. # send 404 error
  28. raise Http404("File asked does not exist")
  29. # read data and send it
  30. with open(filepath, 'r') as f:
  31. lines = [l.replace('\n', '') for l in f.readlines()]
  32. links = {}
  33. for line in lines:
  34. data = line.split(';')
  35. links[data[0]] = data[1:]
  36. data = {
  37. 'links': json.dumps(links)
  38. }
  39. return render(request, 'links/links.html', data)