Exemple #1
0
def star_figure_detail(request,site_id,user_id,template_name="oa/start_form.html"):
    user = get_object_or_404(User,id=user_id)
    schools = get_schools(request.user)
    site = get_object_or_404(WebSite,id=site_id,school__in=schools)
    helpers.set_website_visit(request.user,site)
    if is_teacher(user):
        next = reverse('oa_star_figure_teacher',kwargs={'site_id':site_id})
    else:
        next = reverse('oa_star_figure_student',kwargs={'site_id':site_id}) 
    try:
        figure = user.figure
    except:
        figure = StarFigure(user=user).save()
    
    if request.method == 'POST':
      form = StarFigureForm(request.POST,instance=figure)
      
      if request.is_ajax():
            return helpers.ajax_validate_form(form)
        
      if form.is_valid():
          part = form.save()
          messages.success(request, u'已成功修改%s ' % user.profile.realname)
          return redirect(next)
    else:
      form = StarFigureForm(instance=figure)
    ctx = {'form':form,'user':user,'next':next,'site':site}
    return render(request, template_name, ctx)
Exemple #2
0
def get_school(user):
    """
    获取老师或学生的学校
    """
    if is_teacher(user):
        return user.teacher.school
    if is_student(user):
        return user.student.school
    return None
Exemple #3
0
def get_name(user):
    """
    获取老师或学生的姓名
    """
    if is_teacher(user) and user.teacher.name:
        return user.teacher.name
    if is_student(user) and user.student.name:
        return user.student.name
    try:
        return user.profile.chinese_name_or_username()
    except:
        return ''
Exemple #4
0
def account_setting(request, template_name="oa/account_setting.html"):
    """个人设置"""
    if request.method == 'POST':
        password = request.user.password
        form1 = TeacherUserForm(request.POST,instance=request.user)
        form2 = UserProfileForm(request.POST, request.FILES,instance=request.user.get_profile())
        
        if form1.is_valid() and form2.is_valid():
            u = form1.save(commit=False)
            u.password = password
            u.save()
            profile = form2.save(commit=False)
            realname = request.POST['realname']
            profile.realname = realname
            profile.save()
            if helpers.is_teacher(request.user):
                teacher = request.user.teacher
                teacher.name = profile.realname
                teacher.save()
            if helpers.is_student(request.user):
                student = request.user.student
                student.name = profile.realname
                student.save()
            
            messages.success(request, _('Your profile has been updated.'))
            return redirect("oa_account_setting")
    else:
        form1 = TeacherUserForm(instance=request.user)
        form2 = UserProfileForm(instance=request.user.get_profile())
    
    
    try:
        postjob = request.user.teacher.postjob
    except:
        postjob = None
    ctx = {'form1': form1,'form2': form2,'postjob':postjob}
    ctx.update(csrf(request))
    return render(request, template_name, ctx)
Exemple #5
0
def cross_domain_login(request):
    uid = request.GET.get('uid','')
    salt = request.GET.get('salt','')
    hash = request.GET.get('hash','')
    mask = '3n7j6m9s'
    m = md5.new(str(uid) + str(salt) + mask)
    new_hash = m.hexdigest()
    
    if new_hash == hash:
        user = User.objects.get(pk=uid)
        user.backend = 'django.contrib.auth.backends.ModelBackend'
        user.save()
#        user = auth.authenticate(username=u.username, password=u.password)
        is_teacher = helpers.is_teacher(user)
        is_student = helpers.is_student(user)
        if user is not None and user.is_active:
            auth.login(request, user)
    
        d = json.dumps({'is_teacher':is_teacher,'is_student':is_student,\
                        'username':helpers.get_name(user)})
#        request.GET['callback']+"("+(d)+");"
        return HttpResponse(request.GET['callback']+"("+(d)+");")
    else:
        return HttpResponse(json.dumps({}))
Exemple #6
0
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)