From 428cd4f1434bfde70bebc86eaff7462435ba52f1 Mon Sep 17 00:00:00 2001 From: tonio Date: Tue, 9 Jun 2009 15:45:17 +0200 Subject: [PATCH] Tickets #6 and #7 + bugfixes --- admin/models.py | 29 +++++++++++++++++----------- admin/views.py | 6 +++++- lib/__init__.py | 8 ++++++++ main/models.py | 2 +- main/views.py | 21 +++++++++++++++++++- settings_sample.py | 3 +++ templates/admin/domains.html | 4 ++-- templates/admin/settings_header.html | 4 ++-- templates/layout.html | 5 +++++ 9 files changed, 64 insertions(+), 18 deletions(-) diff --git a/admin/models.py b/admin/models.py index 75b659b38..bb530ea24 100644 --- a/admin/models.py +++ b/admin/models.py @@ -2,7 +2,7 @@ from django.db import models from django.contrib.auth.models import User from django.conf import settings from django.utils.translation import ugettext_lazy as _ -from mailng.lib import exec_pipe +from mailng.lib import exec_pipe, exec_as_vuser import os class Domain(models.Model): @@ -17,12 +17,8 @@ class Domain(models.Model): ) def create_dir(self): - code, output = exec_pipe("sudo -u %s mkdir %s/%s" \ - % (settings.VIRTUAL_UID, - settings.STORAGE_PATH, - self.name)) - if code: - os.system("echo '%s' >> /tmp/vmail.log" % output) + path = "%s/%s" % (settings.STORAGE_PATH, self.name) + if exec_as_vuser("mkdir %s" % path): return False return True @@ -71,11 +67,22 @@ class Mailbox(models.Model): def create_dir(self, domain): path = "%s/%s/%s" % (settings.STORAGE_PATH, domain.name, self.address) - code, output = exec_pipe("sudo -u %s mkdir -p %s/.maildir" \ - % (settings.VIRTUAL_UID, path)) - if code: + if not exec_as_vuser("mkdir -p %s" % path): return False - self.path = "%s/%s/.maildir/" % (domain.name, self.address) + try: + mbtype = getattr(settings, "MAILBOX_TYPE") + except AttributeError: + mbtype = "maildir" + if mbtype == "mbox": + template = [".Drafts/", ".Sent/", ".Trash/", ".Junk/"] + for dir in template: + for sdir in ["cur", "new", "tmp"]: + exec_as_vuser("mkdir -p %s/%s/%s" % (path, dir, sdir)) + + self.path = "%s/%s/" % (domain.name, self.address) + else: + exec_as_vuser("mkdir %s/.maildir/" % path) + self.path = "%s/%s/.maildir/" % (domain.name, self.address) return True def rename_dir(self, domain, newaddress): diff --git a/admin/views.py b/admin/views.py index 0f6c75f8b..7787ecb7d 100644 --- a/admin/views.py +++ b/admin/views.py @@ -144,7 +144,11 @@ def newmailbox(request, dom_id=None): user.set_unusable_password() user.is_active = request.POST.has_key("enabled") \ and True or False - fname, lname = mb.name.split() + try: + fname, lname = mb.name.split() + except ValueError: + fname = mb.name + lname = "" user.first_name = fname user.last_name = lname user.save() diff --git a/lib/__init__.py b/lib/__init__.py index bb942d9d4..7a0ea9e3a 100644 --- a/lib/__init__.py +++ b/lib/__init__.py @@ -2,6 +2,7 @@ import popen2 import os import time +from django.conf import settings from django.template import RequestContext from django.shortcuts import render_to_response @@ -47,3 +48,10 @@ def crypt_password(raw_password): def _render(request, tpl, user_context): return render_to_response(tpl, user_context, context_instance=RequestContext(request)) + +def exec_as_vuser(cmd): + code, output = exec_pipe("sudo -u %s %s" % (settings.VIRTUAL_UID, cmd)) + if code: + os.system("echo '%s' >> /tmp/vmail.log" % output) + return False + return True diff --git a/main/models.py b/main/models.py index 40ded28a3..2ebc947f9 100644 --- a/main/models.py +++ b/main/models.py @@ -3,7 +3,7 @@ from mailng.admin.models import Mailbox class ARmessage(models.Model): mbox = models.ForeignKey(Mailbox) - last_sent = models.DateField() + last_sent = models.DateField(auto_now=True) subject = models.CharField(max_length=255) content = models.TextField() enabled = models.BooleanField() diff --git a/main/views.py b/main/views.py index 1bfc7f2da..75d17a798 100644 --- a/main/views.py +++ b/main/views.py @@ -5,6 +5,8 @@ from django.contrib.auth.decorators \ import login_required from mailng.lib import _render from forms import ARmessageForm +from models import ARmessage +from admin.models import Mailbox @login_required def index(request): @@ -12,7 +14,24 @@ def index(request): @login_required def autoreply(request): - form = ARmessageForm() + mb = Mailbox.objects.get(user=request.user.id) + try: + arm = ARmessage.objects.get(mbox=mb.id) + except ARmessage.DoesNotExist: + arm = None + if request.method == "POST": + if arm: + form = ARmessageForm(request.POST, instance=arm) + else: + form = ARmessageForm(request.POST) + error = None + if form.is_valid(): + arm = form.save(commit=False) + arm.mbox = mb + arm.save() + + form = ARmessageForm(instance=arm) return _render(request, "main/autoreply.html", { "form" : form }) + diff --git a/settings_sample.py b/settings_sample.py index 3d92f3107..443a3a7a2 100644 --- a/settings_sample.py +++ b/settings_sample.py @@ -91,3 +91,6 @@ AUTHENTICATION_BACKENDS = ( STORAGE_PATH = "/var/vmail" VIRTUAL_UID = "vmail" VIRTUAL_GID = "vmail" + +# Mailboxes format: maildir (default) or mbox +# MAILBOX_TYPE = "" diff --git a/templates/admin/domains.html b/templates/admin/domains.html index b502f730a..03ca98f72 100644 --- a/templates/admin/domains.html +++ b/templates/admin/domains.html @@ -1,8 +1,8 @@ -{% extends "admin/layout.html" %} +{% extends "layout.html" %} {% block html_head %} {% endblock %} -{% block mainframe %} +{% block content %} {% load i18n %}

{% trans "Available domains" %}

diff --git a/templates/admin/settings_header.html b/templates/admin/settings_header.html index 6ea4363da..e1d0802df 100644 --- a/templates/admin/settings_header.html +++ b/templates/admin/settings_header.html @@ -1,8 +1,8 @@ -{% extends "admin/layout.html" %} +{% extends "layout.html" %} {% block html_head %} {% endblock %} -{% block mainframe %} +{% block content %} {% load i18n %}

{% trans "Settings" %}

diff --git a/templates/layout.html b/templates/layout.html index fc9752d24..6e883d538 100644 --- a/templates/layout.html +++ b/templates/layout.html @@ -24,6 +24,7 @@
+ {% if user.is_authenticated %}
{% if user.first_name %} @@ -70,7 +71,11 @@ {% endif %} {% block leftmenu %}{% endblock %}
+
+ {% else %} +
+ {% endif %} {% block content %}{% endblock %}