def activate_filters_set(request, name): sc = SieveClient(user=request.user.username, password=request.session["password"]) try: sc.activatescript(name) except SieveClientError as e: return ajax_response(request, "ko", respmsg=str(e)) return ajax_response(request, respmsg=_("Filters set activated"))
def savefs(request, name): if "scriptcontent" not in request.POST: return sc = SieveClient(user=request.user.username, password=request.session["password"]) try: sc.pushscript(name, request.POST["scriptcontent"]) except SieveClientError as e: error = str(e) return ajax_response(request, "ko", respmsg=error) return ajax_response(request, respmsg=_("Filters set saved"))
def getfs(request, name): sc = SieveClient(user=request.user.username, password=request.session["password"]) editormode = request.user.parameters.get_value("editor_mode") error = None try: content = sc.getscript(name, format=editormode) except SieveClientError as e: error = str(e) else: if content is None: error = _("Failed to retrieve filters set") if error is not None: return ajax_response(request, "ko", respmsg=error) if editormode == "raw": htmlcontent = render_to_string("modoboa_sievefilters/rawfilter.html", { "name": name, "scriptcontent": content }) else: htmlcontent = render_to_string("modoboa_sievefilters/guieditor.html", {"fs": content}) menu = (u"<ul id='fsetmenu' class='nav nav-sidebar'>" u"<li class='nav-header'>{}</li>{}</ul>".format( _("Actions"), fset_menu(editormode, name))) resp = dict(menu=menu, content=htmlcontent) return render_to_json_response(resp)
def getfs(request, name): sc = SieveClient(user=request.user.username, password=request.session["password"]) editormode = parameters.get_user(request.user, "EDITOR_MODE") error = None try: content = sc.getscript(name, format=editormode) except SieveClientError as e: error = str(e) else: if content is None: error = _("Failed to retrieve filters set") if error is not None: return ajax_response(request, "ko", respmsg=error) if editormode == "raw": htmlcontent = render_to_string("modoboa_sievefilters/rawfilter.html", dict(name=name, scriptcontent=content)) else: htmlcontent = render_to_string("modoboa_sievefilters/guieditor.html", dict(fs=content)) menu = '<ul id="fsetmenu" class="nav nav-sidebar"><li class="nav-header">%s</li>%s</ul>' % \ (_("Actions"), fset_menu(editormode, name)) resp = dict(menu=menu, content=htmlcontent) return render_to_json_response(resp)
def getfs(request, name): sc = SieveClient(user=request.user.username, password=request.session["password"]) editormode = parameters.get_user(request.user, "EDITOR_MODE") error = None try: content = sc.getscript(name, format=editormode) except SieveClientError as e: error = str(e) else: if content is None: error = _("Failed to retrieve filters set") if error is not None: return ajax_response(request, "ko", respmsg=error) if editormode == "raw": htmlcontent = render_to_string("modoboa_sievefilters/rawfilter.html", dict( name=name, scriptcontent=content )) else: htmlcontent = render_to_string("modoboa_sievefilters/guieditor.html", dict( fs=content )) menu = '<ul id="fsetmenu" class="nav nav-sidebar"><li class="nav-header">%s</li>%s</ul>' % \ (_("Actions"), fset_menu(editormode, name)) resp = dict(menu=menu, content=htmlcontent) return render_to_json_response(resp)
def move_filter(request, setname, fname, direction): sc = SieveClient(user=request.user.username, password=request.session["password"]) fset = sc.getscript(setname, format="fset") fset.movefilter(fname.encode("utf-8"), direction) sc.pushscript(setname, str(fset)) return ajax_response(request, template="modoboa_sievefilters/guieditor.html", fs=fset)
def delfolder(request): name = request.GET.get("name", None) if name is None: raise BadRequest(_("Invalid request")) mbc = IMAPconnector(user=request.user.username, password=request.session["password"]) mbc.delete_folder(name) WebmailNavigationParameters(request).remove('mbox') return ajax_response(request)
def move_filter(request, setname, fname, direction): sc = SieveClient(user=request.user.username, password=request.session["password"]) fset = sc.getscript(setname, format="fset") fset.movefilter(fname.encode("utf-8"), direction) sc.pushscript(setname, str(fset)) return ajax_response( request, template="modoboa_sievefilters/guieditor.html", fs=fset )
def download_filters_set(request, name): sc = SieveClient(user=request.user.username, password=request.session["password"]) try: script = sc.getscript(name) except SieveClientError as e: return ajax_response(request, "ko", respmsg=str(e)) resp = HttpResponse(script) resp["Content-Type"] = "text/plain; charset=utf-8" resp["Content-Length"] = len(script) resp["Content-Disposition"] = build_header('%s.txt' % name) return resp
def delattachment(request): """Delete an attachment.""" name = request.GET.get("name") if not name or "compose_mail" not in request.session: return ajax_response(request, "ko", respmsg=_("Bad query")) error = None for att in request.session["compose_mail"]["attachments"]: if att["tmpname"] == name: request.session["compose_mail"]["attachments"].remove(att) fullpath = os.path.join(settings.MEDIA_ROOT, "webmail", att["tmpname"]) try: os.remove(fullpath) except OSError as e: error = _("Failed to remove attachment: ") + str(e) break request.session.modified = True return ajax_response(request) if error is None: error = _("Unknown attachment") return ajax_response(request, "ko", respmsg=error)
def process_exception(self, request, exception): if not isinstance(exception, ModoboaException): return None if request.is_ajax() or "/api/" in request.path: if exception.http_code is None: return ajax_response(request, status="ko", respmsg=unicode(exception), norefresh=True) return render_to_json_response(unicode(exception), status=exception.http_code) return _render_error(request, user_context=dict(error=str(exception)))
def delattachment(request): if "compose_mail" not in request.session \ or "name" not in request.GET \ or not request.GET["name"]: return ajax_response(request, "ko", respmsg=_("Bad query")) error = None for att in request.session["compose_mail"]["attachments"]: if att["tmpname"] == request.GET["name"]: request.session["compose_mail"]["attachments"].remove(att) fullpath = os.path.join( settings.MEDIA_ROOT, "webmail", att["tmpname"] ) try: os.remove(fullpath) except OSError as e: error = _("Failed to remove attachment: ") + str(e) break request.session.modified = True return ajax_response(request) if error is None: error = _("Unknown attachment") return ajax_response(request, "ko", respmsg=error)
def process_exception(self, request, exception): if not isinstance(exception, ModoboaException): return None if not request.is_ajax(): return _render_error( request, user_context=dict(error=str(exception)) ) if exception.http_code is None: return ajax_response( request, status="ko", respmsg=unicode(exception), norefresh=True ) return render_to_json_response( unicode(exception), status=exception.http_code )
def process_exception(self, request, exception): if not isinstance(exception, ModoboaException): return None if request.is_ajax() or "/api/" in request.path: if exception.http_code is None: return ajax_response( request, status="ko", respmsg=smart_text(exception), norefresh=True ) return render_to_json_response( smart_text(exception), status=exception.http_code ) return _render_error( request, user_context={"error": smart_text(exception)} )