def getRegister(request): form = registerUser(request.POST or None) if form.is_valid(): instance = form.save(commit=False) instance.save() messages.success(request, 'Registration Successfully!') return redirect('login') return render(request, 'register.html', {"form": form})
def delete_users_subject(request, subject_id): subject = SubjectService.find_one(subject_id) students = subject.alumno_set.all() # Delete students for student in students: student.asignaturas.remove(subject) # Delete relationship with lecturers subject.imparteasignatura_set.all().delete() messages.success(request, _('The users have been removed')) return HttpResponseRedirect('/subject/details/' + str(subject_id))
def getCreate(request): if request.user.is_authenticated: u = get_object_or_404(author, name=request.user.id) form = createFrom(request.POST or None, request.FILES or None) if form.is_valid(): instance = form.save(commit=False) instance.article_author = u instance.save() messages.success(request, 'Article is published successfully!') return redirect('profile') return render(request, 'create.html', {"form": form}) else: return redirect('login')
def change_password(request): if request.method == 'POST': form = PasswordChangeForm(request.user, request.POST) if form.is_valid(): user = form.save() update_session_auth_hash(request, user) # Important! messages.success(request, 'Tu password fue actualizada correctamente!') return redirect('persona:change_password') else: messages.error(request, 'Encontramos errores en tu peticion') else: form = PasswordChangeForm(request.user) return render(request, 'persona/change_password.html', {'form': form})
def create_subject(request): if request.POST: form = SubjectEditForm(request.POST) if form.is_valid(): subject = SubjectService.create(form) SubjectService.save(subject) messages.success(request, _("Action completed successfully")) return HttpResponseRedirect("/admin/subject/link/" + str(subject.id)) else: data = {} form = SubjectEditForm(initial=data) template_name = 'subject/edit.html' template_data = {"form": form} return render_to_response(template_name, template_data, context_instance=RequestContext(request))
def delete_subject(request, subject_id): subject = SubjectService.find_one(subject_id) title = subject.nombre subject.delete() messages.success(request, _('The subject ' + title + ' has been removed')) return HttpResponseRedirect('/subject/list')
def import_subject(request): template_data = {} import_errors_create = [] import_errors_link = [] subject_create = [] subject_link = [] if request.POST: form = FileUploadSubjectForm(request.POST, request.FILES) if form.is_valid(): file_name = form.cleaned_data['file_upload'] certifications = set(form.cleaned_data['certifications']) data = file_name.read() file_name.close() try: if form.cleaned_data['file_upload'].content_type == "text/xml": # File XML data = xmltodict.parse(data) if data['asignaturas']: if not isinstance(data['asignaturas']['asignatura'], list): # 1 value subject = data['asignaturas']['asignatura'] data_form = SubjectService.get_form_data_xml( subject) create_link_subject_file(data_form, certifications, import_errors_create, import_errors_link, subject_create, subject_link) else: for subject_data in data['asignaturas'][ 'asignatura']: data_form = SubjectService.get_form_data_xml( subject_data) create_link_subject_file( data_form, certifications, import_errors_create, import_errors_link, subject_create, subject_link) else: # File CSV for subject in csv.reader(data.splitlines()): data_form = SubjectService.get_form_data_csv(subject) create_link_subject_file(data_form, certifications, import_errors_create, import_errors_link, subject_create, subject_link) if not import_errors_create and not import_errors_link: message = _('Action completed successfully') messages.success(request, message) else: if import_errors_create: template_data[ 'import_errors_create'] = import_errors_create if import_errors_link: template_data[ 'import_errors_link'] = import_errors_link if import_errors_create or import_errors_link: message = _('Action completed successfully') messages.warning(request, message) form = FileUploadSubjectForm() except KeyError as e: message = _( "The file structure is wrong. It needs a label called: " + e.message) form.add_error('file_upload', message) SubjectService.rollback(subject_create, subject_link, certifications) except AttributeError: message = _("Please, check the attributes of the subjects") form.add_error('file_upload', message) SubjectService.rollback(subject_create, subject_link, certifications) except TypeError: message = _("Please, check the xml syntax and data values") form.add_error('file_upload', message) SubjectService.rollback(subject_create, subject_link, certifications) except Exception: message = _("Please, check the file") form.add_error('file_upload', message) SubjectService.rollback(subject_create, subject_link, certifications) else: form = FileUploadSubjectForm() template_data['form'] = form template_name = 'subject/import_subject.html' return render_to_response(template_name, template_data, context_instance=RequestContext(request))
def import_subject(request): template_data = {} import_errors_create = [] import_errors_link = [] subject_create = [] subject_link = [] if request.POST: form = FileUploadSubjectForm(request.POST, request.FILES) if form.is_valid(): file_name = form.cleaned_data['file_upload'] certifications = set(form.cleaned_data['certifications']) data = file_name.read() file_name.close() try: if form.cleaned_data['file_upload'].content_type == "text/xml": # File XML data = xmltodict.parse(data) if data['asignaturas']: if not isinstance(data['asignaturas']['asignatura'], list): # 1 value subject = data['asignaturas']['asignatura'] data_form = SubjectService.get_form_data_xml(subject) create_link_subject_file(data_form, certifications, import_errors_create, import_errors_link, subject_create, subject_link) else: for subject_data in data['asignaturas']['asignatura']: data_form = SubjectService.get_form_data_xml(subject_data) create_link_subject_file(data_form, certifications, import_errors_create, import_errors_link, subject_create, subject_link) else: # File CSV for subject in csv.reader(data.splitlines()): data_form = SubjectService.get_form_data_csv(subject) create_link_subject_file(data_form, certifications, import_errors_create, import_errors_link, subject_create, subject_link) if not import_errors_create and not import_errors_link: message = _('Action completed successfully') messages.success(request, message) else: if import_errors_create: template_data['import_errors_create'] = import_errors_create if import_errors_link: template_data['import_errors_link'] = import_errors_link if import_errors_create or import_errors_link: message = _('Action completed successfully') messages.warning(request, message) form = FileUploadSubjectForm() except KeyError as e: message = _("The file structure is wrong. It needs a label called: " + e.message) form.add_error('file_upload', message) SubjectService.rollback(subject_create, subject_link, certifications) except AttributeError: message = _("Please, check the attributes of the subjects") form.add_error('file_upload', message) SubjectService.rollback(subject_create, subject_link, certifications) except TypeError: message = _("Please, check the xml syntax and data values") form.add_error('file_upload', message) SubjectService.rollback(subject_create, subject_link, certifications) except Exception: message = _("Please, check the file") form.add_error('file_upload', message) SubjectService.rollback(subject_create, subject_link, certifications) else: form = FileUploadSubjectForm() template_data['form'] = form template_name = 'subject/import_subject.html' return render_to_response(template_name, template_data, context_instance=RequestContext(request))