Commit e11f60ade21caf94a7350969c66725450079e5e4

Authored by zhenchaozhu
1 parent c51fd49afc
Exists in master

m

homepage/views.py
1 1
2 2
3 import json 3 import json
4 import logging 4 import logging
5 from django.contrib import auth 5 from django.contrib import auth
6 from django.contrib.auth import logout 6 from django.contrib.auth import logout
7 from django.contrib.auth.decorators import login_required 7 from django.contrib.auth.decorators import login_required
8 from django.http import HttpResponseRedirect 8 from django.http import HttpResponseRedirect
9 from django.contrib.auth import get_user_model 9 from django.contrib.auth import get_user_model
10 from django.shortcuts import render_to_response 10 from django.shortcuts import render_to_response
11 from django.template import RequestContext 11 from django.template import RequestContext
12 from django.template.context_processors import csrf 12 from django.template.context_processors import csrf
13 from django.conf import settings
13 14
14 @login_required 15 @login_required
15 def homepage(request): 16 def homepage(request):
16 17
17 username = request.user.username 18 username = request.user.username
18 t = HttpResponseRedirect('/admin/') 19 t = HttpResponseRedirect('/admin/')
19 t.set_cookie('username', username, 864000) 20 t.set_cookie('username', username, 864000)
20 return t 21 return t
21 22
22 23
23 def mylogin(request): 24 def mylogin(request):
24 alert_msg= 0 25 alert_msg= 0
25 if request.method =='GET': 26 if request.method =='GET':
26 cookies_username = request.COOKIES.get('username','') 27 cookies_username = request.COOKIES.get('username','')
27 c = csrf(request) 28 c = csrf(request)
28 c.update({'alert_msg':alert_msg,'cookies_username:':cookies_username}) 29 c.update({'alert_msg':alert_msg,'cookies_username:':cookies_username})
29 return render_to_response('login.html', c) 30 return render_to_response('login.html', c)
30 else: 31 else:
31 postdata = request.POST 32 postdata = request.POST
32 username = postdata.get('username','') 33 username = postdata.get('username','')
33 password = postdata.get('password','') 34 password = postdata.get('password','')
34 user = auth.authenticate(username=username, password=password) 35 post_params = {
36 'comefrom': 2,
37 'user_name': username,
38 'password': password,
39 }
40 resp = request.post(settings.AUTH_DOMAIN, data=post_params, verify=False)
41 if resp.status_code == 200:
42 rst = resp.json()
43 if rst.get('status') == 1:
44 data = rst.get('data')
45 token = data.get('token')
46 suid = data.get('1000')
47 t = HttpResponseRedirect('/admin/')
48 t.set_cookie('pu', username, 864000)
49 t.set_cookie(('pt'), )
50
51 else:
52 pass
35 if user: 53 if user:
36 auth.login(request, user) 54 auth.login(request, user)
37 t = HttpResponseRedirect('/admin/') 55 t = HttpResponseRedirect('/admin/')
38 t.set_cookie('username', username, 864000) 56 t.set_cookie('username', username, 864000)
39 return t 57 return t
40 58
41 59
42 def mylogout(request): 60 def mylogout(request):
43 logout(request) 61 logout(request)
44 return HttpResponseRedirect("/login/") 62 return HttpResponseRedirect("/login/")
middlewares/session_middleware.py
File was created 1 # coding: utf-8
2
3 import requests
4 from django.conf import settings
5 from django.core.cache import caches
6 from django.contrib.auth import get_user_model
7 from django.contrib.auth.models import AnonymousUser
8
9 class SessionWithoutLocalUserMiddleware(object):
10 """
11 统一权限(认证)中间件,Django系统本地不保存用户的情况使用
12 """
13
14 def __init__(self):
15 self.cache_alias = settings.CACHE_MIDDLEWARE_ALIAS
16 self.cache = caches[self.cache_alias]
17 self.UserModel = get_user_model()
18
19 def process_request(self, request):
20 if hasattr(request, "user") and getattr(request.user, "is_superuser", False):
21 # 对于Django系统的admin用户,这里不做任何处理
22 pass
23 else:
24 pt = request.COOKIES.get('pt')
25 pu = request.COOKIES.get('pu')
26 if pt and pu:
27 # 能拿到统一认证session的情况,优先从缓存中拿用户
28 user = self.cache.get(pu)
29 if not user:
30 # 如果缓存未命中,则直接调用统一权限,查询当前session的状态,构造用户,并存入缓存
31 user_info = ''
32
33 manager = Manager()
34 user_info = manager.get_user_info(request)
35 if user_info is None:
36 # 查询session状态失败的情况,构造匿名用户
37 user = AnonymousUser()
38 else:
39 # 查询session状态成功的情况,构造QCCRUser
40 user = user_info
41 self.cache.set(pt, user, 60)
42 request.user = user
43 else:
44 # 拿不到统一认证的session,将当前用户设为匿名用户
45 request.user = AnonymousUser()
46
47
48 class Manager(object):
49
50 def __init__(self):
51 self.auth_domain = 'https://api.xiuyetang.com/sys/user/login'
52
53 def get_user_info(self, request):
54 pass
weapp_sys/settings.py
1 """ 1 """
2 Django settings for weapp_sys project. 2 Django settings for weapp_sys project.
3 3
4 Generated by 'django-admin startproject' using Django 1.8. 4 Generated by 'django-admin startproject' using Django 1.8.
5 5
6 For more information on this file, see 6 For more information on this file, see
7 https://docs.djangoproject.com/en/1.8/topics/settings/ 7 https://docs.djangoproject.com/en/1.8/topics/settings/
8 8
9 For the full list of settings and their values, see 9 For the full list of settings and their values, see
10 https://docs.djangoproject.com/en/1.8/ref/settings/ 10 https://docs.djangoproject.com/en/1.8/ref/settings/
11 """ 11 """
12 12
13 # Build paths inside the project like this: os.path.join(BASE_DIR, ...) 13 # Build paths inside the project like this: os.path.join(BASE_DIR, ...)
14 import os 14 import os
15 15
16 BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) 16 BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
17 17
18 18
19 # Quick-start development settings - unsuitable for production 19 # Quick-start development settings - unsuitable for production
20 # See https://docs.djangoproject.com/en/1.8/howto/deployment/checklist/ 20 # See https://docs.djangoproject.com/en/1.8/howto/deployment/checklist/
21 21
22 # SECURITY WARNING: keep the secret key used in production secret! 22 # SECURITY WARNING: keep the secret key used in production secret!
23 SECRET_KEY = 'y7m+ekd64@hxa0ej(&w6u!itch2glt)+6imlbr7ob8=_nk%@gi' 23 SECRET_KEY = 'y7m+ekd64@hxa0ej(&w6u!itch2glt)+6imlbr7ob8=_nk%@gi'
24 24
25 # SECURITY WARNING: don't run with debug turned on in production! 25 # SECURITY WARNING: don't run with debug turned on in production!
26 DEBUG = True 26 DEBUG = True
27 27
28 ALLOWED_HOSTS = [] 28 ALLOWED_HOSTS = []
29 29
30 30
31 # Application definition 31 # Application definition
32 32
33 INSTALLED_APPS = ( 33 INSTALLED_APPS = (
34 'django.contrib.admin', 34 'django.contrib.admin',
35 'django.contrib.auth', 35 'django.contrib.auth',
36 'django.contrib.contenttypes', 36 'django.contrib.contenttypes',
37 'django.contrib.sessions', 37 'django.contrib.sessions',
38 'django.contrib.messages', 38 'django.contrib.messages',
39 'django.contrib.staticfiles', 39 'django.contrib.staticfiles',
40 'homepage', 40 'homepage',
41 ) 41 )
42 42
43 MIDDLEWARE_CLASSES = ( 43 MIDDLEWARE_CLASSES = (
44 'django.contrib.sessions.middleware.SessionMiddleware', 44 'django.contrib.sessions.middleware.SessionMiddleware',
45 'django.middleware.common.CommonMiddleware', 45 'django.middleware.common.CommonMiddleware',
46 'django.middleware.csrf.CsrfViewMiddleware', 46 'django.middleware.csrf.CsrfViewMiddleware',
47 'django.contrib.auth.middleware.AuthenticationMiddleware', 47 'django.contrib.auth.middleware.AuthenticationMiddleware',
48 'django.contrib.auth.middleware.SessionAuthenticationMiddleware', 48 'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
49 'django.contrib.messages.middleware.MessageMiddleware', 49 'django.contrib.messages.middleware.MessageMiddleware',
50 'django.middleware.clickjacking.XFrameOptionsMiddleware', 50 'django.middleware.clickjacking.XFrameOptionsMiddleware',
51 'django.middleware.security.SecurityMiddleware', 51 'django.middleware.security.SecurityMiddleware',
52 ) 52 )
53 53
54 ROOT_URLCONF = 'weapp_sys.urls' 54 ROOT_URLCONF = 'weapp_sys.urls'
55 55
56 TEMPLATES = [ 56 TEMPLATES = [
57 { 57 {
58 'BACKEND': 'django.template.backends.django.DjangoTemplates', 58 'BACKEND': 'django.template.backends.django.DjangoTemplates',
59 'DIRS': [os.path.join(BASE_DIR, 'templates')], 59 'DIRS': [os.path.join(BASE_DIR, 'templates')],
60 'APP_DIRS': True, 60 'APP_DIRS': True,
61 'OPTIONS': { 61 'OPTIONS': {
62 'context_processors': [ 62 'context_processors': [
63 'django.template.context_processors.debug', 63 'django.template.context_processors.debug',
64 'django.template.context_processors.request', 64 'django.template.context_processors.request',
65 'django.contrib.auth.context_processors.auth', 65 'django.contrib.auth.context_processors.auth',
66 'django.contrib.messages.context_processors.messages', 66 'django.contrib.messages.context_processors.messages',
67 ], 67 ],
68 }, 68 },
69 }, 69 },
70 ] 70 ]
71 71
72 72
73 WSGI_APPLICATION = 'weapp_sys.wsgi.application' 73 WSGI_APPLICATION = 'weapp_sys.wsgi.application'
74 74
75 AUTH_USER_MODEL = 'homepage.ForumUser' 75 AUTH_USER_MODEL = 'homepage.ForumUser'
76 76
77 # Database 77 # Database
78 # https://docs.djangoproject.com/en/1.8/ref/settings/#databases 78 # https://docs.djangoproject.com/en/1.8/ref/settings/#databases
79 79
80 DATABASES = { 80 DATABASES = {
81 'default': { 81 'default': {
82 'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'. 82 'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
83 'NAME': 'forum', # Or path to database file if using sqlite3. 83 'NAME': 'forum', # Or path to database file if using sqlite3.
84 # The following settings are not used with sqlite3: 84 # The following settings are not used with sqlite3:
85 'USER': 'root', 85 'USER': 'root',
86 'PASSWORD': '', 86 'PASSWORD': '',
87 'HOST': '127.0.0.1', # Empty for localhost through domain sockets or '127.0.0.1' for localhost through TCP. 87 'HOST': '127.0.0.1', # Empty for localhost through domain sockets or '127.0.0.1' for localhost through TCP.
88 'PORT': '3306', 88 'PORT': '3306',
89 } 89 }
90 } 90 }
91 91
92 92
93 # Internationalization 93 # Internationalization
94 # https://docs.djangoproject.com/en/1.8/topics/i18n/ 94 # https://docs.djangoproject.com/en/1.8/topics/i18n/
95 95
96 LANGUAGE_CODE = 'zh-CN' 96 LANGUAGE_CODE = 'zh-CN'
97 97
98 TIME_ZONE = 'Asia/Shanghai' 98 TIME_ZONE = 'Asia/Shanghai'
99 99
100 USE_I18N = True 100 USE_I18N = True
101 101
102 USE_L10N = True 102 USE_L10N = True
103 103
104 USE_TZ = False 104 USE_TZ = False
105 105
106 DATETIME_FORMAT = 'Y-m-d H:i:s' 106 DATETIME_FORMAT = 'Y-m-d H:i:s'
107 TIME_FORMAT = 'H:i:s' 107 TIME_FORMAT = 'H:i:s'
108 108
109 109
110 STATIC_ROOT = os.path.join(BASE_DIR, 'static') 110 STATIC_ROOT = os.path.join(BASE_DIR, 'static')
111 STATIC_URL = '/static/' 111 STATIC_URL = '/static/'
112 STATICFILES_DIRS = ( 112 STATICFILES_DIRS = (
113 # Put strings here, like "/home/html/static" or "C:/www/django/static". 113 # Put strings here, like "/home/html/static" or "C:/www/django/static".
114 # Always use forward slashes, even on Windows. 114 # Always use forward slashes, even on Windows.
115 # Don't forget to use absolute paths, not relative paths. 115 # Don't forget to use absolute paths, not relative paths.
116 116
117 # ("css", os.path.join(STATIC_ROOT,'css')), 117 # ("css", os.path.join(STATIC_ROOT,'css')),
118 ("js", os.path.join(STATIC_ROOT, 'js')), 118 ("js", os.path.join(STATIC_ROOT, 'js')),
119 ("image", os.path.join(STATIC_ROOT, 'image')), 119 ("image", os.path.join(STATIC_ROOT, 'image')),
120 ("css", os.path.join(STATIC_ROOT, 'css')), 120 ("css", os.path.join(STATIC_ROOT, 'css')),
121 ("dist", os.path.join(STATIC_ROOT, 'dist')), 121 ("dist", os.path.join(STATIC_ROOT, 'dist')),
122 ("plugins", os.path.join(STATIC_ROOT, 'plugins')), 122 ("plugins", os.path.join(STATIC_ROOT, 'plugins')),
123 ("fonts", os.path.join(STATIC_ROOT, 'fonts')), 123 ("fonts", os.path.join(STATIC_ROOT, 'fonts')),
124 ("font-awesome", os.path.join(STATIC_ROOT, 'font-awesome')), 124 ("font-awesome", os.path.join(STATIC_ROOT, 'font-awesome')),
125 ("img", os.path.join(STATIC_ROOT, 'img')), 125 ("img", os.path.join(STATIC_ROOT, 'img')),
126 ("bootstrap", os.path.join(STATIC_ROOT, 'bootstrap')), 126 ("bootstrap", os.path.join(STATIC_ROOT, 'bootstrap')),
127 ("apps/ueditor", os.path.join(STATIC_ROOT, 'ueditor')), 127 ("apps/ueditor", os.path.join(STATIC_ROOT, 'ueditor')),
128 ("echarts", os.path.join(STATIC_ROOT, 'echarts')), 128 ("echarts", os.path.join(STATIC_ROOT, 'echarts')),
129 ("ueditor", os.path.join(STATIC_ROOT, 'ueditor')), 129 ("ueditor", os.path.join(STATIC_ROOT, 'ueditor')),
130 ("ventor", os.path.join(STATIC_ROOT, 'ventor')), 130 ("ventor", os.path.join(STATIC_ROOT, 'ventor')),
131 ) 131 )
132 132
133 133
134 BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) 134 BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
135 MEDIA_ROOT = os.path.join(BASE_DIR, 'media') 135 MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
136 MEDIA_URL = '/media/' 136 MEDIA_URL = '/media/'
137 137
138 LOGIN_URL = '/login/'
138 LOGIN_URL = '/login/'
139