def put(self, request, *args, **kwargs): data = json.loads(request.body.decode()) courseName = data['courseName'] if courseName == '': return sendError(message='Tienes que darle un nombre al curso') if len(courseName) > 100: return sendError(message='El nombre del curso es demasiado grande') try: course = Course.objects.get(id=kwargs['id'], teacher=request.user.teacher) except Course.DoesNotExist: return sendError(message='El curso no existe para este profesor', code=404) # Uptade course's data course.name = courseName course.save() courseData = {'id': course.id, 'name': course.name} return sendResponse(data=courseData, message='Curso editado correctamente')
def put(self, request, *args, **kwargs): data = json.loads(request.body.decode()) activityName = data['activityName'] if activityName == '': return sendError( message='Tienes que asignarle un nombre a la actividad') if len(activityName) > 100: return sendError( message='El nombre de la activdad es demasiado grande') try: activity = Activity.objects.get(id=kwargs['id'], teacher=request.user.teacher) except Activity.DoesNotExist: return sendError( message='La actividad no existe para este profesor', code=404) # Uptade course's data activity.name = activityName activity.save() activityData = {'id': activity.id, 'name': activity.name} return sendResponse(data=activityData, message='Actividad editada correctamente')
def post(self, request): data = json.loads(request.body.decode()) activityName = data['activityName'] if activityName == '': return sendError(message='Tienes que agregar una actividad') if len(activityName) > 100: return sendError(message='El nombre del curso es demasiado grande') if 'activityDesc' in data: activityDesc = data['activityDesc'] else: activityDesc = '' newActivity = Activity(name=activityName, teacher=request.user.teacher, description=activityDesc) newActivity.save() data = {'id': newActivity.id, 'name': newActivity.name} return sendResponse(data=data, message='Actividad {} agregada'.format( data['name']), code=201)
def post(self, request): data = json.loads(request.body.decode()) courseName = data['courseName'] if courseName == '': return sendError(message='Tienes que darle un nombre al curso') if len(courseName) > 100: return sendError(message='El nombre del curso es demasiado grande') newCourse = Course(name=courseName, teacher=request.user.teacher) newCourse.save() data = {'id': newCourse.id, 'name': newCourse.name} return sendResponse(data=data, message='Grupo {} agregado'.format(data['name']), code=201)
def get(self, request, *args, **kwargs): try: course = Course.objects.get(id=kwargs['id'], teacher=request.user.teacher) except Course.DoesNotExist: return sendError(message='El curso no existe para este profesor', code=404) courseData = {'id': course.id, 'name': course.name} return sendResponse(data=courseData, message='Curso mostrado')
def get(self, request, *args, **kwargs): try: activity = Activity.objects.get(id=kwargs['id'], teacher=request.user.teacher) except expression as identifier: return sendError( message='La actividad no existe para este profesor', code=404) activityData = {'id': activity.id, 'name': activity.name} return sendResponse(data=activityData, message='Actividad eliminada')
def delete(self, request, *args, **kwargs): try: courseToDelete = Course.objects.get(id=kwargs['id'], teacher=request.user.teacher) except Course.DoesNotExist: return sendError(message='El curso no existe para este profesor', code=404) courseDeleted = {'id': courseToDelete.id, 'name': courseToDelete.name} courseToDelete.delete() return sendResponse(data=courseDeleted, message='Curso eliminado')
def getUserInfo(request): """Return the user's basic information to use in frontend""" if not request.user.is_anonymous: data = { 'id': request.user.id, 'username': request.user.username, 'first_name': request.user.first_name, 'last_name': request.user.last_name, 'email': request.user.email, } if hasattr(request.user, 'teacher'): data['type'] = 'teacher' else: data['type'] = 'student' return sendResponse(data, 'Usuario logeado') else: return sendError('Debes estar logeado', 401)