def send_shared_upload_link(request): """ Handle ajax post request to send shared upload link. """ if not request.method == 'POST': raise Http404 content_type = 'application/json; charset=utf-8' if not IS_EMAIL_CONFIGURED: data = json.dumps({'error':_(u'Sending shared upload link failed. Email service is not properly configured, please contact administrator.')}) return HttpResponse(data, status=500, content_type=content_type) from seahub.settings import SITE_NAME form = UploadLinkShareForm(request.POST) if form.is_valid(): email = form.cleaned_data['email'] shared_upload_link = form.cleaned_data['shared_upload_link'] extra_msg = escape(form.cleaned_data['extra_msg']) to_email_list = string2list(email) for to_email in to_email_list: # Add email to contacts. mail_sended.send(sender=None, user=request.user.username, email=to_email) c = { 'email': request.user.username, 'to_email': to_email, 'shared_upload_link': shared_upload_link, } if extra_msg: c['extra_msg'] = extra_msg if REPLACE_FROM_EMAIL: from_email = request.user.username else: from_email = None # use default from email if ADD_REPLY_TO_HEADER: reply_to = request.user.username else: reply_to = None try: send_html_email(_(u'An upload link is shared to you on %s') % SITE_NAME, 'shared_upload_link_email.html', c, from_email, [to_email], reply_to=reply_to) except Exception, e: logger.error(str(e)) data = json.dumps({'error':_(u'Internal server error. Send failed.')}) return HttpResponse(data, status=500, content_type=content_type) data = json.dumps({"msg": _(u'Successfully sent.')}) return HttpResponse(data, status=200, content_type=content_type)
def send_shared_link(request): """ Handle ajax post request to send file shared link. """ if not request.is_ajax() and not request.method == 'POST': raise Http404 result = {} content_type = 'application/json; charset=utf-8' if not IS_EMAIL_CONFIGURED: data = json.dumps({ 'error': _(u'Sending shared link failed. Email service is not properly configured, please contact administrator.' ) }) return HttpResponse(data, status=500, content_type=content_type) from seahub.settings import SITE_NAME form = FileLinkShareForm(request.POST) if form.is_valid(): email = form.cleaned_data['email'] file_shared_link = form.cleaned_data['file_shared_link'] t = loader.get_template('shared_link_email.html') to_email_list = string2list(email) for to_email in to_email_list: # Add email to contacts. mail_sended.send(sender=None, user=request.user.username, email=to_email) c = { 'email': request.user.username, 'to_email': to_email, 'file_shared_link': file_shared_link, 'site_name': SITE_NAME, } try: send_mail(_(u'Your friend shared a file to you on Seafile'), t.render(Context(c)), None, [to_email], fail_silently=False) except Exception, e: logger.error(str(e)) data = json.dumps( {'error': _(u'Internal server error. Send failed.')}) return HttpResponse(data, status=500, content_type=content_type) data = json.dumps({"msg": _(u'Successfully sent.')}) return HttpResponse(data, status=200, content_type=content_type)
def send_shared_link(request): """ Handle ajax post request to send file shared link. """ if not request.method == 'POST': raise Http404 content_type = 'application/json; charset=utf-8' if not IS_EMAIL_CONFIGURED: data = json.dumps({'error':_(u'Sending shared link failed. Email service is not properly configured, please contact administrator.')}) return HttpResponse(data, status=500, content_type=content_type) from seahub.settings import SITE_NAME form = FileLinkShareForm(request.POST) if form.is_valid(): email = form.cleaned_data['email'] file_shared_link = form.cleaned_data['file_shared_link'] file_shared_name = form.cleaned_data['file_shared_name'] file_shared_type = form.cleaned_data['file_shared_type'] extra_msg = escape(form.cleaned_data['extra_msg']) to_email_list = string2list(email) for to_email in to_email_list: # Add email to contacts. mail_sended.send(sender=None, user=request.user.username, email=to_email) c = { 'email': request.user.username, 'to_email': to_email, 'file_shared_link': file_shared_link, 'file_shared_name': file_shared_name, } if extra_msg: c['extra_msg'] = extra_msg try: if file_shared_type == 'f': c['file_shared_type'] = "file" send_html_email(_(u'A file is shared to you on %s') % SITE_NAME, 'shared_link_email.html', c, None, [to_email]) else: c['file_shared_type'] = "directory" send_html_email(_(u'A directory is shared to you on %s') % SITE_NAME, 'shared_link_email.html', c, None, [to_email]) except Exception, e: logger.error(str(e)) data = json.dumps({'error':_(u'Internal server error. Send failed.')}) return HttpResponse(data, status=500, content_type=content_type) data = json.dumps({"msg": _(u'Successfully sent.')}) return HttpResponse(data, status=200, content_type=content_type)
def send_shared_link(request): """ Handle ajax post request to send file shared link. """ if not request.is_ajax() and not request.method == 'POST': raise Http404 result = {} content_type = 'application/json; charset=utf-8' if not IS_EMAIL_CONFIGURED: data = json.dumps({'error':_(u'Sending shared link failed. Email service is not properly configured, please contact administrator.')}) return HttpResponse(data, status=500, content_type=content_type) from seahub.settings import SITE_NAME form = FileLinkShareForm(request.POST) if form.is_valid(): email = form.cleaned_data['email'] file_shared_link = form.cleaned_data['file_shared_link'] t = loader.get_template('shared_link_email.html') to_email_list = string2list(email) for to_email in to_email_list: # Add email to contacts. mail_sended.send(sender=None, user=request.user.username, email=to_email) c = { 'email': request.user.username, 'to_email': to_email, 'file_shared_link': file_shared_link, 'site_name': SITE_NAME, } try: send_mail(_(u'Your friend shared a file to you on Seafile'), t.render(Context(c)), None, [to_email], fail_silently=False) except Exception, e: logger.error(str(e)) data = json.dumps({'error':_(u'Internal server error. Send failed.')}) return HttpResponse(data, status=500, content_type=content_type) data = json.dumps({"msg": _(u'Successfully sent.')}) return HttpResponse(data, status=200, content_type=content_type)
def send_shared_link(request): """ Handle ajax post request to send file shared link. """ if not request.is_ajax() and not request.method == 'POST': raise Http404 result = {} content_type = 'application/json; charset=utf-8' from seahub.settings import SITE_NAME form = FileLinkShareForm(request.POST) if form.is_valid(): email = form.cleaned_data['email'] file_shared_link = form.cleaned_data['file_shared_link'] t = loader.get_template('shared_link_email.html') to_email_list = string2list(email) for to_email in to_email_list: # Add email to contacts. mail_sended.send(sender=None, user=request.user.username, email=to_email) c = { 'email': request.user.username, 'to_email': to_email, 'file_shared_link': file_shared_link, 'site_name': SITE_NAME, } try: send_mail(_(u'Your friend sharing a file to you on Seafile'), t.render(Context(c)), None, [to_email], fail_silently=False) except: data = json.dumps({'error':_(u'Failed to send mail')}) return HttpResponse(data, status=500, content_type=content_type) data = json.dumps("success") return HttpResponse(data, status=200, content_type=content_type) else: return HttpResponseBadRequest(json.dumps(form.errors), content_type=content_type)
def group_add_admin(request, group_id): """ Add group admin. """ group_id = int(group_id) # Checked by URL Conf if request.method != 'POST' or not request.is_ajax(): raise Http404 result = {} content_type = 'application/json; charset=utf-8' member_name_str = request.POST.get('user_name', '') member_list = string2list(member_name_str) for member_name in member_list: # Add user to contacts. mail_sended.send(sender=None, user=request.user.username, email=member_name) if not is_registered_user(member_name): err_msg = _(u'Failed to add, %s is not registrated.') % member_name result['error'] = err_msg return HttpResponse(json.dumps(result), status=400, content_type=content_type) # Check whether user is in the group if is_group_user(group_id, member_name): try: ccnet_threaded_rpc.group_set_admin(group_id, member_name) except SearpcError, e: result['error'] = _(e.msg) return HttpResponse(json.dumps(result), status=500, content_type=content_type) else: try: ccnet_threaded_rpc.group_add_member(group_id, request.user.username, member_name) ccnet_threaded_rpc.group_set_admin(group_id, member_name) except SearpcError, e: result['error'] = _(e.msg) return HttpResponse(json.dumps(result), status=500, content_type=content_type)
fs.path = path fs.token = token try: fs.save() except IntegrityError, e: return api_err(request, '501') domain = RequestSite(request).domain file_shared_link = 'http://%s%sf/%s/' % (domain, settings.SITE_ROOT, token) t = loader.get_template('shared_link_email.html') to_email_list = string2list(emails) for to_email in to_email_list: mail_sended.send(sender=None, user=request.user.username, email=to_email) c = { 'email': request.user.username, 'to_email': to_email, 'file_shared_link': file_shared_link, } try: send_mail('您的好友通过SeaCloud分享了一个文件给您', t.render(Context(c)), None, [to_email], fail_silently=False) except: return api_error(request, '502') return HttpResponse(json.dumps(file_shared_link), status=200, content_type=json_content_type) class RepoFileIdView(ResponseMixin, View): renderers = (JSONRenderer,)
def send_shared_link(request): """ Handle ajax post request to send file shared link. """ if not request.method == 'POST': raise Http404 content_type = 'application/json; charset=utf-8' if not IS_EMAIL_CONFIGURED: data = json.dumps({ 'error': _(u'Sending shared link failed. Email service is not properly configured, please contact administrator.' ) }) return HttpResponse(data, status=500, content_type=content_type) from seahub.settings import SITE_NAME form = FileLinkShareForm(request.POST) if form.is_valid(): email = form.cleaned_data['email'] file_shared_link = form.cleaned_data['file_shared_link'] file_shared_name = form.cleaned_data['file_shared_name'] file_shared_type = form.cleaned_data['file_shared_type'] extra_msg = escape(form.cleaned_data['extra_msg']) to_email_list = string2list(email) for to_email in to_email_list: # Add email to contacts. mail_sended.send(sender=None, user=request.user.username, email=to_email) c = { 'email': request.user.username, 'to_email': to_email, 'file_shared_link': file_shared_link, 'file_shared_name': file_shared_name, } if extra_msg: c['extra_msg'] = extra_msg try: if file_shared_type == 'f': c['file_shared_type'] = "file" send_html_email( _(u'A file is shared to you on %s') % SITE_NAME, 'shared_link_email.html', c, None, [to_email]) else: c['file_shared_type'] = "directory" send_html_email( _(u'A directory is shared to you on %s') % SITE_NAME, 'shared_link_email.html', c, None, [to_email]) except Exception, e: logger.error(str(e)) data = json.dumps( {'error': _(u'Internal server error. Send failed.')}) return HttpResponse(data, status=500, content_type=content_type) data = json.dumps({"msg": _(u'Successfully sent.')}) return HttpResponse(data, status=200, content_type=content_type)
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: for user_group in user_groups: if user_group.group_name == share_to: 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://seafile.com/">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)
def send_shared_link(request): """ Handle ajax post request to send file shared link. """ if not request.method == 'POST': raise Http404 content_type = 'application/json; charset=utf-8' if not IS_EMAIL_CONFIGURED: data = json.dumps({ 'error': _(u'Sending shared link failed. Email service is not properly configured, please contact administrator.' ) }) return HttpResponse(data, status=500, content_type=content_type) from seahub.settings import SITE_NAME form = FileLinkShareForm(request.POST) if form.is_valid(): email = form.cleaned_data['email'] file_shared_link = form.cleaned_data['file_shared_link'] file_shared_name = form.cleaned_data['file_shared_name'] file_shared_type = form.cleaned_data['file_shared_type'] extra_msg = escape(form.cleaned_data['extra_msg']) to_email_list = string2list(email) send_success, send_failed = [], [] for to_email in to_email_list: if not is_valid_email(to_email): send_failed.append(to_email) continue # Add email to contacts. mail_sended.send(sender=None, user=request.user.username, email=to_email) c = { 'email': request.user.username, 'to_email': to_email, 'file_shared_link': file_shared_link, 'file_shared_name': file_shared_name, } if extra_msg: c['extra_msg'] = extra_msg if REPLACE_FROM_EMAIL: from_email = request.user.username else: from_email = None # use default from email if ADD_REPLY_TO_HEADER: reply_to = request.user.username else: reply_to = None try: if file_shared_type == 'f': c['file_shared_type'] = _(u"file") send_html_email(_(u'A file is shared to you on %s') % SITE_NAME, 'shared_link_email.html', c, from_email, [to_email], reply_to=reply_to) else: c['file_shared_type'] = _(u"directory") send_html_email(_(u'A directory is shared to you on %s') % SITE_NAME, 'shared_link_email.html', c, from_email, [to_email], reply_to=reply_to) send_success.append(to_email) except Exception: send_failed.append(to_email) if len(send_success) > 0: data = json.dumps({ "send_success": send_success, "send_failed": send_failed }) return HttpResponse(data, status=200, content_type=content_type) else: data = json.dumps({ "error": _("Internal server error, or please check the email(s) you entered" ) }) return HttpResponse(data, status=400, content_type=content_type) else: return HttpResponseBadRequest(json.dumps(form.errors), content_type=content_type)
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: for user_group in user_groups: if user_group.group_name == share_to: share_to_groups.append(user_group) else: share_to = share_to.lower() if is_valid_username(share_to): share_to_users.append(share_to) origin_repo_id, origin_path = get_origin_repo_info(repo.id) if origin_repo_id is not None: perm_repo_id = origin_repo_id perm_path = origin_path else: perm_repo_id = repo.id perm_path = '/' if share_to_all: share_to_public(request, repo, permission) send_perm_audit_msg('add-repo-perm', username, 'all', \ perm_repo_id, perm_path, permission) for group in share_to_groups: if share_to_group(request, repo, group, permission): send_perm_audit_msg('add-repo-perm', username, group.id, \ perm_repo_id, perm_path, permission) for email in share_to_users: # Add email to contacts. mail_sended.send(sender=None, user=request.user.username, email=email) if share_to_user(request, repo, email, permission): send_perm_audit_msg('add-repo-perm', username, email, \ perm_repo_id, perm_path, permission) return HttpResponseRedirect(next)
def ajax_private_share_dir(request): content_type = 'application/json; charset=utf-8' repo_id = request.POST.get('repo_id', '') path = request.POST.get('path', '') username = request.user.username result = {} repo = seafile_api.get_repo(repo_id) if not repo: result['error'] = _(u'Library does not exist.') return HttpResponse(json.dumps(result), status=400, content_type=content_type) if seafile_api.get_dir_id_by_path(repo_id, path) is None: result['error'] = _(u'Directory does not exist.') return HttpResponse(json.dumps(result), status=400, content_type=content_type) if path != '/': # if share a dir, check sub-repo first try: if is_org_context(request): org_id = request.user.org.org_id sub_repo = seaserv.seafserv_threaded_rpc.get_org_virtual_repo( org_id, repo_id, path, username) else: sub_repo = seafile_api.get_virtual_repo( repo_id, path, username) except SearpcError as e: result['error'] = e.msg return HttpResponse(json.dumps(result), status=500, content_type=content_type) if not sub_repo: name = os.path.basename(path) # create a sub-lib try: # use name as 'repo_name' & 'repo_desc' for sub_repo if is_org_context(request): org_id = request.user.org.org_id sub_repo_id = seaserv.seafserv_threaded_rpc.create_org_virtual_repo( org_id, repo_id, path, name, name, username) else: sub_repo_id = seafile_api.create_virtual_repo( repo_id, path, name, name, username) sub_repo = seafile_api.get_repo(sub_repo_id) except SearpcError as e: result['error'] = e.msg return HttpResponse(json.dumps(result), status=500, content_type=content_type) shared_repo_id = sub_repo.id shared_repo = sub_repo else: shared_repo_id = repo_id shared_repo = repo emails_string = request.POST.get('emails', '') groups_string = request.POST.get('groups', '') perm = request.POST.get('perm', '') emails = string2list(emails_string) groups = string2list(groups_string) # Test whether user is the repo owner. if not seafile_api.is_repo_owner(username, shared_repo_id) and \ not is_org_repo_owner(username, shared_repo_id): result['error'] = _( u'Only the owner of the library has permission to share it.') return HttpResponse(json.dumps(result), status=500, content_type=content_type) # Parsing input values. # no 'share_to_all' share_to_groups, share_to_users, shared_success, shared_failed = [], [], [], [] for email in emails: email = email.lower() if is_valid_username(email): share_to_users.append(email) else: shared_failed.append(email) for group_id in groups: share_to_groups.append(seaserv.get_group(group_id)) for email in share_to_users: # Add email to contacts. mail_sended.send(sender=None, user=request.user.username, email=email) if share_to_user(request, shared_repo, email, perm): shared_success.append(email) else: shared_failed.append(email) for group in share_to_groups: if share_to_group(request, shared_repo, group, perm): shared_success.append(group.group_name) else: shared_failed.append(group.group_name) if len(shared_success) > 0: return HttpResponse(json.dumps({ "shared_success": shared_success, "shared_failed": shared_failed }), content_type=content_type) else: # for case: only share to users and the emails are not valid data = json.dumps( {"error": _("Please check the email(s) you entered")}) return HttpResponse(data, status=400, content_type=content_type)
def send_shared_link(request): """ Handle ajax post request to send file shared link. """ if not request.method == 'POST': raise Http404 content_type = 'application/json; charset=utf-8' if not IS_EMAIL_CONFIGURED: data = json.dumps({'error':_(u'Sending shared link failed. Email service is not properly configured, please contact administrator.')}) return HttpResponse(data, status=500, content_type=content_type) from seahub.settings import SITE_NAME form = FileLinkShareForm(request.POST) if form.is_valid(): email = form.cleaned_data['email'] file_shared_link = form.cleaned_data['file_shared_link'] file_shared_name = form.cleaned_data['file_shared_name'] file_shared_type = form.cleaned_data['file_shared_type'] extra_msg = escape(form.cleaned_data['extra_msg']) to_email_list = string2list(email) send_success, send_failed = [], [] for to_email in to_email_list: if not is_valid_email(to_email): send_failed.append(to_email) continue # Add email to contacts. mail_sended.send(sender=None, user=request.user.username, email=to_email) c = { 'email': request.user.username, 'to_email': to_email, 'file_shared_link': file_shared_link, 'file_shared_name': file_shared_name, } if extra_msg: c['extra_msg'] = extra_msg if REPLACE_FROM_EMAIL: from_email = request.user.username else: from_email = None # use default from email if ADD_REPLY_TO_HEADER: reply_to = request.user.username else: reply_to = None try: if file_shared_type == 'f': c['file_shared_type'] = _(u"file") send_html_email(_(u'A file is shared to you on %s') % SITE_NAME, 'shared_link_email.html', c, from_email, [to_email], reply_to=reply_to ) else: c['file_shared_type'] = _(u"directory") send_html_email(_(u'A directory is shared to you on %s') % SITE_NAME, 'shared_link_email.html', c, from_email, [to_email], reply_to=reply_to) send_success.append(to_email) except Exception: send_failed.append(to_email) if len(send_success) > 0: data = json.dumps({"send_success": send_success, "send_failed": send_failed}) return HttpResponse(data, status=200, content_type=content_type) else: data = json.dumps({"error": _("Internal server error, or please check the email(s) you entered")}) return HttpResponse(data, status=400, content_type=content_type) else: return HttpResponseBadRequest(json.dumps(form.errors), content_type=content_type)
def group_manage(request, group_id): group_id = int(group_id) # Checked by URL Conf group = get_group(group_id) if not group: return HttpResponseRedirect(reverse("group_list", args=[])) user = request.user.username if request.method == "POST": """ Add group members. """ result = {} content_type = "application/json; charset=utf-8" member_name_str = request.POST.get("user_name", "") member_list = string2list(member_name_str) # Add users to contacts. for email in member_list: mail_sended.send(sender=None, user=user, email=email) mail_sended_list = [] if request.cloud_mode: if request.user.org: # Can only invite org users to group. org_id = request.user.org["org_id"] for email in member_list: if not ccnet_threaded_rpc.org_user_exists(org_id, email): err_msg = _(u"Failed to add, %s is not in current organization.") % email result["error"] = err_msg return HttpResponse(json.dumps(result), status=400, content_type=content_type) else: try: ccnet_threaded_rpc.group_add_member(group_id, user, email) except SearpcError, e: result["error"] = _(e.msg) return HttpResponse(json.dumps(result), status=500, content_type=content_type) else: # Can invite unregistered user to group. for email in member_list: if not is_registered_user(email): use_https = request.is_secure() domain = RequestSite(request).domain t = loader.get_template("group/add_member_email.html") c = { "email": user, "to_email": email, "group": group, "domain": domain, "protocol": use_https and "https" or "http", "site_name": SITE_NAME, } try: send_mail( _(u"Your friend added you to a group in SeaCloud."), t.render(Context(c)), None, [email], fail_silently=False, ) mail_sended_list.append(email) except: data = json.dumps({"error": _(u"Failed to send mail.")}) return HttpResponse(data, status=500, content_type=content_type) # Add user to group, unregistered user will see the group # when he logs in. try: ccnet_threaded_rpc.group_add_member(group_id, user, email) except SearpcError, e: result["error"] = _(e.msg) return HttpResponse(json.dumps(result), status=500, content_type=content_type)
def share_repo(request): """ Handle repo share request """ if request.method != 'POST': raise Http404 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'] from_email = request.user.username repo = get_repo(repo_id) if not repo: raise Http404 is_encrypted = True if repo.encrypted else False # Test whether user is the repo owner. if not validate_owner(request, repo_id): return render_permission_error(request, _(u'Only the owner of the library has permission to share it.')) to_email_list = string2list(email_or_group) for to_email in to_email_list: if to_email == 'all': ''' Share to public ''' # ignore 'all' if we're running in cloud mode if not CLOUD_MODE: try: seafserv_threaded_rpc.set_inner_pub_repo(repo_id, permission) except: msg = _(u'Failed to share to all members') message.add_message(request, message.ERROR, msg) continue msg = _(u'Shared to all members successfully, go check it at <a href="%s">Share</a>.') % \ (reverse('share_admin')) messages.add_message(request, messages.INFO, msg) elif to_email.find('@') == -1: ''' Share repo to group ''' # TODO: if we know group id, then we can simplly call group_share_repo group_name = to_email # get all personal groups groups = get_personal_groups(-1, -1) find = False for group in groups: # for every group that user joined, if group name matchs, # then has find the group if group.props.group_name == group_name: from seahub.group.views import group_share_repo group_share_repo(request, repo_id, int(group.props.id), from_email, permission) find = True msg = _(u'Shared to %(group)s successfully,go check it at <a href="%(share)s">Share</a>.') % \ {'group':group_name, 'share':reverse('share_admin')} messages.add_message(request, messages.INFO, msg) break if not find: msg = _(u'Failed to share to %s,as it does not exists.') % group_name messages.add_message(request, messages.ERROR, msg) else: ''' Share repo to user ''' # Add email to contacts. mail_sended.send(sender=None, user=request.user.username, email=to_email) if not is_registered_user(to_email): # Generate shared link and send mail if user has not registered. # kwargs = {'repo_id': repo_id, # 'repo_owner': from_email, # 'anon_email': to_email, # 'is_encrypted': is_encrypted, # } # anonymous_share(request, **kwargs) msg = _(u'Failed to share to %s, as the email is not registered.') % to_email messages.add_message(request, messages.ERROR, msg) continue else: # Record share info to db. try: seafserv_threaded_rpc.add_share(repo_id, from_email, to_email, permission) except SearpcError, e: msg = _(u'Failed to share to %s .') % to_email messages.add_message(request, messages.ERROR, msg) continue msg = _(u'Shared to %(email)s successfully,go check it at <a href="%(share)s">Share</a>.') % \ {'email':to_email, 'share':reverse('share_admin')} messages.add_message(request, messages.INFO, msg)
def ajax_private_share_dir(request): content_type = 'application/json; charset=utf-8' repo_id = request.POST.get('repo_id', '') path = request.POST.get('path', '') username = request.user.username result = {} repo = seafile_api.get_repo(repo_id) if not repo: result['error'] = _(u'Library does not exist.') return HttpResponse(json.dumps(result), status=400, content_type=content_type) if seafile_api.get_dir_id_by_path(repo_id, path) is None: result['error'] = _(u'Directory does not exist.') return HttpResponse(json.dumps(result), status=400, content_type=content_type) if path != '/': # if share a dir, check sub-repo first try: if is_org_context(request): org_id = request.user.org.org_id sub_repo = seaserv.seafserv_threaded_rpc.get_org_virtual_repo( org_id, repo_id, path, username) else: sub_repo = seafile_api.get_virtual_repo(repo_id, path, username) except SearpcError as e: result['error'] = e.msg return HttpResponse(json.dumps(result), status=500, content_type=content_type) if not sub_repo: name = os.path.basename(path) # create a sub-lib try: # use name as 'repo_name' & 'repo_desc' for sub_repo if is_org_context(request): org_id = request.user.org.org_id sub_repo_id = seaserv.seafserv_threaded_rpc.create_org_virtual_repo( org_id, repo_id, path, name, name, username) else: sub_repo_id = seafile_api.create_virtual_repo(repo_id, path, name, name, username) sub_repo = seafile_api.get_repo(sub_repo_id) except SearpcError as e: result['error'] = e.msg return HttpResponse(json.dumps(result), status=500, content_type=content_type) shared_repo_id = sub_repo.id shared_repo = sub_repo else: shared_repo_id = repo_id shared_repo = repo emails_string = request.POST.get('emails', '') groups_string = request.POST.get('groups', '') perm = request.POST.get('perm', '') emails = string2list(emails_string) groups = string2list(groups_string) # Test whether user is the repo owner. if not seafile_api.is_repo_owner(username, shared_repo_id) and \ not is_org_repo_owner(username, shared_repo_id): result['error'] = _(u'Only the owner of the library has permission to share it.') return HttpResponse(json.dumps(result), status=500, content_type=content_type) # Parsing input values. # no 'share_to_all' share_to_groups, share_to_users, shared_success, shared_failed = [], [], [], [] for email in emails: email = email.lower() if is_valid_username(email): share_to_users.append(email) else: shared_failed.append(email) for group_id in groups: share_to_groups.append(seaserv.get_group(group_id)) for email in share_to_users: # Add email to contacts. mail_sended.send(sender=None, user=request.user.username, email=email) if share_to_user(request, shared_repo, email, perm): shared_success.append(email) else: shared_failed.append(email) for group in share_to_groups: if share_to_group(request, shared_repo, group, perm): shared_success.append(group.group_name) else: shared_failed.append(group.group_name) if len(shared_success) > 0: return HttpResponse(json.dumps({ "shared_success": shared_success, "shared_failed": shared_failed }), content_type=content_type) else: # for case: only share to users and the emails are not valid data = json.dumps({"error": _("Please check the email(s) you entered")}) return HttpResponse(data, status=400, content_type=content_type)
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. """ 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'] from_email = request.user.username repo = get_repo(repo_id) if not repo: raise Http404 # Test whether user is the repo owner. if not validate_owner(request, repo_id): msg = _(u'Only the owner of the library has permission to share it.') messages.error(request, msg) return HttpResponseRedirect(reverse('myhome')) # Parsing input values. share_to_list = string2list(email_or_group) share_to_all, share_to_group_names, share_to_users = False, [], [] for share_to in share_to_list: if share_to == 'all': share_to_all = True elif share_to.find('@') == -1: share_to_group_names.append(share_to) else: share_to_users.append(share_to) share_to_groups = [] # get all personal groups for group in seaserv.get_personal_groups_by_user(from_email): # for every group that user joined, if group name matchs, # then has find the group if group.group_name in share_to_group_names: share_to_groups.append(group) if share_to_all and not CLOUD_MODE: share_to_public(request, repo, permission) if not check_user_share_quota( from_email, repo, users=share_to_users, groups=share_to_groups): messages.error( request, _('Failed to share "%s", no enough quota. <a href="http://seafile.com/">Upgrade account.</a>' ) % repo.name) return HttpResponseRedirect(reverse('myhome')) for group in share_to_groups: share_to_group(request, repo, from_email, 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, from_email, email, permission) return HttpResponseRedirect(reverse('myhome'))
def group_manage(request, group_id): group_id = int(group_id) # Checked by URL Conf group = get_group(group_id) if not group: return HttpResponseRedirect(reverse('group_list', args=[])) user = request.user.username if request.method == 'POST': """ Add group members. """ result = {} content_type = 'application/json; charset=utf-8' member_name_str = request.POST.get('user_name', '') member_list = string2list(member_name_str) # Add users to contacts. for email in member_list: mail_sended.send(sender=None, user=user, email=email) mail_sended_list = [] if request.cloud_mode: if request.user.org: # Can only invite org users to group. org_id = request.user.org['org_id'] for email in member_list: if not ccnet_threaded_rpc.org_user_exists(org_id, email): err_msg = _(u'Failed to add, %s is not in current organization.') % email result['error'] = err_msg return HttpResponse(json.dumps(result), status=400, content_type=content_type) else: try: ccnet_threaded_rpc.group_add_member(group_id, user, email) except SearpcError, e: result['error'] = _(e.msg) return HttpResponse(json.dumps(result), status=500, content_type=content_type) else: # Can invite unregistered user to group. for email in member_list: if not is_registered_user(email): use_https = request.is_secure() domain = RequestSite(request).domain t = loader.get_template('group/add_member_email.html') c = { 'email': user, 'to_email': email, 'group': group, 'domain': domain, 'protocol': use_https and 'https' or 'http', 'site_name': SITE_NAME, } try: send_mail(_(u'Your friend added you to a group at Seafile.'), t.render(Context(c)), None, [email], fail_silently=False) mail_sended_list.append(email) except: data = json.dumps({'error': _(u'Failed to send mail.')}) return HttpResponse(data, status=500, content_type=content_type) # Add user to group, unregistered user will see the group # when he logs in. try: ccnet_threaded_rpc.group_add_member(group_id, user, email) except SearpcError, e: result['error'] = _(e.msg) return HttpResponse(json.dumps(result), status=500, content_type=content_type)
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 = settings.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'] from_email = request.user.username repo = seafile_api.get_repo(repo_id) if not repo: raise Http404 # Test whether user is the repo owner. if not validate_owner(request, 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_list = string2list(email_or_group) share_to_all, share_to_group_names, share_to_users = False, [], [] for share_to in share_to_list: if share_to == 'all': share_to_all = True elif share_to.find('@') == -1: share_to_group_names.append(share_to) else: share_to_users.append(share_to) share_to_groups = [] # get all personal groups for group in seaserv.get_personal_groups_by_user(from_email): # for every group that user joined, if group name matchs, # then has find the group if group.group_name in share_to_group_names: share_to_groups.append(group) if share_to_all and not CLOUD_MODE: share_to_public(request, repo, permission) if not check_user_share_quota(from_email, repo, users=share_to_users, groups=share_to_groups): messages.error(request, _('Failed to share "%s", no enough quota. <a href="http://seafile.com/">Upgrade account.</a>') % repo.name) return HttpResponseRedirect(next) for group in share_to_groups: share_to_group(request, repo, from_email, 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, from_email, email, permission) return HttpResponseRedirect(next)
def send_shared_upload_link(request): """ Handle ajax post request to send shared upload link. """ if not request.method == 'POST': raise Http404 content_type = 'application/json; charset=utf-8' if not IS_EMAIL_CONFIGURED: data = json.dumps({ 'error': _('Failed to send email, email service is not properly configured, please contact administrator.' ) }) return HttpResponse(data, status=500, content_type=content_type) form = UploadLinkShareForm(request.POST) if form.is_valid(): email = form.cleaned_data['email'] shared_upload_link = form.cleaned_data['shared_upload_link'] extra_msg = escape(form.cleaned_data['extra_msg']) to_email_list = string2list(email) send_success, send_failed = [], [] # use contact_email, if present username = Profile.objects.get_contact_email_by_user( request.user.username) for to_email in to_email_list: if not is_valid_email(to_email): send_failed.append(to_email) continue # Add email to contacts. mail_sended.send(sender=None, user=request.user.username, email=to_email) c = { 'email': request.user.username, 'to_email': to_email, 'shared_upload_link': shared_upload_link, } if extra_msg: c['extra_msg'] = extra_msg if REPLACE_FROM_EMAIL: from_email = username else: from_email = None # use default from email if ADD_REPLY_TO_HEADER: reply_to = username else: reply_to = None try: send_html_email(_('An upload link is shared to you on %s') % get_site_name(), 'shared_upload_link_email.html', c, from_email, [to_email], reply_to=reply_to) send_success.append(to_email) except Exception: send_failed.append(to_email) if len(send_success) > 0: data = json.dumps({ "send_success": send_success, "send_failed": send_failed }) return HttpResponse(data, status=200, content_type=content_type) else: data = json.dumps({ "error": _("Internal server error, or please check the email(s) you entered" ) }) return HttpResponse(data, status=400, content_type=content_type) else: return HttpResponseBadRequest(json.dumps(form.errors), content_type=content_type)
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: for user_group in user_groups: if user_group.group_name == share_to: 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://seafile.com/">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)