def gen_private_file_share(request, repo_id): emails = request.POST.getlist('emails', '') s_type = request.POST.get('s_type', '') path = request.POST.get('path', '') perm = request.POST.get('perm', 'r') file_or_dir = os.path.basename(path.rstrip('/')) username = request.user.username for email in [e.strip() for e in emails if e.strip()]: if not is_valid_username(email): continue if not is_registered_user(email): messages.error(request, _('Failed to share to "%s", user not found.') % email) continue if s_type == 'f': pfds = PrivateFileDirShare.objects.add_read_only_priv_file_share( username, email, repo_id, path) elif s_type == 'd': pfds = PrivateFileDirShare.objects.add_private_dir_share( username, email, repo_id, path, perm) else: continue # send a signal when sharing file successful share_file_to_user_successful.send(sender=None, priv_share_obj=pfds) messages.success(request, _('Successfully shared %s.') % file_or_dir) next = request.META.get('HTTP_REFERER', None) if not next: next = SITE_ROOT return HttpResponseRedirect(next)
def group_wiki_pages(request, group): """ List wiki pages in group. """ username = request.user.username try: repo = get_group_wiki_repo(group, username) pages = get_wiki_pages(repo) except SearpcError: return render_error(request, _('Internal Server Error')) except WikiDoesNotExist: return render_error(request, _('Wiki does not exists.')) if is_registered_user(username): repo_perm = seafile_api.check_permission_by_path( repo.id, '/', username) else: # when anonymous user visit public group wiki, set permission as 'r' repo_perm = 'r' mods_available = get_available_mods_by_group(group.id) mods_enabled = get_enabled_mods_by_group(group.id) return render_to_response("group/group_wiki_pages.html", { "group": group, "pages": pages, "is_staff": group.is_staff, "repo_id": repo.id, "search_repo_id": repo.id, "search_wiki": True, "repo_perm": repo_perm, "mods_enabled": mods_enabled, "mods_available": mods_available, }, context_instance=RequestContext(request))
def group_wiki_pages(request, group): """ List wiki pages in group. """ username = request.user.username try: repo = get_group_wiki_repo(group, username) pages = get_wiki_pages(repo) except SearpcError: return render_error(request, _('Internal Server Error')) except WikiDoesNotExist: return render_error(request, _('Wiki does not exists.')) if is_registered_user(username): repo_perm = seafile_api.check_permission_by_path(repo.id, '/', username) else: # when anonymous user visit public group wiki, set permission as 'r' repo_perm = 'r' mods_available = get_available_mods_by_group(group.id) mods_enabled = get_enabled_mods_by_group(group.id) return render_to_response("group/group_wiki_pages.html", { "group": group, "pages": pages, "is_staff": group.is_staff, "repo_id": repo.id, "search_repo_id": repo.id, "search_wiki": True, "repo_perm": repo_perm, "mods_enabled": mods_enabled, "mods_available": mods_available, }, context_instance=RequestContext(request))
def delete(self, request, format=None): platform = request.data.get('platform', '') device_id = request.data.get('device_id', '') remote_wipe = request.data.get('wipe_device', '') user = request.data.get('user', '') if not platform: error_msg = 'platform invalid.' return api_error(status.HTTP_400_BAD_REQUEST, error_msg) if not device_id: error_msg = 'device_id invalid.' return api_error(status.HTTP_400_BAD_REQUEST, error_msg) if not user or not is_registered_user(user): error_msg = 'user invalid.' return api_error(status.HTTP_400_BAD_REQUEST, error_msg) remote_wipe = True if remote_wipe == 'true' else False try: do_unlink_device(user, platform, device_id, remote_wipe=remote_wipe) except SearpcError as e: logger.error(e) error_msg = 'Internal Server Error' return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, error_msg) return Response({'success': True})
def share_to_user(request, repo, to_user, permission): """Share repo to a user with given permission. """ repo_id = repo.id from_user = request.user.username if from_user == to_user: return False # permission check if is_org_context(request): org_id = request.user.org.org_id if not seaserv.ccnet_threaded_rpc.org_user_exists(org_id, to_user): return False else: if not is_registered_user(to_user): return False try: if is_org_context(request): org_id = request.user.org.org_id org_share_repo(org_id, repo_id, from_user, to_user, permission) else: seafile_api.share_repo(repo_id, from_user, to_user, permission) except SearpcError as e: return False logger.error(e) else: # send a signal when sharing repo successful share_repo_to_user_successful.send(sender=None, from_user=from_user, to_user=to_user, repo=repo) return True
def delete(self, request, format=None): platform = request.data.get('platform', '') device_id = request.data.get('device_id', '') user = request.data.get('user', '') if not platform: error_msg = 'platform invalid.' return api_error(status.HTTP_400_BAD_REQUEST, error_msg) if not device_id: error_msg = 'device_id invalid.' return api_error(status.HTTP_400_BAD_REQUEST, error_msg) if not user or not is_registered_user(user): error_msg = 'user invalid.' return api_error(status.HTTP_400_BAD_REQUEST, error_msg) try: do_unlink_device(user, platform, device_id) except SearpcError as e: logger.error(e) error_msg = 'Internal Server Error' return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, error_msg) return Response({'success': True})
def share_to_user(request, repo, to_user, permission): """Share repo to a user with given permission. """ repo_id = repo.id from_user = request.user.username if from_user == to_user: msg = _(u'You can not share libray to yourself.') messages.error(request, msg) return if is_registered_user(to_user): try: seafile_api.share_repo(repo_id, from_user, to_user, permission) except Exception, e: logger.error(e) msg = _( u'Failed to share to %s, please try again later.') % to_user messages.error(request, msg) else: # send a signal when sharing repo successful share_repo_to_user_successful.send(sender=None, from_user=from_user, to_user=to_user, repo=repo) msg = _(u'Shared to %(email)s successfully,go check it at <a href="%(share)s">Shares</a>.') % \ {'email':to_user, 'share':reverse('share_admin')} messages.success(request, msg)
def gen_private_file_share(request, repo_id): emails = request.POST.getlist('emails', '') s_type = request.POST.get('s_type', '') path = request.POST.get('path', '') perm = request.POST.get('perm', 'r') file_or_dir = os.path.basename(path.rstrip('/')) username = request.user.username for email in [e.strip() for e in emails if e.strip()]: if not is_valid_username(email): continue if not is_registered_user(email): messages.error( request, _('Failed to share to "%s", user not found.') % email) continue if s_type == 'f': pfds = PrivateFileDirShare.objects.add_read_only_priv_file_share( username, email, repo_id, path) elif s_type == 'd': pfds = PrivateFileDirShare.objects.add_private_dir_share( username, email, repo_id, path, perm) else: continue # send a signal when sharing file successful share_file_to_user_successful.send(sender=None, priv_share_obj=pfds) messages.success(request, _('Successfully shared %s.') % file_or_dir) next = request.META.get('HTTP_REFERER', None) if not next: next = SITE_ROOT return HttpResponseRedirect(next)
def get_registered_contacts_by_user(self, user_email): """Get a user's registered contacts. """ from seahub.views import is_registered_user return [ c for c in super(ContactManager, self).filter( user_email=user_email) if is_registered_user(c.contact_email) ]
def share_to_user(request, repo, from_user, to_user, permission): """Share repo to a user with given permission. """ repo_id = repo.id if from_user == to_user: msg = _(u'You can not share libray to yourself.') messages.error(request, msg) return if is_registered_user(to_user): try: seafile_api.share_repo(repo_id, from_user, to_user, permission) except Exception, e: logger.error(e) msg = _(u'Failed to share to %s, please try again later.') % to_user messages.error(request, msg) else: # send a signal when sharing repo successful share_repo_to_user_successful.send(sender=None, from_user=from_user, to_user=to_user, repo=repo) msg = _(u'Shared to %(email)s successfully,go check it at <a href="%(share)s">Shares</a>.') % \ {'email':to_user, 'share':reverse('share_admin')} messages.success(request, msg)
def message_send(request): """Handle POST request to send message to user(s). """ username = request.user.username next = request.META.get('HTTP_REFERER', None) if next is None: next = SITE_ROOT mass_msg = request.POST.get('mass_msg') mass_emails = request.POST.getlist('mass_email') # e.g: [u'[email protected], u'*****@*****.**'] if not mass_msg: messages.error(request, _(u'message is required')) return HttpResponseRedirect(next) if not mass_emails: messages.error(request, _(u'contact is required')) return HttpResponseRedirect(next) # attachment selected = request.POST.getlist('selected') # selected files & dirs: [u'<repo_id><path>', ...] attached_items = [] if len(selected) > 0: for item in selected: att = {} att['repo_id'] = item[0:36] att['path'] = item[36:] attached_items.append(att) email_sended = [] for to_email in mass_emails: to_email = to_email.strip() if not to_email: continue if to_email == username: messages.error(request, _(u'You can not send message to yourself.')) continue if not is_registered_user(to_email): messages.error(request, _(u'Failed to send message to %s, user not found.') % to_email) continue usermsg = UserMessage.objects.add_unread_message(username, to_email, mass_msg) if len(attached_items) > 0: for att_item in attached_items: repo_id = att_item['repo_id'] path = att_item['path'] pfds = PrivateFileDirShare.objects.add_read_only_priv_file_share( username, to_email, repo_id, path) UserMsgAttachment.objects.add_user_msg_attachment(usermsg, pfds) email_sended.append(to_email) if email_sended: messages.success(request, _(u'Message sent successfully.')) return HttpResponseRedirect(next)
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)
def share_to_user(request, repo, to_user, permission): """Share repo to a user with given permission. """ repo_id = repo.id from_user = request.user.username if from_user == to_user: msg = _(u'You can not share libray to yourself.') messages.error(request, msg) return # permission check if is_org_context(request): org_id = request.user.org.org_id if not seaserv.ccnet_threaded_rpc.org_user_exists(org_id, to_user): msg = _(u'Failed to share to %s, user is not found.') % to_user messages.error(request, msg) return else: if not is_registered_user(to_user): msg = _(u'Failed to share to %s, as the email is not registered.' ) % to_user messages.error(request, msg) return try: if is_org_context(request): org_id = request.user.org.org_id org_share_repo(org_id, repo_id, from_user, to_user, permission) else: seafile_api.share_repo(repo_id, from_user, to_user, permission) except SearpcError as e: logger.error(e) msg = _(u'Failed to share to %s, please try again later.') % to_user messages.error(request, msg) else: # send a signal when sharing repo successful share_repo_to_user_successful.send(sender=None, from_user=from_user, to_user=to_user, repo=repo) msg = _(u'Shared to %(email)s successfully, go check it at <a href="%(share)s">Shares</a>.') % \ {'email': to_user, 'share': reverse('share_admin')} messages.success(request, msg)
def share_to_user(request, repo, to_user, permission): """Share repo to a user with given permission. """ repo_id = repo.id from_user = request.user.username if from_user == to_user: msg = _(u'You can not share libray to yourself.') messages.error(request, msg) return # permission check if is_org_context(request): org_id = request.user.org.org_id if not seaserv.ccnet_threaded_rpc.org_user_exists(org_id, to_user): msg = _(u'Failed to share to %s, user is not found.') % to_user messages.error(request, msg) return else: if not is_registered_user(to_user): msg = _(u'Failed to share to %s, as the email is not registered.') % to_user messages.error(request, msg) return try: if is_org_context(request): org_id = request.user.org.org_id org_share_repo(org_id, repo_id, from_user, to_user, permission) else: seafile_api.share_repo(repo_id, from_user, to_user, permission) except SearpcError as e: logger.error(e) msg = _(u'Failed to share to %s, please try again later.') % to_user messages.error(request, msg) else: # send a signal when sharing repo successful share_repo_to_user_successful.send(sender=None, from_user=from_user, to_user=to_user, repo=repo) msg = _(u'Shared to %(email)s successfully, go check it at <a href="%(share)s">Shares</a>.') % \ {'email': to_user, 'share': reverse('share_admin')} messages.success(request, msg)
def message_send(request): """Handle POST request to send message to user(s). """ username = request.user.username next = request.META.get('HTTP_REFERER', None) if next is None: next = SITE_ROOT mass_msg = request.POST.get('mass_msg') mass_emails = request.POST.getlist('mass_email') # e.g: [u'[email protected], u'*****@*****.**'] if not mass_msg: messages.error(request, _(u'message is required')) return HttpResponseRedirect(next) if not mass_emails: messages.error(request, _(u'contact is required')) return HttpResponseRedirect(next) email_sended = [] for to_email in mass_emails: to_email = to_email.strip() if not to_email: continue if to_email == username: messages.error(request, _(u'You can not send message to yourself.')) continue if not is_registered_user(to_email): messages.error(request, _(u'Failed to send message to %s, user not found.') % to_email) continue UserMessage.objects.add_unread_message(username, to_email, mass_msg) email_sended.append(to_email) if email_sended: messages.success(request, _(u'Message sent successfully.')) return HttpResponseRedirect(next)
def put(self, request, repo_id, format=None): """ Share a repo to users/groups/public. """ share_type = request.GET.get('share_type') user = request.GET.get('user') group_id = request.GET.get('group_id') permission = request.GET.get('permission') if permission !='rw' and permission != "r": return api_error(status.HTTP_400_BAD_REQUEST, 'Permission need to be rw or r.') if share_type == 'personal': if not is_registered_user(user) : return api_error(status.HTTP_400_BAD_REQUEST, 'User does not exist') try : from_email = seafile_api.get_repo_owner(repo_id) seafserv_threaded_rpc.add_share(repo_id, from_email, user, permission) except SearpcError, e: return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, "Searpc Error: " + e.msg)
def put(self, request, repo_id, format=None): """ Share a repo to users/groups/public. """ share_type = request.GET.get('share_type') user = request.GET.get('user') group_id = request.GET.get('group_id') permission = request.GET.get('permission') if permission != 'rw' and permission != "r": return api_error(status.HTTP_400_BAD_REQUEST, 'Permission need to be rw or r.') if share_type == 'personal': if not is_registered_user(user): return api_error(status.HTTP_400_BAD_REQUEST, 'User does not exist') try: from_email = seafile_api.get_repo_owner(repo_id) seafserv_threaded_rpc.add_share(repo_id, from_email, user, permission) except SearpcError, e: return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, "Searpc Error: " + e.msg)
def group_wiki(request, group, page_name="home"): username = request.user.username # get available modules(wiki, etc) mods_available = get_available_mods_by_group(group.id) mods_enabled = get_enabled_mods_by_group(group.id) wiki_exists = True try: content, repo, dirent = get_group_wiki_page(username, group, page_name) except WikiDoesNotExist: wiki_exists = False group_repos = get_group_repos(group.id, username) group_repos = [r for r in group_repos if not r.encrypted] return render_to_response("group/group_wiki.html", { "group": group, "is_staff": group.is_staff, "wiki_exists": wiki_exists, "mods_enabled": mods_enabled, "mods_available": mods_available, "group_repos": group_repos, }, context_instance=RequestContext(request)) except WikiPageMissing: '''create that page for user if he/she is a group member''' if not is_group_user(group.id, username): raise Http404 repo = get_group_wiki_repo(group, username) # No need to check whether repo is none, since repo is already created filename = page_name_to_file_name(clean_page_name(page_name)) if not post_empty_file(repo.id, "/", filename, username): return render_error( request, _("Failed to create wiki page. Please retry later.")) return HttpResponseRedirect( reverse('group_wiki', args=[group.id, page_name])) else: # fetch file modified time and modifier path = '/' + dirent.obj_name try: dirent = seafile_api.get_dirent_by_path(repo.id, path) if dirent: latest_contributor, last_modified = dirent.modifier, dirent.mtime else: latest_contributor, last_modified = None, 0 except SearpcError as e: logger.error(e) latest_contributor, last_modified = None, 0 if is_registered_user(username): repo_perm = seafile_api.check_permission_by_path( repo.id, '/', username) else: # when anonymous user visit public group wiki, set permission as 'r' repo_perm = 'r' wiki_index_exists = True index_pagename = 'index' index_content = None try: index_content, index_repo, index_dirent = get_group_wiki_page( username, group, index_pagename) except (WikiDoesNotExist, WikiPageMissing) as e: wiki_index_exists = False return render_to_response( "group/group_wiki.html", { "group": group, "is_staff": group.is_staff, "wiki_exists": wiki_exists, "content": content, "page": os.path.splitext(dirent.obj_name)[0], "last_modified": last_modified, "latest_contributor": latest_contributor or _("Unknown"), "path": path, "repo_id": repo.id, "search_repo_id": repo.id, "search_wiki": True, "mods_enabled": mods_enabled, "mods_available": mods_available, "repo_perm": repo_perm, "wiki_index_exists": wiki_index_exists, "index_content": index_content, }, context_instance=RequestContext(request))
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) else: # Can only invite registered user to group if not in cloud mode. for email in member_list: if not is_registered_user(email): err_msg = _(u'Failed to add, %s is not registerd.') result['error'] = err_msg % email return HttpResponse(json.dumps(result), status=400, content_type=content_type) # Add user to group. 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) if mail_sended_list: msg = ungettext( 'Successfully added. An email has been sent.',
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 message_send(request): """Handle POST request to send message to user(s). """ if not request.is_ajax() or request.method != 'POST': raise Http404 content_type = 'application/json; charset=utf-8' result = {} username = request.user.username fr = request.GET.get('from') if not fr: result['error'] = [_(u'Argument missing')] return HttpResponse(json.dumps(result), content_type=content_type) mass_msg = request.POST.get('mass_msg') mass_emails = request.POST.getlist( 'mass_email') # e.g: [u'[email protected], u'*****@*****.**'] if not mass_msg: result['error'] = [_(u'message is required')] return HttpResponse(json.dumps(result), content_type=content_type) if not mass_emails: result['error'] = [_(u'contact is required')] return HttpResponse(json.dumps(result), content_type=content_type) # attachment selected = request.POST.getlist( 'selected') # selected files & dirs: [u'<repo_id><path>', ...] attached_items = [] if len(selected) > 0: for item in selected: if item[-1] == '/': # dir is not allowed, for now continue att = {} att['repo_id'] = item[0:36] att['path'] = item[36:] attached_items.append(att) email_sended = [] errors = [] msgs = [] for to_email in mass_emails: to_email = to_email.strip() if not to_email or not is_valid_username(to_email): continue if to_email == username: errors.append(_(u'You can not send message to yourself.')) continue if not is_registered_user(to_email): errors.append( _(u'Failed to send message to %s, user not found.') % to_email) continue usermsg = UserMessage.objects.add_unread_message( username, to_email, mass_msg) msgs.append(usermsg) if len(attached_items) > 0: for att_item in attached_items: repo_id = att_item['repo_id'] path = att_item['path'] pfds = PrivateFileDirShare.objects.add_read_only_priv_file_share( username, to_email, repo_id, path) UserMsgAttachment.objects.add_user_msg_attachment( usermsg, pfds) email_sended.append(to_email) html = '' if email_sended: ctx = {} if fr == 'all': # from 'all_msg_list' page ctx['msgs'] = msg_info_list(msgs, username) html = render_to_string('message/all_msg.html', ctx) else: ctx['msg'] = msgs[0] html = render_to_string('message/user_msg.html', ctx) return HttpResponse(json.dumps({ "html": html, "error": errors }), content_type=content_type) else: return HttpResponse(json.dumps({ "html": html, "error": errors }), status=400, content_type=content_type)
def message_send(request): """Handle POST request to send message to user(s). """ if not request.is_ajax() or request.method != 'POST': raise Http404 content_type = 'application/json; charset=utf-8' result = {} username = request.user.username fr = request.GET.get('from') if not fr: result['error'] = [_(u'Argument missing')] return HttpResponse(json.dumps(result), content_type=content_type) mass_msg = request.POST.get('mass_msg') mass_emails = request.POST.getlist('mass_email') # e.g: [u'[email protected], u'*****@*****.**'] if not mass_msg: result['error'] = [_(u'message is required')] return HttpResponse(json.dumps(result), content_type=content_type) if not mass_emails: result['error'] = [_(u'contact is required')] return HttpResponse(json.dumps(result), content_type=content_type) # attachment selected = request.POST.getlist('selected') # selected files & dirs: [u'<repo_id><path>', ...] attached_items = [] if len(selected) > 0: for item in selected: if item[-1] == '/': # dir is not allowed, for now continue att = {} att['repo_id'] = item[0:36] att['path'] = item[36:] attached_items.append(att) email_sended = [] errors = [] msgs = [] for to_email in mass_emails: to_email = to_email.strip() if not to_email or not is_valid_username(to_email): continue if to_email == username: errors.append(_(u'You can not send message to yourself.')) continue if not is_registered_user(to_email): errors.append(_(u'Failed to send message to %s, user not found.') % to_email) continue usermsg = UserMessage.objects.add_unread_message(username, to_email, mass_msg) usermsg.attachments = [] if len(attached_items) > 0: for att_item in attached_items: repo_id = att_item['repo_id'] path = att_item['path'] pfds = PrivateFileDirShare.objects.add_read_only_priv_file_share( username, to_email, repo_id, path) att = UserMsgAttachment.objects.add_user_msg_attachment(usermsg, pfds) att.repo_id = repo_id att.path = path att.name = os.path.basename(path.rstrip('/')) att.token = pfds.token usermsg.attachments.append(att) msgs.append(usermsg) email_sended.append(to_email) html = '' if email_sended: ctx = {} if fr == 'all': # from 'all_msg_list' page ctx['msgs'] = msg_info_list(msgs, username) html = render_to_string('message/all_msg.html', ctx) else: ctx['msg'] = msgs[0] html = render_to_string('message/user_msg.html', ctx, context_instance=RequestContext(request)) return HttpResponse(json.dumps({"html": html, "error": errors}), content_type=content_type) else: return HttpResponse(json.dumps({"html": html, "error": errors}), status=400, 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 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 group_wiki(request, group, page_name="home"): username = request.user.username # get available modules(wiki, etc) mods_available = get_available_mods_by_group(group.id) mods_enabled = get_enabled_mods_by_group(group.id) wiki_exists = True try: content, repo, dirent = get_group_wiki_page(username, group, page_name) except WikiDoesNotExist: wiki_exists = False group_repos = get_group_repos(group.id, username) group_repos = [r for r in group_repos if not r.encrypted] return render_to_response("group/group_wiki.html", { "group" : group, "is_staff": group.is_staff, "wiki_exists": wiki_exists, "mods_enabled": mods_enabled, "mods_available": mods_available, "group_repos": group_repos, }, context_instance=RequestContext(request)) except WikiPageMissing: '''create that page for user if he/she is a group member''' if not is_group_user(group.id, username): raise Http404 repo = get_group_wiki_repo(group, username) # No need to check whether repo is none, since repo is already created filename = page_name_to_file_name(clean_page_name(page_name)) if not post_empty_file(repo.id, "/", filename, username): return render_error(request, _("Failed to create wiki page. Please retry later.")) return HttpResponseRedirect(reverse('group_wiki', args=[group.id, page_name])) else: # fetch file modified time and modifier path = '/' + dirent.obj_name try: dirent = seafile_api.get_dirent_by_path(repo.id, path) if dirent: latest_contributor, last_modified = dirent.modifier, dirent.mtime else: latest_contributor, last_modified = None, 0 except SearpcError as e: logger.error(e) latest_contributor, last_modified = None, 0 if is_registered_user(username): repo_perm = seafile_api.check_permission_by_path(repo.id, '/', username) else: # when anonymous user visit public group wiki, set permission as 'r' repo_perm = 'r' wiki_index_exists = True index_pagename = 'index' index_content = None try: index_content, index_repo, index_dirent = get_group_wiki_page(username, group, index_pagename) except (WikiDoesNotExist, WikiPageMissing) as e: wiki_index_exists = False return render_to_response("group/group_wiki.html", { "group" : group, "is_staff": group.is_staff, "wiki_exists": wiki_exists, "content": content, "page": os.path.splitext(dirent.obj_name)[0], "last_modified": last_modified, "latest_contributor": latest_contributor or _("Unknown"), "path": path, "repo_id": repo.id, "search_repo_id": repo.id, "search_wiki": True, "mods_enabled": mods_enabled, "mods_available": mods_available, "repo_perm": repo_perm, "wiki_index_exists": wiki_index_exists, "index_content": index_content, }, context_instance=RequestContext(request))
def message_send(request): """Handle POST request to send message to user(s)/group(s). """ if not request.is_ajax() or request.method != 'POST': raise Http404 content_type = 'application/json; charset=utf-8' result = {} username = request.user.username fr = request.GET.get('from') if not fr: result['error'] = [_(u'Argument missing')] return HttpResponse(json.dumps(result), content_type=content_type) mass_msg = request.POST.get('mass_msg') mass_emails = request.POST.getlist('mass_email') # e.g: [u'[email protected], u'*****@*****.**'] mass_group_ids = [int(x) for x in request.POST.getlist('mass_group')] if not mass_msg: result['error'] = [_(u'message is required')] return HttpResponse(json.dumps(result), content_type=content_type) if not mass_emails and not mass_group_ids: result['error'] = [_(u'contact/group is required')] return HttpResponse(json.dumps(result), content_type=content_type) # attachment selected = request.POST.getlist('selected') # selected files & dirs: [u'<repo_id><path>', ...] attached_items = [] if len(selected) > 0: for item in selected: if item[-1] == '/': # dir is not allowed, for now continue att = {} att['repo_id'] = item[0:36] att['path'] = item[36:] attached_items.append(att) email_sent = [] group_sent = [] errors = [] user_msgs = [] group_msgs = [] for to_email in mass_emails: to_email = to_email.strip() if not to_email or not is_valid_username(to_email): continue if to_email == username: errors.append(_(u'You can not send message to yourself.')) continue if not is_registered_user(to_email): errors.append(_(u'Failed to send message to %s, user not found.') % to_email) continue user_msg = UserMessage.objects.add_unread_message(username, to_email, mass_msg) user_msgs.append(user_msg) if len(attached_items) > 0: for att_item in attached_items: repo_id = att_item['repo_id'] path = att_item['path'] pfds = PrivateFileDirShare.objects.add_read_only_priv_file_share( username, to_email, repo_id, path) UserMsgAttachment.objects.add_user_msg_attachment(user_msg, pfds) email_sent.append(to_email) joined_groups = [] for group_id in mass_group_ids: group = get_group(group_id) if not group: continue joined_groups.append(group) if not is_group_user(group_id, username): errors.append(_(u'You can not send message to group %s, you didn\'t join in.') % group.name) continue group_msg = GroupMessage(group_id=group_id, from_email=username, message=mass_msg) group_msg.save() grpmsg_added.send(sender=GroupMessage, group_id=group_id, from_email=username) group_msgs.append(group_msg) if len(attached_items) > 0: for att_item in attached_items: repo_id = att_item['repo_id'] path = att_item['path'] ma = MessageAttachment(group_message=group_msg, repo_id=repo_id, attach_type='file', path=path, src='recommend') ma.save() group_sent.append(group_id) html = '' if email_sent or group_sent: ctx = {} if fr == 'all': # from 'all_msg_list' page user_msgs = user_msg_info_list(user_msgs, username) group_msgs = group_msg_info_list(joined_groups, username) ctx['msgs'] = sorted(user_msgs.items() + group_msgs.items(), key=lambda x: x[1]['last_time'], reverse=True) html = render_to_string('message/all_msg.html', ctx, context_instance=RequestContext(request)) else: ctx['msg'] = user_msgs[0] html = render_to_string('message/user_msg.html', ctx) return HttpResponse(json.dumps({"html": html, "error": errors}), content_type=content_type) else: return HttpResponse(json.dumps({"html": html, "error": errors}), status=400, content_type=content_type)