mirror of
https://github.com/haiwen/seahub.git
synced 2025-09-01 23:20:51 +00:00
Change account context storage from cache to session
This commit is contained in:
@@ -1,16 +1,12 @@
|
||||
from seahub.utils import get_cur_ctx
|
||||
|
||||
def org(request):
|
||||
"""
|
||||
Add org info and base template that html page will extends to context.
|
||||
"""
|
||||
if hasattr(request.user, 'org') and request.user.org is not None:
|
||||
if request.user.org['is_staff']:
|
||||
base_template = 'org_admin_base.html'
|
||||
else:
|
||||
base_template = 'org_base.html'
|
||||
return {'cur_ctx': 'org',
|
||||
'org': request.user.org,
|
||||
'base_template': base_template}
|
||||
else:
|
||||
return {'cur_ctx': '',
|
||||
'base_template': 'myhome_base.html'}
|
||||
ctx_dict = get_cur_ctx(request)
|
||||
base_template = ctx_dict['base_template']
|
||||
org_dict = ctx_dict['org_dict']
|
||||
return {'base_template': base_template,
|
||||
'org': org_dict}
|
||||
|
||||
|
@@ -3,11 +3,11 @@ from django.http import HttpResponseRedirect
|
||||
|
||||
from seaserv import get_org_by_url_prefix, get_orgs_by_user
|
||||
|
||||
from settings import ORG_CACHE_PREFIX
|
||||
try:
|
||||
from seahub.settings import CLOUD_MODE
|
||||
except ImportError:
|
||||
CLOUD_MODE = False
|
||||
from seahub.utils import get_cur_ctx
|
||||
|
||||
class OrganizationMiddleware(object):
|
||||
"""
|
||||
@@ -20,8 +20,8 @@ class OrganizationMiddleware(object):
|
||||
request.cloud_mode = True
|
||||
|
||||
# Get current org context
|
||||
org = cache.get(ORG_CACHE_PREFIX + request.user.username)
|
||||
request.user.org = org
|
||||
ctx_dict = get_cur_ctx(request)
|
||||
request.user.org = ctx_dict.get('org_dict', None)
|
||||
|
||||
# Get all orgs user created.
|
||||
orgs = get_orgs_by_user(request.user.username)
|
||||
|
@@ -1,3 +1 @@
|
||||
from django.conf import settings
|
||||
|
||||
ORG_CACHE_PREFIX = getattr(settings, 'ORG_CACHE_PREFIX', 'ORGANIZATION_')
|
||||
|
@@ -46,7 +46,7 @@
|
||||
{% if repo.latest_modify %}
|
||||
<td>{{ repo.latest_modify|translate_commit_time }}</td>
|
||||
{% else %}
|
||||
<td>—— ——</td>
|
||||
<td>--</td>
|
||||
{% endif %}
|
||||
<td>
|
||||
<img src="{{ MEDIA_URL }}img/sync-20.png" data="{{ repo.props.id }}" class="download-btn vh" title="同步到本地" alt="同步" />
|
||||
|
@@ -35,7 +35,7 @@
|
||||
<td><a href="{% url user_profile user.email %}">{{ user.props.email }}</a></td>
|
||||
<td>
|
||||
{% if not user.is_self %}
|
||||
<button class="remove-user-btn" data="{% url org_user_remove org.url_prefix user.email %}">删除</button>
|
||||
<button class="remove-user-btn" data="{% url org_user_remove request.user.org.url_prefix user.email %}">删除</button>
|
||||
{% endif %}
|
||||
</td>
|
||||
</tr>
|
||||
|
@@ -1,23 +1,7 @@
|
||||
import sys
|
||||
from django.core.cache import cache
|
||||
from settings import ORG_CACHE_PREFIX
|
||||
|
||||
from seaserv import get_org_id_by_repo_id
|
||||
|
||||
def clear_org_ctx(request):
|
||||
"""
|
||||
Clear current context.
|
||||
"""
|
||||
cache.delete(ORG_CACHE_PREFIX + request.user.username)
|
||||
request.user.org = None
|
||||
|
||||
def set_org_ctx(request, org_dict):
|
||||
"""
|
||||
Set current context to org.
|
||||
"""
|
||||
cache.set(ORG_CACHE_PREFIX + request.user.username, org_dict, sys.maxint)
|
||||
request.user.org = org_dict
|
||||
|
||||
def access_org_repo(request, repo_id):
|
||||
"""
|
||||
Check whether user can view org repo.
|
||||
@@ -28,6 +12,3 @@ def access_org_repo(request, repo_id):
|
||||
cur_org_id = request.user.org['org_id']
|
||||
org_id = get_org_id_by_repo_id(repo_id)
|
||||
return True if cur_org_id == org_id else False
|
||||
|
||||
|
||||
|
||||
|
@@ -19,14 +19,12 @@ from seaserv import ccnet_threaded_rpc, get_orgs_by_user, get_org_repos, \
|
||||
|
||||
from forms import OrgCreateForm
|
||||
from signals import org_user_added
|
||||
from settings import ORG_CACHE_PREFIX
|
||||
from utils import set_org_ctx
|
||||
from notifications.models import UserNotification
|
||||
from registration.models import RegistrationProfile
|
||||
from seahub.forms import RepoCreateForm
|
||||
import seahub.settings as seahub_settings
|
||||
from seahub.utils import render_error, render_permission_error, validate_group_name, \
|
||||
emails2list, gen_token
|
||||
from seahub.utils import render_error, render_permission_error, gen_token, \
|
||||
validate_group_name, emails2list, set_cur_ctx, calculate_repo_last_modify
|
||||
from seahub.views import myhome
|
||||
|
||||
@login_required
|
||||
@@ -65,11 +63,15 @@ def org_info(request, url_prefix):
|
||||
if not org:
|
||||
return HttpResponseRedirect(reverse(myhome))
|
||||
|
||||
set_org_ctx(request, org._dict)
|
||||
ctx_dict = {'base_template': 'org_base.html',
|
||||
'org_dict': org._dict}
|
||||
set_cur_ctx(request, ctx_dict)
|
||||
|
||||
org_members = ccnet_threaded_rpc.get_org_emailusers(url_prefix,
|
||||
0, sys.maxint)
|
||||
repos = get_org_repos(org.org_id, 0, sys.maxint)
|
||||
calculate_repo_last_modify(repos)
|
||||
repos.sort(lambda x, y: cmp(y.latest_modify, x.latest_modify))
|
||||
|
||||
url = 'organizations/%s/repo/create/' % org.url_prefix
|
||||
return render_to_response('organizations/org_info.html', {
|
||||
@@ -139,6 +141,10 @@ def org_useradmin(request, url_prefix):
|
||||
if not request.user.org['is_staff']:
|
||||
raise Http404
|
||||
|
||||
ctx_dict = {'base_template': 'org_admin_base.html',
|
||||
'org_dict': request.user.org}
|
||||
set_cur_ctx(request, ctx_dict)
|
||||
|
||||
if request.method == 'POST':
|
||||
emails = request.POST.get('added-member-name')
|
||||
|
||||
|
@@ -1,5 +1,5 @@
|
||||
{% extends "base.html" %}
|
||||
{% block top_bar_manager_class %} class="cur"{% endblock %}
|
||||
{% block top_bar_sys_manager_class %} class="cur"{% endblock %}
|
||||
{% block nav %}
|
||||
<ul class="nav">
|
||||
{% if request.user.is_staff %}
|
||||
|
@@ -41,19 +41,14 @@
|
||||
{% endif %}
|
||||
{% endif %}
|
||||
|
||||
{% if request.user.is_staff or request.user.org.is_staff %}
|
||||
{% if request.user.is_staff %}
|
||||
<a href="{{ SITE_ROOT }}sys/useradmin/"
|
||||
{% else %}
|
||||
<a href="{% url org_useradmin request.user.org.url_prefix %}"
|
||||
<a href="{{ SITE_ROOT }}sys/useradmin/"{% block top_bar_sys_manager_class %}{% endblock %}>系统管理</a>
|
||||
<a href="{{ SITE_ROOT }}home/my/"{% block top_bar_myaccount_class %}{% endblock %}>个人工作台</a>
|
||||
{% endif %}
|
||||
{% block top_bar_manager_class %}{% endblock %}>管理员工作台</a>
|
||||
{% if request.user.is_staff %}
|
||||
<a href="{{ SITE_ROOT }}home/my/"
|
||||
{% else %}
|
||||
<a href="{% url organizations.views.org_info org.url_prefix %}"
|
||||
{% endif %}
|
||||
{% block top_bar_myaccount_class %}{% endblock %}>个人工作台</a>
|
||||
|
||||
{% if request.user.org.is_staff %}
|
||||
<a href="{% url org_useradmin request.user.org.url_prefix %}"{% block top_bar_org_manager_class %}{% endblock %}>管理员工作台</a>
|
||||
<a href="{% url organizations.views.org_info request.user.org.url_prefix %}"{% block top_bar_org_myaccount_class %}{% endblock %}>个人工作台</a>
|
||||
{% endif %}
|
||||
</div>
|
||||
|
||||
|
@@ -116,7 +116,7 @@
|
||||
{% if repo.latest_modify %}
|
||||
<td>{{ repo.latest_modify|translate_commit_time }}</td>
|
||||
{% else %}
|
||||
<td>—— ——</td>
|
||||
<td>--</tdb>
|
||||
{% endif %}
|
||||
<td>
|
||||
<img src="{{ MEDIA_URL }}img/sync-20.png" data="{{ repo.props.id }}" class="download-btn vh" title="同步到本地" alt="同步" />
|
||||
@@ -147,7 +147,7 @@
|
||||
{% if repo.latest_modify %}
|
||||
<td>{{ repo.latest_modify|translate_commit_time }}</td>
|
||||
{% else %}
|
||||
<td>—— ——</td>
|
||||
<td>--</td>
|
||||
{% endif %}
|
||||
<td>
|
||||
<img src="{{ MEDIA_URL }}img/sync-20.png" data="{{ repo.props.id }}" class="download-btn vh" title="同步到本地" alt="同步" />
|
||||
|
@@ -1,5 +1,5 @@
|
||||
{% extends "base.html" %}
|
||||
{% block top_bar_manager_class %} class="cur"{% endblock %}
|
||||
{% block top_bar_org_manager_class %} class="cur"{% endblock %}
|
||||
{% block nav %}
|
||||
<ul class="nav">
|
||||
{% if request.user.org.is_staff %}
|
||||
|
@@ -1,5 +1,5 @@
|
||||
{% extends "base.html" %}
|
||||
{% block top_bar_myaccount_class %} class="cur"{% endblock %}
|
||||
{% block top_bar_org_myaccount_class %} class="cur"{% endblock %}
|
||||
{% block nav %}
|
||||
<ul class="nav">
|
||||
<li>
|
||||
|
10
utils.py
10
utils.py
@@ -313,3 +313,13 @@ def emails2list(emails):
|
||||
s.add(e)
|
||||
return [ x for x in s ]
|
||||
|
||||
def get_cur_ctx(request):
|
||||
ctx_dict = request.session.get('current_context', {
|
||||
'base_template': 'myhome_base.html',
|
||||
'org_dict': None})
|
||||
return ctx_dict
|
||||
|
||||
def set_cur_ctx(request, ctx_dict):
|
||||
request.session['current_context'] = ctx_dict
|
||||
request.user.org = ctx_dict.get('org_dict', None)
|
||||
|
||||
|
9
views.py
9
views.py
@@ -40,14 +40,14 @@ from seahub.base.models import UuidObjidMap
|
||||
from seahub.contacts.models import Contact
|
||||
from seahub.contacts.signals import mail_sended
|
||||
from seahub.notifications.models import UserNotification
|
||||
from seahub.organizations.utils import clear_org_ctx, access_org_repo
|
||||
from seahub.organizations.utils import access_org_repo
|
||||
from forms import AddUserForm, FileLinkShareForm, RepoCreateForm
|
||||
from utils import render_permission_error, render_error, list_to_string, \
|
||||
get_httpserver_root, get_ccnetapplet_root, gen_token, \
|
||||
calculate_repo_last_modify, valid_previewed_file, \
|
||||
check_filename_with_rename, get_accessible_repos, EMPTY_SHA1, \
|
||||
get_file_revision_id_size, get_ccnet_server_addr_port, \
|
||||
gen_file_get_url, emails2list
|
||||
gen_file_get_url, emails2list, set_cur_ctx
|
||||
from seahub.profile.models import Profile
|
||||
try:
|
||||
from settings import CROCODOC_API_TOKEN
|
||||
@@ -701,8 +701,9 @@ def myhome(request):
|
||||
profile = Profile.objects.filter(user=request.user.username)[0]
|
||||
nickname = profile.nickname
|
||||
|
||||
# clear org context in cache and set request.user.org to None
|
||||
clear_org_ctx(request)
|
||||
ctx_dict = {'base_template': 'myhome_base.html',
|
||||
'org_dict': None}
|
||||
set_cur_ctx(request, ctx_dict)
|
||||
|
||||
return render_to_response('myhome.html', {
|
||||
"myname": email,
|
||||
|
Reference in New Issue
Block a user