@login_required
@require_POST
def share_repo(request):
"""
Handle POST method to share a repo to public/groups/users based on form
data. Return to ``myhome`` page and notify user whether success or failure.
"""
next = request.META.get('HTTP_REFERER', None)
if not next:
next = SITE_ROOT
form = RepoShareForm(request.POST)
if not form.is_valid():
# TODO: may display error msg on form
raise Http404
email_or_group = form.cleaned_data['email_or_group']
repo_id = form.cleaned_data['repo_id']
permission = form.cleaned_data['permission']
repo = seafile_api.get_repo(repo_id)
if not repo:
raise Http404
# Test whether user is the repo owner.
username = request.user.username
if not seafile_api.is_repo_owner(username, repo_id) and \
not is_org_repo_owner(username, repo_id):
msg = _(u'Only the owner of the library has permission to share it.')
messages.error(request, msg)
return HttpResponseRedirect(next)
# Parsing input values.
share_to_all, share_to_groups, share_to_users = False, [], []
user_groups = request.user.joined_groups
share_to_list = string2list(email_or_group)
for share_to in share_to_list:
if share_to == 'all':
share_to_all = True
#elif share_to.find('@') == -1:
elif share_to[0] == '%':
for user_group in user_groups:
if user_group.group_name == share_to[1:]:
share_to_groups.append(user_group)
else:
share_to = share_to.lower()
if is_valid_username(share_to):
share_to_users.append(share_to)
if share_to_all:
share_to_public(request, repo, permission)
if not check_user_share_quota(username, repo, users=share_to_users,
groups=share_to_groups):
messages.error(request, _('Failed to share "%s", no enough quota. <a href="http://www.i2cloud.cn/">Upgrade account.</a>') % repo.name)
return HttpResponseRedirect(next)
for group in share_to_groups:
share_to_group(request, repo, group, permission)
for email in share_to_users:
# Add email to contacts.
mail_sended.send(sender=None, user=request.user.username, email=email)
share_to_user(request, repo, email, permission)
return HttpResponseRedirect(next)
上面的代码中的
第一个是在匹配固定格式...
后面那个冒号是python条件判断表达式的语法...