No Description

views.py 3.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. import json
  2. import sys
  3. from django.shortcuts import render
  4. from django.http.response import JsonResponse
  5. from rest_framework import status
  6. from .models import Facultad, Curso, Matricula
  7. from rest_framework.decorators import api_view
  8. from rest_framework.parsers import JSONParser
  9. from .serializers import FacultadSerializer, CursoSerializer
  10. from .organizar import files3
  11. # Create your views here.
  12. @api_view(['POST', ])
  13. def insertarFacultades(request):
  14. faculties = ['Administración de Empresas', 'Administración de Empresas Graduado', 'Arquitectura', 'Arquitectura Graduado', 'Asuntos Académicos',
  15. 'Ciencias Militares', 'Ciencias Naturales', 'Ciencias Naturales Graduado', 'Ciencias Sociales', 'Ciencias Sociales Graduado',
  16. 'Escuela de Comunicación', 'Escuela de Comunicación Graduada', 'Educación', 'Educación Continua (BEOF)', 'Educación Graduado',
  17. 'Escuela de Derecho', 'Escuela Graduada de Ciencias y Tecnologías de la Información', 'Estudios Generales', 'Humanidades',
  18. 'Humanidades Graduado', 'Planificación']
  19. if request.method == 'POST':
  20. for faculty in faculties:
  21. facultad_serializer = FacultadSerializer(data={'fname': faculty})
  22. if facultad_serializer.is_valid():
  23. facultad_serializer.save()
  24. return JsonResponse({"message": 'se crearon todas las facultades'}, status=status.HTTP_201_CREATED)
  25. @api_view(['POST', ])
  26. def insertarTodosLosCursos(request):
  27. if request.method == 'POST':
  28. for file in files3:
  29. check = file['file'].split('.')
  30. path = "C:/Users/danie/Documents/Projects/Companion App/server/segundo_sem" if check[
  31. 0][-1] == '2' else "C:/Users/danie/Documents/Projects/Companion App/server/primer_sem"
  32. with open(path + '/' + file['file']) as f:
  33. data = json.load(f)
  34. fac_id = file['num']
  35. for key in data:
  36. if key != 'Horario ':
  37. code = key
  38. name = data[key][0]
  39. creds = data[key][1]
  40. curso_serializer = CursoSerializer(
  41. data={'name': name, 'code': code, 'creditos': creds, 'fac_id': fac_id})
  42. if curso_serializer.is_valid():
  43. curso_serializer.save()
  44. return JsonResponse({'message': 'se insertaron todos los cursos'}, status=status.HTTP_201_CREATED)
  45. @api_view(['GET', 'POST'])
  46. def hello_world(request):
  47. if request.method == 'POST':
  48. return JsonResponse({"message": "Got some data!", "data": request.data})
  49. return JsonResponse({"message": "Hello, world!"})
  50. @api_view(['POST', ])
  51. def fetchMatricula(request):
  52. if request.method == 'POST':
  53. json = []
  54. for i in Curso.objects.all():
  55. json.append({"id": i.fac_id.id, "name": i.name,
  56. "code": i.code, "credits": i.creditos})
  57. return JsonResponse(json, safe=False)
  58. @api_view(['POST', 'GET'])
  59. def testQuery(request):
  60. json = []
  61. for i in Curso.objects.all():
  62. json.append({"id": i.fac_id.id, "name": i.name,
  63. "code": i.code, "credits": i.creditos})
  64. return JsonResponse(json, safe=False)