def show_register_btn(user, contest): if not user.is_authenticated(): return False has_not_started = not contest_info.has_started(contest) own_contest = user_info.has_contest_ownership(user, contest) is_not_public_user = not is_public_user(user) user_is_admin = user.has_admin_auth() return has_not_started and (own_contest or is_not_public_user or user_is_admin)
def user_can_register_contest(user, contest): if not user.is_authenticated(): return False if user.has_admin_auth(): return False if is_public_user(user): return False has_ownership = has_contest_ownership(user, contest) if has_ownership: return False return True
def user_profile(request, username): try: profile_user = User.objects.get(username=username) piechart_data = get_user_statistics(profile_user) render_data = {} render_data['profile_user'] = profile_user render_data['piechart_data'] = dumps(piechart_data) if request.user == profile_user and not is_public_user(profile_user): render_data['profile_form'] = UserProfileForm( instance=profile_user) if can_change_userlevel(request.user, profile_user): render_data['userlevel_form'] = UserLevelForm(instance=profile_user, request_user=request.user) if request.user == profile_user and request.method == 'POST' \ and 'profile_form' in request.POST: profile_form = UserProfileForm(request.POST, instance=profile_user) render_data['profile_form'] = profile_form if profile_form.is_valid() and request.user == profile_user: logger.info('User %s update profile' % username) profile_form.save() update_session_auth_hash(request, profile_user) request.user = profile_user messages.success(request, 'Update profile successfully!') if request.method == 'POST' and 'userlevel_form' in request.POST: userlevel_form = UserLevelForm( request.POST, request_user=request.user) if can_change_userlevel(request.user, profile_user): if userlevel_form.is_valid(request.user): user_level = userlevel_form.cleaned_data['user_level'] logger.info("User %s update %s's user level to %s" % (request.user, username, user_level)) profile_user.user_level = user_level profile_user.save() render_data['userlevel_form'] = userlevel_form messages.success( request, 'Update user level successfully!') else: user_level = userlevel_form.cleaned_data['user_level'] messages.warning(request, "You can't switch user %s to %s" % (profile_user, user_level)) return render_index(request, 'users/profile.html', render_data) except User.DoesNotExist: logger.warning('User %s does not exist' % username) raise Http404('User %s does not exist' % username)
def confirm_login_allowed(self, user): if is_public_user(user) and not attends_not_ended_contest(user): user.is_active = False user.save() super(AuthenticationForm, self).confirm_login_allowed(user)