def _get_upload_link_info(self, uls): data = {} token = uls.token repo_id = uls.repo_id try: repo = seafile_api.get_repo(repo_id) except Exception as e: logger.error(e) repo = None path = uls.path if path: obj_name = "/" if path == "/" else os.path.basename(path.rstrip("/")) else: obj_name = "" if uls.ctime: ctime = datetime_to_isoformat_timestr(uls.ctime) else: ctime = "" data["repo_id"] = repo_id data["repo_name"] = repo.repo_name if repo else "" data["path"] = path data["obj_name"] = obj_name data["view_cnt"] = uls.view_cnt data["ctime"] = ctime data["link"] = gen_shared_upload_link(token) data["token"] = token data["username"] = uls.username return data
def _get_upload_link_info(self, uls): data = {} token = uls.token repo_id = uls.repo_id try: repo = seafile_api.get_repo(repo_id) except Exception as e: logger.error(e) repo = None path = uls.path if path: obj_name = '/' if path == '/' else os.path.basename(path.rstrip('/')) else: obj_name = '' if uls.ctime: ctime = datetime_to_isoformat_timestr(uls.ctime) else: ctime = '' data['repo_id'] = repo_id data['repo_name'] = repo.repo_name if repo else '' data['path'] = path data['obj_name'] = obj_name data['view_cnt'] = uls.view_cnt data['ctime'] = ctime data['link'] = gen_shared_upload_link(token) data['token'] = token data['username'] = uls.username return data
def get_upload_link_info(uls): data = {} token = uls.token repo_id = uls.repo_id try: repo = seafile_api.get_repo(repo_id) except Exception as e: logger.error(e) repo = None path = uls.path if path: obj_name = '/' if path == '/' else os.path.basename(path.rstrip('/')) else: obj_name = '' if uls.ctime: ctime = datetime_to_isoformat_timestr(uls.ctime) else: ctime = '' data['repo_id'] = repo_id data['repo_name'] = repo.repo_name if repo else '' data['path'] = path data['obj_name'] = obj_name data['view_cnt'] = uls.view_cnt data['ctime'] = ctime data['link'] = gen_shared_upload_link(token) data['token'] = token data['username'] = uls.username return data
def get_dir_shared_upload_link(uploadlink): # dir shared upload link if uploadlink: dir_shared_upload_link = gen_shared_upload_link(uploadlink.token) else: dir_shared_upload_link = '' return dir_shared_upload_link
def get_upload_link_info(upload_link): data = {} token = upload_link.token path = upload_link.path if path: obj_name = '/' if path == '/' else os.path.basename(path.rstrip('/')) else: obj_name = '' if upload_link.ctime: ctime = datetime_to_isoformat_timestr(upload_link.ctime) else: ctime = '' if upload_link.expire_date: expire_date = datetime_to_isoformat_timestr(upload_link.expire_date) else: expire_date = '' creator_email = upload_link.username data['creator_email'] = creator_email data['creator_name'] = email2nickname(creator_email) data['creator_contact_email'] = email2contact_email(creator_email) data['path'] = path data['obj_name'] = obj_name data['token'] = token data['link'] = gen_shared_upload_link(token) data['ctime'] = ctime data['expire_date'] = expire_date return data
def get_shared_upload_link(request): """ Handle ajax request to generate dir upload link. """ content_type = 'application/json; charset=utf-8' repo_id = request.GET.get('repo_id', '') path = request.GET.get('p', '') use_passwd = True if int(request.POST.get('use_passwd', '0')) == 1 else False passwd = request.POST.get('passwd') if use_passwd else None if not (repo_id and path): err = _('Invalid arguments') data = json.dumps({'error': err}) return HttpResponse(data, status=400, content_type=content_type) if path == '/': # can not share root dir err = _('You cannot share the library in this way.') data = json.dumps({'error': err}) return HttpResponse(data, status=400, content_type=content_type) else: if path[-1] != '/': # append '/' at end of path path += '/' repo = seaserv.get_repo(repo_id) if not repo: messages.error(request, _(u'Library does not exist')) return HttpResponse(status=400, content_type=content_type) user_perm = check_folder_permission(repo.id, path, request.user.username) if user_perm == 'r': messages.error(request, _(u'Permission denied')) return HttpResponse(status=403, content_type=content_type) elif user_perm == 'rw': l = UploadLinkShare.objects.filter(repo_id=repo_id).filter( username=request.user.username).filter(path=path) if len(l) > 0: upload_link = l[0] token = upload_link.token else: username = request.user.username uls = UploadLinkShare.objects.create_upload_link_share( username, repo_id, path, passwd) token = uls.token shared_upload_link = gen_shared_upload_link(token) data = json.dumps({ 'token': token, 'shared_upload_link': shared_upload_link }) return HttpResponse(data, status=200, content_type=content_type) else: messages.error(request, _(u'Operation failed')) return HttpResponse(json.dumps(), status=500, content_type=content_type)
def _get_upload_link_info(self, uls): data = {} token = uls.token data['repo_id'] = uls.repo_id data['path'] = uls.path data['ctime'] = uls.ctime data['link'] = gen_shared_upload_link(token) data['token'] = token data['username'] = uls.username return data
def get_shared_upload_link(request): """ Handle ajax request to generate dir upload link. """ content_type = 'application/json; charset=utf-8' repo_id = request.GET.get('repo_id', '') path = request.GET.get('p', '') use_passwd = True if int(request.POST.get('use_passwd', '0')) == 1 else False passwd = request.POST.get('passwd') if use_passwd else None if not (repo_id and path): err = _('Invalid arguments') data = json.dumps({'error': err}) return HttpResponse(data, status=400, content_type=content_type) if path == '/': # can not share root dir err = _('You cannot share the library in this way.') data = json.dumps({'error': err}) return HttpResponse(data, status=400, content_type=content_type) else: if path[-1] != '/': # append '/' at end of path path += '/' repo = seaserv.get_repo(repo_id) if not repo: messages.error(request, _(u'Library does not exist')) return HttpResponse(status=400, content_type=content_type) user_perm = check_folder_permission(repo.id, path, request.user.username) if user_perm == 'r': messages.error(request, _(u'Permission denied')) return HttpResponse(status=403, content_type=content_type) elif user_perm == 'rw': l = UploadLinkShare.objects.filter(repo_id=repo_id).filter( username=request.user.username).filter(path=path) if len(l) > 0: upload_link = l[0] token = upload_link.token else: username = request.user.username uls = UploadLinkShare.objects.create_upload_link_share( username, repo_id, path, passwd) token = uls.token shared_upload_link = gen_shared_upload_link(token) data = json.dumps({'token': token, 'shared_upload_link': shared_upload_link}) return HttpResponse(data, status=200, content_type=content_type) else: messages.error(request, _(u'Operation failed')) return HttpResponse(json.dumps(), status=500, content_type=content_type)
def list_shared_links(request): """List shared links, and remove invalid links(file/dir is deleted or moved). """ username = request.user.username # download links fileshares = FileShare.objects.filter(username=username) p_fileshares = [] # personal file share for fs in fileshares: if is_personal_repo(fs.repo_id): # only list files in personal repos r = seafile_api.get_repo(fs.repo_id) if not r: fs.delete() continue if fs.s_type == 'f': if seafile_api.get_file_id_by_path(r.id, fs.path) is None: fs.delete() continue fs.filename = os.path.basename(fs.path) fs.shared_link = gen_file_share_link(fs.token) else: if seafile_api.get_dir_id_by_path(r.id, fs.path) is None: fs.delete() continue fs.filename = os.path.basename(fs.path.rstrip('/')) fs.shared_link = gen_dir_share_link(fs.token) fs.repo = r p_fileshares.append(fs) # upload links uploadlinks = UploadLinkShare.objects.filter(username=username) p_uploadlinks = [] for link in uploadlinks: if is_personal_repo(link.repo_id): r = seafile_api.get_repo(link.repo_id) if not r: link.delete() continue if seafile_api.get_dir_id_by_path(r.id, link.path) is None: link.delete() continue link.dir_name = os.path.basename(link.path.rstrip('/')) link.shared_link = gen_shared_upload_link(link.token) link.repo = r p_uploadlinks.append(link) return render_to_response('share/links.html', { "fileshares": p_fileshares, "uploadlinks": p_uploadlinks, }, context_instance=RequestContext(request))
def get(self, request, format=None): username = request.user.username uploadlinks = UploadLinkShare.objects.filter(username=username) p_uploadlinks = [] for link in uploadlinks: r = seafile_api.get_repo(link.repo_id) if not r: link.delete() continue if seafile_api.get_dir_id_by_path(r.id, link.path) is None: link.delete() continue if link.path != '/': link.dir_name = os.path.basename(link.path.rstrip('/')) else: link.dir_name = link.path link.shared_link = gen_shared_upload_link(link.token) link.repo = r if link.expire_date: expire_date = link.expire_date.strftime( "%Y-%m-%dT%H:%M:%S") + DateFormat( link.expire_date).format('O') else: expire_date = "" p_uploadlinks.append({ "username": link.username, "repo_id": link.repo_id, "path": link.path, "token": link.token, "ctime": link.ctime.strftime("%Y-%m-%dT%H:%M:%S") + DateFormat(link.ctime).format('O'), "view_cnt": link.view_cnt, "expire_date": expire_date, }) return HttpResponse(json.dumps(p_uploadlinks), status=200, content_type=json_content_type)
def get(self, request, format=None): username = request.user.username uploadlinks = UploadLinkShare.objects.filter(username=username) p_uploadlinks = [] for link in uploadlinks: r = seafile_api.get_repo(link.repo_id) if not r: link.delete() continue if seafile_api.get_dir_id_by_path(r.id, link.path) is None: link.delete() continue if link.path != '/': link.dir_name = os.path.basename(link.path.rstrip('/')) else: link.dir_name = link.path link.shared_link = gen_shared_upload_link(link.token) link.repo = r if link.expire_date: expire_date = datetime_to_isoformat_timestr(link.expire_date) else: expire_date = "" p_uploadlinks.append({ "username": link.username, "repo_id": link.repo_id, "path": link.path, "token": link.token, "ctime": datetime_to_isoformat_timestr(link.ctime), "view_cnt": link.view_cnt, "expire_date": expire_date, }) return HttpResponse(json.dumps(p_uploadlinks), status=200, content_type=json_content_type)
def list_shared_upload_links(request): """List upload links, and remove invalid links(dir is deleted or moved). """ username = request.user.username uploadlinks = UploadLinkShare.objects.filter(username=username) p_uploadlinks = [] for link in uploadlinks: if is_personal_repo(link.repo_id): r = seafile_api.get_repo(link.repo_id) if not r: link.delete() continue if seafile_api.get_dir_id_by_path(r.id, link.path) is None: link.delete() continue link.dir_name = os.path.basename(link.path.rstrip('/')) link.shared_link = gen_shared_upload_link(link.token) link.repo = r p_uploadlinks.append(link) return render_to_response('repo/shared_upload_links.html', { "uploadlinks": p_uploadlinks, }, context_instance=RequestContext(request))
def get_user_upload_link_info(uls): data = {} repo_id = uls.repo_id try: repo = seafile_api.get_repo(repo_id) except Exception as e: logger.error(e) repo = None path = uls.path if path: obj_name = '/' if path == '/' else os.path.basename(path.rstrip('/')) else: obj_name = '' data['repo_name'] = repo.repo_name if repo else '' data['path'] = path data['token'] = uls.token data['link'] = gen_shared_upload_link(uls.token) data['obj_name'] = obj_name data['view_cnt'] = uls.view_cnt return data
def get_repo_dirents(request, repo, commit, path, offset=-1, limit=-1): """List repo dirents based on commit id and path. Use ``offset`` and ``limit`` to do paginating. Returns: A tupple of (file_list, dir_list, dirent_more) TODO: Some unrelated parts(file sharing, stars, modified info, etc) need to be pulled out to multiple functions. """ dir_list = [] file_list = [] dirent_more = False if commit.root_id == EMPTY_SHA1: return ([], [], False) if limit == -1 else ([], [], False) else: try: dirs = seafile_api.list_dir_by_commit_and_path( commit.repo_id, commit.id, path, offset, limit) if not dirs: return ([], [], False) except SearpcError as e: logger.error(e) return ([], [], False) if limit != -1 and limit == len(dirs): dirent_more = True username = request.user.username starred_files = get_dir_starred_files(username, repo.id, path) fileshares = FileShare.objects.filter(repo_id=repo.id).filter( username=username) uploadlinks = UploadLinkShare.objects.filter(repo_id=repo.id).filter( username=username) view_dir_base = reverse("view_common_lib_dir", args=[repo.id, '']) dl_dir_base = reverse('repo_download_dir', args=[repo.id]) file_history_base = reverse('file_revisions', args=[repo.id]) for dirent in dirs: dirent.last_modified = dirent.mtime dirent.sharelink = '' dirent.uploadlink = '' if stat.S_ISDIR(dirent.props.mode): dpath = posixpath.join(path, dirent.obj_name) if dpath[-1] != '/': dpath += '/' for share in fileshares: if dpath == share.path: dirent.sharelink = gen_dir_share_link(share.token) dirent.sharetoken = share.token break for link in uploadlinks: if dpath == link.path: dirent.uploadlink = gen_shared_upload_link(link.token) dirent.uploadtoken = link.token break p_dpath = posixpath.join(path, dirent.obj_name) dirent.view_link = view_dir_base + '?p=' + urlquote(p_dpath) dirent.dl_link = dl_dir_base + '?p=' + urlquote(p_dpath) dir_list.append(dirent) else: file_list.append(dirent) if repo.version == 0: dirent.file_size = get_file_size(repo.store_id, repo.version, dirent.obj_id) else: dirent.file_size = dirent.size dirent.starred = False fpath = posixpath.join(path, dirent.obj_name) p_fpath = posixpath.join(path, dirent.obj_name) dirent.view_link = reverse('view_lib_file', args=[repo.id, p_fpath]) dirent.dl_link = get_file_download_link( repo.id, dirent.obj_id, p_fpath) dirent.history_link = file_history_base + '?p=' + urlquote( p_fpath) if fpath in starred_files: dirent.starred = True for share in fileshares: if fpath == share.path: dirent.sharelink = gen_file_share_link(share.token) dirent.sharetoken = share.token break return (file_list, dir_list, dirent_more)
def post(self, request): if not IS_EMAIL_CONFIGURED: error_msg = _( u'Sending shared link failed. Email service is not properly configured, please contact administrator.' ) return api_error(status.HTTP_403_FORBIDDEN, error_msg) # check args email = request.POST.get('email', None) if not email: error_msg = 'email invalid.' return api_error(status.HTTP_400_BAD_REQUEST, error_msg) token = request.POST.get('token', None) if not token: error_msg = 'token invalid.' return api_error(status.HTTP_400_BAD_REQUEST, error_msg) extra_msg = request.POST.get('extra_msg', '') # check if token exists try: link = UploadLinkShare.objects.get(token=token) except UploadLinkShare.DoesNotExist: error_msg = 'token %s not found.' % token return api_error(status.HTTP_404_NOT_FOUND, error_msg) # check if is upload link owner username = request.user.username if not link.is_owner(username): error_msg = 'Permission denied.' return api_error(status.HTTP_403_FORBIDDEN, error_msg) result = {} result['failed'] = [] result['success'] = [] to_email_list = string2list(email) # use contact_email, if present useremail = Profile.objects.get_contact_email_by_user( request.user.username) for to_email in to_email_list: failed_info = {} if not is_valid_email(to_email): failed_info['email'] = to_email failed_info['error_msg'] = _(u'email invalid.') result['failed'].append(failed_info) continue # prepare basic info c = { 'email': username, 'to_email': to_email, 'extra_msg': extra_msg, } if REPLACE_FROM_EMAIL: from_email = useremail else: from_email = None # use default from email if ADD_REPLY_TO_HEADER: reply_to = useremail else: reply_to = None c['shared_upload_link'] = gen_shared_upload_link(token) title = _( u'An upload link is shared to you on %s') % get_site_name() template = 'shared_upload_link_email.html' # send email try: send_html_email(title, template, c, from_email, [to_email], reply_to=reply_to) result['success'].append(to_email) except Exception as e: logger.error(e) failed_info['email'] = to_email failed_info['error_msg'] = _(u'Internal Server Error') result['failed'].append(failed_info) return Response(result)
def get_repo_dirents(request, repo, commit, path, offset=-1, limit=-1): """List repo dirents based on commit id and path. Use ``offset`` and ``limit`` to do paginating. Returns: A tupple of (file_list, dir_list, dirent_more) TODO: Some unrelated parts(file sharing, stars, modified info, etc) need to be pulled out to multiple functions. """ dir_list = [] file_list = [] dirent_more = False if commit.root_id == EMPTY_SHA1: return ([], [], False) if limit == -1 else ([], [], False) else: try: dirs = seafile_api.list_dir_by_commit_and_path(commit.repo_id, commit.id, path, offset, limit) if not dirs: return ([], [], False) except SearpcError as e: logger.error(e) return ([], [], False) if limit != -1 and limit == len(dirs): dirent_more = True username = request.user.username starred_files = get_dir_starred_files(username, repo.id, path) fileshares = FileShare.objects.filter(repo_id=repo.id).filter(username=username) uploadlinks = UploadLinkShare.objects.filter(repo_id=repo.id).filter(username=username) view_dir_base = reverse("view_common_lib_dir", args=[repo.id, '']) dl_dir_base = reverse('repo_download_dir', args=[repo.id]) file_history_base = reverse('file_revisions', args=[repo.id]) for dirent in dirs: dirent.last_modified = dirent.mtime dirent.sharelink = '' dirent.uploadlink = '' if stat.S_ISDIR(dirent.props.mode): dpath = posixpath.join(path, dirent.obj_name) if dpath[-1] != '/': dpath += '/' for share in fileshares: if dpath == share.path: dirent.sharelink = gen_dir_share_link(share.token) dirent.sharetoken = share.token break for link in uploadlinks: if dpath == link.path: dirent.uploadlink = gen_shared_upload_link(link.token) dirent.uploadtoken = link.token break p_dpath = posixpath.join(path, dirent.obj_name) dirent.view_link = view_dir_base + '?p=' + urlquote(p_dpath) dirent.dl_link = dl_dir_base + '?p=' + urlquote(p_dpath) dir_list.append(dirent) else: file_list.append(dirent) if repo.version == 0: dirent.file_size = get_file_size(repo.store_id, repo.version, dirent.obj_id) else: dirent.file_size = dirent.size dirent.starred = False fpath = posixpath.join(path, dirent.obj_name) p_fpath = posixpath.join(path, dirent.obj_name) dirent.view_link = reverse('view_lib_file', args=[repo.id, p_fpath]) dirent.dl_link = get_file_download_link(repo.id, dirent.obj_id, p_fpath) dirent.history_link = file_history_base + '?p=' + urlquote(p_fpath) if fpath in starred_files: dirent.starred = True for share in fileshares: if fpath == share.path: dirent.sharelink = gen_file_share_link(share.token) dirent.sharetoken = share.token break return (file_list, dir_list, dirent_more)
def ajax_get_upload_link(request): content_type = 'application/json; charset=utf-8' if request.method == 'GET': repo_id = request.GET.get('repo_id', '') path = request.GET.get('p', '') username = request.user.username if not path.endswith('/'): path = path + '/' l = UploadLinkShare.objects.filter(repo_id=repo_id).filter( username=username).filter(path=path) if len(l) > 0: token = l[0].token data = { 'upload_link': gen_shared_upload_link(token), 'token': token, } else: data = {} return HttpResponse(json.dumps(data), content_type=content_type) elif request.method == 'POST': if not request.user.permissions.can_generate_shared_link(): err = _('You do not have permission to generate shared link') data = json.dumps({'error': err}) return HttpResponse(data, status=403, content_type=content_type) repo_id = request.POST.get('repo_id', '') path = request.POST.get('p', '') use_passwd = True if int(request.POST.get('use_passwd', '0')) == 1 else False passwd = request.POST.get('passwd') if use_passwd else None if not (repo_id and path): err = _('Invalid arguments') data = json.dumps({'error': err}) return HttpResponse(data, status=400, content_type=content_type) if passwd and len(passwd) < config.SHARE_LINK_PASSWORD_MIN_LENGTH: err = _('Password is too short') data = json.dumps({'error': err}) return HttpResponse(data, status=400, content_type=content_type) if path[-1] != '/': # append '/' at end of path path += '/' repo = seaserv.get_repo(repo_id) if not repo: err = 'Library %s not found.' % repo_id data = json.dumps({'error': err}) return HttpResponse(data, status=404, content_type=content_type) user_perm = check_folder_permission(request, repo_id, '/') if user_perm == 'rw': l = UploadLinkShare.objects.filter(repo_id=repo_id).filter( username=request.user.username).filter(path=path) if len(l) > 0: upload_link = l[0] token = upload_link.token else: username = request.user.username uls = UploadLinkShare.objects.create_upload_link_share( username, repo_id, path, passwd) token = uls.token shared_upload_link = gen_shared_upload_link(token) data = json.dumps({'token': token, 'upload_link': shared_upload_link}) return HttpResponse(data, content_type=content_type) else: return HttpResponse(json.dumps({'error': _(u'Permission denied')}), status=403, content_type=content_type)
def list_shared_links(request): """List shared links, and remove invalid links(file/dir is deleted or moved). """ username = request.user.username # download links fileshares = FileShare.objects.filter(username=username) fs_files, fs_dirs = [], [] for fs in fileshares: r = seafile_api.get_repo(fs.repo_id) if not r: fs.delete() continue if fs.is_file_share_link(): if seafile_api.get_file_id_by_path(r.id, fs.path) is None: fs.delete() continue fs.filename = os.path.basename(fs.path) fs.shared_link = gen_file_share_link(fs.token) else: if seafile_api.get_dir_id_by_path(r.id, fs.path) is None: fs.delete() continue if fs.path != '/': fs.filename = os.path.basename(fs.path.rstrip('/')) else: fs.filename = fs.path fs.shared_link = gen_dir_share_link(fs.token) fs.repo = r if fs.expire_date is not None and timezone.now() > fs.expire_date: fs.is_expired = True fs_files.append(fs) if fs.is_file_share_link() else fs_dirs.append(fs) fs_files.sort(lambda x, y: cmp(x.filename, y.filename)) fs_dirs.sort(lambda x, y: cmp(x.filename, y.filename)) # upload links uploadlinks = UploadLinkShare.objects.filter(username=username) p_uploadlinks = [] for link in uploadlinks: r = seafile_api.get_repo(link.repo_id) if not r: link.delete() continue if seafile_api.get_dir_id_by_path(r.id, link.path) is None: link.delete() continue if link.path != '/': link.dir_name = os.path.basename(link.path.rstrip('/')) else: link.dir_name = link.path link.shared_link = gen_shared_upload_link(link.token) link.repo = r p_uploadlinks.append(link) p_uploadlinks.sort(lambda x, y: cmp(x.dir_name, y.dir_name)) return render_to_response('share/links.html', { "fileshares": fs_dirs + fs_files, "uploadlinks": p_uploadlinks, }, context_instance=RequestContext(request))
token = gen_token(max_length=10) upload_link = UploadLinkShare() upload_link.username = request.user.username upload_link.repo_id = repo_id upload_link.path = path upload_link.token = token try: upload_link.save() except IntegrityError, e: err = _('Failed to get the link, please retry later.') data = json.dumps({'error': err}) return HttpResponse(data, status=500, content_type=content_type) shared_upload_link = gen_shared_upload_link(token) data = json.dumps({'token': token, 'shared_upload_link': shared_upload_link}) return HttpResponse(data, status=200, content_type=content_type) @login_required def send_shared_upload_link(request): """ Handle ajax post request to send shared upload link. """ if not request.is_ajax() and not request.method == 'POST': raise Http404 content_type = 'application/json; charset=utf-8' if not IS_EMAIL_CONFIGURED:
def post(self, request): if not IS_EMAIL_CONFIGURED: error_msg = _(u'Sending shared link failed. Email service is not properly configured, please contact administrator.') return api_error(status.HTTP_403_FORBIDDEN, error_msg) # check args email = request.POST.get('email', None) if not email: error_msg = 'email invalid.' return api_error(status.HTTP_400_BAD_REQUEST, error_msg) token = request.POST.get('token', None) if not token: error_msg = 'token invalid.' return api_error(status.HTTP_400_BAD_REQUEST, error_msg) extra_msg = request.POST.get('extra_msg', '') # check if token exists try: link = UploadLinkShare.objects.get(token=token) except UploadLinkShare.DoesNotExist: error_msg = 'token %s not found.' % token return api_error(status.HTTP_404_NOT_FOUND, error_msg) # check if is upload link owner username = request.user.username if not link.is_owner(username): error_msg = 'Permission denied.' return api_error(status.HTTP_403_FORBIDDEN, error_msg) result = {} result['failed'] = [] result['success'] = [] to_email_list = string2list(email) # use contact_email, if present useremail = Profile.objects.get_contact_email_by_user(request.user.username) for to_email in to_email_list: failed_info = {} if not is_valid_email(to_email): failed_info['email'] = to_email failed_info['error_msg'] = 'email invalid.' result['failed'].append(failed_info) continue # prepare basic info c = { 'email': username, 'to_email': to_email, 'extra_msg': extra_msg, } if REPLACE_FROM_EMAIL: from_email = useremail else: from_email = None # use default from email if ADD_REPLY_TO_HEADER: reply_to = useremail else: reply_to = None c['shared_upload_link'] = gen_shared_upload_link(token) title = _(u'An upload link is shared to you on %s') % SITE_NAME template = 'shared_upload_link_email.html' # send email try: send_html_email(title, template, c, from_email, [to_email], reply_to=reply_to) result['success'].append(to_email) except Exception as e: logger.error(e) failed_info['email'] = to_email failed_info['error_msg'] = 'Internal Server Error' result['failed'].append(failed_info) return Response(result)
token = gen_token(max_length=10) upload_link = UploadLinkShare() upload_link.username = request.user.username upload_link.repo_id = repo_id upload_link.path = path upload_link.token = token try: upload_link.save() except IntegrityError, e: err = _('Failed to get the link, please retry later.') data = json.dumps({'error': err}) return HttpResponse(data, status=500, content_type=content_type) shared_upload_link = gen_shared_upload_link(token) data = json.dumps({ 'token': token, 'shared_upload_link': shared_upload_link }) return HttpResponse(data, status=200, content_type=content_type) @login_required def send_shared_upload_link(request): """ Handle ajax post request to send shared upload link. """ if not request.is_ajax() and not request.method == 'POST': raise Http404
def ajax_get_upload_link(request): content_type = 'application/json; charset=utf-8' if request.method == 'GET': repo_id = request.GET.get('repo_id', '') path = request.GET.get('p', '') username = request.user.username if not path.endswith('/'): path = path + '/' l = UploadLinkShare.objects.filter(repo_id=repo_id).filter( username=username).filter(path=path) if len(l) > 0: token = l[0].token data = { 'upload_link': gen_shared_upload_link(token), 'token': token, } else: data = {} return HttpResponse(json.dumps(data), content_type=content_type) elif request.method == 'POST': if not request.user.permissions.can_generate_shared_link(): err = _('You do not have permission to generate shared link') data = json.dumps({'error': err}) return HttpResponse(data, status=403, content_type=content_type) repo_id = request.POST.get('repo_id', '') path = request.POST.get('p', '') use_passwd = True if int(request.POST.get('use_passwd', '0')) == 1 else False passwd = request.POST.get('passwd') if use_passwd else None if not (repo_id and path): err = _('Invalid arguments') data = json.dumps({'error': err}) return HttpResponse(data, status=400, content_type=content_type) if passwd and len(passwd) < config.SHARE_LINK_PASSWORD_MIN_LENGTH: err = _('Password is too short') data = json.dumps({'error': err}) return HttpResponse(data, status=400, content_type=content_type) if path[-1] != '/': # append '/' at end of path path += '/' repo = seaserv.get_repo(repo_id) if not repo: err = 'Library %s not found.' % repo_id data = json.dumps({'error': err}) return HttpResponse(data, status=404, content_type=content_type) user_perm = check_folder_permission(request, repo_id, '/') if user_perm == 'rw': l = UploadLinkShare.objects.filter(repo_id=repo_id).filter( username=request.user.username).filter(path=path) if len(l) > 0: upload_link = l[0] token = upload_link.token else: username = request.user.username uls = UploadLinkShare.objects.create_upload_link_share( username, repo_id, path, passwd) token = uls.token shared_upload_link = gen_shared_upload_link(token) data = json.dumps({ 'token': token, 'upload_link': shared_upload_link }) return HttpResponse(data, content_type=content_type) else: return HttpResponse(json.dumps({'error': _(u'Permission denied')}), status=403, content_type=content_type)