Alias management.
authorRadek Czajka <rczajka@rczajka.pl>
Fri, 5 Apr 2019 12:43:02 +0000 (14:43 +0200)
committerRadek Czajka <rczajka@rczajka.pl>
Fri, 5 Apr 2019 12:43:02 +0000 (14:43 +0200)
12 files changed:
src/accounts/templates/account/base.html
src/cas/urls.py
src/emails/__init__.py
src/emails/models.py
src/emails/templates/emails/base.html [new file with mode: 0644]
src/emails/templates/emails/list_aliases.html [new file with mode: 0644]
src/emails/templates/emails/my_aliases.html [new file with mode: 0644]
src/emails/templatetags/__init__.py [new file with mode: 0644]
src/emails/templatetags/emails.py [new file with mode: 0644]
src/emails/urls.py [new file with mode: 0644]
src/emails/views.py [new file with mode: 0644]
src/services/templatetags/services.py

index 9cdc8ee..0b7cd3c 100644 (file)
@@ -1,6 +1,7 @@
 {% extends "base.html" %}
 {% load gravatar i18n %}
 {% load use_ssh from services %}
 {% extends "base.html" %}
 {% load gravatar i18n %}
 {% load use_ssh from services %}
+{% load use_email from emails %}
 
 {% block content %}
   <aside class="menu">
 
 {% block content %}
   <aside class="menu">
     {% block menu %}
       <p><a {% if menu == 'profile' %}class="active"{% endif %} href="{% url 'accounts_profile' %}">{% trans "Your profile" %}</a></p>
       <p><a {% if menu == 'password' %}class="active"{% endif %} href="{% url 'password_change' %}">{% trans "Password change" %}</a></p>
     {% block menu %}
       <p><a {% if menu == 'profile' %}class="active"{% endif %} href="{% url 'accounts_profile' %}">{% trans "Your profile" %}</a></p>
       <p><a {% if menu == 'password' %}class="active"{% endif %} href="{% url 'password_change' %}">{% trans "Password change" %}</a></p>
-      <!--p><a {% if menu == 'email' %}class="active"{% endif %} href="">{% trans "E-mail" %}</a></p-->
+      {% use_email as use_email %}
+      {% if use_email %}
+      <p><a {% if menu == 'email' %}class="active"{% endif %} href="{% url 'emails' %}">{% trans "E-mail" %}</a></p>
+      {% endif %}
       {% use_ssh as use_ssh %}
       {% if use_ssh %}
         <p><a {% if menu == 'ssh' %}class="active"{% endif %} href="{% url 'ssh_keys' %}">{% trans "SSH keys" %}</a></p>
       {% use_ssh as use_ssh %}
       {% if use_ssh %}
         <p><a {% if menu == 'ssh' %}class="active"{% endif %} href="{% url 'ssh_keys' %}">{% trans "SSH keys" %}</a></p>
