import json import sys from django.shortcuts import render from django.http.response import JsonResponse from rest_framework import status from .models import Facultad, Curso, Matricula from rest_framework.decorators import api_view from rest_framework.parsers import JSONParser from .serializers import FacultadSerializer, CursoSerializer from .organizar import files3 # Create your views here. @api_view(['POST', ]) def insertarFacultades(request): faculties = ['Administración de Empresas', 'Administración de Empresas Graduado', 'Arquitectura', 'Arquitectura Graduado', 'Asuntos Académicos', 'Ciencias Militares', 'Ciencias Naturales', 'Ciencias Naturales Graduado', 'Ciencias Sociales', 'Ciencias Sociales Graduado', 'Escuela de Comunicación', 'Escuela de Comunicación Graduada', 'Educación', 'Educación Continua (BEOF)', 'Educación Graduado', 'Escuela de Derecho', 'Escuela Graduada de Ciencias y Tecnologías de la Información', 'Estudios Generales', 'Humanidades', 'Humanidades Graduado', 'Planificación'] if request.method == 'POST': for faculty in faculties: facultad_serializer = FacultadSerializer(data={'fname': faculty}) if facultad_serializer.is_valid(): facultad_serializer.save() return JsonResponse({"message": 'se crearon todas las facultades'}, status=status.HTTP_201_CREATED) @api_view(['POST', ]) def insertarTodosLosCursos(request): if request.method == 'POST': for file in files3: check = file['file'].split('.') path = "C:/Users/danie/Documents/Projects/Companion App/server/segundo_sem" if check[ 0][-1] == '2' else "C:/Users/danie/Documents/Projects/Companion App/server/primer_sem" with open(path + '/' + file['file']) as f: data = json.load(f) fac_id = file['num'] for key in data: if key != 'Horario ': code = key name = data[key][0] creds = data[key][1] curso_serializer = CursoSerializer( data={'name': name, 'code': code, 'creditos': creds, 'fac_id': fac_id}) if curso_serializer.is_valid(): curso_serializer.save() return JsonResponse({'message': 'se insertaron todos los cursos'}, status=status.HTTP_201_CREATED) @api_view(['GET', 'POST']) def hello_world(request): if request.method == 'POST': return JsonResponse({"message": "Got some data!", "data": request.data}) return JsonResponse({"message": "Hello, world!"}) @api_view(['POST', ]) def fetchMatricula(request): if request.method == 'POST': json = [] for i in Curso.objects.all(): json.append({"id": i.fac_id.id, "name": i.name, "code": i.code, "credits": i.creditos}) return JsonResponse(json, safe=False) @api_view(['POST', 'GET']) def testQuery(request): json = [] for i in Curso.objects.all(): json.append({"id": i.fac_id.id, "name": i.name, "code": i.code, "credits": i.creditos}) return JsonResponse(json, safe=False)