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

@ -26,8 +26,8 @@ Alias list
{% for alias in domain.aliases %} {% for alias in domain.aliases %}
<tr> <tr>
<td> <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_edit', alias=alias.address) }}" 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_delete', alias=alias.address) }}" onclick="return confirm('Are you sure?')" title="Delete"><i class="fa fa-trash"></i></a>
</td> </td>
<td>{{ alias }}</td> <td>{{ alias }}</td>
<td>{{ alias.destination or '-' }}</td> <td>{{ alias.destination or '-' }}</td>

@ -28,14 +28,14 @@ User list
{% for user in domain.users %} {% for user in domain.users %}
<tr> <tr>
<td> <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_edit', user_email=user.address) }}" 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_delete', user_email=user.address) }}" onclick="return confirm('Are you sure?')" title="Delete"><i class="fa fa-trash"></i></a>
</td> </td>
<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_settings', user_email=user.address) }}" 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_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.get_id()) }}" title="Auto-reply"><i class="fa fa-plane"></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.get_id()) }}" title="Fetched accounts"><i class="fa fa-download"></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>
<td>{{ user }}</td> <td>{{ user }}</td>
<td> <td>

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

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

Loading…
Cancel
Save