Store the full address as a computed field

master
Pierre Jaury 9 years ago
parent f2a6fe89db
commit 18af763293

@ -55,24 +55,32 @@ class Address(Base):
"""
__abstract__ = True
localpart = db.Column(db.String(80), primary_key=True, nullable=False)
localpart = db.Column(db.String(80), nullable=False)
@declarative.declared_attr
def domain_name(cls):
return db.Column(db.String(80), db.ForeignKey(Domain.name),
primary_key=True, nullable=False)
nullable=False)
def __str__(self):
return '{0}@{1}'.format(self.localpart, self.domain_name)
def get_id(self):
return str(self)
# This field is redundant with both localpart and domain name.
# It is however very useful for quick lookups without joining tables,
# especially when the mail server il reading the database.
@declarative.declared_attr
def address(cls):
updater = lambda context: "{0}@{1}".format(
context.current_parameters["localpart"],
context.current_parameters["domain_name"],
)
return db.Column(db.String(255),
primary_key=True, nullable=False,
default=updater)
@classmethod
def get_by_email(cls, email):
localpart, domain = email.split('@', maxsplit=1)
# Get the user object
return cls.query.filter_by(domain_name=domain, localpart=localpart).first()
return cls.query.filter_by(address=email).first()
def __str__(self):
return self.address
class User(Address):
@ -102,6 +110,9 @@ class User(Address):
is_active = True
is_anonymous = False
def get_id(self):
return self.address
pw_context = context.CryptContext(
["sha512_crypt", "sha256_crypt", "md5_crypt"]
)

@ -26,8 +26,8 @@ Alias list
{% for alias in domain.aliases %}
<tr>
<td>
<a href="{{ url_for('.alias_edit', alias=alias.get_id()) }}" title="Edit"><i class="fa fa-pencil"></i></a>&nbsp;
<a href="{{ url_for('.alias_delete', alias=alias.get_id()) }}" onclick="return confirm('Are you sure?')" title="Delete"><i class="fa fa-trash"></i></a>
<a href="{{ url_for('.alias_edit', alias=alias.address) }}" title="Edit"><i class="fa fa-pencil"></i></a>&nbsp;
<a href="{{ url_for('.alias_delete', alias=alias.address) }}" onclick="return confirm('Are you sure?')" title="Delete"><i class="fa fa-trash"></i></a>
</td>
<td>{{ alias }}</td>
<td>{{ alias.destination or '-' }}</td>

@ -28,14 +28,14 @@ User list
{% for user in domain.users %}
<tr>
<td>
<a href="{{ url_for('.user_edit', user_email=user.get_id()) }}" title="Edit"><i class="fa fa-pencil"></i></a>&nbsp;
<a href="{{ url_for('.user_delete', user_email=user.get_id()) }}" onclick="return confirm('Are you sure?')" title="Delete"><i class="fa fa-trash"></i></a>
<a href="{{ url_for('.user_edit', user_email=user.address) }}" title="Edit"><i class="fa fa-pencil"></i></a>&nbsp;
<a href="{{ url_for('.user_delete', user_email=user.address) }}" onclick="return confirm('Are you sure?')" title="Delete"><i class="fa fa-trash"></i></a>
</td>
<td>
<a href="{{ url_for('.user_settings', user_email=user.get_id()) }}" title="Settings"><i class="fa fa-wrench"></i></a>&nbsp;
<a href="{{ url_for('.user_forward', user_email=user.get_id()) }}" title="Auto-forward"><i class="fa fa-share"></i></a>&nbsp;
<a href="{{ url_for('.user_reply', user_email=user.get_id()) }}" title="Auto-reply"><i class="fa fa-plane"></i></a>&nbsp;
<a href="{{ url_for('.user_fetchmail', user_email=user.get_id()) }}" title="Fetched accounts"><i class="fa fa-download"></i></a>&nbsp;
<a href="{{ url_for('.user_settings', user_email=user.address) }}" title="Settings"><i class="fa fa-wrench"></i></a>&nbsp;
<a href="{{ url_for('.user_forward', user_email=user.address) }}" title="Auto-forward"><i class="fa fa-share"></i></a>&nbsp;
<a href="{{ url_for('.user_reply', user_email=user.address) }}" title="Auto-reply"><i class="fa fa-plane"></i></a>&nbsp;
<a href="{{ url_for('.user_fetchmail', user_email=user.address) }}" title="Fetched accounts"><i class="fa fa-download"></i></a>&nbsp;
</td>
<td>{{ user }}</td>
<td>

@ -5,12 +5,10 @@ connect = /data/freeposte.db
password_query = \
SELECT password \
FROM user \
WHERE user.domain_name = '%d' \
AND user.localpart = '%n'
WHERE user.address = '%u'
# Mostly get the user quota
user_query = \
SELECT '*:bytes=' || user.quota_bytes AS quota_rule \
FROM user \
WHERE user.domain_name = '%d' \
AND user.localpart = '%n'
WHERE user.address = '%u'

@ -2,5 +2,4 @@ dbpath = /data/freeposte.db
query =
SELECT destination
FROM alias
WHERE alias.domain_name = '%d'
AND alias.localpart = '%u'
WHERE alias.address = '%s'

Loading…
Cancel
Save