def index(request): """ Codeschool index page. """ # It redirects to the login page or to the profile page. It should probably # do something more interesting in the future. if isinstance(request.user, AnonymousUser): return redirect(reverse('auth:login')) else: return redirect('/unb-gama/')
def index(request): """ Codeschool index page. """ # It redirects to the login page or to the profile page. It should probably # do something more interesting in the future. if isinstance(request.user, AnonymousUser): return redirect(reverse('auth:login')) else: username = request.user.username kwargs = {'username': username} return redirect(reverse('auth:userena_profile_detail', kwargs=kwargs))
def login(request): context = { 'signin': AuthenticationForm(), 'signup': SignupForm(), 'signup_opt': SignupOptionalForm() } if request.method == 'POST': if request.POST['action-type'] == 'signup': form = SignupOptionalForm(request.POST) if not form.is_valid(): context['signup'] = SignupOptionalForm(request.POST) context['signup_opt'] = form context['default_tab'] = 1 else: context['signup_opt'] = SignupOptionalForm(request.POST) context['action'] = 'signup' context['default_tab'] = 1 response = views.signup( request, signup_form=SignupForm, template_name='cs_auth/login.jinja2', extra_context=context, ) # It redirects on success: we intercept add the extra # information if isinstance(response, http.HttpResponseRedirect): # Fill extra info in signup form aux = request.POST success_url = '/accounts/%s' % aux['username'] user = User.objects.get(username=aux['username']) user.first_name = aux['first_name'] user.last_name = aux['last_name'] # Fill extra profile info form = SignupOptionalForm(request.POST) form.is_valid() aux = form.cleaned_data user.profile.about_me = aux['about_me'] user.profile.gender = aux['gender'] user.profile.date_of_birth = aux['date_of_birth'] # Save modifications and go user.save() user.profile.save() return redirect(success_url) return response else: context['action'] = 'signin' return views.signin( request, template_name='cs_auth/login.jinja2', extra_context=context, ) return render(request, 'cs_auth/login.jinja2', context)
def login(request): context = {'signin': AuthenticationForm(), 'signup': SignupForm(), 'signup_opt': SignupOptionalForm()} if request.method == 'POST': if request.POST['action-type'] == 'signup': form = SignupOptionalForm(request.POST) if not form.is_valid(): context['signup'] = SignupOptionalForm(request.POST) context['signup_opt'] = form context['default_tab'] = 1 else: context['signup_opt'] = SignupOptionalForm(request.POST) context['action'] = 'signup' context['default_tab'] = 1 response = views.signup( request, signup_form=SignupForm, template_name='cs_auth/login.jinja2', extra_context=context, ) # It redirects on success: we intercept add the extra # information if isinstance(response, http.HttpResponseRedirect): # Fill extra info in signup form aux = request.POST success_url = '/accounts/%s' % aux['username'] user = User.objects.get(username=aux['username']) user.first_name = aux['first_name'] user.last_name = aux['last_name'] # Fill extra profile info form = SignupOptionalForm(request.POST) form.is_valid() aux = form.cleaned_data user.profile.about_me = aux['about_me'] user.profile.gender = aux['gender'] user.profile.date_of_birth = aux['date_of_birth'] # Save modifications and go user.save() user.profile.save() return redirect(success_url) return response else: context['action'] = 'signin' return views.signin( request, template_name='cs_auth/login.jinja2', extra_context=context, ) return render(request, 'cs_auth/login.jinja2', context)
def post_signup(self, request): context = self.get_context_data(action='signup') form = SignupForm(request.POST) opt_form = SignupOptionalForm(request.POST) # Render forms if they are invalid if not (opt_form.is_valid() and form.is_valid()): context['signup'] = form context['signup_opt'] = opt_form return super().render_to_response(context) # Validate and proceed context['signup_opt'] = opt_form response = views.signup( request, signup_form=SignupForm, template_name=self.template_name, extra_context=context, success_url='/', ) # It redirects on success: we intercept and add extra # information if isinstance(response, http.HttpResponseRedirect): # Fill extra info in signup form aux = form.cleaned_data success_url = '/accounts/%s' % aux['username'] user = User.objects.get(username=aux['username']) user.first_name = aux['first_name'] user.last_name = aux['last_name'] # Fill extra profile info opt_form = SignupOptionalForm(request.POST) opt_form.is_valid() aux = opt_form.cleaned_data user.profile.about_me = aux['about_me'] user.profile.gender = aux['gender'] user.profile.date_of_birth = aux['date_of_birth'] # Save modifications and go user.save() user.profile.save() return redirect(success_url) return response
def post_signup(self, request): context = self.get_context_data(action='signup') form = SignupForm(request.POST) opt_form = SignupOptionalForm(request.POST) # Render forms if they are invalid if not (opt_form.is_valid() and form.is_valid()): context['signup'] = form context['signup_opt'] = opt_form return super().render_to_response(context) # Validate and proceed context['signup_opt'] = opt_form response = views.signup( request, signup_form=SignupForm, template_name=self.template_name, extra_context=context, success_url='/', ) # It redirects on success: we intercept and add extra # information if isinstance(response, http.HttpResponseRedirect): # Fill extra info in signup form aux = form.cleaned_data success_url = '/accounts/%s' % aux['username'] user = User.objects.get(username=aux['username']) user.first_name = aux['first_name'] user.last_name = aux['last_name'] # Fill extra profile info opt_form = SignupOptionalForm(request.POST) opt_form.is_valid() aux = opt_form.cleaned_data user.profile.about_me = aux['about_me'] user.profile.gender = aux['gender'] user.profile.date_of_birth = aux['date_of_birth'] # Save modifications and go user.save() user.profile.save() return redirect(success_url) return response
def course_detail(request, pk): course = get_object_or_404(models.Course, pk=pk) if request.method == 'POST': if request.POST['action'] == 'cancel-subscription': if request.user != course.teacher: course.students.remove(request.user) return redirect('../') else: raise RuntimeError('teachers cannot unsubscribe of their own ' 'courses') return render_context( request, 'cs_courses/course-detail.jinja2', course=course, role=course.role(request.user), user_activities=course.user_activities(request.user), )
def view_edit_key(self, question, key): key = question.answer_keys.get(pk=key) self.context.update({ 'question': question, 'language': key.language, 'key': key, }) if self.request.method == 'POST': form = AnswerKeyEditForm(self.request.POST, instance=key) if form.is_valid(): key = form.save() key.update() return redirect('../../edit') else: form = AnswerKeyEditForm(instance=key) self.context['form'] = form return render(self.request, 'cs_questions/io/edit-key.jinja2', self.context)
def view_edit_key(self, question, key): key = question.answer_keys.get(pk=key) self.context.update({ 'question': question, 'language': key.language, 'key': key, }) if self.request.method == 'POST': form = AnswerKeyEditForm(self.request.POST, instance=key) if form.is_valid(): key = form.save() key.update() return redirect('../../edit') else: form = AnswerKeyEditForm(instance=key) self.context['form'] = form return render(self.request, 'cs_questions/io/edit-key.jinja2', self.context)
def view_add_key(self, question): used_langs = question.answer_keys.select_related('language') languages = ProgrammingLanguage.objects.exclude(pk__in=used_langs) self.context.update(question=question, languages=languages) if self.request.method == 'POST': form = AnswerKeyAddForm(self.request.POST) if form.is_valid(): key = question.answer_keys.create( source=form.cleaned_data['source'], placeholder=form.cleaned_data['placeholder'], language=form.cleaned_data['language'], ) key.update() return redirect('../edit') else: form = AnswerKeyAddForm() self.context['form'] = form return render(self.request, 'cs_questions/io/add-key.jinja2', self.context)
def login(request): if request.method != 'POST': raise http.HttpResponseBadRequest # Load form and validate form = forms.LoginForm(request.POST) if not form.is_valid(): return index(request, login_form=form) # Process form username = form.cleaned_data['username'] password = form.cleaned_data['password'] try: users.get(username=username) user = auth.authenticate(username=username, password=password) auth.login(request, user) return redirect(root_url) except models.User.DoesNotExist: pass form.add_error('username', 'Usuário ou senha inválidos') return index(request, login_form=form)
def view_add_key(self, question): used_langs = question.answer_keys.select_related('language') languages = ProgrammingLanguage.objects.exclude(pk__in=used_langs) self.context.update(question=question, languages=languages) if self.request.method == 'POST': form = AnswerKeyAddForm(self.request.POST) if form.is_valid(): key = question.answer_keys.create( source=form.cleaned_data['source'], placeholder=form.cleaned_data['placeholder'], language=form.cleaned_data['language'], ) key.update() return redirect('../edit') else: form = AnswerKeyAddForm() self.context['form'] = form return render(self.request, 'cs_questions/io/add-key.jinja2', self.context)
def register(request): if request.method != 'POST': raise http.HttpResponseBadRequest # Load form and validate form = forms.RegisterForm(request.POST) if not form.is_valid(): return index(request, register_form=form, selected_tab=1) # Process form username = form.cleaned_data['username'] name = form.cleaned_data['name'] surname = form.cleaned_data['surname'] email = form.cleaned_data['email'] password = form.cleaned_data['password'] password_confirmation = form.cleaned_data['password_confirmation'] # Check valid password if password != password_confirmation: form.add_error('password_confirmation', 'Senhas não conferem') return index(request, register_form=form, selected_tab=1) # Check user if request.user.is_authenticated(): auth.logout(request) try: users.get(username=username) form.add_error('username', 'Usuário existente') return index(request, register_form=form, selected_tab=1) except User.DoesNotExist: user = users.create_user(username, email, password, first_name=name, last_name=surname) user.save() user = auth.authenticate(username=username, password=password) auth.login(request, user) return redirect(root_url)
def teacher_add_activities(request, pk): course = get_object_or_404(models.Course, pk=pk) if request.method != 'POST' or course.teacher != request.user: return http.HttpResponseForbidden() post = dict(request.POST) checked = [int(key[6:]) for key in post if key.startswith('check-')] activities = [get_object_or_404(Activity, pk=pk) for pk in checked] duration = course.activity_duration() start, end = course.next_time_slot() if 'add-now' in post: for activity in activities: activity.reschedule_now(duration) activity.save() elif 'add-next' in post: for activity in activities: activity.reschedule(start, end) activity.save() elif 'edit' in post: pass return redirect(course.get_absolute_url())
def index(request): if request.user is None: return redirect('/accounts/login/') else: return redirect('/accounts/%s/' % request.user.username)
def index(request): if request.user is None: return redirect('/accounts/login/') else: return redirect('/accounts/%s/' % request.user.username)