views.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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 get_base_data(expe_name=None):
  12. '''
  13. Used to store default data to send for each view
  14. '''
  15. data = {}
  16. data['BASE'] = settings.WEB_PREFIX_URL
  17. return data
  18. def list_files(request):
  19. # get param
  20. # TODO : implement view which list all expe links file
  21. experiment_path = cfg.expe_data_folder
  22. files = []
  23. if os.path.exists(experiment_path):
  24. files = sorted(os.listdir(experiment_path))
  25. data = get_base_data()
  26. data['folder'] = files
  27. return render(request, 'links/files.html', data)
  28. def user_links(request):
  29. filename = request.GET.get('filename')
  30. if filename is None:
  31. # send 404 error
  32. raise Http404("Page does not exist")
  33. filepath = os.path.join(cfg.expe_data_folder, filename)
  34. if not os.path.exists(filepath):
  35. # send 404 error
  36. raise Http404("File asked does not exist")
  37. # read data and send it
  38. with open(filepath, 'r') as f:
  39. lines = [l.replace('\n', '') for l in f.readlines()]
  40. links = {}
  41. for line in lines:
  42. data = line.split(';')
  43. links[data[0]] = data[1:]
  44. data = get_base_data()
  45. data['links'] = json.dumps(links)
  46. return render(request, 'links/links.html', data)