index f3785ac..6f7a09e 100644 (file)
@@ -18,6 +18,7 @@ urlpatterns = [
     path('admin/', admin.site.urls),
 
     path('accounts/', include('accounts.urls')),
     path('admin/', admin.site.urls),
 
     path('accounts/', include('accounts.urls')),
+    path('email/', include('emails.urls')),
     path('services/', include('services.urls')),
     path('ssh/', include('ssh_keys.urls')),
     path('auth/', include('django.contrib.auth.urls')),
     path('services/', include('services.urls')),
     path('ssh/', include('ssh_keys.urls')),
     path('auth/', include('django.contrib.auth.urls')),
index e69de29..7b19d0e 100644 (file)
@@ -0,0 +1,4 @@
+from django.conf import settings
+
+
+BASE_DOMAINS = getattr(settings, 'EMAILS_BASE_DOMAINS', [])
index 988b5a1..b666162 100644 (file)
@@ -1,5 +1,7 @@
 from django.db import models
 from django.utils.translation import ugettext_lazy as _
 from django.db import models
 from django.utils.translation import ugettext_lazy as _
+from . import BASE_DOMAINS
+
 
 
 class Alias(models.Model):
 
 
 class Alias(models.Model):
@@ -18,3 +20,13 @@ class Alias(models.Model):
 
     def __str__(self):
         return '{} -> {}'.format(self.source, self.destination)
 
     def __str__(self):
         return '{} -> {}'.format(self.source, self.destination)
+
+    @classmethod
+    def get_from_user(cls, user):
+        lookups = ["{}@{}".format(user.username, domain) for domain in BASE_DOMAINS]
+        return cls.objects.filter(source__in=lookups)
+
+    @classmethod
+    def get_to_user(cls, user):
+        lookups = ["{}@{}".format(user.username, domain) for domain in BASE_DOMAINS]
+        return cls.objects.filter(destination__in=lookups)
diff --git a/src/emails/templates/emails/base.html b/src/emails/templates/emails/base.html
new file mode 100644 (file)
index 0000000..696c8e9
--- /dev/null
@@ -0,0 +1,8 @@
+{% extends "account/base.html" %}
+
+
+{% block menu %}
+  {% with menu='email' %}
+    {{ block.super }}
+  {% endwith %}
+{% endblock %}
diff --git a/src/emails/templates/emails/list_aliases.html b/src/emails/templates/emails/list_aliases.html
new file mode 100644 (file)
index 0000000..7d841d5
--- /dev/null
@@ -0,0 +1,13 @@
+{% load list_aliases from emails %}
+<ul>
+       {% for alias in aliases %}
+       <li>
+               {% if admin_links %}
+               <a href="{% url 'admin:emails_alias_changelist' %}?source__exact={{ alias }}">
+               {% endif %}
+               {{ alias }}
+               {% if admin_links %}</a>{% endif %}
+               {% list_aliases alias %}
+       </li>
+       {% endfor %}
+</ul>
diff --git a/src/emails/templates/emails/my_aliases.html b/src/emails/templates/emails/my_aliases.html
new file mode 100644 (file)
index 0000000..a2d234b
--- /dev/null
@@ -0,0 +1,28 @@
+{% extends "emails/base.html" %}
+{% load i18n %}
+{% load list_aliases from emails %}
+
+
+{% block accounts-content %}
+  <h1>{% trans "E-mail" %}</h1>
+
+  <h2>{% trans "Default addresses" %}:</h2>
+  <ul>
+  {% for a in base %}
+  <li>{{ a }}</li>
+  {% endfor %}
+  </ul>
+
+  <h2>{% trans "Aliases" %}:</h2>
+  {% list_aliases base %}
+
+  {% if from_me %}
+  <h2>{% trans "E-mail forwarded to" %}:</h2>
+  <ul>
+  {% for alias in from_me %}
+  <li>{{ alias.destination }}</li>
+  {% endfor %}
+  </ul>
+  {% endif %}
+
+{% endblock %}
diff --git a/src/emails/templatetags/__init__.py b/src/emails/templatetags/__init__.py
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/src/emails/templatetags/emails.py b/src/emails/templatetags/emails.py
new file mode 100644 (file)
index 0000000..91cafd2
--- /dev/null
@@ -0,0 +1,24 @@
+from django.template import Library
+from emails.models import Alias
+
+
+register = Library()
+
+
+@register.simple_tag(takes_context=True)
+def use_email(context):
+    user = context['request'].user
+    if user.is_anonymous: return False
+    return user.groups.filter(name='e-mail').exists()
+
+
+@register.inclusion_tag('emails/list_aliases.html', takes_context=True)
+def list_aliases(context, emails):
+    if isinstance(emails, str):
+        emails = [emails]
+    admin_links = context.get('admin_links', context['request'].user.has_perm('emails.can_change_alias'))
+    return {
+        "admin_links": admin_links,
+        "aliases": [a.source for a in Alias.objects.filter(destination__in=emails)],
+        "request": context['request'],
+    }
diff --git a/src/emails/urls.py b/src/emails/urls.py
new file mode 100644 (file)
index 0000000..9e385e0
--- /dev/null
@@ -0,0 +1,7 @@
+from django.urls import path
+from . import views
+
+
+urlpatterns = [
+    path('', views.my_aliases, name='emails')
+]
diff --git a/src/emails/views.py b/src/emails/views.py
new file mode 100644 (file)
index 0000000..16fa837
--- /dev/null
@@ -0,0 +1,13 @@
+from django.contrib.auth.decorators import login_required
+from django.shortcuts import render
+from .models import Alias
+from . import BASE_DOMAINS
+
+
+@login_required
+def my_aliases(request):
+    return render(request, 'emails/my_aliases.html', {
+        'base': ['{}@{}'.format(request.user.username, dom) for dom in BASE_DOMAINS],
+        'to_me': Alias.get_to_user(request.user),
+        'from_me': Alias.get_from_user(request.user),
+        })
index 959e0f8..307d565 100644 (file)
@@ -8,5 +8,5 @@ register = Library()
 @register.simple_tag(takes_context=True)
 def use_ssh(context):
     user = context['request'].user
 @register.simple_tag(takes_context=True)
 def use_ssh(context):
     user = context['request'].user
-    if user.is_anonymous: return True
+    if user.is_anonymous: return False
     return Service.for_user(user).filter(uses_ssh=True).exists()
     return Service.for_user(user).filter(uses_ssh=True).exists()