def index_for_site(request,site): ctx = {'channel':'site_index'} if request.user.is_authenticated(): ctx.update({'user_login':request.user}) school = site.school auth_form = AuthenticationForm if request.method == 'POST': form = auth_form(request.POST) if request.is_ajax(): return helpers.ajax_validate_error(form) if form.is_valid(): identification, password, remember_me = (form.cleaned_data['identification'], form.cleaned_data['password'], form.cleaned_data['remember_me']) user = authenticate(identification=identification,password=password) try: profile = user.get_profile() except: p = Profile() p.user = user p.save() if user.is_active: login(request, user) if remember_me: request.session.set_expiry(userena_settings.USERENA_REMEMBER_ME_DAYS[1] * 86400) else: request.session.set_expiry(0) messages.success(request, _('You have been signed in.')) # Whereto now? return redirect(request.get_full_path()) else: return redirect(reverse('userena_disabled', kwargs={'username': user.username})) else: form = AuthenticationForm() school_actives = school_active(site,17) student_actives = school_active(site,18) articals = get_parts(site,19) news = get_parts(site,10) print news,'nnnnnnnnnnnnnn' announces = get_parts(site,9) tip = get_tip(site) start_teachers = get_start(site.school,'teacher') start_students = get_start(site.school,'student') tiles = Tile.objects.get_tiles_all_unlogin()[0:4] links = Link.objects.filter(site=site) parent_domain = helpers.get_parent_domain(request) ctx.update({'form':form,'school_actives':school_actives,'student_actives':student_actives,\ 'articals':articals,'news':news,'announces':announces,'tip':tip,'links':links,\ 'start_teachers':start_teachers,'start_students':start_students,'tiles':tiles,\ 'site':site,'parent_domain':parent_domain}) return ctx
def index(request, template_name="kinger/tile_index.html"): """ 家长页面首页, 选择分类使会用 ``request.session`` 记录 `channel` 分类.用于详情页查询. :param channel: ``string``, 分别是 *Baby* 默认项, *推荐 - tips*, *所有 - all* 分类. :param tag_q: ``string``, 数据格式如: *1,3,4*. 服务于 *tips* 分类,根据标签 ``id`` 过滤 :params type_id: ``int``, 单个 id. 服务于 *baby* 分类。 """ site = get_domain_redirct(request) print site,'site-----------------' if site: template_name="oa/site/index.html" ctx = {'channel':'site_index'} if request.user.is_authenticated(): ctx.update({'user_login':request.user}) school = site.school auth_form = AuthenticationForm if request.method == 'POST': form = auth_form(request.POST) if request.is_ajax(): return helpers.ajax_validate_error(form) if form.is_valid(): identification, password, remember_me = (form.cleaned_data['identification'], form.cleaned_data['password'], form.cleaned_data['remember_me']) user = authenticate(identification=identification,password=password) try: profile = user.get_profile() except: p = Profile() p.user = user p.save() if user.is_active: login(request, user) # log = Access_log() # log.type = 1 # log.user = user # log.url = request.get_full_path() # log.save() if remember_me: request.session.set_expiry(userena_settings.USERENA_REMEMBER_ME_DAYS[1] * 86400) else: request.session.set_expiry(0) messages.success(request, _('You have been signed in.')) # Whereto now? return redirect(request.get_full_path()) else: return redirect(reverse('userena_disabled', kwargs={'username': user.username})) else: form = AuthenticationForm() school_actives = school_active(site,17) student_actives = school_active(site,18) articals = get_parts(site,19) news = get_parts(site,10) print news,'nnnnnnnnnnnnnn' announces = get_parts(site,9) tip = get_tip(site) start_teachers = get_start(site.school,'teacher') start_students = get_start(site.school,'student') tiles = Tile.objects.get_tiles_all_unlogin()[0:4] links = Link.objects.filter(site=site) parent_domain = get_parent_domain(request) ctx.update({'form':form,'school_actives':school_actives,'student_actives':student_actives,\ 'articals':articals,'news':news,'announces':announces,'tip':tip,'links':links,\ 'start_teachers':start_teachers,'start_students':start_students,'tiles':tiles,\ 'site':site,'parent_domain':parent_domain}) return render(request, template_name,ctx) else: channel_ctx = {} channel = request.GET.get("channel",'all') user = request.user category = TileCategory.objects.all() if request.user.is_authenticated(): #用户登录日志 log = Access_log() log.type = 2 log.user = user log.url = request.get_full_path() log.save() tags = [] types = [] if channel == "baby": current_time = datetime.datetime.now() category = category.filter(is_tips=False) parent_category = category.filter(parent__pk=0).exclude(pk=10) tiles = Tile.objects.get_tiles_baby(user).filter(category__parent__in=parent_category) record_tiles = Tile.objects.get_tiles_baby(user).filter(category__parent__id=10) today_daily_tiles = get_daily_category_tiles(record_tiles, category, current_time) #today_daily_tiles = get_daily_category_tiles(tiles, category, current_time) latest_active = get_daily_activitie_tiles(user) latest_cookbook = get_daily_cook_books(user,current_time) is_read = 1 if CookbookRead.objects.filter(user=user,cookbook=latest_cookbook,is_read=True) else 0 # 根据页面上得到category过滤返回的瓦片分类 scat_id = request.GET.get("scat_id", '') scat_pks = [int(x) for x in filter(None, scat_id.split(","))] if scat_pks: category_list = TileCategory.objects.filter(pk__in=scat_pks) q_category = Tile.objects.get_q_category(category_list) tiles = tiles.filter(q_category) book_item = cook_book_item(latest_cookbook) template_name = "kinger/tile_index_baby.html" channel_ctx = {"scat_id":scat_id, "parent_category":parent_category, "book_item":book_item, "current_time":current_time,\ "today_daily_tiles":today_daily_tiles,"latest_active":latest_active,"latest_cookbook":latest_cookbook,"is_read":is_read} elif channel == "tips": category = category.filter(is_tips=True) parent_category = category.filter(parent__pk=0).exclude(pk=10) tiles = Tile.objects.get_tiles_edu(user).filter(category__parent__in=parent_category) # 根据页面上得到category过滤返回的瓦片分类 scat_id = request.GET.get("scat_id", '31') scat_pks = [int(x) for x in filter(None, scat_id.split(","))] if scat_pks: category_list = TileCategory.objects.filter(pk__in=scat_pks) q_category = Tile.objects.get_q_category(category_list) tiles = tiles.filter(q_category) channel_ctx = {"scat_id":scat_id, "parent_category":parent_category} else: # 个人的以及推荐的 tiles = Tile.objects.get_tiles_all_login(user) daily_category = get_daily_category() if daily_category: tiles = tiles.exclude(category__parent=daily_category) else: # 公开推荐的 channel = "all" tiles = Tile.objects.get_tiles_all_unlogin() ctx = {} content_type = ContentType.objects.get_for_model(Tile) ctx.update({"tiles": tiles, "content_type": content_type, "channel": channel,"category":category}) ctx.update(channel_ctx) request.session['kinger_channel'] = channel if request.is_ajax(): page = int(request.GET.get("page",'1')) start = (page - 1) * 15 end = page * 15 tiles = tiles[start:end] ctx['tiles'] = tiles template_name = "kinger/tile_index_container.html" return render(request, template_name, ctx) return render(request, template_name, ctx)
def signin(request, auth_form=AuthenticationForm, template_name='userena/signin_form.html', redirect_field_name=REDIRECT_FIELD_NAME, redirect_signin_function=signin_redirect, extra_context=None): """ Signin using email or username with password. Signs a user in by combining email/username with password. If the combination is correct and the user :func:`is_active` the :func:`redirect_signin_function` is called with the arguments ``REDIRECT_FIELD_NAME`` and an instance of the :class:`User` whois is trying the login. The returned value of the function will be the URL that is redirected to. A user can also select to be remembered for ``USERENA_REMEMBER_DAYS``. :param auth_form: Form to use for signing the user in. Defaults to the :class:`AuthenticationForm` supplied by userena. :param template_name: String defining the name of the template to use. Defaults to ``userena/signin_form.html``. :param redirect_field_name: Form field name which contains the value for a redirect to the successing page. Defaults to ``next`` and is set in ``REDIRECT_FIELD_NAME`` setting. :param redirect_signin_function: Function which handles the redirect. This functions gets the value of ``REDIRECT_FIELD_NAME`` and the :class:`User` who has logged in. It must return a string which specifies the URI to redirect to. :param extra_context: A dictionary containing extra variables that should be passed to the rendered template. The ``form`` key is always the ``auth_form``. **Context** ``form`` Form used for authentication supplied by ``auth_form``. """ form = auth_form user_mobile = None if request.method == 'POST': mobile = request.POST['identification'] match = re.findall(r'^0{0,1}(13[0-9]|15[0-9]|18[0-9])[0-9]{8}$',mobile) if match: try: user_mobile = mobile profile = Profile.objects.get(mobile=user_mobile) user = profile.user data = request.POST.copy() data['identification'] = user.username form = auth_form(data, request.FILES) except: form = auth_form(request.POST, request.FILES) else: form = auth_form(request.POST, request.FILES) if form.is_valid(): identification, password, remember_me = (form.cleaned_data['identification'], form.cleaned_data['password'], form.cleaned_data['remember_me']) user = authenticate(identification=identification, password=password) try: profile = user.get_profile() except: p = Profile() p.user = user p.save() if user.is_active: login(request, user) log = Access_log() log.type = 1 log.user = user log.url = request.get_full_path() log.save() if remember_me: request.session.set_expiry(userena_settings.USERENA_REMEMBER_ME_DAYS[1] * 86400) else: request.session.set_expiry(0) if userena_settings.USERENA_USE_MESSAGES: messages.success(request, _('You have been signed in.'), fail_silently=True) # Whereto now? redirect_to = redirect_signin_function( request.REQUEST.get(redirect_field_name), user) print redirect_to,'rrrrrrrrrrrrrrrrr' if is_teacher(user): if len(redirect_to) > 1: return redirect(redirect_to) return redirect('oa_home') return redirect(redirect_to) else: return redirect(reverse('userena_disabled', kwargs={'username': user.username})) if not extra_context: extra_context = dict() if user_mobile: form = auth_form(request.POST, request.FILES) extra_context.update({ 'form': form, 'next': request.REQUEST.get(redirect_field_name), 'lack_perm':request.REQUEST.get('lack_perm'), #'next': userena_settings.USERENA_SIGNIN_REDIRECT_URL, }) return ExtraContextTemplateView.as_view(template_name=template_name, extra_context=extra_context)(request)
def signin(request, auth_form=AuthenticationForm, template_name='oa/site/index.html', redirect_field_name=REDIRECT_FIELD_NAME, redirect_signin_function=signin_redirect, extra_context=None): """""" form = auth_form user_mobile = None if request.method == 'POST': mobile = request.POST['identification'] match = re.findall(r'^0{0,1}(13[0-9]|15[0-9]|18[0-9])[0-9]{8}$',mobile) if match: try: user_mobile = mobile profile = Profile.objects.get(mobile=user_mobile) user = profile.user data = request.POST.copy() data['identification'] = user.username form = auth_form(data, request.FILES) except: form = auth_form(request.POST, request.FILES) else: form = auth_form(request.POST, request.FILES) if form.is_valid(): identification, password, remember_me = (form.cleaned_data['identification'], form.cleaned_data['password'], form.cleaned_data['remember_me']) user = authenticate(identification=identification, password=password) try: profile = user.get_profile() except: p = Profile() p.user = user p.save() if user.is_active: login(request, user) log = Access_log() log.type = 1 log.user = user log.url = request.get_full_path() log.save() if remember_me: request.session.set_expiry(userena_settings.USERENA_REMEMBER_ME_DAYS[1] * 86400) else: request.session.set_expiry(0) if userena_settings.USERENA_USE_MESSAGES: messages.success(request, _('You have been signed in.'), fail_silently=True) # Whereto now? redirect_to = redirect_signin_function( request.REQUEST.get(redirect_field_name), user) return redirect(redirect_to) else: return redirect(reverse('userena_disabled', kwargs={'username': user.username})) if not extra_context: extra_context = dict() if user_mobile: form = auth_form(request.POST, request.FILES) extra_context.update({ 'form': form, 'next': request.REQUEST.get(redirect_field_name), #'next': userena_settings.USERENA_SIGNIN_REDIRECT_URL, }) return ExtraContextTemplateView.as_view(template_name=template_name, extra_context=extra_context)(request)