Commit bc0abb2c authored by Gabriel Detraz's avatar Gabriel Detraz

Renomme le modèle des addresses mails

parent 43fb83ec
......@@ -596,12 +596,12 @@ class WhitelistSerializer(NamespacedHMSerializer):
fields = ('user', 'raison', 'date_start', 'date_end', 'active', 'api_url')
class LocalEmailAccountSerializer(NamespacedHMSerializer):
"""Serialize `users.models.LocalEmailAccount` objects.
class EMailAddressSerializer(NamespacedHMSerializer):
"""Serialize `users.models.EMailAddress` objects.
"""
class Meta:
model = users.LocalEmailAccount
model = users.EMailAddress
fields = ('user', 'local_part', 'complete_email_address', 'api_url')
......@@ -627,7 +627,7 @@ class ServiceRegenSerializer(NamespacedHMSerializer):
class LocalEmailUsersSerializer(NamespacedHMSerializer):
local_email_accounts = LocalEmailAccountSerializer(
email_address = EMailAddressSerializer(
read_only=True,
many=True
)
......@@ -635,7 +635,7 @@ class LocalEmailUsersSerializer(NamespacedHMSerializer):
class Meta:
model = users.User
fields = ('local_email_enabled', 'local_email_redirect',
'local_email_accounts')
'email_address')
# DHCP
......
......@@ -93,7 +93,7 @@ router.register_viewset(r'users/listright', views.ListRightViewSet)
router.register_viewset(r'users/shell', views.ShellViewSet, base_name='shell')
router.register_viewset(r'users/ban', views.BanViewSet)
router.register_viewset(r'users/whitelist', views.WhitelistViewSet)
router.register_viewset(r'users/localemailaccount', views.LocalEmailAccountViewSet)
router.register_viewset(r'users/emailaddress', views.EMailAddressViewSet)
# SERVICE REGEN
router.register_viewset(r'services/regen', views.ServiceRegenViewSet, base_name='serviceregen')
# DHCP
......
......@@ -469,19 +469,19 @@ class WhitelistViewSet(viewsets.ReadOnlyModelViewSet):
serializer_class = serializers.WhitelistSerializer
class LocalEmailAccountViewSet(viewsets.ReadOnlyModelViewSet):
"""Exposes list and details of `users.models.LocalEmailAccount` objects.
class EMailAddressViewSet(viewsets.ReadOnlyModelViewSet):
"""Exposes list and details of `users.models.EMailAddress` objects.
"""
serializer_class = serializers.LocalEmailAccountSerializer
queryset = users.LocalEmailAccount.objects.none()
serializer_class = serializers.EMailAddressSerializer
queryset = users.EMailAddress.objects.none()
def get_queryset(self):
if preferences.OptionalUser.get_cached_value(
'local_email_accounts_enabled'):
return (users.LocalEmailAccount.objects
return (users.EMailAddress.objects
.filter(user__local_email_enabled=True))
else:
return users.LocalEmailAccount.objects.none()
return users.EMailAddress.objects.none()
# SERVICE REGEN
......
......@@ -24,7 +24,7 @@ class Migration(migrations.Migration):
),
migrations.AddField(
model_name='optionaluser',
name='max_local_email_accounts',
field=models.IntegerField(default=15, help_text='Maximum number of local email accounts for a standard user'),
name='max_email_address',
field=models.IntegerField(default=15, help_text='Maximum number of local email address for a standard user'),
),
]
......@@ -93,9 +93,9 @@ class OptionalUser(AclMixin, PreferencesModel):
default = "@example.org",
help_text="Domain to use for local email accounts",
)
max_local_email_accounts = models.IntegerField(
max_email_address = models.IntegerField(
default = 15,
help_text = "Maximum number of local email accounts for a standard user"
help_text = "Maximum number of local email address for a standard user"
)
class Meta:
......
......@@ -66,8 +66,8 @@ with this program; if not, write to the Free Software Foundation, Inc.,
<td>{{ useroptions.local_email_domain }}</td>
</tr>
<tr>
<th>Nombre de comptes maximum</th>
<td>{{ useroptions.max_local_email_accounts }}</td>
<th>Nombre d'alias mail max</th>
<td>{{ useroptions.max_email_address }}</td>
</tr>
</table>
......
......@@ -34,7 +34,7 @@ from reversion.admin import VersionAdmin
from .models import (
User,
LocalEmailAccount,
EMailAddress,
ServiceUser,
School,
ListRight,
......@@ -109,7 +109,7 @@ class BanAdmin(VersionAdmin):
pass
class LocalEmailAccountAdmin(VersionAdmin):
class EMailAddressAdmin(VersionAdmin):
"""Gestion des alias mail"""
pass
......@@ -219,7 +219,7 @@ admin.site.register(School, SchoolAdmin)
admin.site.register(ListRight, ListRightAdmin)
admin.site.register(ListShell, ListShellAdmin)
admin.site.register(Ban, BanAdmin)
admin.site.register(LocalEmailAccount, LocalEmailAccountAdmin)
admin.site.register(EMailAddress, EMailAddressAdmin)
admin.site.register(Whitelist, WhitelistAdmin)
admin.site.register(Request, RequestAdmin)
# Now register the new UserAdmin...
......
......@@ -53,7 +53,7 @@ from .models import (
School,
ListRight,
Whitelist,
LocalEmailAccount,
EMailAddress,
ListShell,
Ban,
Adherent,
......@@ -591,16 +591,16 @@ class WhitelistForm(FormRevMixin, ModelForm):
widgets = {'date_end':DateTimePicker}
class LocalEmailAccountForm(FormRevMixin, ModelForm):
"""Create and edit a local email account"""
class EMailAddressForm(FormRevMixin, ModelForm):
"""Create and edit a local email address"""
def __init__(self, *args, **kwargs):
prefix = kwargs.pop('prefix', self.Meta.model.__name__)
super(LocalEmailAccountForm, self).__init__(*args, prefix=prefix, **kwargs)
super(EMailAddressForm, self).__init__(*args, prefix=prefix, **kwargs)
self.fields['local_part'].label = "Local part of the email"
self.fields['local_part'].help_text = "Can't contain @"
class Meta:
model = LocalEmailAccount
model = EMailAddress
exclude = ['user']
......
......@@ -10,21 +10,21 @@ import re2o.mixins
class Migration(migrations.Migration):
def create_initial_local_email_account(apps, schema_editor):
def create_initial_email_address(apps, schema_editor):
db_alias = schema_editor.connection.alias
User = apps.get_model("users", "User")
LocalEmailAccount = apps.get_model("users", "LocalEmailAccount")
EMailAddress = apps.get_model("users", "EMailAddress")
users = User.objects.using(db_alias).all()
for user in users:
LocalEmailAccount.objects.using(db_alias).create(
EMailAddress.objects.using(db_alias).create(
local_part=user.pseudo,
user=user
)
def delete_all_local_email_accounts(apps, schema_editor):
def delete_all_email_address(apps, schema_editor):
db_alias = schema_editor.connection.alias
LocalEmailAccount = apps.get_model("users", "LocalEmailAccount")
LocalEmailAccount.objects.using(db_alias).delete()
EMailAddress = apps.get_model("users", "EMailAddress")
EMailAddress.objects.using(db_alias).delete()
dependencies = [
('users', '0072_auto_20180426_2021'),
......@@ -32,14 +32,14 @@ class Migration(migrations.Migration):
operations = [
migrations.CreateModel(
name='LocalEmailAccount',
name='EMailAddress',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('local_part', models.CharField(help_text="Local part of the email address", max_length=128, unique=True)),
('user', models.ForeignKey(help_text='User of the local email', on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
],
bases=(re2o.mixins.RevMixin, re2o.mixins.AclMixin, models.Model),
options={'permissions': (('view_localemailaccount', 'Can see a local email account object'),), 'verbose_name': 'Local email account', 'verbose_name_plural': 'Local email accounts'},
options={'permissions': (('view_emailaddress', 'Can see a local email account object'),), 'verbose_name': 'Local email account', 'verbose_name_plural': 'Local email accounts'},
),
migrations.AddField(
model_name='user',
......@@ -51,8 +51,7 @@ class Migration(migrations.Migration):
name='local_email_redirect',
field=models.BooleanField(default=False, help_text='Whether or not to redirect the local email messages to the main email.'),
),
migrations.RunPython(create_initial_local_email_account,
delete_all_local_email_accounts),
),
migrations.RunPython(create_initial_email_address,
delete_all_email_address),
]
......@@ -683,11 +683,11 @@ class User(RevMixin, FieldPermissionModelMixin, AbstractBaseUser,
return
@cached_property
def local_email_accounts(self):
def email_address(self):
if (OptionalUser.get_cached_value('local_email_accounts_enabled')
and self.local_email_enabled):
return self.localemailaccount_set.all()
return LocalEmailAccount.objects.none()
return self.emailaddress_set.all()
return EMailAddress.objects.none()
def get_next_domain_name(self):
"""Look for an available name for a new interface for
......@@ -935,7 +935,7 @@ class User(RevMixin, FieldPermissionModelMixin, AbstractBaseUser,
def clean(self, *args, **kwargs):
"""Check if this pseudo is already used by any mailalias.
Better than raising an error in post-save and catching it"""
if (LocalEmailAccount.objects
if (EMailAddress.objects
.filter(local_part=self.pseudo)
.exclude(user=self)):
raise ValidationError("This pseudo is already in use.")
......@@ -1066,7 +1066,7 @@ def user_post_save(**kwargs):
Synchronise le ldap"""
is_created = kwargs['created']
user = kwargs['instance']
LocalEmailAccount.objects.get_or_create(local_part=user.pseudo, user=user)
EMailAddress.objects.get_or_create(local_part=user.pseudo, user=user)
if is_created:
user.notif_inscription()
user.state_sync()
......@@ -1648,7 +1648,7 @@ class LdapServiceUserGroup(ldapdb.models.Model):
return self.name
class LocalEmailAccount(RevMixin, AclMixin, models.Model):
class EMailAddress(RevMixin, AclMixin, models.Model):
"""Defines a local email account for a user
"""
user = models.ForeignKey(
......@@ -1664,7 +1664,7 @@ class LocalEmailAccount(RevMixin, AclMixin, models.Model):
class Meta:
permissions = (
("view_localemailaccount", "Can see a local email account object"),
("view_emailaddress", "Can see a local email account object"),
)
verbose_name = "Local email account"
verbose_name_plural = "Local email accounts"
......@@ -1678,7 +1678,7 @@ class LocalEmailAccount(RevMixin, AclMixin, models.Model):
@staticmethod
def can_create(user_request, userid, *_args, **_kwargs):
"""Check if a user can create a `LocalEmailAccount` object.
"""Check if a user can create a `EMailAddress` object.
Args:
user_request: The user who wants to create the object.
......@@ -1688,15 +1688,15 @@ class LocalEmailAccount(RevMixin, AclMixin, models.Model):
a message and a boolean which is True if the user can create
a local email account.
"""
if user_request.has_perm('users.add_localemailaccount'):
if user_request.has_perm('users.add_emailaddress'):
return True, None
if not OptionalUser.get_cached_value('local_email_accounts_enabled'):
return False, "The local email accounts are not enabled."
if int(user_request.id) != int(userid):
return False, "You don't have the right to add a local email account to another user."
elif user_request.local_email_accounts.count() >= OptionalUser.get_cached_value('max_local_email_accounts'):
elif user_request.email_address.count() >= OptionalUser.get_cached_value('max_email_address'):
return False, "You have reached the limit of {} local email account.".format(
OptionalUser.get_cached_value('max_local_email_accounts')
OptionalUser.get_cached_value('max_email_address')
)
return True, None
......@@ -1710,7 +1710,7 @@ class LocalEmailAccount(RevMixin, AclMixin, models.Model):
a message and a boolean which is True if the user can see
the local email account.
"""
if user_request.has_perm('users.view_localemailaccount'):
if user_request.has_perm('users.view_emailaddress'):
return True, None
if not OptionalUser.get_cached_value('local_email_accounts_enabled'):
return False, "The local email accounts are not enabled."
......@@ -1731,7 +1731,7 @@ class LocalEmailAccount(RevMixin, AclMixin, models.Model):
if self.local_part == self.user.pseudo:
return False, ("You cannot delete a local email account whose "
"local part is the same as the username.")
if user_request.has_perm('users.delete_localemailaccount'):
if user_request.has_perm('users.delete_emailaddress'):
return True, None
if not OptionalUser.get_cached_value('local_email_accounts_enabled'):
return False, "The local email accounts are not enabled."
......@@ -1753,7 +1753,7 @@ class LocalEmailAccount(RevMixin, AclMixin, models.Model):
if self.local_part == self.user.pseudo:
return False, ("You cannot edit a local email account whose "
"local part is the same as the username.")
if user_request.has_perm('users.change_localemailaccount'):
if user_request.has_perm('users.change_emailaddress'):
return True, None
if not OptionalUser.get_cached_value('local_email_accounts_enabled'):
return False, "The local email accounts are not enabled."
......@@ -1765,5 +1765,5 @@ class LocalEmailAccount(RevMixin, AclMixin, models.Model):
def clean(self, *args, **kwargs):
if "@" in self.local_part:
raise ValidationError("The local part cannot contain a @")
super(LocalEmailAccount, self).clean(*args, **kwargs)
super(EMailAddress, self).clean(*args, **kwargs)
......@@ -25,32 +25,32 @@ with this program; if not, write to the Free Software Foundation, Inc.,
{% load acl %}
{% load logs_extra %}
{% if localemailaccount_list.paginator %}
{% include "pagination.html" with list=alias_list %}
{% if emailaddress_list.paginator %}
{% include "pagination.html" with list=emailaddress_list %}
{% endif %}
<table class="table table-striped">
<thead>
<tr>
<th>Local email address</th>
<th>Email address</th>
<th></th>
</tr>
</thead>
{% for localemailaccount in localemailaccount_list %}
<td>{{ localemailaccount.complete_email_address }}</td>
{% for emailaddress in emailaddress_list %}
<td>{{ emailaddress.complete_email_address }}</td>
<td class="text-right">
{% can_delete localemailaccount %}
{% include 'buttons/suppr.html' with href='users:del-localemailaccount' id=localemailaccount.id %}
{% can_delete emailaddress %}
{% include 'buttons/suppr.html' with href='users:del-emailaddress' id=emailaddress.id %}
{% acl_end %}
{% can_edit localemailaccount %}
{% include 'buttons/edit.html' with href='users:edit-localemailaccount' id=localemailaccount.id %}
{% can_edit emailaddress %}
{% include 'buttons/edit.html' with href='users:edit-emailaddress' id=emailaddress.id %}
{% acl_end %}
{% history_button localemailaccount %}
{% history_button emailaddress %}
</td>
</tr>
{% endfor %}
</table>
{% if localemailaccount_list.paginator %}
{% include "pagination.html" with list=alias_list %}
{% if emailaddress_list.paginator %}
{% include "pagination.html" with list=emailaddress_list %}
{% endif %}
......@@ -29,6 +29,6 @@ with this program; if not, write to the Free Software Foundation, Inc.,
{% block content %}
<h2>Local email accounts</h2>
{% include "users/aff_localemailaccounts.html" with localemailaccount_list=localemailaccount_list %}
{% include "users/aff_emailaddress.html" with emailaddress_list=emailaddress_list %}
{% endblock %}
......@@ -433,18 +433,18 @@ with this program; if not, write to the Free Software Foundation, Inc.,
<th>Enable the local email account</th>
<td>{{ users.local_email_enabled | tick }}</td>
<th>Enable the local email redirection</th>
<td>{{ users.local_email_redirect | tick }}</td>
</tr>
</table>
</div>
{% if users.local_email_enabled %}
{% can_create LocalEmailAccount users.id %}
<a class="btn btn-primary btn-sm" role="button" href="{% url 'users:add-localemailaccount' users.id %}">
<i class="fa fa-plus-square"></i> Add a local email account
<td>{{ users.local_email_redirect | tick }}</td>
</tr>
</table>
</div>
{% if users.local_email_enabled %}
{% can_create EMailAddress users.id %}
<a class="btn btn-primary btn-sm" role="button" href="{% url 'users:add-emailaddress' users.id %}">
<i class="fa fa-plus-square"></i> Add an email address
</a>
{% acl_end %}
{% if localemailaccount_list %}
{% include "users/aff_localemailaccounts.html" with localemailaccount_list=localemailaccount_list %}
{% if emailaddress_list %}
{% include "users/aff_emailaddress.html" with emailaddress_list=emailaddress_list %}
{% endif %}
{% endif %}
{% else %}
......
......@@ -64,15 +64,15 @@ urlpatterns = [
url(r'^del_whitelist/(?P<whitelistid>[0-9]+)$',
views.del_whitelist,
name='del-whitelist'),
url(r'^add_localemailaccount/(?P<userid>[0-9]+)$',
views.add_localemailaccount,
name='add-localemailaccount'),
url(r'^edit_localemailaccount/(?P<localemailaccountid>[0-9]+)$',
views.edit_localemailaccount,
name='edit-localemailaccount'),
url(r'^del_localemailaccount/(?P<localemailaccountid>[0-9]+)$',
views.del_localemailaccount,
name='del-localemailaccount'),
url(r'^add_emailaddress/(?P<userid>[0-9]+)$',
views.add_emailaddress,
name='add-emailaddress'),
url(r'^edit_emailaddress/(?P<emailaddressid>[0-9]+)$',
views.edit_emailaddress,
name='edit-emailaddress'),
url(r'^del_emailaddress/(?P<emailaddressid>[0-9]+)$',
views.del_emailaddress,
name='del-emailaddress'),
url(r'^edit_email_settings/(?P<userid>[0-9]+)$',
views.edit_email_settings,
name='edit-email-settings'),
......
......@@ -81,12 +81,12 @@ from .models import (
Adherent,
Club,
ListShell,
LocalEmailAccount,
EMailAddress,
)
from .forms import (
BanForm,
WhitelistForm,
LocalEmailAccountForm,
EMailAddressForm,
EmailSettingsForm,
DelSchoolForm,
DelListRightForm,
......@@ -496,24 +496,24 @@ def del_whitelist(request, whitelist, **_kwargs):
@login_required
@can_create(LocalEmailAccount)
@can_create(EMailAddress)
@can_edit(User)
def add_localemailaccount(request, user, userid):
def add_emailaddress(request, user, userid):
""" Create a new local email account"""
localemailaccount_instance = LocalEmailAccount(user=user)
localemailaccount = LocalEmailAccountForm(
emailaddress_instance = EMailAddress(user=user)
emailaddress = EMailAddressForm(
request.POST or None,
instance=localemailaccount_instance
instance=emailaddress_instance
)
if localemailaccount.is_valid():
localemailaccount.save()
if emailaddress.is_valid():
emailaddress.save()
messages.success(request, "Local email account created")
return redirect(reverse(
'users:profil',
kwargs={'userid': str(userid)}
))
return form(
{'userform': localemailaccount,
{'userform': emailaddress,
'showCGU': False,
'action_name': 'Add a local email account'},
'users/user.html',
......@@ -522,23 +522,23 @@ def add_localemailaccount(request, user, userid):
@login_required
@can_edit(LocalEmailAccount)
def edit_localemailaccount(request, localemailaccount_instance, **_kwargs):
@can_edit(EMailAddress)
def edit_emailaddress(request, emailaddress_instance, **_kwargs):
""" Edit a local email account"""
localemailaccount = LocalEmailAccountForm(
emailaddress = EMailAddressForm(
request.POST or None,
instance=localemailaccount_instance
instance=emailaddress_instance
)
if localemailaccount.is_valid():
if localemailaccount.changed_data:
localemailaccount.save()
if emailaddress.is_valid():
if emailaddress.changed_data:
emailaddress.save()
messages.success(request, "Local email account modified")
return redirect(reverse(
'users:profil',
kwargs={'userid': str(localemailaccount_instance.user.id)}
kwargs={'userid': str(emailaddress_instance.user.id)}
))
return form(
{'userform': localemailaccount,
{'userform': emailaddress,
'showCGU': False,
'action_name': 'Edit a local email account'},
'users/user.html',
......@@ -547,18 +547,18 @@ def edit_localemailaccount(request, localemailaccount_instance, **_kwargs):
@login_required
@can_delete(LocalEmailAccount)
def del_localemailaccount(request, localemailaccount, **_kwargs):
@can_delete(EMailAddress)
def del_emailaddress(request, emailaddress, **_kwargs):
"""Delete a local email account"""
if request.method == "POST":
localemailaccount.delete()
emailaddress.delete()
messages.success(request, "Local email account deleted")
return redirect(reverse(
'users:profil',
kwargs={'userid': str(localemailaccount.user.id)}
kwargs={'userid': str(emailaddress.user.id)}
))
return form(
{'objet': localemailaccount, 'objet_name': 'localemailaccount'},
{'objet': emailaddress, 'objet_name': 'emailaddress'},
'users/delete.html',
request
)
......@@ -1013,7 +1013,7 @@ def profil(request, users, **_kwargs):
'user_solde': user_solde,
'solde_activated': Paiement.objects.filter(is_balance=True).exists(),
'asso_name': AssoOption.objects.first().name,
'localemailaccount_list': users.local_email_accounts,
'emailaddress_list': users.email_address,
'local_email_accounts_enabled': (
OptionalUser.objects.first().local_email_accounts_enabled
)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment