Compare commits
52 Commits
master
...
dev/github
Author | SHA1 | Date |
---|---|---|
Santiago L | a169bc60b8 | |
Santiago L | ebe8e95a75 | |
Santiago L | da29e86860 | |
Santiago L | 8079de4e76 | |
Santiago L | 9957b4ebeb | |
Santiago L | b6852348eb | |
Cayo Puigdefabregas | b2f3bcc617 | |
Cayo Puigdefabregas | c16d067bd8 | |
Cayo Puigdefabregas | 950e04df92 | |
Cayo Puigdefabregas | 091530bfd4 | |
Cayo Puigdefabregas | 22b95b5b51 | |
Cayo Puigdefabregas | e7c037ce72 | |
Cayo Puigdefabregas | 20e9c14524 | |
Cayo Puigdefabregas | d22aebf68a | |
Cayo Puigdefabregas | 6e03d7bd54 | |
Cayo Puigdefabregas | fc74db4a76 | |
Cayo Puigdefabregas | e5beae6360 | |
Cayo Puigdefabregas | 57688426b6 | |
Cayo Puigdefabregas | 1f57cdb48d | |
Santiago L | f87ad48b7c | |
Cayo Puigdefabregas | 481515363b | |
Cayo Puigdefabregas | e5ca77f018 | |
Cayo Puigdefabregas | b05481c662 | |
Cayo Puigdefabregas | 2d1cd175ee | |
Santiago L | 4536d651ec | |
Cayo Puigdefabregas | a6f829e66c | |
Cayo Puigdefabregas | 824bc7f8cd | |
Cayo Puigdefabregas | 4286c4f77a | |
Cayo Puigdefabregas | 7bee5facbc | |
Cayo Puigdefabregas | 153f869f0d | |
Cayo Puigdefabregas | 88193de18a | |
Cayo Puigdefabregas | 3f5ed20926 | |
Cayo Puigdefabregas | 28001247d2 | |
Cayo Puigdefabregas | d6e94fbc5d | |
Cayo Puigdefabregas | c6f8e2cf61 | |
Cayo Puigdefabregas | 9ebce376ec | |
Cayo Puigdefabregas | 8ad269357f | |
Cayo Puigdefabregas | 550c4db74e | |
Cayo Puigdefabregas | 2497d31c49 | |
Cayo Puigdefabregas | cc1a2622c5 | |
Cayo Puigdefabregas | 05a2c4078a | |
Cayo Puigdefabregas | 13210c332e | |
Cayo Puigdefabregas | df9e413ece | |
Cayo Puigdefabregas | af3f9058af | |
Cayo Puigdefabregas | 25112f20ea | |
Cayo Puigdefabregas | 4ec22e4e36 | |
Cayo Puigdefabregas | 38329f84df | |
Cayo Puigdefabregas | e5b7f03347 | |
Cayo Puigdefabregas | e1cbc385d0 | |
Cayo Puigdefabregas | d6d3aabd92 | |
Cayo Puigdefabregas | 15d3c4feff | |
Cayo Puigdefabregas | 5df58ff3e6 |
|
@ -0,0 +1,5 @@
|
|||
SECRET_KEY=k_=*vfue(^campsl63)7w5m&cu9u4o4-!vaw94qzyrymyv0hgg
|
||||
DEBUG=True
|
||||
ALLOWED_HOSTS=.localhost,127.0.0.1
|
||||
DATABASE_URL=postgres://USER:PASSWORD@HOST:PORT/NAME
|
||||
STATIC_ROOT=PATH_TO_STATIC_ROOT
|
|
@ -0,0 +1,76 @@
|
|||
name: Django CI
|
||||
|
||||
on:
|
||||
push:
|
||||
branches: [ master ]
|
||||
pull_request:
|
||||
|
||||
jobs:
|
||||
build:
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
# Service containers to run with `container-job`
|
||||
services:
|
||||
# Label used to access the service container
|
||||
postgres:
|
||||
# Docker Hub image
|
||||
image: postgres
|
||||
ports:
|
||||
- 5432:5432
|
||||
# Provide the password for postgres
|
||||
env:
|
||||
POSTGRES_DB: test_myapp
|
||||
POSTGRES_USER: testuser
|
||||
POSTGRES_PASSWORD: s3cretPass
|
||||
# Set health checks to wait until postgres has started
|
||||
options: >-
|
||||
--health-cmd pg_isready
|
||||
--health-interval 10s
|
||||
--health-timeout 5s
|
||||
--health-retries 5
|
||||
strategy:
|
||||
max-parallel: 4
|
||||
matrix:
|
||||
python-version: [3.6]
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- name: Set up Python ${{ matrix.python-version }}
|
||||
uses: actions/setup-python@v1
|
||||
with:
|
||||
python-version: ${{ matrix.python-version }}
|
||||
- name: Install dependencies
|
||||
run: |
|
||||
sudo apt-get update -qy
|
||||
sudo apt-get -y install python3-dev libxml2 libxml2-dev libxslt-dev bind9utils ca-certificates gettext libcrack2-dev libxml2-dev libxslt1-dev ssh-client wget xvfb zlib1g-dev git iceweasel dnsutils postgresql-contrib libgirepository1.0-dev
|
||||
python -m pip install --upgrade pip
|
||||
pip install wheel
|
||||
pip install -r total_requirements.txt
|
||||
pip install -e .
|
||||
- name: Lint with flake8
|
||||
run: |
|
||||
# stop the build if there are Python syntax errors or undefined names
|
||||
# flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
|
||||
# exit-zero treats all errors as warnings.
|
||||
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=120 --statistics
|
||||
- name: Run Tests
|
||||
run: |
|
||||
# orchestra-admin startproject panel
|
||||
django-admin.py startproject panel --template=orchestra/conf/ribaguifi_template -v3
|
||||
#python panel/manage.py test orchestra --noinput -v3
|
||||
coverage run --source='orchestra' panel/manage.py test orchestra --noinput -v3
|
||||
coverage report
|
||||
coverage xml
|
||||
|
||||
env:
|
||||
SECRET_KEY: zrhnooq6)sb+0+xb)(o0rvbf5)a(vc8ncv&1&kng@3i_pmx3oy
|
||||
DEBUG: True
|
||||
ALLOWED_HOSTS: .localhost,127.0.0.1
|
||||
DATABASE_URL: postgres://testuser:s3cretPass@localhost:5432/test_myapp
|
||||
POSTGRES_HOST: postgres
|
||||
POSTGRES_PORT: 5432
|
||||
|
||||
- name: Upload coverage to Codecov
|
||||
uses: codecov/codecov-action@v1
|
||||
with:
|
||||
token: ${{ secrets.CODECOV_TOKEN }}
|
|
@ -0,0 +1,70 @@
|
|||
We need have python3.6
|
||||
|
||||
#Install Packages
|
||||
```bash
|
||||
apt=(
|
||||
bind9utils
|
||||
ca-certificates
|
||||
gettext
|
||||
libcrack2-dev
|
||||
libxml2-dev
|
||||
libxslt1-dev
|
||||
ssh-client
|
||||
wget
|
||||
xvfb
|
||||
zlib1g-dev
|
||||
git
|
||||
iceweasel
|
||||
dnsutils
|
||||
postgresql-contrib
|
||||
)
|
||||
sudo apt-get install --no-install-recommends -y ${apt[@]}
|
||||
```
|
||||
|
||||
It is necessary install *wkhtmltopdf*
|
||||
You can install it from https://wkhtmltopdf.org/downloads.html
|
||||
|
||||
Clone this repository
|
||||
```bash
|
||||
git clone https://github.com/ribaguifi/django-orchestra
|
||||
```
|
||||
|
||||
Prepare env and install requirements
|
||||
```bash
|
||||
cd django-orchestra
|
||||
python3.6 -m venv env
|
||||
source env/bin/activate
|
||||
pip3 install --upgrade pip
|
||||
pip3 install -r total_requirements.txt
|
||||
pip3 install -e .
|
||||
```
|
||||
|
||||
Configure project using environment file (you can use provided example as quickstart):
|
||||
```bash
|
||||
cp .env.example .env
|
||||
```
|
||||
|
||||
Prepare your Postgres database (create database, user and grant permissions):
|
||||
```sql
|
||||
CREATE DATABASE myproject;
|
||||
CREATE USER myuser WITH PASSWORD 'password';
|
||||
GRANT ALL PRIVILEGES ON DATABASE myproject TO myuser;
|
||||
```
|
||||
|
||||
Prepare a new project:
|
||||
|
||||
```bash
|
||||
django-admin.py startproject PROJECT_NAME --template="orchestra/conf/ribaguifi_template"
|
||||
```
|
||||
|
||||
Run migrations:
|
||||
```bash
|
||||
python3 manage.py migrate
|
||||
```
|
||||
|
||||
(Optional) You can start a Django development server to check that everything is ok.
|
||||
```bash
|
||||
python3 manage.py runserver
|
||||
```
|
||||
|
||||
Open [http://127.0.0.1:8000/](http://127.0.0.1:8000/) in your browser.
|
|
@ -0,0 +1,41 @@
|
|||
FROM python:3.6
|
||||
|
||||
RUN apt-get -y update
|
||||
RUN pip3 install wheel
|
||||
|
||||
RUN apt-get -y install python3-dev
|
||||
|
||||
RUN apt-get install -y bind9utils ca-certificates gettext libcrack2-dev libxml2-dev\
|
||||
libxslt1-dev ssh-client wget xvfb zlib1g-dev git iceweasel dnsutils postgresql-contrib\
|
||||
curl sudo vim libgirepository1.0-dev
|
||||
|
||||
RUN apt-get clean
|
||||
|
||||
RUN useradd orchestra --shell /bin/bash && \
|
||||
{ echo "orchestra:orchestra" | chpasswd; } && \
|
||||
mkhomedir_helper orchestra && \
|
||||
adduser orchestra sudo
|
||||
|
||||
# RUN echo 'EXPORT $PATH="$PATH:/home/orchestra/.local/bin/"' > /home/orchestra/.bashrc
|
||||
# RUN git clone https://github.com/ribaguifi/django-orchestra.git
|
||||
# RUN orchestra-admin startproject panel
|
||||
# RUN python3 panel/manage.py migrate
|
||||
# RUN python3 panel/manage.py runserver
|
||||
|
||||
# install wkhtmltox
|
||||
RUN apt-get install -y xfonts-75dpi
|
||||
RUN wget https://github.com/wkhtmltopdf/packaging/releases/download/0.12.6-1/wkhtmltox_0.12.6-1.buster_amd64.deb -O /tmp/wkhtmltox.deb
|
||||
RUN dpkg -i /tmp/wkhtmltox.deb
|
||||
|
||||
RUN wget https://github.com/mozilla/geckodriver/releases/download/v0.29.0/geckodriver-v0.29.0-linux64.tar.gz -O /tmp/geckodriver.tar.gz
|
||||
RUN tar -xf /tmp/geckodriver.tar.gz -C /usr/local/bin/
|
||||
|
||||
# install orchestra requirements
|
||||
RUN pip3 install --upgrade pip
|
||||
|
||||
# TODO(@slamora): requirements.txt duplicates ../totaL_requirements.txt
|
||||
# Docker compose security policy forbiddes access to parent folders
|
||||
COPY requirements.txt requirements.txt
|
||||
RUN pip3 install -r requirements.txt
|
||||
|
||||
EXPOSE 8000
|
|
@ -0,0 +1,33 @@
|
|||
# orchestra environment based on docker-compose
|
||||
|
||||
Docker compose environment to develop django-orchestra.
|
||||
|
||||
**NOTE**: On web container, volume `/code` contains the source code of the host.
|
||||
|
||||
1. Build (or rebuild if any change done) the containers:
|
||||
```
|
||||
cd examples/
|
||||
docker-compose build
|
||||
```
|
||||
|
||||
2. Start the containers:
|
||||
```
|
||||
docker-compose up
|
||||
```
|
||||
|
||||
3. Run a bash on `web` container:
|
||||
```
|
||||
docker-compose run web bash
|
||||
```
|
||||
|
||||
4. Run on the web docker container the first time:
|
||||
```
|
||||
su - orchestra
|
||||
bash /code/examples/init_project.sh
|
||||
```
|
||||
|
||||
5. Run tests or do whatever you need:
|
||||
```
|
||||
cd panel
|
||||
python manage.py test --noinput orchestra.contrib.lists.tests.functional_tests.tests.AdminListTest.test_add
|
||||
```
|
|
@ -0,0 +1,4 @@
|
|||
create database orchestra;
|
||||
CREATE USER orchestra WITH PASSWORD 'orchestra';
|
||||
GRANT ALL PRIVILEGES ON DATABASE orchestra TO orchestra;
|
||||
ALTER ROLE orchestra CREATEDB;
|
|
@ -0,0 +1,17 @@
|
|||
version: '3'
|
||||
services:
|
||||
web:
|
||||
build: .
|
||||
ports:
|
||||
- 8000:8000
|
||||
volumes:
|
||||
- ..:/code
|
||||
|
||||
postgres:
|
||||
image: postgres
|
||||
ports:
|
||||
- 5432:5432
|
||||
environment:
|
||||
POSTGRES_DB: orchestra
|
||||
POSTGRES_USER: orchestra
|
||||
POSTGRES_PASSWORD: orchestra
|
|
@ -0,0 +1,5 @@
|
|||
SECRET_KEY=zrhnooq6)sb+0+xb)(o0rvbf5)a(vc8ncv&1&kng@3i_pmx3oy
|
||||
DEBUG=True
|
||||
ALLOWED_HOSTS=.localhost,127.0.0.1
|
||||
DATABASE_URL=postgres://orchestra:orchestra@postgres:5432/orchestra
|
||||
STATIC_ROOT=PATH_TO_STATIC_ROOT
|
|
@ -0,0 +1,27 @@
|
|||
sudo pip3 install -e /code
|
||||
psql -U orchestra -h postgres < /code/examples/createdb.sql
|
||||
|
||||
cd ~
|
||||
django-admin.py startproject panel --template="/code/orchestra/conf/ribaguifi_template"
|
||||
cp /code/examples/env.example panel/.env
|
||||
|
||||
cd panel
|
||||
python3 manage.py setupcronbeat
|
||||
python3 manage.py syncperiodictasks
|
||||
|
||||
sudo apt-get install -y rabbitmq-server
|
||||
sudo python3 manage.py setupcelery --username orchestra
|
||||
|
||||
sudo python3 manage.py setuplog
|
||||
|
||||
python3 manage.py collectstatic --noinput
|
||||
sudo apt-get install -y nginx-full uwsgi uwsgi-plugin-python3
|
||||
sudo python3 manage.py setupnginx --user orchestra
|
||||
|
||||
sudo /etc/init.d/rabbitmq-server start
|
||||
sudo pip uninstall django-celery
|
||||
sudo pip install -r /code/requirements.txt
|
||||
sudo python3 manage.py startservices
|
||||
|
||||
|
||||
# python3 panel/manage.py migrate
|
|
@ -0,0 +1,53 @@
|
|||
Django==1.10.5
|
||||
django-fluent-dashboard==0.6.1
|
||||
django-admin-tools==0.8.0
|
||||
django-extensions==1.7.4
|
||||
django-celery==3.1.17
|
||||
djangorestframework==3.4.7
|
||||
django-celery-email
|
||||
django-debug-toolbar
|
||||
django-cors-headers
|
||||
django-countries
|
||||
django-filter==0.15.2
|
||||
django-flat-theme
|
||||
django-fluent-dashboard
|
||||
django-iban
|
||||
django-localflavor
|
||||
django-multiselectfield
|
||||
django-nose==1.4.4
|
||||
django-reversion
|
||||
django-transaction-signals
|
||||
celery==3.1.23
|
||||
kombu==3.0.35
|
||||
billiard==3.3.0.23
|
||||
Markdown==2.4
|
||||
ecdsa==0.11
|
||||
Pygments==1.6
|
||||
jsonfield==0.9.22
|
||||
python_dateutil
|
||||
requests
|
||||
phonenumbers
|
||||
amqp==1.4.9
|
||||
anyjson
|
||||
pytz
|
||||
cracklib
|
||||
lxml==3.3.5
|
||||
selenium
|
||||
xvfbwrapper
|
||||
freezegun==1.1.0
|
||||
coverage
|
||||
flake8
|
||||
sqlparse
|
||||
pyinotify
|
||||
PyMySQL
|
||||
dj_database_url==0.5.0
|
||||
psycopg2
|
||||
python-decouple
|
||||
https://github.com/glic3rinu/passlib/archive/master.zip
|
||||
paramiko
|
||||
mysqlclient
|
||||
pycrypto==2.6.1
|
||||
pygobject
|
||||
six
|
||||
nose
|
||||
-e git+https://github.com/ribaguifi/orchestra-orm.git#egg=orchestra-orm
|
|
@ -93,7 +93,7 @@ Remember create a database for your project and give permitions for the correct
|
|||
```
|
||||
psql -U postgres
|
||||
psql (12.4)
|
||||
Digite «help» para obtener ayuda.
|
||||
Digite «help».
|
||||
|
||||
postgres=# CREATE database orchesta;
|
||||
postgres=# CREATE USER orchesta WITH PASSWORD 'orquesta';
|
||||
|
|
|
@ -3,7 +3,7 @@ from collections import OrderedDict
|
|||
from functools import update_wrapper
|
||||
|
||||
from django.contrib import admin
|
||||
from django.urls import reverse
|
||||
from django.core.urlresolvers import reverse
|
||||
from django.shortcuts import render, redirect
|
||||
from django.utils.safestring import mark_safe
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
|
@ -56,7 +56,7 @@ def search(request):
|
|||
if service.search:
|
||||
models.add(service.model)
|
||||
model_name_map[service.model._meta.model_name] = service.model
|
||||
|
||||
|
||||
# Account direct access
|
||||
if search_term.endswith('!'):
|
||||
from ..contrib.accounts.models import Account
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
from django.urls import reverse
|
||||
from django.core.urlresolvers import reverse
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
from fluent_dashboard import dashboard, appsettings
|
||||
from fluent_dashboard.modules import CmsAppIconList
|
||||
|
@ -11,7 +11,7 @@ class AppDefaultIconList(CmsAppIconList):
|
|||
def __init__(self, *args, **kwargs):
|
||||
self.icons = kwargs.pop('icons')
|
||||
super(AppDefaultIconList, self).__init__(*args, **kwargs)
|
||||
|
||||
|
||||
def get_icon_for_model(self, app_name, model_name, default=None):
|
||||
icon = self.icons.get('.'.join((app_name, model_name)))
|
||||
return super(AppDefaultIconList, self).get_icon_for_model(app_name, model_name, default=icon)
|
||||
|
@ -19,7 +19,7 @@ class AppDefaultIconList(CmsAppIconList):
|
|||
|
||||
class OrchestraIndexDashboard(dashboard.FluentIndexDashboard):
|
||||
""" Gets application modules from services, accounts and administration registries """
|
||||
|
||||
|
||||
def __init__(self, **kwargs):
|
||||
super(dashboard.FluentIndexDashboard, self).__init__(**kwargs)
|
||||
self.children.append(self.get_personal_module())
|
||||
|
@ -27,7 +27,7 @@ class OrchestraIndexDashboard(dashboard.FluentIndexDashboard):
|
|||
recent_actions = self.get_recent_actions_module()
|
||||
recent_actions.enabled = True
|
||||
self.children.append(recent_actions)
|
||||
|
||||
|
||||
def process_registered_view(self, module, view_name, options):
|
||||
app_name, name = view_name.split('_')[:-1]
|
||||
module.icons['.'.join((app_name, name))] = options.get('icon')
|
||||
|
@ -47,7 +47,7 @@ class OrchestraIndexDashboard(dashboard.FluentIndexDashboard):
|
|||
'title': options.get('verbose_name_plural'),
|
||||
'url': add_url,
|
||||
})
|
||||
|
||||
|
||||
def get_application_modules(self):
|
||||
modules = []
|
||||
# Honor settings override, hacky. I Know
|
||||
|
|
|
@ -5,7 +5,7 @@ from django import forms
|
|||
from django.contrib.admin import helpers
|
||||
from django.core import validators
|
||||
from django.forms.models import modelformset_factory, BaseModelFormSet
|
||||
from django.template import Template
|
||||
from django.template import Template, Context
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
|
||||
from orchestra.forms.widgets import SpanWidget
|
||||
|
@ -28,9 +28,9 @@ class AdminFormMixin(object):
|
|||
' {% include "admin/includes/fieldset.html" %}'
|
||||
'{% endfor %}'
|
||||
)
|
||||
context = {
|
||||
context = Context({
|
||||
'adminform': adminform
|
||||
}
|
||||
})
|
||||
return template.render(context)
|
||||
|
||||
|
||||
|
@ -71,9 +71,9 @@ class AdminFormSet(BaseModelFormSet):
|
|||
</div>
|
||||
</div>""")
|
||||
)
|
||||
context = {
|
||||
context = Context({
|
||||
'formset': self
|
||||
}
|
||||
})
|
||||
return template.render(context)
|
||||
|
||||
|
||||
|
@ -93,7 +93,7 @@ class AdminPasswordChangeForm(forms.Form):
|
|||
required=False, validators=[validate_password])
|
||||
password2 = forms.CharField(label=_("Password (again)"), widget=forms.PasswordInput,
|
||||
required=False)
|
||||
|
||||
|
||||
def __init__(self, user, *args, **kwargs):
|
||||
self.related = kwargs.pop('related', [])
|
||||
self.raw = kwargs.pop('raw', False)
|
||||
|
@ -109,7 +109,7 @@ class AdminPasswordChangeForm(forms.Form):
|
|||
self.fields['password2_%i' % ix] = forms.CharField(label=_("Password (again)"),
|
||||
widget=forms.PasswordInput, required=False)
|
||||
setattr(self, 'clean_password2_%i' % ix, partial(self.clean_password2, ix=ix))
|
||||
|
||||
|
||||
def clean_password2(self, ix=''):
|
||||
if ix != '':
|
||||
ix = '_%i' % ix
|
||||
|
@ -129,7 +129,7 @@ class AdminPasswordChangeForm(forms.Form):
|
|||
code='password_mismatch',
|
||||
)
|
||||
return password2
|
||||
|
||||
|
||||
def clean_password(self, ix=''):
|
||||
if ix != '':
|
||||
ix = '_%i' % ix
|
||||
|
@ -146,14 +146,14 @@ class AdminPasswordChangeForm(forms.Form):
|
|||
code='bad_hash',
|
||||
)
|
||||
return password
|
||||
|
||||
|
||||
def clean(self):
|
||||
if not self.password_provided:
|
||||
raise forms.ValidationError(
|
||||
self.error_messages['password_missing'],
|
||||
code='password_missing',
|
||||
)
|
||||
|
||||
|
||||
def save(self, commit=True):
|
||||
"""
|
||||
Saves the new password.
|
||||
|
@ -182,7 +182,7 @@ class AdminPasswordChangeForm(forms.Form):
|
|||
if commit:
|
||||
rel.save(update_fields=['password'])
|
||||
return self.user
|
||||
|
||||
|
||||
def _get_changed_data(self):
|
||||
data = super().changed_data
|
||||
for name in self.fields.keys():
|
||||
|
@ -202,7 +202,7 @@ class SendEmailForm(forms.Form):
|
|||
widget=forms.TextInput(attrs={'size': '118'}))
|
||||
message = forms.CharField(label=_("Message"),
|
||||
widget=forms.Textarea(attrs={'cols': 118, 'rows': 15}))
|
||||
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
initial = kwargs.get('initial')
|
||||
|
@ -210,7 +210,7 @@ class SendEmailForm(forms.Form):
|
|||
self.fields['to'].widget = SpanWidget(original=initial['to'])
|
||||
else:
|
||||
self.fields.pop('to')
|
||||
|
||||
|
||||
def clean_comma_separated_emails(self, value):
|
||||
clean_value = []
|
||||
for email in value.split(','):
|
||||
|
@ -222,7 +222,7 @@ class SendEmailForm(forms.Form):
|
|||
raise validators.ValidationError("Comma separated email addresses.")
|
||||
clean_value.append(email)
|
||||
return clean_value
|
||||
|
||||
|
||||
def clean_extra_to(self):
|
||||
extra_to = self.cleaned_data['extra_to']
|
||||
return self.clean_comma_separated_emails(extra_to)
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
from copy import deepcopy
|
||||
|
||||
from admin_tools.menu import items, Menu
|
||||
from django.urls import reverse
|
||||
from django.core.urlresolvers import reverse
|
||||
from django.utils.text import capfirst
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
|
||||
|
@ -16,7 +16,7 @@ def api_link(context):
|
|||
opts = context['cl'].opts
|
||||
else:
|
||||
return reverse('api-root')
|
||||
if 'object_id' in context:
|
||||
if 'object_id' in context:
|
||||
object_id = context['object_id']
|
||||
try:
|
||||
return reverse('%s-detail' % opts.model_name, args=[object_id])
|
||||
|
@ -42,7 +42,7 @@ def process_registry(register):
|
|||
item = items.MenuItem(name, url)
|
||||
item.options = options
|
||||
return item
|
||||
|
||||
|
||||
childrens = {}
|
||||
for model, options in register.get().items():
|
||||
if options.get('menu', True):
|
||||
|
@ -68,7 +68,7 @@ def process_registry(register):
|
|||
|
||||
class OrchestraMenu(Menu):
|
||||
template = 'admin/orchestra/menu.html'
|
||||
|
||||
|
||||
def init_with_context(self, context):
|
||||
self.children = [
|
||||
# items.MenuItem(
|
||||
|
|
|
@ -6,11 +6,11 @@ from functools import wraps
|
|||
from django.conf import settings
|
||||
from django.contrib import admin
|
||||
from django.core.exceptions import ObjectDoesNotExist
|
||||
from django.urls import reverse, NoReverseMatch
|
||||
from django.core.urlresolvers import reverse, NoReverseMatch
|
||||
from django.db import models
|
||||
from django.shortcuts import redirect
|
||||
from django.utils import timezone
|
||||
from django.utils.html import escape, format_html
|
||||
from django.utils.html import escape
|
||||
from django.utils.safestring import mark_safe
|
||||
|
||||
from orchestra.models.utils import get_field_value
|
||||
|
@ -113,21 +113,21 @@ def admin_link(*args, **kwargs):
|
|||
return '---'
|
||||
if not getattr(obj, 'pk', None):
|
||||
return '---'
|
||||
display_ = kwargs.get('display')
|
||||
if display_:
|
||||
display_ = getattr(obj, display_, display_)
|
||||
display = kwargs.get('display')
|
||||
if display:
|
||||
display = getattr(obj, display, display)
|
||||
else:
|
||||
display_ = obj
|
||||
display = obj
|
||||
try:
|
||||
url = change_url(obj)
|
||||
except NoReverseMatch:
|
||||
# Does not has admin
|
||||
return str(display_)
|
||||
return str(display)
|
||||
extra = ''
|
||||
if kwargs['popup']:
|
||||
extra = mark_safe('onclick="return showAddAnotherPopup(this);"')
|
||||
extra = 'onclick="return showAddAnotherPopup(this);"'
|
||||
title = "Change %s" % obj._meta.verbose_name
|
||||
return format_html('<a href="{}" title="{}" {}>{}</a>', url, title, extra, display_)
|
||||
return mark_safe('<a href="%s" title="%s" %s>%s</a>' % (url, title, extra, display))
|
||||
|
||||
|
||||
@admin_field
|
||||
|
@ -158,7 +158,7 @@ def admin_date(*args, **kwargs):
|
|||
date = date.strftime("%Y-%m-%d %H:%M:%S %Z")
|
||||
else:
|
||||
date = date.strftime("%Y-%m-%d")
|
||||
return format_html('<span title="{0}">{1}</span>', date, natural)
|
||||
return '<span title="{0}">{1}</span>'.format(date, escape(natural))
|
||||
|
||||
|
||||
def get_object_from_url(modeladmin, request):
|
||||
|
|
|
@ -1,12 +1,12 @@
|
|||
from rest_framework import status
|
||||
from rest_framework.decorators import action
|
||||
from rest_framework.decorators import detail_route
|
||||
from rest_framework.response import Response
|
||||
|
||||
from .serializers import SetPasswordSerializer
|
||||
|
||||
|
||||
class SetPasswordApiMixin(object):
|
||||
@action(detail=True, methods=['post'], serializer_class=SetPasswordSerializer)
|
||||
@detail_route(methods=['post'], serializer_class=SetPasswordSerializer)
|
||||
def set_password(self, request, pk):
|
||||
obj = self.get_object()
|
||||
data = request.data
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
from django.urls import NoReverseMatch
|
||||
from django.core.urlresolvers import NoReverseMatch
|
||||
from rest_framework.reverse import reverse
|
||||
|
||||
|
||||
|
@ -23,16 +23,16 @@ def link_wrap(view, view_names):
|
|||
return wrapper
|
||||
|
||||
|
||||
def insert_links(viewset, basename):
|
||||
collection_links = ['api-root', '%s-list' % basename]
|
||||
object_links = ['api-root', '%s-list' % basename, '%s-detail' % basename]
|
||||
def insert_links(viewset, base_name):
|
||||
collection_links = ['api-root', '%s-list' % base_name]
|
||||
object_links = ['api-root', '%s-list' % base_name, '%s-detail' % base_name]
|
||||
exception_links = ['api-root']
|
||||
list_links = ['api-root']
|
||||
retrieve_links = ['api-root', '%s-list' % basename]
|
||||
retrieve_links = ['api-root', '%s-list' % base_name]
|
||||
# Determine any `@action` or `@link` decorated methods on the viewset
|
||||
for methodname in dir(viewset):
|
||||
method = getattr(viewset, methodname)
|
||||
view_name = '%s-%s' % (basename, methodname.replace('_', '-'))
|
||||
view_name = '%s-%s' % (base_name, methodname.replace('_', '-'))
|
||||
if hasattr(method, 'collection_bind_to_methods'):
|
||||
list_links.append(view_name)
|
||||
retrieve_links.append(view_name)
|
||||
|
|
|
@ -18,33 +18,33 @@ class LogApiMixin(object):
|
|||
message = _('Added.')
|
||||
self.log(request, message, ADDITION, instance=self.serializer.instance)
|
||||
return response
|
||||
|
||||
|
||||
def perform_create(self, serializer):
|
||||
""" stores serializer for accessing instance on create() """
|
||||
super(LogApiMixin, self).perform_create(serializer)
|
||||
self.serializer = serializer
|
||||
|
||||
|
||||
def update(self, request, *args, **kwargs):
|
||||
from django.contrib.admin.models import CHANGE
|
||||
response = super(LogApiMixin, self).update(request, *args, **kwargs)
|
||||
message = _('Changed data')
|
||||
self.log(request, message, CHANGE)
|
||||
return response
|
||||
|
||||
|
||||
def partial_update(self, request, *args, **kwargs):
|
||||
from django.contrib.admin.models import CHANGE
|
||||
response = super(LogApiMixin, self).partial_update(request, *args, **kwargs)
|
||||
message = _('Changed %s') % response.data
|
||||
self.log(request, message, CHANGE)
|
||||
return response
|
||||
|
||||
|
||||
def destroy(self, request, *args, **kwargs):
|
||||
from django.contrib.admin.models import DELETION
|
||||
message = _('Deleted')
|
||||
self.log(request, message, DELETION)
|
||||
response = super(LogApiMixin, self).destroy(request, *args, **kwargs)
|
||||
return response
|
||||
|
||||
|
||||
def log(self, request, message, action, instance=None):
|
||||
from django.contrib.admin.models import LogEntry
|
||||
instance = instance or self.get_object()
|
||||
|
@ -64,21 +64,21 @@ class LinkHeaderRouter(DefaultRouter):
|
|||
APIRoot = import_class(settings.ORCHESTRA_API_ROOT_VIEW)
|
||||
APIRoot.router = self
|
||||
return APIRoot.as_view()
|
||||
|
||||
def register(self, prefix, viewset, basename=None):
|
||||
|
||||
def register(self, prefix, viewset, base_name=None):
|
||||
""" inserts link headers on every viewset """
|
||||
if basename is None:
|
||||
basename = self.get_default_basename(viewset)
|
||||
insert_links(viewset, basename)
|
||||
self.registry.append((prefix, viewset, basename))
|
||||
|
||||
if base_name is None:
|
||||
base_name = self.get_default_base_name(viewset)
|
||||
insert_links(viewset, base_name)
|
||||
self.registry.append((prefix, viewset, base_name))
|
||||
|
||||
def get_viewset(self, prefix_or_model):
|
||||
for _prefix, viewset, __ in self.registry:
|
||||
if _prefix == prefix_or_model or viewset.queryset.model == prefix_or_model:
|
||||
return viewset
|
||||
msg = "%s does not have a regiestered viewset" % prefix_or_model
|
||||
raise KeyError(msg)
|
||||
|
||||
|
||||
def insert(self, prefix_or_model, name, field, **kwargs):
|
||||
""" Dynamically add new fields to an existing serializer """
|
||||
viewset = self.get_viewset(prefix_or_model)
|
||||
|
|
|
@ -11,7 +11,7 @@ class APIRoot(views.APIView):
|
|||
'ORCHESTRA_SITE_NAME',
|
||||
'ORCHESTRA_SITE_VERBOSE_NAME'
|
||||
)
|
||||
|
||||
|
||||
def get(self, request, format=None):
|
||||
root_url = reverse('api-root', request=request, format=format)
|
||||
token_url = reverse('api-token-auth', request=request, format=format)
|
||||
|
@ -23,7 +23,7 @@ class APIRoot(views.APIView):
|
|||
'accountancy': {},
|
||||
'services': {},
|
||||
}
|
||||
if not request.user.is_anonymous:
|
||||
if not request.user.is_anonymous():
|
||||
list_name = '{basename}-list'
|
||||
detail_name = '{basename}-detail'
|
||||
for prefix, viewset, basename in self.router.registry:
|
||||
|
@ -60,7 +60,7 @@ class APIRoot(views.APIView):
|
|||
for name in self.names
|
||||
})
|
||||
return Response(body, headers=headers)
|
||||
|
||||
|
||||
def options(self, request):
|
||||
metadata = super(APIRoot, self).options(request)
|
||||
metadata.data['settings'] = {
|
||||
|
|
|
@ -17,7 +17,7 @@ class SetPasswordSerializer(serializers.Serializer):
|
|||
|
||||
class HyperlinkedModelSerializer(serializers.HyperlinkedModelSerializer):
|
||||
""" support for postonly_fields, fields whose value can only be set on post """
|
||||
|
||||
|
||||
def validate(self, attrs):
|
||||
""" calls model.clean() """
|
||||
attrs = super(HyperlinkedModelSerializer, self).validate(attrs)
|
||||
|
@ -39,7 +39,7 @@ class HyperlinkedModelSerializer(serializers.HyperlinkedModelSerializer):
|
|||
instance = ModelClass(**validated_data)
|
||||
instance.clean()
|
||||
return attrs
|
||||
|
||||
|
||||
def post_only_cleanning(self, instance, validated_data):
|
||||
""" removes postonly_fields from attrs """
|
||||
model_attrs = dict(**validated_data)
|
||||
|
@ -49,12 +49,12 @@ class HyperlinkedModelSerializer(serializers.HyperlinkedModelSerializer):
|
|||
if attr in post_only_fields:
|
||||
model_attrs.pop(attr)
|
||||
return model_attrs
|
||||
|
||||
|
||||
def update(self, instance, validated_data):
|
||||
""" removes postonly_fields from attrs when not posting """
|
||||
model_attrs = self.post_only_cleanning(instance, validated_data)
|
||||
return super(HyperlinkedModelSerializer, self).update(instance, model_attrs)
|
||||
|
||||
|
||||
def partial_update(self, instance, validated_data):
|
||||
""" removes postonly_fields from attrs when not posting """
|
||||
model_attrs = self.post_only_cleanning(instance, validated_data)
|
||||
|
@ -64,10 +64,7 @@ class HyperlinkedModelSerializer(serializers.HyperlinkedModelSerializer):
|
|||
class RelatedHyperlinkedModelSerializer(HyperlinkedModelSerializer):
|
||||
""" returns object on to_internal_value based on URL """
|
||||
def to_internal_value(self, data):
|
||||
try:
|
||||
url = data.get('url')
|
||||
except AttributeError:
|
||||
url = None
|
||||
url = data.get('url')
|
||||
if not url:
|
||||
raise ValidationError({
|
||||
'url': "URL is required."
|
||||
|
@ -83,16 +80,16 @@ class SetPasswordHyperlinkedSerializer(HyperlinkedModelSerializer):
|
|||
password = serializers.CharField(max_length=128, label=_('Password'),
|
||||
validators=[validate_password], write_only=True, required=False,
|
||||
style={'widget': widgets.PasswordInput})
|
||||
|
||||
def validate_password(self, value):
|
||||
|
||||
def validate_password(self, attrs, source):
|
||||
""" POST only password """
|
||||
if self.instance:
|
||||
if value:
|
||||
if 'password' in attrs:
|
||||
raise serializers.ValidationError(_("Can not set password"))
|
||||
elif not value:
|
||||
elif 'password' not in attrs:
|
||||
raise serializers.ValidationError(_("Password required"))
|
||||
return value
|
||||
|
||||
return attrs
|
||||
|
||||
def validate(self, attrs):
|
||||
""" remove password in case is not a real model field """
|
||||
try:
|
||||
|
@ -105,7 +102,7 @@ class SetPasswordHyperlinkedSerializer(HyperlinkedModelSerializer):
|
|||
if password is not None:
|
||||
attrs['password'] = password
|
||||
return attrs
|
||||
|
||||
|
||||
def create(self, validated_data):
|
||||
password = validated_data.pop('password')
|
||||
instance = self.Meta.model(**validated_data)
|
||||
|
|
|
@ -116,20 +116,17 @@ function install_requirements () {
|
|||
update-locale LANG=en_US.UTF-8
|
||||
fi
|
||||
|
||||
# lxml: libxml2-dev, libxslt1-dev, zlib1g-dev
|
||||
APT="bind9utils \
|
||||
ca-certificates \
|
||||
gettext \
|
||||
libcrack2-dev \
|
||||
libxml2-dev \
|
||||
libxslt1-dev \
|
||||
python3 \
|
||||
python3-pip \
|
||||
python3-dev \
|
||||
python3-lxml \
|
||||
ssh-client \
|
||||
wget \
|
||||
xvfb \
|
||||
zlib1g-dev"
|
||||
xvfb"
|
||||
if $testing; then
|
||||
APT="${APT} \
|
||||
git \
|
||||
|
@ -150,14 +147,13 @@ function install_requirements () {
|
|||
fi
|
||||
|
||||
# cracklib and lxml are excluded on the requirements.txt because they need unconvinient system dependencies
|
||||
PIP="$(wget http://git.io/orchestra-requirements.txt -O - | tr '\n' ' ') \
|
||||
cracklib \
|
||||
lxml==3.3.5"
|
||||
PIP="$(wget https://raw.githubusercontent.com/ribaguifi/django-orchestra/dev/github-actions/requirements.txt -O - | tr '\n' ' ') \
|
||||
cracklib"
|
||||
if $testing; then
|
||||
PIP="${PIP} \
|
||||
selenium \
|
||||
xvfbwrapper \
|
||||
freezegun==0.3.14 \
|
||||
freezegun \
|
||||
coverage \
|
||||
flake8 \
|
||||
django-debug-toolbar==1.3.0 \
|
||||
|
|
|
@ -27,7 +27,7 @@ class crontab_parser(object):
|
|||
_range = r'(\w+?)-(\w+)'
|
||||
_steps = r'/(\w+)?'
|
||||
_star = r'\*'
|
||||
|
||||
|
||||
def __init__(self, max_=60, min_=0):
|
||||
self.max_ = max_
|
||||
self.min_ = min_
|
||||
|
@ -45,14 +45,14 @@ class crontab_parser(object):
|
|||
raise self.ParseException('empty part')
|
||||
acc |= set(self._parse_part(part))
|
||||
return acc
|
||||
|
||||
|
||||
def _parse_part(self, part):
|
||||
for regex, handler in self.pats:
|
||||
m = regex.match(part)
|
||||
if m:
|
||||
return handler(m.groups())
|
||||
return self._expand_range((part, ))
|
||||
|
||||
|
||||
def _expand_range(self, toks):
|
||||
fr = self._expand_number(toks[0])
|
||||
if len(toks) > 1:
|
||||
|
@ -62,19 +62,19 @@ class crontab_parser(object):
|
|||
list(range(self.min_, to + 1)))
|
||||
return list(range(fr, to + 1))
|
||||
return [fr]
|
||||
|
||||
|
||||
def _range_steps(self, toks):
|
||||
if len(toks) != 3 or not toks[2]:
|
||||
raise self.ParseException('empty filter')
|
||||
return self._expand_range(toks[:2])[::int(toks[2])]
|
||||
|
||||
|
||||
def _star_steps(self, toks):
|
||||
if not toks or not toks[0]:
|
||||
raise self.ParseException('empty filter')
|
||||
return self._expand_star()[::int(toks[0])]
|
||||
def _expand_star(self, *args):
|
||||
return list(range(self.min_, self.max_ + self.min_))
|
||||
|
||||
|
||||
def _expand_number(self, s):
|
||||
if isinstance(s, str) and s[0] == '-':
|
||||
raise self.ParseException('negative numbers not supported')
|
||||
|
@ -99,7 +99,7 @@ class Setting(object):
|
|||
def __init__(self, manage):
|
||||
self.manage = manage
|
||||
self.settings_file = self.get_settings_file(manage)
|
||||
|
||||
|
||||
def get_settings(self):
|
||||
""" get db settings from settings.py file without importing """
|
||||
settings = {'__file__': self.settings_file}
|
||||
|
@ -111,7 +111,7 @@ class Setting(object):
|
|||
content += line
|
||||
exec(content, settings)
|
||||
return settings
|
||||
|
||||
|
||||
def get_settings_file(self, manage):
|
||||
with open(manage, 'r') as handler:
|
||||
regex = re.compile(r'"DJANGO_SETTINGS_MODULE"\s*,\s*"([^"]+)"')
|
||||
|
@ -128,7 +128,7 @@ class Setting(object):
|
|||
class DB(object):
|
||||
def __init__(self, settings):
|
||||
self.settings = settings['DATABASES']['default']
|
||||
|
||||
|
||||
def connect(self):
|
||||
if self.settings['ENGINE'] == 'django.db.backends.sqlite3':
|
||||
import sqlite3
|
||||
|
@ -138,7 +138,7 @@ class DB(object):
|
|||
self.conn = psycopg2.connect("dbname='{NAME}' user='{USER}' host='{HOST}' password='{PASSWORD}'".format(**self.settings))
|
||||
else:
|
||||
raise ValueError("%s engine not supported." % self.settings['ENGINE'])
|
||||
|
||||
|
||||
def query(self, query):
|
||||
cur = self.conn.cursor()
|
||||
try:
|
||||
|
@ -147,7 +147,7 @@ class DB(object):
|
|||
finally:
|
||||
cur.close()
|
||||
return result
|
||||
|
||||
|
||||
def close(self):
|
||||
self.conn.close()
|
||||
|
||||
|
@ -161,7 +161,7 @@ def fire_pending_tasks(manage, db):
|
|||
"WHERE p.crontab_id = c.id AND p.enabled = {}"
|
||||
).format(enabled)
|
||||
return db.query(query)
|
||||
|
||||
|
||||
def is_due(now, minute, hour, day_of_week, day_of_month, month_of_year):
|
||||
n_minute, n_hour, n_day_of_week, n_day_of_month, n_month_of_year = now
|
||||
return (
|
||||
|
@ -171,14 +171,14 @@ def fire_pending_tasks(manage, db):
|
|||
n_day_of_month in crontab_parser(31, 1).parse(day_of_month) and
|
||||
n_month_of_year in crontab_parser(12, 1).parse(month_of_year)
|
||||
)
|
||||
|
||||
|
||||
now = datetime.utcnow()
|
||||
now = tuple(map(int, now.strftime("%M %H %w %d %m").split()))
|
||||
for minute, hour, day_of_week, day_of_month, month_of_year, task_id in get_tasks(db):
|
||||
if is_due(now, minute, hour, day_of_week, day_of_month, month_of_year):
|
||||
command = 'python3 -W ignore::DeprecationWarning {manage} runtask {task_id}'.format(
|
||||
manage=manage, task_id=task_id)
|
||||
proc = run(command, run_async=True)
|
||||
proc = run(command, async=True)
|
||||
yield proc
|
||||
|
||||
|
||||
|
@ -187,7 +187,7 @@ def fire_pending_messages(settings, db):
|
|||
MAILER_DEFERE_SECONDS = settings.get('MAILER_DEFERE_SECONDS', (300, 600, 60*60, 60*60*24))
|
||||
now = datetime.utcnow()
|
||||
query_or = []
|
||||
|
||||
|
||||
for num, seconds in enumerate(MAILER_DEFERE_SECONDS):
|
||||
delta = timedelta(seconds=seconds)
|
||||
epoch = now-delta
|
||||
|
@ -198,10 +198,10 @@ def fire_pending_messages(settings, db):
|
|||
WHERE (mailer_message.state = 'QUEUED'
|
||||
OR (mailer_message.state = 'DEFERRED' AND (%s))) LIMIT 1""" % ' OR '.join(query_or)
|
||||
return bool(db.query(query))
|
||||
|
||||
|
||||
if has_pending_messages(settings, db):
|
||||
command = 'python3 -W ignore::DeprecationWarning {manage} sendpendingmessages'.format(manage=manage)
|
||||
proc = run(command, run_async=True)
|
||||
proc = run(command, async=True)
|
||||
yield proc
|
||||
|
||||
|
||||
|
|
|
@ -25,7 +25,6 @@ SECRET_KEY = '{{ secret_key }}'
|
|||
# SECURITY WARNING: don't run with debug turned on in production!
|
||||
DEBUG = True
|
||||
|
||||
ALLOWED_HOSTS = []
|
||||
|
||||
# Application definition
|
||||
|
||||
|
@ -66,7 +65,6 @@ INSTALLED_APPS = [
|
|||
'admin_tools.dashboard',
|
||||
'rest_framework',
|
||||
'rest_framework.authtoken',
|
||||
'django_filters',
|
||||
'passlib.ext.django',
|
||||
'django_countries',
|
||||
# 'debug_toolbar',
|
||||
|
@ -86,21 +84,6 @@ INSTALLED_APPS = [
|
|||
]
|
||||
|
||||
|
||||
MIDDLEWARE = [
|
||||
'django.middleware.security.SecurityMiddleware',
|
||||
'django.contrib.sessions.middleware.SessionMiddleware',
|
||||
'django.middleware.common.CommonMiddleware',
|
||||
'django.middleware.csrf.CsrfViewMiddleware',
|
||||
'django.contrib.auth.middleware.AuthenticationMiddleware',
|
||||
'django.contrib.messages.middleware.MessageMiddleware',
|
||||
'django.middleware.clickjacking.XFrameOptionsMiddleware',
|
||||
|
||||
'orchestra.core.caches.RequestCacheMiddleware',
|
||||
# also handles transations, ATOMIC_REQUESTS does not wrap middlewares
|
||||
'orchestra.contrib.orchestration.middlewares.OperationsMiddleware',
|
||||
]
|
||||
|
||||
|
||||
ROOT_URLCONF = '{{ project_name }}.urls'
|
||||
|
||||
TEMPLATES = [
|
||||
|
@ -144,24 +127,6 @@ DATABASES = {
|
|||
}
|
||||
|
||||
|
||||
# Password validation
|
||||
# https://docs.djangoproject.com/en/{{ docs_version }}/ref/settings/#auth-password-validators
|
||||
|
||||
AUTH_PASSWORD_VALIDATORS = [
|
||||
{
|
||||
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
|
||||
},
|
||||
{
|
||||
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
|
||||
},
|
||||
{
|
||||
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
|
||||
},
|
||||
{
|
||||
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
|
||||
},
|
||||
]
|
||||
|
||||
# Internationalization
|
||||
# https://docs.djangoproject.com/en/{{ docs_version }}/topics/i18n/
|
||||
|
||||
|
@ -203,6 +168,22 @@ LOCALE_PATHS = (
|
|||
ORCHESTRA_SITE_NAME = '{{ project_name }}'
|
||||
|
||||
|
||||
MIDDLEWARE_CLASSES = (
|
||||
'django.contrib.sessions.middleware.SessionMiddleware',
|
||||
'django.middleware.common.CommonMiddleware',
|
||||
'django.middleware.csrf.CsrfViewMiddleware',
|
||||
'django.contrib.auth.middleware.AuthenticationMiddleware',
|
||||
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
|
||||
# 'django.middleware.locale.LocaleMiddleware'
|
||||
'django.contrib.messages.middleware.MessageMiddleware',
|
||||
'django.middleware.clickjacking.XFrameOptionsMiddleware',
|
||||
'django.middleware.security.SecurityMiddleware',
|
||||
'orchestra.core.caches.RequestCacheMiddleware',
|
||||
# also handles transations, ATOMIC_REQUESTS does not wrap middlewares
|
||||
'orchestra.contrib.orchestration.middlewares.OperationsMiddleware',
|
||||
)
|
||||
|
||||
|
||||
AUTH_USER_MODEL = 'accounts.Account'
|
||||
|
||||
|
||||
|
@ -261,6 +242,7 @@ PASSLIB_CONFIG = (
|
|||
"default = sha512_crypt\n"
|
||||
"deprecated = django_pbkdf2_sha1, django_salted_sha1, django_salted_md5, "
|
||||
" django_des_crypt, des_crypt, hex_md5\n"
|
||||
"all__vary_rounds = 0.05\n"
|
||||
"django_pbkdf2_sha256__min_rounds = 10000\n"
|
||||
"sha512_crypt__min_rounds = 80000\n"
|
||||
"staff__django_pbkdf2_sha256__default_rounds = 12500\n"
|
||||
|
|
|
@ -0,0 +1,13 @@
|
|||
#!/usr/bin/env python3
|
||||
import os
|
||||
import sys
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
if sys.version_info < (3, 3):
|
||||
cmd = ' '.join(sys.argv)
|
||||
sys.stderr.write("Sorry, Orchestra requires at least Python 3.3, try with:\n$ python3 %s\n" % cmd)
|
||||
sys.exit(1)
|
||||
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "{{ project_name }}.settings")
|
||||
from django.core.management import execute_from_command_line
|
||||
execute_from_command_line(sys.argv)
|
|
@ -0,0 +1,261 @@
|
|||
"""
|
||||
Django settings for {{ project_name }} project.
|
||||
|
||||
Generated by 'django-admin startproject' using Django {{ django_version }}.
|
||||
|
||||
For more information on this file, see
|
||||
https://docs.djangoproject.com/en/{{ docs_version }}/topics/settings/
|
||||
|
||||
For the full list of settings and their values, see
|
||||
https://docs.djangoproject.com/en/{{ docs_version }}/ref/settings/
|
||||
"""
|
||||
|
||||
import os
|
||||
from decouple import config, Csv
|
||||
from dj_database_url import parse as db_url
|
||||
|
||||
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
|
||||
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
||||
|
||||
|
||||
# Quick-start development settings - unsuitable for production
|
||||
# See https://docs.djangoproject.com/en/{{ docs_version }}/howto/deployment/checklist/
|
||||
|
||||
# SECURITY WARNING: keep the secret key used in production secret!
|
||||
SECRET_KEY = '{{ secret_key }}'
|
||||
# SECRET_KEY = config('SECRET_KEY')
|
||||
|
||||
# SECURITY WARNING: don't run with debug turned on in production!
|
||||
DEBUG = config('DEBUG', default=False, cast=bool)
|
||||
ALLOWED_HOSTS = config('ALLOWED_HOSTS', default=[], cast=Csv())
|
||||
|
||||
|
||||
# Application definition
|
||||
|
||||
INSTALLED_APPS = [
|
||||
# django-orchestra apps
|
||||
'orchestra',
|
||||
'orchestra.contrib.accounts',
|
||||
'orchestra.contrib.systemusers',
|
||||
'orchestra.contrib.contacts',
|
||||
'orchestra.contrib.orchestration',
|
||||
'orchestra.contrib.bills',
|
||||
'orchestra.contrib.payments',
|
||||
'orchestra.contrib.tasks',
|
||||
'orchestra.contrib.mailer',
|
||||
'orchestra.contrib.history',
|
||||
'orchestra.contrib.issues',
|
||||
'orchestra.contrib.services',
|
||||
'orchestra.contrib.plans',
|
||||
'orchestra.contrib.orders',
|
||||
'orchestra.contrib.domains',
|
||||
'orchestra.contrib.mailboxes',
|
||||
'orchestra.contrib.lists',
|
||||
'orchestra.contrib.webapps',
|
||||
'orchestra.contrib.websites',
|
||||
'orchestra.contrib.letsencrypt',
|
||||
'orchestra.contrib.databases',
|
||||
'orchestra.contrib.vps',
|
||||
'orchestra.contrib.saas',
|
||||
'orchestra.contrib.miscellaneous',
|
||||
|
||||
# Third-party apps
|
||||
'django_extensions',
|
||||
'djcelery',
|
||||
'fluent_dashboard',
|
||||
'admin_tools',
|
||||
'admin_tools.theming',
|
||||
'admin_tools.menu',
|
||||
'admin_tools.dashboard',
|
||||
'rest_framework',
|
||||
'rest_framework.authtoken',
|
||||
'passlib.ext.django',
|
||||
'django_countries',
|
||||
# 'debug_toolbar',
|
||||
|
||||
# Django.contrib
|
||||
'django.contrib.auth',
|
||||
'django.contrib.contenttypes',
|
||||
'django.contrib.sessions',
|
||||
'django.contrib.messages',
|
||||
'django.contrib.staticfiles',
|
||||
'django.contrib.admin.apps.SimpleAdminConfig',
|
||||
|
||||
# Last to load
|
||||
'orchestra.contrib.resources',
|
||||
'orchestra.contrib.settings',
|
||||
# 'django_nose',
|
||||
]
|
||||
|
||||
|
||||
ROOT_URLCONF = '{{ project_name }}.urls'
|
||||
|
||||
TEMPLATES = [
|
||||
{
|
||||
'BACKEND': 'django.template.backends.django.DjangoTemplates',
|
||||
'DIRS': [],
|
||||
'OPTIONS': {
|
||||
'context_processors': [
|
||||
'django.template.context_processors.debug',
|
||||
'django.template.context_processors.request',
|
||||
'django.contrib.auth.context_processors.auth',
|
||||
'django.contrib.messages.context_processors.messages',
|
||||
'orchestra.core.context_processors.site',
|
||||
],
|
||||
'loaders': [
|
||||
'admin_tools.template_loaders.Loader',
|
||||
'django.template.loaders.filesystem.Loader',
|
||||
'django.template.loaders.app_directories.Loader',
|
||||
],
|
||||
},
|
||||
},
|
||||
]
|
||||
|
||||
|
||||
WSGI_APPLICATION = '{{ project_name }}.wsgi.application'
|
||||
|
||||
|
||||
# Database
|
||||
# https://docs.djangoproject.com/en/{{ docs_version }}/ref/settings/#databases
|
||||
|
||||
DATABASES = {
|
||||
'default': config(
|
||||
'DATABASE_URL',
|
||||
default='sqlite:///' + os.path.join(BASE_DIR, 'db.sqlite3'),
|
||||
cast=db_url
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
# Internationalization
|
||||
# https://docs.djangoproject.com/en/{{ docs_version }}/topics/i18n/
|
||||
|
||||
LANGUAGE_CODE = 'en-us'
|
||||
|
||||
|
||||
try:
|
||||
TIME_ZONE = open('/etc/timezone', 'r').read().strip()
|
||||
except IOError:
|
||||
TIME_ZONE = 'UTC'
|
||||
|
||||
USE_I18N = True
|
||||
|
||||
USE_L10N = True
|
||||
|
||||
USE_TZ = True
|
||||
|
||||
# Static files (CSS, JavaScript, Images)
|
||||
# https://docs.djangoproject.com/en/{{ docs_version }}/howto/static-files/
|
||||
|
||||
STATIC_URL = '/static/'
|
||||
|
||||
|
||||
# Absolute path to the directory static files should be collected to.
|
||||
# Don't put anything in this directory yourself; store your static files
|
||||
# in apps' "static/" subdirectories and in STATICFILES_DIRS.
|
||||
# Example: "/home/media/media.lawrence.com/static/"
|
||||
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
|
||||
|
||||
# Absolute filesystem path to the directory that will hold user-uploaded files.
|
||||
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
|
||||
|
||||
|
||||
# Path used for database translations files
|
||||
LOCALE_PATHS = (
|
||||
os.path.join(BASE_DIR, 'locale'),
|
||||
)
|
||||
|
||||
ORCHESTRA_SITE_NAME = '{{ project_name }}'
|
||||
|
||||
|
||||
MIDDLEWARE_CLASSES = (
|
||||
'django.contrib.sessions.middleware.SessionMiddleware',
|
||||
'django.middleware.common.CommonMiddleware',
|
||||
'django.middleware.csrf.CsrfViewMiddleware',
|
||||
'django.contrib.auth.middleware.AuthenticationMiddleware',
|
||||
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
|
||||
# 'django.middleware.locale.LocaleMiddleware'
|
||||
'django.contrib.messages.middleware.MessageMiddleware',
|
||||
'django.middleware.clickjacking.XFrameOptionsMiddleware',
|
||||
'django.middleware.security.SecurityMiddleware',
|
||||
'orchestra.core.caches.RequestCacheMiddleware',
|
||||
# also handles transations, ATOMIC_REQUESTS does not wrap middlewares
|
||||
'orchestra.contrib.orchestration.middlewares.OperationsMiddleware',
|
||||
)
|
||||
|
||||
|
||||
AUTH_USER_MODEL = 'accounts.Account'
|
||||
|
||||
|
||||
AUTHENTICATION_BACKENDS = [
|
||||
'orchestra.permissions.auth.OrchestraPermissionBackend',
|
||||
'django.contrib.auth.backends.ModelBackend',
|
||||
]
|
||||
|
||||
|
||||
EMAIL_BACKEND = 'orchestra.contrib.mailer.backends.EmailBackend'
|
||||
|
||||
|
||||
# Needed for Bulk operations
|
||||
DATA_UPLOAD_MAX_NUMBER_FIELDS = None
|
||||
|
||||
|
||||
#################################
|
||||
## 3RD PARTY APPS CONIGURATION ##
|
||||
#################################
|
||||
|
||||
# Admin Tools
|
||||
ADMIN_TOOLS_MENU = 'orchestra.admin.menu.OrchestraMenu'
|
||||
|
||||
# Fluent dashboard
|
||||
ADMIN_TOOLS_INDEX_DASHBOARD = 'orchestra.admin.dashboard.OrchestraIndexDashboard'
|
||||
FLUENT_DASHBOARD_ICON_THEME = '../orchestra/icons'
|
||||
|
||||
|
||||
# Django-celery
|
||||
import djcelery
|
||||
djcelery.setup_loader()
|
||||
CELERYBEAT_SCHEDULER = 'djcelery.schedulers.DatabaseScheduler'
|
||||
CELERY_ALWAYS_EAGER = True
|
||||
CELERY_TASK_ALWAYS_EAGER = True
|
||||
task_always_eager = True
|
||||
TASK_ALWAYS_EAGER = True
|
||||
|
||||
|
||||
# rest_framework
|
||||
REST_FRAMEWORK = {
|
||||
'DEFAULT_PERMISSION_CLASSES': (
|
||||
'orchestra.permissions.api.OrchestraPermissionBackend',
|
||||
),
|
||||
'DEFAULT_AUTHENTICATION_CLASSES': (
|
||||
'rest_framework.authentication.SessionAuthentication',
|
||||
'rest_framework.authentication.TokenAuthentication',
|
||||
),
|
||||
'DEFAULT_FILTER_BACKENDS': (
|
||||
('rest_framework.filters.DjangoFilterBackend',)
|
||||
),
|
||||
}
|
||||
|
||||
|
||||
# Use a UNIX compatible hash
|
||||
PASSLIB_CONFIG = (
|
||||
"[passlib]\n"
|
||||
"schemes = sha512_crypt, django_pbkdf2_sha256, django_pbkdf2_sha1, "
|
||||
" django_bcrypt, django_bcrypt_sha256, django_salted_sha1, des_crypt, "
|
||||
" django_salted_md5, django_des_crypt, hex_md5, bcrypt, phpass\n"
|
||||
"default = sha512_crypt\n"
|
||||
"deprecated = django_pbkdf2_sha1, django_salted_sha1, django_salted_md5, "
|
||||
" django_des_crypt, des_crypt, hex_md5\n"
|
||||
"all__vary_rounds = 0.05\n"
|
||||
"django_pbkdf2_sha256__min_rounds = 10000\n"
|
||||
"sha512_crypt__min_rounds = 80000\n"
|
||||
"staff__django_pbkdf2_sha256__default_rounds = 12500\n"
|
||||
"staff__sha512_crypt__default_rounds = 100000\n"
|
||||
"superuser__django_pbkdf2_sha256__default_rounds = 15000\n"
|
||||
"superuser__sha512_crypt__default_rounds = 120000\n"
|
||||
)
|
||||
|
||||
|
||||
SHELL_PLUS_PRE_IMPORTS = (
|
||||
('orchestra.contrib.orchestration.managers', ('orchestrate',)),
|
||||
)
|
|
@ -0,0 +1,6 @@
|
|||
from django.conf.urls import include, url
|
||||
|
||||
|
||||
urlpatterns = [
|
||||
url(r'', include('orchestra.urls')),
|
||||
]
|
|
@ -0,0 +1,14 @@
|
|||
"""
|
||||
WSGI config for {{ project_name }} project.
|
||||
|
||||
It exposes the WSGI callable as a module-level variable named ``application``.
|
||||
|
||||
For more information on this file, see
|
||||
https://docs.djangoproject.com/en/{{ docs_version }}/howto/deployment/wsgi/
|
||||
"""
|
||||
|
||||
import os
|
||||
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "{{ project_name }}.settings")
|
||||
|
||||
from django.core.wsgi import get_wsgi_application
|
||||
application = get_wsgi_application()
|
|
@ -4,7 +4,7 @@ from django.contrib import messages
|
|||
from django.contrib.admin import helpers
|
||||
from django.contrib.admin.utils import NestedObjects, quote
|
||||
from django.contrib.auth import get_permission_codename
|
||||
from django.urls import reverse, NoReverseMatch
|
||||
from django.core.urlresolvers import reverse, NoReverseMatch
|
||||
from django.db import router
|
||||
from django.shortcuts import redirect, render
|
||||
from django.template.response import TemplateResponse
|
||||
|
@ -53,14 +53,14 @@ def service_report(modeladmin, request, queryset):
|
|||
fields.append((model, name))
|
||||
fields = sorted(fields, key=lambda f: f[0]._meta.verbose_name_plural.lower())
|
||||
fields = [field for model, field in fields]
|
||||
|
||||
|
||||
for account in queryset.prefetch_related(*fields):
|
||||
items = []
|
||||
for field in fields:
|
||||
related_manager = getattr(account, field)
|
||||
items.append((related_manager.model._meta, related_manager.all()))
|
||||
accounts.append((account, items))
|
||||
|
||||
|
||||
context = {
|
||||
'accounts': accounts,
|
||||
'date': timezone.now().today()
|
||||
|
@ -71,21 +71,21 @@ def service_report(modeladmin, request, queryset):
|
|||
def delete_related_services(modeladmin, request, queryset):
|
||||
opts = modeladmin.model._meta
|
||||
app_label = opts.app_label
|
||||
|
||||
|
||||
using = router.db_for_write(modeladmin.model)
|
||||
collector = NestedObjects(using=using)
|
||||
collector.collect(queryset)
|
||||
registered_services = services.get()
|
||||
related_services = []
|
||||
to_delete = []
|
||||
|
||||
|
||||
admin_site = modeladmin.admin_site
|
||||
|
||||
|
||||
def format(obj, account=False):
|
||||
has_admin = obj.__class__ in admin_site._registry
|
||||
opts = obj._meta
|
||||
no_edit_link = '%s: %s' % (capfirst(opts.verbose_name), force_text(obj))
|
||||
|
||||
|
||||
if has_admin:
|
||||
try:
|
||||
admin_url = reverse(
|
||||
|
@ -95,7 +95,7 @@ def delete_related_services(modeladmin, request, queryset):
|
|||
except NoReverseMatch:
|
||||
# Change url doesn't exist -- don't display link to edit
|
||||
return no_edit_link
|
||||
|
||||
|
||||
# Display a link to the admin page.
|
||||
context = (capfirst(opts.verbose_name), admin_url, obj)
|
||||
if account:
|
||||
|
@ -106,7 +106,7 @@ def delete_related_services(modeladmin, request, queryset):
|
|||
# Don't display link to edit, because it either has no
|
||||
# admin or is edited inline.
|
||||
return no_edit_link
|
||||
|
||||
|
||||
def format_nested(objs, result):
|
||||
if isinstance(objs, list):
|
||||
current = []
|
||||
|
@ -115,7 +115,7 @@ def delete_related_services(modeladmin, request, queryset):
|
|||
result.append(current)
|
||||
else:
|
||||
result.append(format(objs))
|
||||
|
||||
|
||||
for nested in collector.nested():
|
||||
if isinstance(nested, list):
|
||||
# Is lists of objects
|
||||
|
@ -141,7 +141,7 @@ def delete_related_services(modeladmin, request, queryset):
|
|||
# Prevent the deletion of the main system user, which will delete the account
|
||||
main_systemuser = nested.main_systemuser
|
||||
related_services.append(format(nested, account=True))
|
||||
|
||||
|
||||
# The user has already confirmed the deletion.
|
||||
# Do the deletion and return a None to display the change list view again.
|
||||
if request.POST.get('post'):
|
||||
|
@ -165,17 +165,17 @@ def delete_related_services(modeladmin, request, queryset):
|
|||
modeladmin.message_user(request, msg, messages.SUCCESS)
|
||||
# Return None to display the change list page again.
|
||||
return None
|
||||
|
||||
|
||||
if len(queryset) == 1:
|
||||
objects_name = force_text(opts.verbose_name)
|
||||
else:
|
||||
objects_name = force_text(opts.verbose_name_plural)
|
||||
|
||||
|
||||
model_count = {}
|
||||
for model, objs in collector.model_objs.items():
|
||||
count = 0
|
||||
# discount main systemuser
|
||||
if model is modeladmin.model.main_systemuser.field.related_model:
|
||||
if model is modeladmin.model.main_systemuser.field.rel.to:
|
||||
count = len(objs) - 1
|
||||
# Discount account
|
||||
elif model is not modeladmin.model and model in registered_services:
|
||||
|
@ -220,10 +220,10 @@ def disable_selected(modeladmin, request, queryset, disable=True):
|
|||
n)
|
||||
)
|
||||
return None
|
||||
|
||||
|
||||
user = request.user
|
||||
admin_site = modeladmin.admin_site
|
||||
|
||||
|
||||
def format(obj):
|
||||
has_admin = obj.__class__ in admin_site._registry
|
||||
opts = obj._meta
|
||||
|
@ -238,7 +238,7 @@ def disable_selected(modeladmin, request, queryset, disable=True):
|
|||
except NoReverseMatch:
|
||||
# Change url doesn't exist -- don't display link to edit
|
||||
return no_edit_link
|
||||
|
||||
|
||||
p = '%s.%s' % (opts.app_label, get_permission_codename('delete', opts))
|
||||
if not user.has_perm(p):
|
||||
perms_needed.add(opts.verbose_name)
|
||||
|
@ -249,19 +249,19 @@ def disable_selected(modeladmin, request, queryset, disable=True):
|
|||
# Don't display link to edit, because it either has no
|
||||
# admin or is edited inline.
|
||||
return no_edit_link
|
||||
|
||||
|
||||
display = []
|
||||
for account in queryset:
|
||||
current = []
|
||||
for related in account.get_services_to_disable():
|
||||
current.append(format(related))
|
||||
display.append([format(account), current])
|
||||
|
||||
|
||||
if len(queryset) == 1:
|
||||
objects_name = force_text(opts.verbose_name)
|
||||
else:
|
||||
objects_name = force_text(opts.verbose_name_plural)
|
||||
|
||||
|
||||
context = dict(
|
||||
admin_site.each_context(request),
|
||||
action_name='disable_selected' if disable else 'enable_selected',
|
||||
|
|
|
@ -8,7 +8,7 @@ from django.conf.urls import url
|
|||
from django.contrib import admin, messages
|
||||
from django.contrib.admin.utils import unquote
|
||||
from django.contrib.auth import admin as auth
|
||||
from django.urls import reverse
|
||||
from django.core.urlresolvers import reverse
|
||||
from django.http import HttpResponseRedirect
|
||||
from django.templatetags.static import static
|
||||
from django.utils.safestring import mark_safe
|
||||
|
@ -71,15 +71,15 @@ class AccountAdmin(ChangePasswordAdminMixin, auth.UserAdmin, ExtendedModelAdmin)
|
|||
)
|
||||
change_view_actions = (disable_selected, service_report, enable_selected)
|
||||
ordering = ()
|
||||
|
||||
|
||||
main_systemuser_link = admin_link('main_systemuser')
|
||||
|
||||
|
||||
def formfield_for_dbfield(self, db_field, **kwargs):
|
||||
""" Make value input widget bigger """
|
||||
if db_field.name == 'comments':
|
||||
kwargs['widget'] = forms.Textarea(attrs={'cols': 70, 'rows': 4})
|
||||
return super(AccountAdmin, self).formfield_for_dbfield(db_field, **kwargs)
|
||||
|
||||
|
||||
def render_change_form(self, request, context, add=False, change=False, form_url='', obj=None):
|
||||
if not add:
|
||||
if request.method == 'GET' and not obj.is_active:
|
||||
|
@ -96,7 +96,7 @@ class AccountAdmin(ChangePasswordAdminMixin, auth.UserAdmin, ExtendedModelAdmin)
|
|||
})
|
||||
return super(AccountAdmin, self).render_change_form(
|
||||
request, context, add, change, form_url, obj)
|
||||
|
||||
|
||||
def get_fieldsets(self, request, obj=None):
|
||||
fieldsets = super(AccountAdmin, self).get_fieldsets(request, obj)
|
||||
if not obj:
|
||||
|
@ -106,7 +106,7 @@ class AccountAdmin(ChangePasswordAdminMixin, auth.UserAdmin, ExtendedModelAdmin)
|
|||
fieldsets = list(fieldsets)
|
||||
fieldsets.insert(1, (_("Related services"), {'fields': fields}))
|
||||
return fieldsets
|
||||
|
||||
|
||||
def save_model(self, request, obj, form, change):
|
||||
if not change:
|
||||
form.save_model(obj)
|
||||
|
@ -133,7 +133,7 @@ class AccountAdmin(ChangePasswordAdminMixin, auth.UserAdmin, ExtendedModelAdmin)
|
|||
if msg:
|
||||
messages.warning(request, mark_safe(msg % context))
|
||||
super(AccountAdmin, self).save_model(request, obj, form, change)
|
||||
|
||||
|
||||
def get_change_view_actions(self, obj=None):
|
||||
views = super().get_change_view_actions(obj=obj)
|
||||
if obj is not None:
|
||||
|
@ -141,7 +141,7 @@ class AccountAdmin(ChangePasswordAdminMixin, auth.UserAdmin, ExtendedModelAdmin)
|
|||
return [view for view in views if view.url_name != 'enable']
|
||||
return [view for view in views if view.url_name != 'disable']
|
||||
return views
|
||||
|
||||
|
||||
def get_actions(self, request):
|
||||
actions = super().get_actions(request)
|
||||
if 'delete_selected' in actions:
|
||||
|
@ -157,8 +157,7 @@ class AccountListAdmin(AccountAdmin):
|
|||
list_display = ('select_account', 'username', 'type', 'username')
|
||||
actions = None
|
||||
change_list_template = 'admin/accounts/account/select_account_list.html'
|
||||
|
||||
@mark_safe
|
||||
|
||||
def select_account(self, instance):
|
||||
# TODO get query string from request.META['QUERY_STRING'] to preserve filters
|
||||
context = {
|
||||
|
@ -168,8 +167,9 @@ class AccountListAdmin(AccountAdmin):
|
|||
}
|
||||
return _('<a href="%(url)s">%(plus)s Add to %(name)s</a>') % context
|
||||
select_account.short_description = _("account")
|
||||
select_account.allow_tags = True
|
||||
select_account.admin_order_field = 'username'
|
||||
|
||||
|
||||
def changelist_view(self, request, extra_context=None):
|
||||
app_label = request.META['PATH_INFO'].split('/')[-5]
|
||||
model = request.META['PATH_INFO'].split('/')[-4]
|
||||
|
@ -206,8 +206,7 @@ class AccountAdminMixin(object):
|
|||
change_form_template = 'admin/accounts/account/change_form.html'
|
||||
account = None
|
||||
list_select_related = ('account',)
|
||||
|
||||
@mark_safe
|
||||
|
||||
def display_active(self, instance):
|
||||
if not instance.is_active:
|
||||
return '<img src="%s" alt="False">' % static('admin/img/icon-no.svg')
|
||||
|
@ -216,14 +215,16 @@ class AccountAdminMixin(object):
|
|||
return '<img style="width:13px" src="%s" alt="False" title="%s">' % (static('admin/img/inline-delete.svg'), msg)
|
||||
return '<img src="%s" alt="False">' % static('admin/img/icon-yes.svg')
|
||||
display_active.short_description = _("active")
|
||||
display_active.allow_tags = True
|
||||
display_active.admin_order_field = 'is_active'
|
||||
|
||||
|
||||
def account_link(self, instance):
|
||||
account = instance.account if instance.pk else self.account
|
||||
return admin_link()(account)
|
||||
account_link.short_description = _("account")
|
||||
account_link.allow_tags = True
|
||||
account_link.admin_order_field = 'account__username'
|
||||
|
||||
|
||||
def get_form(self, request, obj=None, **kwargs):
|
||||
""" Warns user when object's account is disabled """
|
||||
form = super(AccountAdminMixin, self).get_form(request, obj, **kwargs)
|
||||
|
@ -246,7 +247,7 @@ class AccountAdminMixin(object):
|
|||
# Not available in POST
|
||||
form.initial_account = self.get_changeform_initial_data(request).get('account')
|
||||
return form
|
||||
|
||||
|
||||
def get_fields(self, request, obj=None):
|
||||
""" remove account or account_link depending on the case """
|
||||
fields = super(AccountAdminMixin, self).get_fields(request, obj)
|
||||
|
@ -262,13 +263,13 @@ class AccountAdminMixin(object):
|
|||
except ValueError:
|
||||
pass
|
||||
return fields
|
||||
|
||||
|
||||
def get_readonly_fields(self, request, obj=None):
|
||||
""" provide account for filter_by_account_fields """
|
||||
if obj:
|
||||
self.account = obj.account
|
||||
return super(AccountAdminMixin, self).get_readonly_fields(request, obj)
|
||||
|
||||
|
||||
def formfield_for_dbfield(self, db_field, **kwargs):
|
||||
""" Filter by account """
|
||||
formfield = super(AccountAdminMixin, self).formfield_for_dbfield(db_field, **kwargs)
|
||||
|
@ -276,14 +277,14 @@ class AccountAdminMixin(object):
|
|||
if self.account:
|
||||
# Hack widget render in order to append ?account=id to the add url
|
||||
old_render = formfield.widget.render
|
||||
|
||||
|
||||
def render(*args, **kwargs):
|
||||
output = old_render(*args, **kwargs)
|
||||
output = output.replace('/add/"', '/add/?account=%s"' % self.account.pk)
|
||||
with_qargs = r'/add/?\1&account=%s"' % self.account.pk
|
||||
output = re.sub(r'/add/\?([^".]*)"', with_qargs, output)
|
||||
return mark_safe(output)
|
||||
|
||||
|
||||
formfield.widget.render = render
|
||||
# Filter related object by account
|
||||
formfield.queryset = formfield.queryset.filter(account=self.account)
|
||||
|
@ -301,21 +302,21 @@ class AccountAdminMixin(object):
|
|||
formfield.initial = 1
|
||||
formfield.queryset = formfield.queryset.order_by('username')
|
||||
return formfield
|
||||
|
||||
|
||||
def get_formset(self, request, obj=None, **kwargs):
|
||||
""" provides form.account for convinience """
|
||||
formset = super(AccountAdminMixin, self).get_formset(request, obj, **kwargs)
|
||||
formset.form.account = self.account
|
||||
formset.account = self.account
|
||||
return formset
|
||||
|
||||
|
||||
def get_account_from_preserve_filters(self, request):
|
||||
preserved_filters = self.get_preserved_filters(request)
|
||||
preserved_filters = dict(parse_qsl(preserved_filters))
|
||||
cl_filters = preserved_filters.get('_changelist_filters')
|
||||
if cl_filters:
|
||||
return dict(parse_qsl(cl_filters)).get('account')
|
||||
|
||||
|
||||
def changeform_view(self, request, object_id=None, form_url='', extra_context=None):
|
||||
account_id = self.get_account_from_preserve_filters(request)
|
||||
if not object_id:
|
||||
|
@ -330,7 +331,7 @@ class AccountAdminMixin(object):
|
|||
context.update(extra_context or {})
|
||||
return super(AccountAdminMixin, self).changeform_view(
|
||||
request, object_id, form_url=form_url, extra_context=context)
|
||||
|
||||
|
||||
def changelist_view(self, request, extra_context=None):
|
||||
account_id = request.GET.get('account')
|
||||
context = {}
|
||||
|
@ -366,7 +367,7 @@ class SelectAccountAdminMixin(AccountAdminMixin):
|
|||
account = Account.objects.get(pk=request.GET['account'])
|
||||
[setattr(inline, 'account', account) for inline in inlines]
|
||||
return inlines
|
||||
|
||||
|
||||
def get_urls(self):
|
||||
""" Hooks select account url """
|
||||
urls = super(AccountAdminMixin, self).get_urls()
|
||||
|
@ -380,7 +381,7 @@ class SelectAccountAdminMixin(AccountAdminMixin):
|
|||
name='%s_%s_select_account' % info),
|
||||
]
|
||||
return select_urls + urls
|
||||
|
||||
|
||||
def add_view(self, request, form_url='', extra_context=None):
|
||||
""" Redirects to select account view if required """
|
||||
if request.user.is_superuser:
|
||||
|
@ -405,7 +406,7 @@ class SelectAccountAdminMixin(AccountAdminMixin):
|
|||
return super(AccountAdminMixin, self).add_view(
|
||||
request, form_url=form_url, extra_context=context)
|
||||
return HttpResponseRedirect('./select-account/?%s' % request.META['QUERY_STRING'])
|
||||
|
||||
|
||||
def save_model(self, request, obj, form, change):
|
||||
"""
|
||||
Given a model instance save it to the database.
|
||||
|
|
|
@ -34,7 +34,7 @@ def create_account_creation_form():
|
|||
fields[field_name] = forms.BooleanField(
|
||||
initial=True, required=False, label=label, help_text=help_text)
|
||||
create_related.append((model, key, kwargs, help_text))
|
||||
|
||||
|
||||
def clean(self, create_related=create_related):
|
||||
""" unique usernames between accounts and system users """
|
||||
cleaned_data = UserCreationForm.clean(self)
|
||||
|
@ -47,7 +47,7 @@ def create_account_creation_form():
|
|||
# Previous validation error
|
||||
return
|
||||
errors = {}
|
||||
systemuser_model = Account.main_systemuser.field.related_model
|
||||
systemuser_model = Account.main_systemuser.field.rel.to
|
||||
if systemuser_model.objects.filter(username=account.username).exists():
|
||||
errors['username'] = _("A system user with this name already exists.")
|
||||
for model, key, related_kwargs, __ in create_related:
|
||||
|
@ -62,11 +62,11 @@ def create_account_creation_form():
|
|||
params={'type': verbose_name})
|
||||
if errors:
|
||||
raise ValidationError(errors)
|
||||
|
||||
|
||||
def save_model(self, account):
|
||||
enable_systemuser=self.cleaned_data['enable_systemuser']
|
||||
account.save(active_systemuser=enable_systemuser)
|
||||
|
||||
|
||||
def save_related(self, account):
|
||||
for model, key, related_kwargs, __ in settings.ACCOUNTS_CREATE_RELATED:
|
||||
model = apps.get_model(model)
|
||||
|
@ -76,14 +76,14 @@ def create_account_creation_form():
|
|||
key: eval(value, {'account': account}) for key, value in related_kwargs.items()
|
||||
}
|
||||
model.objects.create(account=account, **kwargs)
|
||||
|
||||
|
||||
fields.update({
|
||||
'create_related_fields': list(fields.keys()),
|
||||
'clean': clean,
|
||||
'save_model': save_model,
|
||||
'save_related': save_related,
|
||||
})
|
||||
|
||||
|
||||
return type('AccountCreationForm', (UserCreationForm,), fields)
|
||||
|
||||
|
||||
|
|
|
@ -3,7 +3,6 @@ from __future__ import unicode_literals
|
|||
|
||||
from django.db import models, migrations
|
||||
import django.core.validators
|
||||
import django.db.models.deletion
|
||||
import django.utils.timezone
|
||||
import django.contrib.auth.models
|
||||
|
||||
|
@ -33,7 +32,7 @@ class Migration(migrations.Migration):
|
|||
('is_superuser', models.BooleanField(help_text='Designates that this user has all permissions without explicitly assigning them.', default=False, verbose_name='superuser status')),
|
||||
('is_active', models.BooleanField(help_text='Designates whether this account should be treated as active. Unselect this instead of deleting accounts.', default=True, verbose_name='active')),
|
||||
('date_joined', models.DateTimeField(verbose_name='date joined', default=django.utils.timezone.now)),
|
||||
('main_systemuser', models.ForeignKey(to='systemusers.SystemUser', editable=False, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='accounts_main')),
|
||||
('main_systemuser', models.ForeignKey(to='systemusers.SystemUser', editable=False, null=True, related_name='accounts_main')),
|
||||
],
|
||||
options={
|
||||
'abstract': False,
|
||||
|
|
|
@ -1,86 +0,0 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
# Generated by Django 1.10.5 on 2021-04-22 11:08
|
||||
from __future__ import unicode_literals
|
||||
|
||||
import django.contrib.auth.models
|
||||
import django.core.validators
|
||||
from django.db import migrations, models
|
||||
import django.db.models.deletion
|
||||
import django.utils.timezone
|
||||
import orchestra.contrib.accounts.models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
replaces = [('accounts', '0001_initial'), ('accounts', '0002_auto_20170528_2005'), ('accounts', '0003_auto_20210330_1049'), ('accounts', '0004_auto_20210422_1108')]
|
||||
|
||||
initial = True
|
||||
|
||||
dependencies = [
|
||||
('systemusers', '0001_initial'),
|
||||
('auth', '0006_require_contenttypes_0002'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name='Account',
|
||||
fields=[
|
||||
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('password', models.CharField(max_length=128, verbose_name='password')),
|
||||
('last_login', models.DateTimeField(blank=True, null=True, verbose_name='last login')),
|
||||
('username', models.CharField(help_text='Required. 64 characters or fewer. Letters, digits and ./-/_ only.', max_length=32, unique=True, validators=[django.core.validators.RegexValidator('^[\\w.-]+$', 'Enter a valid username.', 'invalid')], verbose_name='username')),
|
||||
('short_name', models.CharField(blank=True, max_length=64, verbose_name='short name')),
|
||||
('full_name', models.CharField(max_length=256, verbose_name='full name')),
|
||||
('email', models.EmailField(help_text='Used for password recovery', max_length=254, verbose_name='email address')),
|
||||
('type', models.CharField(choices=[('INDIVIDUAL', 'Individual'), ('ASSOCIATION', 'Association'), ('CUSTOMER', 'Customer'), ('COMPANY', 'Company'), ('PUBLICBODY', 'Public body'), ('STAFF', 'Staff'), ('FRIEND', 'Friend')], default='INDIVIDUAL', max_length=32, verbose_name='type')),
|
||||
('language', models.CharField(choices=[('EN', 'English')], default='EN', max_length=2, verbose_name='language')),
|
||||
('comments', models.TextField(blank=True, max_length=256, verbose_name='comments')),
|
||||
('is_superuser', models.BooleanField(default=False, help_text='Designates that this user has all permissions without explicitly assigning them.', verbose_name='superuser status')),
|
||||
('is_active', models.BooleanField(default=True, help_text='Designates whether this account should be treated as active. Unselect this instead of deleting accounts.', verbose_name='active')),
|
||||
('date_joined', models.DateTimeField(default=django.utils.timezone.now, verbose_name='date joined')),
|
||||
('main_systemuser', models.ForeignKey(editable=False, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='accounts_main', to='systemusers.SystemUser')),
|
||||
],
|
||||
options={
|
||||
'abstract': False,
|
||||
},
|
||||
managers=[
|
||||
('objects', django.contrib.auth.models.UserManager()),
|
||||
],
|
||||
),
|
||||
migrations.AlterModelManagers(
|
||||
name='account',
|
||||
managers=[
|
||||
('objects', orchestra.contrib.accounts.models.AccountManager()),
|
||||
],
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='account',
|
||||
name='language',
|
||||
field=models.CharField(choices=[('CA', 'Catalan'), ('ES', 'Spanish'), ('EN', 'English')], default='CA', max_length=2, verbose_name='language'),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='account',
|
||||
name='type',
|
||||
field=models.CharField(choices=[('INDIVIDUAL', 'Individual'), ('ASSOCIATION', 'Association'), ('CUSTOMER', 'Customer'), ('STAFF', 'Staff'), ('FRIEND', 'Friend')], default='INDIVIDUAL', max_length=32, verbose_name='type'),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='account',
|
||||
name='username',
|
||||
field=models.CharField(help_text='Required. 32 characters or fewer. Letters, digits and ./-/_ only.', max_length=32, unique=True, validators=[django.core.validators.RegexValidator('^[\\w.-]+$', 'Enter a valid username.', 'invalid')], verbose_name='username'),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='account',
|
||||
name='language',
|
||||
field=models.CharField(choices=[('EN', 'English')], default='EN', max_length=2, verbose_name='language'),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='account',
|
||||
name='type',
|
||||
field=models.CharField(choices=[('INDIVIDUAL', 'Individual'), ('ASSOCIATION', 'Association'), ('CUSTOMER', 'Customer'), ('COMPANY', 'Company'), ('PUBLICBODY', 'Public body'), ('STAFF', 'Staff'), ('FRIEND', 'Friend')], default='INDIVIDUAL', max_length=32, verbose_name='type'),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='account',
|
||||
name='main_systemuser',
|
||||
field=models.ForeignKey(editable=False, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='accounts_main', to='systemusers.SystemUser'),
|
||||
),
|
||||
]
|
|
@ -1,25 +0,0 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
# Generated by Django 1.10.5 on 2021-03-30 10:49
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('accounts', '0002_auto_20170528_2005'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AlterField(
|
||||
model_name='account',
|
||||
name='language',
|
||||
field=models.CharField(choices=[('EN', 'English')], default='EN', max_length=2, verbose_name='language'),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='account',
|
||||
name='type',
|
||||
field=models.CharField(choices=[('INDIVIDUAL', 'Individual'), ('ASSOCIATION', 'Association'), ('CUSTOMER', 'Customer'), ('COMPANY', 'Company'), ('PUBLICBODY', 'Public body'), ('STAFF', 'Staff'), ('FRIEND', 'Friend')], default='INDIVIDUAL', max_length=32, verbose_name='type'),
|
||||
),
|
||||
]
|
|
@ -29,7 +29,7 @@ class Account(auth.AbstractBaseUser):
|
|||
validators.RegexValidator(r'^[\w.-]+$', _("Enter a valid username."), 'invalid')
|
||||
])
|
||||
main_systemuser = models.ForeignKey(settings.ACCOUNTS_SYSTEMUSER_MODEL, null=True,
|
||||
related_name='accounts_main', editable=False, on_delete=models.SET_NULL)
|
||||
related_name='accounts_main', editable=False)
|
||||
short_name = models.CharField(_("short name"), max_length=64, blank=True)
|
||||
full_name = models.CharField(_("full name"), max_length=256)
|
||||
email = models.EmailField(_('email address'), help_text=_("Used for password recovery"))
|
||||
|
@ -46,28 +46,27 @@ class Account(auth.AbstractBaseUser):
|
|||
help_text=_("Designates whether this account should be treated as active. "
|
||||
"Unselect this instead of deleting accounts."))
|
||||
date_joined = models.DateTimeField(_("date joined"), default=timezone.now)
|
||||
|
||||
|
||||
objects = AccountManager()
|
||||
|
||||
|
||||
USERNAME_FIELD = 'username'
|
||||
REQUIRED_FIELDS = ['email']
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
# ignore `is_staff` kwarg because is handled with `is_superuser`
|
||||
kwargs.pop('is_staff', None)
|
||||
super().__init__(*args, **kwargs)
|
||||
|
||||
|
||||
def __str__(self):
|
||||
return self.name
|
||||
|
||||
|
||||
@property
|
||||
def name(self):
|
||||
return self.username
|
||||
|
||||
|
||||
@property
|
||||
def is_staff(self):
|
||||
return self.is_superuser
|
||||
|
||||
|
||||
def save(self, active_systemuser=False, *args, **kwargs):
|
||||
created = not self.pk
|
||||
if not created:
|
||||
|
@ -80,21 +79,21 @@ class Account(auth.AbstractBaseUser):
|
|||
self.save(update_fields=('main_systemuser',))
|
||||
elif was_active != self.is_active:
|
||||
self.notify_related()
|
||||
|
||||
|
||||
def clean(self):
|
||||
self.short_name = self.short_name.strip()
|
||||
self.full_name = self.full_name.strip()
|
||||
|
||||
|
||||
def disable(self):
|
||||
self.is_active = False
|
||||
self.save(update_fields=('is_active',))
|
||||
self.notify_related()
|
||||
|
||||
|
||||
def enable(self):
|
||||
self.is_active = True
|
||||
self.save(update_fields=('is_active',))
|
||||
self.notify_related()
|
||||
|
||||
|
||||
def get_services_to_disable(self):
|
||||
related_fields = [
|
||||
f for f in self._meta.get_fields()
|
||||
|
@ -106,20 +105,20 @@ class Account(auth.AbstractBaseUser):
|
|||
if source in core.services and hasattr(source, 'active'):
|
||||
for obj in getattr(self, rel.get_accessor_name()).all():
|
||||
yield obj
|
||||
|
||||
|
||||
def notify_related(self):
|
||||
""" Trigger save() on related objects that depend on this account """
|
||||
for obj in self.get_services_to_disable():
|
||||
signals.pre_save.send(sender=type(obj), instance=obj)
|
||||
signals.post_save.send(sender=type(obj), instance=obj)
|
||||
# OperationsMiddleware.collect(Operation.SAVE, instance=obj, update_fields=())
|
||||
|
||||
|
||||
def get_contacts_emails(self, usages=None):
|
||||
contacts = self.contacts.all()
|
||||
if usages is not None:
|
||||
contactes = contacts.filter(email_usages=usages)
|
||||
return contacts.values_list('email', flat=True)
|
||||
|
||||
|
||||
def send_email(self, template, context, email_from=None, usages=None, attachments=[], html=None):
|
||||
contacts = self.contacts.filter(email_usages=usages)
|
||||
email_to = self.get_contacts_emails(usages)
|
||||
|
@ -131,14 +130,14 @@ class Account(auth.AbstractBaseUser):
|
|||
with translation.override(self.language):
|
||||
send_email_template(template, extra_context, email_to, email_from=email_from,
|
||||
html=html, attachments=attachments)
|
||||
|
||||
|
||||
def get_full_name(self):
|
||||
return self.full_name or self.short_name or self.username
|
||||
|
||||
|
||||
def get_short_name(self):
|
||||
""" Returns the short name for the user """
|
||||
return self.short_name or self.username or self.full_name
|
||||
|
||||
|
||||
def has_perm(self, perm, obj=None):
|
||||
"""
|
||||
Returns True if the user has the specified permission. This method
|
||||
|
@ -165,7 +164,7 @@ class Account(auth.AbstractBaseUser):
|
|||
elif obj and getattr(obj, 'account', None) == self:
|
||||
return True
|
||||
|
||||
|
||||
|
||||
def has_perms(self, perm_list, obj=None):
|
||||
"""
|
||||
Returns True if the user has each of the specified permissions. If
|
||||
|
@ -176,7 +175,7 @@ class Account(auth.AbstractBaseUser):
|
|||
if not self.has_perm(perm, obj):
|
||||
return False
|
||||
return True
|
||||
|
||||
|
||||
def has_module_perms(self, app_label):
|
||||
"""
|
||||
Returns True if the user has any permissions in the given app label.
|
||||
|
|
|
@ -5,7 +5,7 @@ from datetime import date
|
|||
from django.contrib import messages
|
||||
from django.contrib.admin import helpers
|
||||
from django.core.exceptions import ValidationError
|
||||
from django.urls import reverse
|
||||
from django.core.urlresolvers import reverse
|
||||
from django.db import transaction
|
||||
from django.forms.models import modelformset_factory
|
||||
from django.http import HttpResponse, HttpResponseRedirect
|
||||
|
@ -179,7 +179,7 @@ def undo_billing(modeladmin, request, queryset):
|
|||
group[line.order].append(line)
|
||||
except KeyError:
|
||||
group[line.order] = [line]
|
||||
|
||||
|
||||
# Validate
|
||||
for order, lines in group.items():
|
||||
prev = None
|
||||
|
@ -211,7 +211,7 @@ def undo_billing(modeladmin, request, queryset):
|
|||
messages.error(request, "Order does not have lines!.")
|
||||
order.billed_until = billed_until
|
||||
order.billed_on = billed_on
|
||||
|
||||
|
||||
# Commit changes
|
||||
norders, nlines = 0, 0
|
||||
for order, lines in group.items():
|
||||
|
@ -221,7 +221,7 @@ def undo_billing(modeladmin, request, queryset):
|
|||
# TODO update order history undo billing
|
||||
order.save(update_fields=('billed_until', 'billed_on'))
|
||||
norders += 1
|
||||
|
||||
|
||||
messages.success(request, _("%(norders)s orders and %(nlines)s lines undoed.") % {
|
||||
'nlines': nlines,
|
||||
'norders': norders
|
||||
|
|
|
@ -2,12 +2,11 @@ from django import forms
|
|||
from django.conf.urls import url
|
||||
from django.contrib import admin, messages
|
||||
from django.contrib.admin.utils import unquote
|
||||
from django.urls import reverse
|
||||
from django.core.urlresolvers import reverse
|
||||
from django.db import models
|
||||
from django.db.models import F, Sum, Prefetch
|
||||
from django.db.models.functions import Coalesce
|
||||
from django.templatetags.static import static
|
||||
from django.utils.html import format_html
|
||||
from django.utils.safestring import mark_safe
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
from django.shortcuts import redirect
|
||||
|
@ -16,7 +15,7 @@ from orchestra.admin import ExtendedModelAdmin
|
|||
from orchestra.admin.utils import admin_date, insertattr, admin_link, change_url
|
||||
from orchestra.contrib.accounts.actions import list_accounts
|
||||
from orchestra.contrib.accounts.admin import AccountAdminMixin, AccountAdmin
|
||||
from orchestra.forms.widgets import PaddingCheckboxSelectMultiple
|
||||
from orchestra.forms.widgets import paddingCheckboxSelectMultiple
|
||||
|
||||
from . import settings, actions
|
||||
from .filters import (BillTypeListFilter, HasBillContactListFilter, TotalListFilter,
|
||||
|
@ -40,18 +39,18 @@ PAYMENT_STATE_COLORS = {
|
|||
class BillSublineInline(admin.TabularInline):
|
||||
model = BillSubline
|
||||
fields = ('description', 'total', 'type')
|
||||
|
||||
|
||||
def get_readonly_fields(self, request, obj=None):
|
||||
fields = super().get_readonly_fields(request, obj)
|
||||
if obj and not obj.bill.is_open:
|
||||
return self.get_fields(request)
|
||||
return fields
|
||||
|
||||
|
||||
def get_max_num(self, request, obj=None):
|
||||
if obj and not obj.bill.is_open:
|
||||
return 0
|
||||
return super().get_max_num(request, obj)
|
||||
|
||||
|
||||
def has_delete_permission(self, request, obj=None):
|
||||
if obj and not obj.bill.is_open:
|
||||
return False
|
||||
|
@ -65,10 +64,9 @@ class BillLineInline(admin.TabularInline):
|
|||
'subtotal', 'display_total',
|
||||
)
|
||||
readonly_fields = ('display_total', 'order_link')
|
||||
|
||||
|
||||
order_link = admin_link('order', display='pk')
|
||||
|
||||
@mark_safe
|
||||
|
||||
def display_total(self, line):
|
||||
if line.pk:
|
||||
total = line.compute_total()
|
||||
|
@ -80,7 +78,8 @@ class BillLineInline(admin.TabularInline):
|
|||
return '<a href="%s" title="%s">%s <img src="%s"></img></a>' % (url, content, total, img)
|
||||
return '<a href="%s">%s</a>' % (url, total)
|
||||
display_total.short_description = _("Total")
|
||||
|
||||
display_total.allow_tags = True
|
||||
|
||||
def formfield_for_dbfield(self, db_field, **kwargs):
|
||||
""" Make value input widget bigger """
|
||||
if db_field.name == 'description':
|
||||
|
@ -88,7 +87,7 @@ class BillLineInline(admin.TabularInline):
|
|||
elif db_field.name not in ('start_on', 'end_on'):
|
||||
kwargs['widget'] = forms.TextInput(attrs={'size':'6'})
|
||||
return super().formfield_for_dbfield(db_field, **kwargs)
|
||||
|
||||
|
||||
def get_queryset(self, request):
|
||||
qs = super().get_queryset(request)
|
||||
return qs.prefetch_related('sublines').select_related('order')
|
||||
|
@ -97,35 +96,36 @@ class BillLineInline(admin.TabularInline):
|
|||
class ClosedBillLineInline(BillLineInline):
|
||||
# TODO reimplement as nested inlines when upstream
|
||||
# https://code.djangoproject.com/ticket/9025
|
||||
|
||||
|
||||
fields = (
|
||||
'display_description', 'order_link', 'start_on', 'end_on', 'rate', 'quantity', 'tax',
|
||||
'display_subtotal', 'display_total'
|
||||
)
|
||||
readonly_fields = fields
|
||||
can_delete = False
|
||||
|
||||
@mark_safe
|
||||
|
||||
def display_description(self, line):
|
||||
descriptions = [line.description]
|
||||
for subline in line.sublines.all():
|
||||
descriptions.append(' ' * 4 + subline.description)
|
||||
descriptions.append(' '*4+subline.description)
|
||||
return '<br>'.join(descriptions)
|
||||
display_description.short_description = _("Description")
|
||||
|
||||
@mark_safe
|
||||
display_description.allow_tags = True
|
||||
|
||||
def display_subtotal(self, line):
|
||||
subtotals = [' ' + str(line.subtotal)]
|
||||
for subline in line.sublines.all():
|
||||
subtotals.append(str(subline.total))
|
||||
return '<br>'.join(subtotals)
|
||||
display_subtotal.short_description = _("Subtotal")
|
||||
|
||||
display_subtotal.allow_tags = True
|
||||
|
||||
def display_total(self, line):
|
||||
if line.pk:
|
||||
return line.compute_total()
|
||||
display_total.short_description = _("Total")
|
||||
|
||||
display_total.allow_tags = True
|
||||
|
||||
def has_add_permission(self, request):
|
||||
return False
|
||||
|
||||
|
@ -158,28 +158,28 @@ class BillLineAdmin(admin.ModelAdmin):
|
|||
list_select_related = ('bill', 'bill__account')
|
||||
search_fields = ('description', 'bill__number')
|
||||
inlines = (BillSublineInline,)
|
||||
|
||||
|
||||
account_link = admin_link('bill__account')
|
||||
bill_link = admin_link('bill')
|
||||
order_link = admin_link('order')
|
||||
amended_line_link = admin_link('amended_line')
|
||||
|
||||
|
||||
def display_is_open(self, instance):
|
||||
return instance.bill.is_open
|
||||
display_is_open.short_description = _("Is open")
|
||||
display_is_open.boolean = True
|
||||
|
||||
|
||||
def display_sublinetotal(self, instance):
|
||||
total = instance.subline_total
|
||||
return total if total is not None else '---'
|
||||
display_sublinetotal.short_description = _("Sublines")
|
||||
display_sublinetotal.admin_order_field = 'subline_total'
|
||||
|
||||
|
||||
def display_total(self, instance):
|
||||
return round(instance.computed_total or 0, 2)
|
||||
display_total.short_description = _("Total")
|
||||
display_total.admin_order_field = 'computed_total'
|
||||
|
||||
|
||||
def get_readonly_fields(self, request, obj=None):
|
||||
fields = super().get_readonly_fields(request, obj)
|
||||
if obj and not obj.bill.is_open:
|
||||
|
@ -188,7 +188,7 @@ class BillLineAdmin(admin.ModelAdmin):
|
|||
'subtotal', 'order_billed_on', 'order_billed_until'
|
||||
]
|
||||
return fields
|
||||
|
||||
|
||||
def get_queryset(self, request):
|
||||
qs = super().get_queryset(request)
|
||||
qs = qs.annotate(
|
||||
|
@ -196,7 +196,7 @@ class BillLineAdmin(admin.ModelAdmin):
|
|||
computed_total=(F('subtotal') + Sum(Coalesce('sublines__total', 0))) * (1+F('tax')/100),
|
||||
)
|
||||
return qs
|
||||
|
||||
|
||||
def has_delete_permission(self, request, obj=None):
|
||||
if obj and not obj.bill.is_open:
|
||||
return False
|
||||
|
@ -209,7 +209,7 @@ class BillLineManagerAdmin(BillLineAdmin):
|
|||
if self.bill_ids:
|
||||
return qset.filter(bill_id__in=self.bill_ids)
|
||||
return qset
|
||||
|
||||
|
||||
def changelist_view(self, request, extra_context=None):
|
||||
GET_copy = request.GET.copy()
|
||||
bill_ids = GET_copy.pop('ids', None)
|
||||
|
@ -242,7 +242,6 @@ class BillLineManagerAdmin(BillLineAdmin):
|
|||
|
||||
|
||||
class BillAdminMixin(AccountAdminMixin):
|
||||
@mark_safe
|
||||
def display_total_with_subtotals(self, bill):
|
||||
if bill.pk:
|
||||
currency = settings.BILLS_CURRENCY.lower()
|
||||
|
@ -252,10 +251,10 @@ class BillAdminMixin(AccountAdminMixin):
|
|||
subtotals.append(_("Taxes %s%% VAT %s &%s;") % (tax, subtotal[1], currency))
|
||||
subtotals = '\n'.join(subtotals)
|
||||
return '<span title="%s">%s &%s;</span>' % (subtotals, bill.compute_total(), currency)
|
||||
display_total_with_subtotals.allow_tags = True
|
||||
display_total_with_subtotals.short_description = _("total")
|
||||
display_total_with_subtotals.admin_order_field = 'approx_total'
|
||||
|
||||
@mark_safe
|
||||
def display_payment_state(self, bill):
|
||||
if bill.pk:
|
||||
t_opts = bill.transactions.model._meta
|
||||
|
@ -277,6 +276,7 @@ class BillAdminMixin(AccountAdminMixin):
|
|||
color = PAYMENT_STATE_COLORS.get(bill.payment_state, 'grey')
|
||||
return '<a href="{url}" style="color:{color}" title="{title}">{name}</a>'.format(
|
||||
url=url, color=color, name=state, title=title)
|
||||
display_payment_state.allow_tags = True
|
||||
display_payment_state.short_description = _("Payment")
|
||||
|
||||
def get_queryset(self, request):
|
||||
|
@ -304,9 +304,9 @@ class AmendInline(BillAdminMixin, admin.TabularInline):
|
|||
verbose_name_plural = _("Amends")
|
||||
can_delete = False
|
||||
extra = 0
|
||||
|
||||
|
||||
self_link = admin_link('__str__')
|
||||
|
||||
|
||||
def has_add_permission(self, *args, **kwargs):
|
||||
return False
|
||||
|
||||
|
@ -354,12 +354,12 @@ class BillAdmin(BillAdminMixin, ExtendedModelAdmin):
|
|||
'closed_on_display', 'updated_on_display', 'display_total_with_subtotals',
|
||||
)
|
||||
date_hierarchy = 'closed_on'
|
||||
|
||||
|
||||
created_on_display = admin_date('created_on', short_description=_("Created"))
|
||||
closed_on_display = admin_date('closed_on', short_description=_("Closed"))
|
||||
updated_on_display = admin_date('updated_on', short_description=_("Updated"))
|
||||
amend_of_link = admin_link('amend_of')
|
||||
|
||||
|
||||
# def amend_links(self, bill):
|
||||
# links = []
|
||||
# for amend in bill.amends.all():
|
||||
|
@ -368,25 +368,27 @@ class BillAdmin(BillAdminMixin, ExtendedModelAdmin):
|
|||
# return '<br>'.join(links)
|
||||
# amend_links.short_description = _("Amends")
|
||||
# amend_links.allow_tags = True
|
||||
|
||||
|
||||
def num_lines(self, bill):
|
||||
return bill.lines__count
|
||||
num_lines.admin_order_field = 'lines__count'
|
||||
num_lines.short_description = _("lines")
|
||||
|
||||
|
||||
def display_total(self, bill):
|
||||
currency = settings.BILLS_CURRENCY.lower()
|
||||
return format_html('{} &{};', bill.compute_total(), currency)
|
||||
return '%s &%s;' % (bill.compute_total(), currency)
|
||||
display_total.allow_tags = True
|
||||
display_total.short_description = _("total")
|
||||
display_total.admin_order_field = 'approx_total'
|
||||
|
||||
|
||||
def type_link(self, bill):
|
||||
bill_type = bill.type.lower()
|
||||
url = reverse('admin:bills_%s_changelist' % bill_type)
|
||||
return format_html('<a href="{}">{}</a>', url, bill.get_type_display())
|
||||
return '<a href="%s">%s</a>' % (url, bill.get_type_display())
|
||||
type_link.allow_tags = True
|
||||
type_link.short_description = _("type")
|
||||
type_link.admin_order_field = 'type'
|
||||
|
||||
|
||||
def get_urls(self):
|
||||
""" Hook bill lines management URLs on bill admin """
|
||||
urls = super().get_urls()
|
||||
|
@ -397,13 +399,13 @@ class BillAdmin(BillAdminMixin, ExtendedModelAdmin):
|
|||
name='bills_bill_manage_lines'),
|
||||
]
|
||||
return extra_urls + urls
|
||||
|
||||
|
||||
def get_readonly_fields(self, request, obj=None):
|
||||
fields = super().get_readonly_fields(request, obj)
|
||||
if obj and not obj.is_open:
|
||||
fields += self.add_fields
|
||||
return fields
|
||||
|
||||
|
||||
def get_fieldsets(self, request, obj=None):
|
||||
fieldsets = super().get_fieldsets(request, obj)
|
||||
if obj:
|
||||
|
@ -416,7 +418,7 @@ class BillAdmin(BillAdminMixin, ExtendedModelAdmin):
|
|||
if obj.is_open:
|
||||
fieldsets = fieldsets[0:-1]
|
||||
return fieldsets
|
||||
|
||||
|
||||
def get_change_view_actions(self, obj=None):
|
||||
actions = super().get_change_view_actions(obj)
|
||||
exclude = []
|
||||
|
@ -426,7 +428,7 @@ class BillAdmin(BillAdminMixin, ExtendedModelAdmin):
|
|||
if obj.type not in obj.AMEND_MAP:
|
||||
exclude += ['amend_bills']
|
||||
return [action for action in actions if action.__name__ not in exclude]
|
||||
|
||||
|
||||
def get_inline_instances(self, request, obj=None):
|
||||
cls = type(self)
|
||||
if obj and not obj.is_open:
|
||||
|
@ -437,7 +439,7 @@ class BillAdmin(BillAdminMixin, ExtendedModelAdmin):
|
|||
else:
|
||||
cls.inlines = [BillLineInline]
|
||||
return super().get_inline_instances(request, obj)
|
||||
|
||||
|
||||
def formfield_for_dbfield(self, db_field, **kwargs):
|
||||
""" Make value input widget bigger """
|
||||
if db_field.name == 'comments':
|
||||
|
@ -448,7 +450,7 @@ class BillAdmin(BillAdminMixin, ExtendedModelAdmin):
|
|||
if db_field.name == 'amend_of':
|
||||
formfield.queryset = formfield.queryset.filter(is_open=False)
|
||||
return formfield
|
||||
|
||||
|
||||
def change_view(self, request, object_id, **kwargs):
|
||||
# TODO raise404, here and everywhere
|
||||
bill = self.get_object(request, unquote(object_id))
|
||||
|
@ -469,7 +471,7 @@ admin.site.register(BillLine, BillLineAdmin)
|
|||
class BillContactInline(admin.StackedInline):
|
||||
model = BillContact
|
||||
fields = ('name', 'address', ('city', 'zipcode'), 'country', 'vat')
|
||||
|
||||
|
||||
def formfield_for_dbfield(self, db_field, **kwargs):
|
||||
""" Make value input widget bigger """
|
||||
if db_field.name == 'name':
|
||||
|
@ -477,7 +479,7 @@ class BillContactInline(admin.StackedInline):
|
|||
if db_field.name == 'address':
|
||||
kwargs['widget'] = forms.Textarea(attrs={'cols': 70, 'rows': 2})
|
||||
if db_field.name == 'email_usage':
|
||||
kwargs['widget'] = PaddingCheckboxSelectMultiple(45)
|
||||
kwargs['widget'] = paddingCheckboxSelectMultiple(45)
|
||||
return super().formfield_for_dbfield(db_field, **kwargs)
|
||||
|
||||
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
from django.http import HttpResponse
|
||||
from rest_framework import viewsets
|
||||
from rest_framework.decorators import action
|
||||
from rest_framework.decorators import detail_route
|
||||
|
||||
from orchestra.api import router, LogApiMixin
|
||||
from orchestra.contrib.accounts.api import AccountApiMixin
|
||||
|
@ -14,8 +14,8 @@ from .serializers import BillSerializer
|
|||
class BillViewSet(LogApiMixin, AccountApiMixin, viewsets.ModelViewSet):
|
||||
queryset = Bill.objects.all()
|
||||
serializer_class = BillSerializer
|
||||
|
||||
@action(detail=True, methods=['get'])
|
||||
|
||||
@detail_route(methods=['get'])
|
||||
def document(self, request, pk):
|
||||
bill = self.get_object()
|
||||
content_type = request.META.get('HTTP_ACCEPT')
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
from django.contrib.admin import SimpleListFilter
|
||||
from django.urls import reverse
|
||||
from django.core.urlresolvers import reverse
|
||||
from django.db.models import Q
|
||||
from django.utils.safestring import mark_safe
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
|
@ -11,11 +11,11 @@ class BillTypeListFilter(SimpleListFilter):
|
|||
""" Filter tickets by created_by according to request.user """
|
||||
title = 'Type'
|
||||
parameter_name = ''
|
||||
|
||||
|
||||
def __init__(self, request, *args, **kwargs):
|
||||
super(BillTypeListFilter, self).__init__(request, *args, **kwargs)
|
||||
self.request = request
|
||||
|
||||
|
||||
def lookups(self, request, model_admin):
|
||||
return (
|
||||
('bill', _("All")),
|
||||
|
@ -25,13 +25,13 @@ class BillTypeListFilter(SimpleListFilter):
|
|||
('amendmentfee', _("Amendment fee")),
|
||||
('amendmentinvoice', _("Amendment invoice")),
|
||||
)
|
||||
|
||||
|
||||
def queryset(self, request, queryset):
|
||||
return queryset
|
||||
|
||||
|
||||
def value(self):
|
||||
return self.request.path.split('/')[-2]
|
||||
|
||||
|
||||
def choices(self, cl):
|
||||
query = self.request.GET.urlencode()
|
||||
for lookup, title in self.lookup_choices:
|
||||
|
@ -45,7 +45,7 @@ class BillTypeListFilter(SimpleListFilter):
|
|||
class TotalListFilter(SimpleListFilter):
|
||||
title = _("total")
|
||||
parameter_name = 'total'
|
||||
|
||||
|
||||
def lookups(self, request, model_admin):
|
||||
return (
|
||||
('gt', mark_safe("total > 0")),
|
||||
|
@ -53,7 +53,7 @@ class TotalListFilter(SimpleListFilter):
|
|||
('eq', "total = 0"),
|
||||
('ne', mark_safe("total ≠ 0")),
|
||||
)
|
||||
|
||||
|
||||
def queryset(self, request, queryset):
|
||||
if self.value() == 'gt':
|
||||
return queryset.filter(approx_total__gt=0)
|
||||
|
@ -70,13 +70,13 @@ class HasBillContactListFilter(SimpleListFilter):
|
|||
""" Filter Nodes by group according to request.user """
|
||||
title = _("has bill contact")
|
||||
parameter_name = 'bill'
|
||||
|
||||
|
||||
def lookups(self, request, model_admin):
|
||||
return (
|
||||
('True', _("Yes")),
|
||||
('False', _("No")),
|
||||
)
|
||||
|
||||
|
||||
def queryset(self, request, queryset):
|
||||
if self.value() == 'True':
|
||||
return queryset.filter(billcontact__isnull=False)
|
||||
|
@ -87,7 +87,7 @@ class HasBillContactListFilter(SimpleListFilter):
|
|||
class PaymentStateListFilter(SimpleListFilter):
|
||||
title = _("payment state")
|
||||
parameter_name = 'payment_state'
|
||||
|
||||
|
||||
def lookups(self, request, model_admin):
|
||||
return (
|
||||
('OPEN', _("Open")),
|
||||
|
@ -95,7 +95,7 @@ class PaymentStateListFilter(SimpleListFilter):
|
|||
('PENDING', _("Pending")),
|
||||
('BAD_DEBT', _("Bad debt")),
|
||||
)
|
||||
|
||||
|
||||
def queryset(self, request, queryset):
|
||||
# FIXME use queryset.computed_total instead of approx_total, bills.admin.BillAdmin.get_queryset
|
||||
Transaction = queryset.model.transactions.field.remote_field.related_model
|
||||
|
@ -137,7 +137,7 @@ class PaymentStateListFilter(SimpleListFilter):
|
|||
class AmendedListFilter(SimpleListFilter):
|
||||
title = _("amended")
|
||||
parameter_name = 'amended'
|
||||
|
||||
|
||||
def lookups(self, request, model_admin):
|
||||
return (
|
||||
('3', _("Closed amends")),
|
||||
|
@ -145,7 +145,7 @@ class AmendedListFilter(SimpleListFilter):
|
|||
('1', _("Any amends")),
|
||||
('0', _("No amends")),
|
||||
)
|
||||
|
||||
|
||||
def queryset(self, request, queryset):
|
||||
if self.value() is None:
|
||||
return queryset
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
from django.contrib import messages
|
||||
from django.urls import reverse
|
||||
from django.core.urlresolvers import reverse
|
||||
from django.utils.encoding import force_text
|
||||
from django.utils.html import format_html
|
||||
from django.utils.safestring import mark_safe
|
||||
|
@ -21,7 +21,7 @@ def validate_contact(request, bill, error=True):
|
|||
message = msg.format(relation=_("Related"), account=account, url=url)
|
||||
send(request, mark_safe(message))
|
||||
valid = False
|
||||
main = type(bill).account.field.related_model.objects.get_main()
|
||||
main = type(bill).account.field.rel.to.objects.get_main()
|
||||
if not hasattr(main, 'billcontact'):
|
||||
account = force_text(main)
|
||||
url = reverse('admin:accounts_account_change', args=(main.id,))
|
||||
|
|
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
|
@ -1,7 +1,6 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
from __future__ import unicode_literals
|
||||
|
||||
import django.db.models.deletion
|
||||
from django.db import models, migrations
|
||||
|
||||
|
||||
|
@ -15,7 +14,7 @@ class Migration(migrations.Migration):
|
|||
migrations.AddField(
|
||||
model_name='bill',
|
||||
name='amend_of',
|
||||
field=models.ForeignKey(to='bills.Bill', blank=True, on_delete=django.db.models.deletion.CASCADE, related_name='amends', verbose_name='amend of', null=True),
|
||||
field=models.ForeignKey(to='bills.Bill', blank=True, related_name='amends', verbose_name='amend of', null=True),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='billcontact',
|
||||
|
|
File diff suppressed because one or more lines are too long
|
@ -1,12 +1,12 @@
|
|||
import datetime
|
||||
from dateutil.relativedelta import relativedelta
|
||||
|
||||
from django.urls import reverse
|
||||
from django.core.urlresolvers import reverse
|
||||
from django.core.validators import ValidationError, RegexValidator
|
||||
from django.db import models
|
||||
from django.db.models import F, Sum
|
||||
from django.db.models.functions import Coalesce
|
||||
from django.template import loader
|
||||
from django.template import loader, Context
|
||||
from django.utils import timezone, translation
|
||||
from django.utils.encoding import force_text
|
||||
from django.utils.functional import cached_property
|
||||
|
@ -24,7 +24,7 @@ from . import settings
|
|||
|
||||
class BillContact(models.Model):
|
||||
account = models.OneToOneField('accounts.Account', verbose_name=_("account"),
|
||||
related_name='billcontact', on_delete=models.CASCADE)
|
||||
related_name='billcontact')
|
||||
name = models.CharField(_("name"), max_length=256, blank=True,
|
||||
help_text=_("Account full name will be used when left blank."))
|
||||
address = models.TextField(_("address"))
|
||||
|
@ -36,13 +36,13 @@ class BillContact(models.Model):
|
|||
choices=settings.BILLS_CONTACT_COUNTRIES,
|
||||
default=settings.BILLS_CONTACT_DEFAULT_COUNTRY)
|
||||
vat = models.CharField(_("VAT number"), max_length=64)
|
||||
|
||||
|
||||
def __str__(self):
|
||||
return self.name
|
||||
|
||||
|
||||
def get_name(self):
|
||||
return self.name or self.account.get_full_name()
|
||||
|
||||
|
||||
def clean(self):
|
||||
self.vat = self.vat.strip()
|
||||
self.city = self.city.strip()
|
||||
|
@ -99,12 +99,12 @@ class Bill(models.Model):
|
|||
INVOICE: AMENDMENTINVOICE,
|
||||
FEE: AMENDMENTFEE,
|
||||
}
|
||||
|
||||
|
||||
number = models.CharField(_("number"), max_length=16, unique=True, blank=True)
|
||||
account = models.ForeignKey('accounts.Account', verbose_name=_("account"),
|
||||
related_name='%(class)s', on_delete=models.CASCADE)
|
||||
related_name='%(class)s')
|
||||
amend_of = models.ForeignKey('self', null=True, blank=True, verbose_name=_("amend of"),
|
||||
related_name='amends', on_delete=models.SET_NULL)
|
||||
related_name='amends')
|
||||
type = models.CharField(_("type"), max_length=16, choices=TYPES)
|
||||
created_on = models.DateField(_("created on"), auto_now_add=True)
|
||||
closed_on = models.DateField(_("closed on"), blank=True, null=True, db_index=True)
|
||||
|
@ -115,37 +115,37 @@ class Bill(models.Model):
|
|||
# total = models.DecimalField(max_digits=12, decimal_places=2, null=True)
|
||||
comments = models.TextField(_("comments"), blank=True)
|
||||
html = models.TextField(_("HTML"), blank=True)
|
||||
|
||||
|
||||
objects = BillManager()
|
||||
|
||||
|
||||
class Meta:
|
||||
get_latest_by = 'id'
|
||||
|
||||
|
||||
def __str__(self):
|
||||
return self.number
|
||||
|
||||
|
||||
@classmethod
|
||||
def get_class_type(cls):
|
||||
if cls is models.DEFERRED:
|
||||
cls = cls.__base__
|
||||
return cls.__name__.upper()
|
||||
|
||||
|
||||
@cached_property
|
||||
def total(self):
|
||||
return self.compute_total()
|
||||
|
||||
|
||||
@cached_property
|
||||
def seller(self):
|
||||
return Account.objects.get_main().billcontact
|
||||
|
||||
|
||||
@cached_property
|
||||
def buyer(self):
|
||||
return self.account.billcontact
|
||||
|
||||
|
||||
@property
|
||||
def has_multiple_pages(self):
|
||||
return self.type != self.FEE
|
||||
|
||||
|
||||
@cached_property
|
||||
def payment_state(self):
|
||||
if self.is_open or self.get_type() == self.PROFORMA:
|
||||
|
@ -192,7 +192,7 @@ class Bill(models.Model):
|
|||
elif executed:
|
||||
return self.EXECUTED
|
||||
return self.BAD_DEBT
|
||||
|
||||
|
||||
def clean(self):
|
||||
if self.amend_of_id:
|
||||
errors = {}
|
||||
|
@ -206,27 +206,27 @@ class Bill(models.Model):
|
|||
errors['amend_of'] = _("Related invoice is an amendment.")
|
||||
if errors:
|
||||
raise ValidationError(errors)
|
||||
|
||||
|
||||
def get_payment_state_display(self):
|
||||
value = self.payment_state
|
||||
return force_text(dict(self.PAYMENT_STATES).get(value, value))
|
||||
|
||||
|
||||
def get_current_transaction(self):
|
||||
return self.transactions.exclude_rejected().first()
|
||||
|
||||
|
||||
def get_type(self):
|
||||
return self.type or self.get_class_type()
|
||||
|
||||
|
||||
@property
|
||||
def is_amend(self):
|
||||
return self.type in self.AMEND_MAP.values()
|
||||
|
||||
|
||||
def get_amend_type(self):
|
||||
amend_type = self.AMEND_MAP.get(self.type)
|
||||
if amend_type is None:
|
||||
raise TypeError("%s has no associated amend type." % self.type)
|
||||
return amend_type
|
||||
|
||||
|
||||
def get_number(self):
|
||||
cls = type(self)
|
||||
if cls is models.DEFERRED:
|
||||
|
@ -250,16 +250,16 @@ class Bill(models.Model):
|
|||
zeros = (number_length - len(str(number))) * '0'
|
||||
number = zeros + str(number)
|
||||
return '{prefix}{year}{number}'.format(prefix=prefix, year=year, number=number)
|
||||
|
||||
|
||||
def get_due_date(self, payment=None):
|
||||
now = timezone.now()
|
||||
if payment:
|
||||
return now + payment.get_due_delta()
|
||||
return now + relativedelta(months=1)
|
||||
|
||||
|
||||
def get_absolute_url(self):
|
||||
return reverse('admin:bills_bill_view', args=(self.pk,))
|
||||
|
||||
|
||||
def close(self, payment=False):
|
||||
if not self.is_open:
|
||||
raise TypeError("Bill not in Open state.")
|
||||
|
@ -278,10 +278,10 @@ class Bill(models.Model):
|
|||
self.html = self.render(payment=payment)
|
||||
self.save()
|
||||
return transaction
|
||||
|
||||
|
||||
def get_billing_contact_emails(self):
|
||||
return self.account.get_contacts_emails(usages=(Contact.BILLING,))
|
||||
|
||||
|
||||
def send(self):
|
||||
pdf = self.as_pdf()
|
||||
self.account.send_email(
|
||||
|
@ -298,12 +298,12 @@ class Bill(models.Model):
|
|||
)
|
||||
self.is_sent = True
|
||||
self.save(update_fields=['is_sent'])
|
||||
|
||||
|
||||
def render(self, payment=False, language=None):
|
||||
with translation.override(language or self.account.language):
|
||||
if payment is False:
|
||||
payment = self.account.paymentsources.get_default()
|
||||
context = {
|
||||
context = Context({
|
||||
'bill': self,
|
||||
'lines': self.lines.all().prefetch_related('sublines'),
|
||||
'seller': self.seller,
|
||||
|
@ -318,29 +318,29 @@ class Bill(models.Model):
|
|||
'payment': payment and payment.get_bill_context(),
|
||||
'default_due_date': self.get_due_date(payment=payment),
|
||||
'now': timezone.now(),
|
||||
}
|
||||
})
|
||||
template_name = 'BILLS_%s_TEMPLATE' % self.get_type()
|
||||
template = getattr(settings, template_name, settings.BILLS_DEFAULT_TEMPLATE)
|
||||
bill_template = loader.get_template(template)
|
||||
html = bill_template.render(context)
|
||||
html = html.replace('-pageskip-', '<pdf:nextpage />')
|
||||
return html
|
||||
|
||||
|
||||
def as_pdf(self):
|
||||
html = self.html or self.render()
|
||||
return html_to_pdf(html, pagination=self.has_multiple_pages)
|
||||
|
||||
|
||||
def updated(self):
|
||||
self.updated_on = timezone.now()
|
||||
self.save(update_fields=('updated_on',))
|
||||
|
||||
|
||||
def save(self, *args, **kwargs):
|
||||
if not self.type:
|
||||
self.type = self.get_type()
|
||||
if not self.number:
|
||||
self.number = self.get_number()
|
||||
super(Bill, self).save(*args, **kwargs)
|
||||
|
||||
|
||||
@cached
|
||||
def compute_subtotals(self):
|
||||
subtotals = {}
|
||||
|
@ -354,21 +354,21 @@ class Bill(models.Model):
|
|||
for tax, subtotal in subtotals.items():
|
||||
result[tax] = [subtotal, round(tax/100*subtotal, 2)]
|
||||
return result
|
||||
|
||||
|
||||
@cached
|
||||
def compute_base(self):
|
||||
bases = self.lines.annotate(
|
||||
bases=F('subtotal') + Sum(Coalesce('sublines__total', 0))
|
||||
)
|
||||
return round(bases.aggregate(Sum('bases'))['bases__sum'] or 0, 2)
|
||||
|
||||
|
||||
@cached
|
||||
def compute_tax(self):
|
||||
taxes = self.lines.annotate(
|
||||
taxes=(F('subtotal') + Coalesce(Sum('sublines__total'), 0)) * (F('tax')/100)
|
||||
)
|
||||
return round(taxes.aggregate(Sum('taxes'))['taxes__sum'] or 0, 2)
|
||||
|
||||
|
||||
@cached
|
||||
def compute_total(self):
|
||||
if 'lines' in getattr(self, '_prefetched_objects_cache', ()):
|
||||
|
@ -416,7 +416,7 @@ class ProForma(Bill):
|
|||
|
||||
class BillLine(models.Model):
|
||||
""" Base model for bill item representation """
|
||||
bill = models.ForeignKey(Bill, verbose_name=_("bill"), related_name='lines', on_delete=models.CASCADE)
|
||||
bill = models.ForeignKey(Bill, verbose_name=_("bill"), related_name='lines')
|
||||
description = models.CharField(_("description"), max_length=256)
|
||||
rate = models.DecimalField(_("rate"), blank=True, null=True, max_digits=12, decimal_places=2)
|
||||
quantity = models.DecimalField(_("quantity"), blank=True, null=True, max_digits=12,
|
||||
|
@ -434,24 +434,24 @@ class BillLine(models.Model):
|
|||
created_on = models.DateField(_("created"), auto_now_add=True)
|
||||
# Amendment
|
||||
amended_line = models.ForeignKey('self', verbose_name=_("amended line"),
|
||||
related_name='amendment_lines', null=True, blank=True, on_delete=models.CASCADE)
|
||||
|
||||
related_name='amendment_lines', null=True, blank=True)
|
||||
|
||||
class Meta:
|
||||
get_latest_by = 'id'
|
||||
|
||||
|
||||
def __str__(self):
|
||||
return "#%i" % self.pk if self.pk else self.description
|
||||
|
||||
|
||||
def get_verbose_quantity(self):
|
||||
return self.verbose_quantity or self.quantity
|
||||
|
||||
|
||||
def clean(self):
|
||||
if not self.verbose_quantity:
|
||||
quantity = str(self.quantity)
|
||||
# Strip trailing zeros
|
||||
if quantity.endswith('0'):
|
||||
self.verbose_quantity = quantity.strip('0').strip('.')
|
||||
|
||||
|
||||
def get_verbose_period(self):
|
||||
from django.template.defaultfilters import date
|
||||
date_format = "N 'y"
|
||||
|
@ -467,7 +467,7 @@ class BillLine(models.Model):
|
|||
if ini == end:
|
||||
return ini
|
||||
return "{ini} / {end}".format(ini=ini, end=end)
|
||||
|
||||
|
||||
@cached
|
||||
def compute_total(self):
|
||||
total = self.subtotal or 0
|
||||
|
@ -478,7 +478,7 @@ class BillLine(models.Model):
|
|||
else:
|
||||
total += self.sublines.aggregate(sub_total=Sum('total'))['sub_total'] or 0
|
||||
return round(total, 2)
|
||||
|
||||
|
||||
def get_absolute_url(self):
|
||||
return change_url(self)
|
||||
|
||||
|
@ -493,12 +493,12 @@ class BillSubline(models.Model):
|
|||
(COMPENSATION, _("Compensation")),
|
||||
(OTHER, _("Other")),
|
||||
)
|
||||
|
||||
|
||||
# TODO: order info for undoing
|
||||
line = models.ForeignKey(BillLine, verbose_name=_("bill line"), related_name='sublines', on_delete=models.CASCADE)
|
||||
line = models.ForeignKey(BillLine, verbose_name=_("bill line"), related_name='sublines')
|
||||
description = models.CharField(_("description"), max_length=256)
|
||||
total = models.DecimalField(max_digits=12, decimal_places=2)
|
||||
type = models.CharField(_("type"), max_length=16, choices=TYPES, default=OTHER)
|
||||
|
||||
|
||||
def __str__(self):
|
||||
return "%s %i" % (self.description, self.total)
|
||||
|
|
|
@ -7,7 +7,7 @@ from orchestra.admin.actions import SendEmail
|
|||
from orchestra.admin.utils import insertattr, change_url
|
||||
from orchestra.contrib.accounts.actions import list_accounts
|
||||
from orchestra.contrib.accounts.admin import AccountAdmin, AccountAdminMixin
|
||||
from orchestra.forms.widgets import PaddingCheckboxSelectMultiple
|
||||
from orchestra.forms.widgets import paddingCheckboxSelectMultiple
|
||||
|
||||
from .filters import EmailUsageListFilter
|
||||
from .models import Contact
|
||||
|
@ -61,18 +61,18 @@ class ContactAdmin(AccountAdminMixin, ExtendedModelAdmin):
|
|||
}),
|
||||
)
|
||||
actions = (SendEmail(), list_accounts)
|
||||
|
||||
|
||||
def dispaly_name(self, contact):
|
||||
return str(contact)
|
||||
dispaly_name.short_description = _("Name")
|
||||
dispaly_name.admin_order_field = 'short_name'
|
||||
|
||||
|
||||
def formfield_for_dbfield(self, db_field, **kwargs):
|
||||
""" Make value input widget bigger """
|
||||
if db_field.name == 'address':
|
||||
kwargs['widget'] = forms.Textarea(attrs={'cols': 70, 'rows': 2})
|
||||
if db_field.name == 'email_usage':
|
||||
kwargs['widget'] = PaddingCheckboxSelectMultiple(130)
|
||||
kwargs['widget'] = paddingCheckboxSelectMultiple(130)
|
||||
return super(ContactAdmin, self).formfield_for_dbfield(db_field, **kwargs)
|
||||
|
||||
|
||||
|
@ -86,14 +86,14 @@ class ContactInline(admin.StackedInline):
|
|||
fields = (
|
||||
('short_name', 'full_name'), 'email', 'email_usage', ('phone', 'phone2'),
|
||||
)
|
||||
|
||||
|
||||
def get_extra(self, request, obj=None, **kwargs):
|
||||
return 0 if obj and obj.contacts.exists() else 1
|
||||
|
||||
|
||||
def get_view_on_site_url(self, obj=None):
|
||||
if obj:
|
||||
return change_url(obj)
|
||||
|
||||
|
||||
def formfield_for_dbfield(self, db_field, **kwargs):
|
||||
""" Make value input widget bigger """
|
||||
if db_field.name == 'short_name':
|
||||
|
@ -101,7 +101,7 @@ class ContactInline(admin.StackedInline):
|
|||
if db_field.name == 'address':
|
||||
kwargs['widget'] = forms.Textarea(attrs={'cols': 70, 'rows': 2})
|
||||
if db_field.name == 'email_usage':
|
||||
kwargs['widget'] = PaddingCheckboxSelectMultiple(45)
|
||||
kwargs['widget'] = paddingCheckboxSelectMultiple(45)
|
||||
return super(ContactInline, self).formfield_for_dbfield(db_field, **kwargs)
|
||||
|
||||
|
||||
|
|
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
|
@ -29,11 +29,11 @@ class Contact(models.Model):
|
|||
('ADDS', _("Announcements")),
|
||||
('EMERGENCY', _("Emergency contact")),
|
||||
)
|
||||
|
||||
|
||||
objects = ContactQuerySet.as_manager()
|
||||
|
||||
|
||||
account = models.ForeignKey('accounts.Account', verbose_name=_("Account"),
|
||||
related_name='contacts', null=True, on_delete=models.SET_NULL)
|
||||
related_name='contacts', null=True)
|
||||
short_name = models.CharField(_("short name"), max_length=128)
|
||||
full_name = models.CharField(_("full name"), max_length=256, blank=True)
|
||||
email = models.EmailField()
|
||||
|
@ -54,10 +54,10 @@ class Contact(models.Model):
|
|||
country = models.CharField(_("country"), max_length=20, blank=True,
|
||||
choices=settings.CONTACTS_COUNTRIES,
|
||||
default=settings.CONTACTS_DEFAULT_COUNTRY)
|
||||
|
||||
|
||||
def __str__(self):
|
||||
return self.full_name or self.short_name
|
||||
|
||||
|
||||
def clean(self):
|
||||
self.short_name = self.short_name.strip()
|
||||
self.full_name = self.full_name.strip()
|
||||
|
|
|
@ -1,8 +1,6 @@
|
|||
from django.conf.urls import url
|
||||
from django.contrib import admin
|
||||
from django.contrib.auth.admin import UserAdmin
|
||||
from django.utils.html import format_html
|
||||
from django.utils.safestring import mark_safe
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
|
||||
from orchestra.admin import ExtendedModelAdmin, ChangePasswordAdminMixin
|
||||
|
@ -51,17 +49,17 @@ class DatabaseAdmin(SelectAccountAdminMixin, ExtendedModelAdmin):
|
|||
filter_by_account_fields = ('users',)
|
||||
list_prefetch_related = ('users',)
|
||||
actions = (list_accounts, save_selected)
|
||||
|
||||
@mark_safe
|
||||
|
||||
def display_users(self, db):
|
||||
links = []
|
||||
for user in db.users.all():
|
||||
link = format_html('<a href="{}">{}</a>', change_url(user), user.username)
|
||||
link = '<a href="%s">%s</a>' % (change_url(user), user.username)
|
||||
links.append(link)
|
||||
return '<br>'.join(links)
|
||||
display_users.short_description = _("Users")
|
||||
display_users.allow_tags = True
|
||||
display_users.admin_order_field = 'users__username'
|
||||
|
||||
|
||||
def save_model(self, request, obj, form, change):
|
||||
super(DatabaseAdmin, self).save_model(request, obj, form, change)
|
||||
if not change:
|
||||
|
@ -100,24 +98,24 @@ class DatabaseUserAdmin(SelectAccountAdminMixin, ChangePasswordAdminMixin, Exten
|
|||
filter_by_account_fields = ('databases',)
|
||||
list_prefetch_related = ('databases',)
|
||||
actions = (list_accounts, save_selected)
|
||||
|
||||
@mark_safe
|
||||
|
||||
def display_databases(self, user):
|
||||
links = []
|
||||
for db in user.databases.all():
|
||||
link = format_html('<a href="{}">{}</a>', change_url(db), db.name)
|
||||
link = '<a href="%s">%s</a>' % (change_url(db), db.name)
|
||||
links.append(link)
|
||||
return '<br>'.join(links)
|
||||
display_databases.short_description = _("Databases")
|
||||
display_databases.allow_tags = True
|
||||
display_databases.admin_order_field = 'databases__name'
|
||||
|
||||
|
||||
def get_urls(self):
|
||||
useradmin = UserAdmin(DatabaseUser, self.admin_site)
|
||||
return [
|
||||
url(r'^(\d+)/password/$',
|
||||
self.admin_site.admin_view(useradmin.user_change_password))
|
||||
] + super(DatabaseUserAdmin, self).get_urls()
|
||||
|
||||
|
||||
def save_model(self, request, obj, form, change):
|
||||
""" set password """
|
||||
if not change:
|
||||
|
|
|
@ -17,11 +17,11 @@ class DatabaseUserCreationForm(forms.ModelForm):
|
|||
password2 = forms.CharField(label=_("Password confirmation"), required=False,
|
||||
widget=forms.PasswordInput,
|
||||
help_text=_("Enter the same password as above, for verification."))
|
||||
|
||||
|
||||
class Meta:
|
||||
model = DatabaseUser
|
||||
fields = ('username', 'account', 'type')
|
||||
|
||||
|
||||
def clean_password2(self):
|
||||
password1 = self.cleaned_data.get("password1")
|
||||
password2 = self.cleaned_data.get("password2")
|
||||
|
@ -40,11 +40,11 @@ class DatabaseCreationForm(DatabaseUserCreationForm):
|
|||
'invalid': _("This value may contain 16 characters or fewer, only letters, numbers and "
|
||||
"@/./+/-/_ characters.")})
|
||||
user = forms.ModelChoiceField(required=False, queryset=DatabaseUser.objects)
|
||||
|
||||
|
||||
class Meta:
|
||||
model = Database
|
||||
fields = ('username', 'account', 'type')
|
||||
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super(DatabaseCreationForm, self).__init__(*args, **kwargs)
|
||||
account_id = self.initial.get('account', self.initial_account)
|
||||
|
@ -53,13 +53,13 @@ class DatabaseCreationForm(DatabaseUserCreationForm):
|
|||
choices = [ (u.pk, "%s (%s)" % (u, u.get_type_display())) for u in qs ]
|
||||
self.fields['user'].queryset = qs
|
||||
self.fields['user'].choices = [(None, '--------'),] + choices
|
||||
|
||||
|
||||
def clean_username(self):
|
||||
username = self.cleaned_data.get('username')
|
||||
if DatabaseUser.objects.filter(username=username).exists():
|
||||
raise ValidationError("Provided username already exists.")
|
||||
return username
|
||||
|
||||
|
||||
def clean_password2(self):
|
||||
username = self.cleaned_data.get('username')
|
||||
password1 = self.cleaned_data.get('password1')
|
||||
|
@ -70,14 +70,14 @@ class DatabaseCreationForm(DatabaseUserCreationForm):
|
|||
msg = _("The two password fields didn't match.")
|
||||
raise ValidationError(msg)
|
||||
return password2
|
||||
|
||||
|
||||
def clean_user(self):
|
||||
user = self.cleaned_data.get('user')
|
||||
if user and user.type != self.cleaned_data.get('type'):
|
||||
msg = _("Database type and user type doesn't match")
|
||||
raise ValidationError(msg)
|
||||
return user
|
||||
|
||||
|
||||
def clean(self):
|
||||
cleaned_data = super(DatabaseCreationForm, self).clean()
|
||||
if 'user' in cleaned_data and 'username' in cleaned_data:
|
||||
|
@ -91,7 +91,7 @@ class DatabaseCreationForm(DatabaseUserCreationForm):
|
|||
|
||||
class ReadOnlySQLPasswordHashField(ReadOnlyPasswordHashField):
|
||||
class ReadOnlyPasswordHashWidget(forms.Widget):
|
||||
def render(self, name, value, attrs, renderer=None):
|
||||
def render(self, name, value, attrs):
|
||||
original = ReadOnlyPasswordHashField.widget().render(name, value, attrs)
|
||||
if 'Invalid' not in original:
|
||||
return original
|
||||
|
@ -114,10 +114,10 @@ class DatabaseUserChangeForm(forms.ModelForm):
|
|||
"this user's password, but you can change the password "
|
||||
"using <a href='../password/'>this form</a>. "
|
||||
"<a onclick='return showAddAnotherPopup(this);' href='../hash/'>Show hash</a>."))
|
||||
|
||||
|
||||
class Meta:
|
||||
model = DatabaseUser
|
||||
fields = ('username', 'password', 'type', 'account')
|
||||
|
||||
|
||||
def clean_password(self):
|
||||
return self.initial["password"]
|
||||
|
|
|
@ -3,7 +3,6 @@ from __future__ import unicode_literals
|
|||
|
||||
from django.db import models, migrations
|
||||
from django.conf import settings
|
||||
import django.db.models.deletion
|
||||
import orchestra.core.validators
|
||||
|
||||
|
||||
|
@ -20,7 +19,7 @@ class Migration(migrations.Migration):
|
|||
('id', models.AutoField(serialize=False, primary_key=True, verbose_name='ID', auto_created=True)),
|
||||
('name', models.CharField(verbose_name='name', max_length=64, validators=[orchestra.core.validators.validate_name])),
|
||||
('type', models.CharField(default='mysql', choices=[('mysql', 'MySQL'), ('postgres', 'PostgreSQL')], verbose_name='type', max_length=32)),
|
||||
('account', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='databases', verbose_name='Account', to=settings.AUTH_USER_MODEL)),
|
||||
('account', models.ForeignKey(related_name='databases', verbose_name='Account', to=settings.AUTH_USER_MODEL)),
|
||||
],
|
||||
),
|
||||
migrations.CreateModel(
|
||||
|
@ -30,7 +29,7 @@ class Migration(migrations.Migration):
|
|||
('username', models.CharField(verbose_name='username', max_length=16, validators=[orchestra.core.validators.validate_name])),
|
||||
('password', models.CharField(verbose_name='password', max_length=256)),
|
||||
('type', models.CharField(default='mysql', choices=[('mysql', 'MySQL'), ('postgres', 'PostgreSQL')], verbose_name='type', max_length=32)),
|
||||
('account', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='databaseusers', verbose_name='Account', to=settings.AUTH_USER_MODEL)),
|
||||
('account', models.ForeignKey(related_name='databaseusers', verbose_name='Account', to=settings.AUTH_USER_MODEL)),
|
||||
],
|
||||
options={
|
||||
'verbose_name_plural': 'DB users',
|
||||
|
|
|
@ -1,82 +0,0 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
# Generated by Django 1.10.5 on 2021-04-22 11:25
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.conf import settings
|
||||
from django.db import migrations, models
|
||||
import django.db.models.deletion
|
||||
import orchestra.core.validators
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
replaces = [('databases', '0001_initial'), ('databases', '0002_auto_20170528_2005'), ('databases', '0003_database_comments'), ('databases', '0004_auto_20210330_1049')]
|
||||
|
||||
initial = True
|
||||
|
||||
dependencies = [
|
||||
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name='Database',
|
||||
fields=[
|
||||
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('name', models.CharField(max_length=64, validators=[orchestra.core.validators.validate_name], verbose_name='name')),
|
||||
('type', models.CharField(choices=[('mysql', 'MySQL'), ('postgres', 'PostgreSQL')], default='mysql', max_length=32, verbose_name='type')),
|
||||
('account', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='databases', to=settings.AUTH_USER_MODEL, verbose_name='Account')),
|
||||
],
|
||||
),
|
||||
migrations.CreateModel(
|
||||
name='DatabaseUser',
|
||||
fields=[
|
||||
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('username', models.CharField(max_length=16, validators=[orchestra.core.validators.validate_name], verbose_name='username')),
|
||||
('password', models.CharField(max_length=256, verbose_name='password')),
|
||||
('type', models.CharField(choices=[('mysql', 'MySQL'), ('postgres', 'PostgreSQL')], default='mysql', max_length=32, verbose_name='type')),
|
||||
('account', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='databaseusers', to=settings.AUTH_USER_MODEL, verbose_name='Account')),
|
||||
],
|
||||
options={
|
||||
'verbose_name_plural': 'DB users',
|
||||
},
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='database',
|
||||
name='users',
|
||||
field=models.ManyToManyField(blank=True, related_name='databases', to='databases.DatabaseUser', verbose_name='users'),
|
||||
),
|
||||
migrations.AlterUniqueTogether(
|
||||
name='databaseuser',
|
||||
unique_together=set([('username', 'type')]),
|
||||
),
|
||||
migrations.AlterUniqueTogether(
|
||||
name='database',
|
||||
unique_together=set([('name', 'type')]),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='database',
|
||||
name='type',
|
||||
field=models.CharField(choices=[('mysql', 'MySQL')], default='mysql', max_length=32, verbose_name='type'),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='databaseuser',
|
||||
name='type',
|
||||
field=models.CharField(choices=[('mysql', 'MySQL')], default='mysql', max_length=32, verbose_name='type'),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='database',
|
||||
name='comments',
|
||||
field=models.TextField(blank=True, default=''),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='database',
|
||||
name='type',
|
||||
field=models.CharField(choices=[('mysql', 'MySQL'), ('postgres', 'PostgreSQL')], default='mysql', max_length=32, verbose_name='type'),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='databaseuser',
|
||||
name='type',
|
||||
field=models.CharField(choices=[('mysql', 'MySQL'), ('postgres', 'PostgreSQL')], default='mysql', max_length=32, verbose_name='type'),
|
||||
),
|
||||
]
|
|
@ -1,30 +0,0 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
# Generated by Django 1.10.5 on 2021-03-30 10:49
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('databases', '0003_database_comments'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AlterField(
|
||||
model_name='database',
|
||||
name='comments',
|
||||
field=models.TextField(blank=True, default=''),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='database',
|
||||
name='type',
|
||||
field=models.CharField(choices=[('mysql', 'MySQL'), ('postgres', 'PostgreSQL')], default='mysql', max_length=32, verbose_name='type'),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='databaseuser',
|
||||
name='type',
|
||||
field=models.CharField(choices=[('mysql', 'MySQL'), ('postgres', 'PostgreSQL')], default='mysql', max_length=32, verbose_name='type'),
|
||||
),
|
||||
]
|
|
@ -12,7 +12,7 @@ class Database(models.Model):
|
|||
""" Represents a basic database for a web application """
|
||||
MYSQL = 'mysql'
|
||||
POSTGRESQL = 'postgresql'
|
||||
|
||||
|
||||
name = models.CharField(_("name"), max_length=64, # MySQL limit
|
||||
validators=[validators.validate_name])
|
||||
users = models.ManyToManyField('databases.DatabaseUser', blank=True,
|
||||
|
@ -20,16 +20,16 @@ class Database(models.Model):
|
|||
type = models.CharField(_("type"), max_length=32,
|
||||
choices=settings.DATABASES_TYPE_CHOICES,
|
||||
default=settings.DATABASES_DEFAULT_TYPE)
|
||||
account = models.ForeignKey('accounts.Account', on_delete=models.CASCADE,
|
||||
verbose_name=_("Account"), related_name='databases')
|
||||
account = models.ForeignKey('accounts.Account', verbose_name=_("Account"),
|
||||
related_name='databases')
|
||||
comments = models.TextField(default="", blank=True)
|
||||
|
||||
|
||||
class Meta:
|
||||
unique_together = ('name', 'type')
|
||||
|
||||
|
||||
def __str__(self):
|
||||
return "%s" % self.name
|
||||
|
||||
|
||||
@property
|
||||
def owner(self):
|
||||
""" database owner is the first user related to it """
|
||||
|
@ -39,7 +39,7 @@ class Database(models.Model):
|
|||
if user is not None:
|
||||
return user.databaseuser
|
||||
return None
|
||||
|
||||
|
||||
@property
|
||||
def active(self):
|
||||
return self.account.is_active
|
||||
|
@ -53,26 +53,26 @@ Database.users.through._meta.unique_together = (
|
|||
class DatabaseUser(models.Model):
|
||||
MYSQL = Database.MYSQL
|
||||
POSTGRESQL = Database.POSTGRESQL
|
||||
|
||||
|
||||
username = models.CharField(_("username"), max_length=16, # MySQL usernames 16 char long
|
||||
validators=[validators.validate_name])
|
||||
password = models.CharField(_("password"), max_length=256)
|
||||
type = models.CharField(_("type"), max_length=32,
|
||||
choices=settings.DATABASES_TYPE_CHOICES,
|
||||
default=settings.DATABASES_DEFAULT_TYPE)
|
||||
account = models.ForeignKey('accounts.Account', on_delete=models.CASCADE,
|
||||
verbose_name=_("Account"), related_name='databaseusers')
|
||||
|
||||
account = models.ForeignKey('accounts.Account', verbose_name=_("Account"),
|
||||
related_name='databaseusers')
|
||||
|
||||
class Meta:
|
||||
verbose_name_plural = _("DB users")
|
||||
unique_together = ('username', 'type')
|
||||
|
||||
|
||||
def __str__(self):
|
||||
return self.username
|
||||
|
||||
|
||||
def get_username(self):
|
||||
return self.username
|
||||
|
||||
|
||||
def set_password(self, password):
|
||||
if self.type == self.MYSQL:
|
||||
# MySQL stores sha1(sha1(password).binary).hex
|
||||
|
|
|
@ -1,24 +1,22 @@
|
|||
import os
|
||||
import socket
|
||||
import time
|
||||
import unittest
|
||||
|
||||
import MySQLdb
|
||||
from unittest import skip
|
||||
from django.conf import settings as djsettings
|
||||
from django.core.management.base import CommandError
|
||||
from django.urls import reverse
|
||||
from orchestra.admin.utils import change_url
|
||||
from orchestra.contrib.orchestration.models import Route, Server
|
||||
from orchestra.utils.sys import sshrun
|
||||
from orchestra.utils.tests import (BaseLiveServerTestCase, random_ascii,
|
||||
save_response_on_error, snapshot_on_error)
|
||||
from django.core.urlresolvers import reverse
|
||||
from selenium.webdriver.support.select import Select
|
||||
|
||||
from orchestra.admin.utils import change_url
|
||||
from orchestra.contrib.orchestration.models import Server, Route
|
||||
from orchestra.utils.sys import sshrun
|
||||
from orchestra.utils.tests import (BaseLiveServerTestCase, random_ascii, save_response_on_error,
|
||||
snapshot_on_error)
|
||||
|
||||
from ... import backends, settings
|
||||
from ...models import Database, DatabaseUser
|
||||
|
||||
TEST_REST_API = int(os.getenv('TEST_REST_API', '0'))
|
||||
|
||||
|
||||
class DatabaseTestMixin(object):
|
||||
MASTER_SERVER = os.environ.get('ORCHESTRA_SECOND_SERVER', 'localhost')
|
||||
|
@ -26,40 +24,42 @@ class DatabaseTestMixin(object):
|
|||
'orchestra.contrib.orchestration',
|
||||
'orcgestra.apps.databases',
|
||||
)
|
||||
|
||||
|
||||
def setUp(self):
|
||||
super(DatabaseTestMixin, self).setUp()
|
||||
self.add_route()
|
||||
djsettings.DEBUG = True
|
||||
|
||||
|
||||
def add_route(self):
|
||||
raise NotImplementedError
|
||||
|
||||
|
||||
def save(self):
|
||||
raise NotImplementedError
|
||||
|
||||
|
||||
def add(self):
|
||||
raise NotImplementedError
|
||||
|
||||
|
||||
def delete(self):
|
||||
raise NotImplementedError
|
||||
|
||||
|
||||
def update(self):
|
||||
raise NotImplementedError
|
||||
|
||||
|
||||
def disable(self):
|
||||
raise NotImplementedError
|
||||
|
||||
|
||||
def add_group(self, username, groupname):
|
||||
raise NotImplementedError
|
||||
|
||||
|
||||
@skip("Skip because not exists get_auth_token in orm.api.Api")
|
||||
def test_add(self):
|
||||
dbname = '%s_database' % random_ascii(5)
|
||||
username = '%s_dbuser' % random_ascii(5)
|
||||
password = '@!?%spppP001' % random_ascii(5)
|
||||
self.add(dbname, username, password)
|
||||
self.validate_create_table(dbname, username, password)
|
||||
|
||||
|
||||
@skip("Skip because not exists get_auth_token in orm.api.Api")
|
||||
def test_delete(self):
|
||||
dbname = '%s_database' % random_ascii(5)
|
||||
username = '%s_dbuser' % random_ascii(5)
|
||||
|
@ -70,7 +70,8 @@ class DatabaseTestMixin(object):
|
|||
self.delete_user(username)
|
||||
self.validate_delete(dbname, username, password)
|
||||
self.validate_delete_user(dbname, username)
|
||||
|
||||
|
||||
@skip("Skip because not exists get_auth_token in orm.api.Api")
|
||||
def test_change_password(self):
|
||||
dbname = '%s_database' % random_ascii(5)
|
||||
username = '%s_dbuser' % random_ascii(5)
|
||||
|
@ -83,7 +84,8 @@ class DatabaseTestMixin(object):
|
|||
self.change_password(username, new_password)
|
||||
self.validate_login_error(dbname, username, password)
|
||||
self.validate_create_table(dbname, username, new_password)
|
||||
|
||||
|
||||
@skip("Skip because not exists get_auth_token in orm.api.Api")
|
||||
def test_add_user(self):
|
||||
dbname = '%s_database' % random_ascii(5)
|
||||
username = '%s_dbuser' % random_ascii(5)
|
||||
|
@ -100,7 +102,8 @@ class DatabaseTestMixin(object):
|
|||
self.add_user_to_db(username2, dbname)
|
||||
self.validate_create_table(dbname, username, password)
|
||||
self.validate_create_table(dbname, username2, password2)
|
||||
|
||||
|
||||
@skip("Skip because not exists get_auth_token in orm.api.Api")
|
||||
def test_delete_user(self):
|
||||
dbname = '%s_database' % random_ascii(5)
|
||||
username = '%s_dbuser' % random_ascii(5)
|
||||
|
@ -119,7 +122,8 @@ class DatabaseTestMixin(object):
|
|||
self.delete_user(username2)
|
||||
self.validate_login_error(dbname, username2, password2)
|
||||
self.validate_delete_user(username2, password2)
|
||||
|
||||
|
||||
@skip("Skip because not exists get_auth_token in orm.api.Api")
|
||||
def test_swap_user(self):
|
||||
dbname = '%s_database' % random_ascii(5)
|
||||
username = '%s_dbuser' % random_ascii(5)
|
||||
|
@ -139,7 +143,7 @@ class DatabaseTestMixin(object):
|
|||
|
||||
class MySQLControllerMixin(object):
|
||||
db_type = 'mysql'
|
||||
|
||||
|
||||
def setUp(self):
|
||||
super(MySQLControllerMixin, self).setUp()
|
||||
# Get local ip address used to reach self.MASTER_SERVER
|
||||
|
@ -147,7 +151,7 @@ class MySQLControllerMixin(object):
|
|||
s.connect((self.MASTER_SERVER, 22))
|
||||
settings.DATABASES_DEFAULT_HOST = s.getsockname()[0]
|
||||
s.close()
|
||||
|
||||
|
||||
def add_route(self):
|
||||
server = Server.objects.create(name=self.MASTER_SERVER)
|
||||
backend = backends.MySQLController.get_name()
|
||||
|
@ -156,22 +160,22 @@ class MySQLControllerMixin(object):
|
|||
match = "databaseuser.type == '%s'" % self.db_type
|
||||
backend = backends.MySQLUserController.get_name()
|
||||
Route.objects.create(backend=backend, match=match, host=server)
|
||||
|
||||
|
||||
def validate_create_table(self, name, username, password):
|
||||
db = MySQLdb.connect(host=self.MASTER_SERVER, port=3306, user=username, passwd=password, db=name)
|
||||
cur = db.cursor()
|
||||
cur.execute('CREATE TABLE table_%s ( id INT ) ;' % random_ascii(10))
|
||||
|
||||
|
||||
def validate_login_error(self, dbname, username, password):
|
||||
self.assertRaises(MySQLdb.OperationalError,
|
||||
self.validate_create_table, dbname, username, password
|
||||
)
|
||||
|
||||
|
||||
def validate_delete(self, dbname, username, password):
|
||||
self.validate_login_error(dbname, username, password)
|
||||
self.assertRaises(CommandError,
|
||||
sshrun, self.MASTER_SERVER, 'mysql %s' % dbname, display=False)
|
||||
|
||||
|
||||
def validate_delete_user(self, name, username):
|
||||
context = {
|
||||
'name': name,
|
||||
|
@ -183,12 +187,11 @@ class MySQLControllerMixin(object):
|
|||
"""mysql mysql -e 'SELECT * FROM user WHERE user="%(username)s";'""" % context, display=False).stdout)
|
||||
|
||||
|
||||
@unittest.skipUnless(TEST_REST_API, "REST API tests")
|
||||
class RESTDatabaseMixin(DatabaseTestMixin):
|
||||
def setUp(self):
|
||||
super(RESTDatabaseMixin, self).setUp()
|
||||
self.rest_login()
|
||||
|
||||
|
||||
@save_response_on_error
|
||||
def add(self, dbname, username, password):
|
||||
user = self.rest.databaseusers.create(username=username, password=password, type=self.db_type)
|
||||
|
@ -196,31 +199,31 @@ class RESTDatabaseMixin(DatabaseTestMixin):
|
|||
'username': user.username
|
||||
}]
|
||||
self.rest.databases.create(name=dbname, users=users, type=self.db_type)
|
||||
|
||||
|
||||
@save_response_on_error
|
||||
def delete(self, dbname):
|
||||
self.rest.databases.retrieve(name=dbname).delete()
|
||||
|
||||
|
||||
@save_response_on_error
|
||||
def change_password(self, username, password):
|
||||
user = self.rest.databaseusers.retrieve(username=username).get()
|
||||
user.set_password(password)
|
||||
|
||||
|
||||
@save_response_on_error
|
||||
def add_user(self, username, password):
|
||||
self.rest.databaseusers.create(username=username, password=password, type=self.db_type)
|
||||
|
||||
|
||||
@save_response_on_error
|
||||
def add_user_to_db(self, username, dbname):
|
||||
user = self.rest.databaseusers.retrieve(username=username).get()
|
||||
db = self.rest.databases.retrieve(name=dbname).get()
|
||||
db.users.append(user)
|
||||
db.save()
|
||||
|
||||
|
||||
@save_response_on_error
|
||||
def delete_user(self, username):
|
||||
self.rest.databaseusers.retrieve(username=username).delete()
|
||||
|
||||
|
||||
@save_response_on_error
|
||||
def swap_user(self, username, username2, dbname):
|
||||
user = self.rest.databaseusers.retrieve(username=username2).get()
|
||||
|
@ -234,84 +237,84 @@ class AdminDatabaseMixin(DatabaseTestMixin):
|
|||
def setUp(self):
|
||||
super(AdminDatabaseMixin, self).setUp()
|
||||
self.admin_login()
|
||||
|
||||
|
||||
@snapshot_on_error
|
||||
def add(self, dbname, username, password):
|
||||
url = self.live_server_url + reverse('admin:databases_database_add')
|
||||
self.selenium.get(url)
|
||||
|
||||
|
||||
type_input = self.selenium.find_element_by_id('id_type')
|
||||
type_select = Select(type_input)
|
||||
type_select.select_by_value(self.db_type)
|
||||
|
||||
|
||||
name_field = self.selenium.find_element_by_id('id_name')
|
||||
name_field.send_keys(dbname)
|
||||
|
||||
|
||||
username_field = self.selenium.find_element_by_id('id_username')
|
||||
username_field.send_keys(username)
|
||||
|
||||
|
||||
password_field = self.selenium.find_element_by_id('id_password1')
|
||||
password_field.send_keys(password)
|
||||
password_field = self.selenium.find_element_by_id('id_password2')
|
||||
password_field.send_keys(password)
|
||||
|
||||
|
||||
name_field.submit()
|
||||
self.assertNotEqual(url, self.selenium.current_url)
|
||||
|
||||
|
||||
@snapshot_on_error
|
||||
def delete(self, dbname):
|
||||
db = Database.objects.get(name=dbname)
|
||||
self.admin_delete(db)
|
||||
|
||||
|
||||
@snapshot_on_error
|
||||
def change_password(self, username, password):
|
||||
user = DatabaseUser.objects.get(username=username)
|
||||
self.admin_change_password(user, password)
|
||||
|
||||
|
||||
@snapshot_on_error
|
||||
def add_user(self, username, password):
|
||||
url = self.live_server_url + reverse('admin:databases_databaseuser_add')
|
||||
self.selenium.get(url)
|
||||
|
||||
|
||||
type_input = self.selenium.find_element_by_id('id_type')
|
||||
type_select = Select(type_input)
|
||||
type_select.select_by_value(self.db_type)
|
||||
|
||||
|
||||
username_field = self.selenium.find_element_by_id('id_username')
|
||||
username_field.send_keys(username)
|
||||
|
||||
|
||||
password_field = self.selenium.find_element_by_id('id_password1')
|
||||
password_field.send_keys(password)
|
||||
password_field = self.selenium.find_element_by_id('id_password2')
|
||||
password_field.send_keys(password)
|
||||
|
||||
|
||||
username_field.submit()
|
||||
self.assertNotEqual(url, self.selenium.current_url)
|
||||
|
||||
|
||||
@snapshot_on_error
|
||||
def add_user_to_db(self, username, dbname):
|
||||
database = Database.objects.get(name=dbname, type=self.db_type)
|
||||
url = self.live_server_url + change_url(database)
|
||||
self.selenium.get(url)
|
||||
|
||||
|
||||
user = DatabaseUser.objects.get(username=username, type=self.db_type)
|
||||
users_from = self.selenium.find_element_by_id('id_users_from')
|
||||
users_select = Select(users_from)
|
||||
users_select.select_by_value(str(user.pk))
|
||||
|
||||
|
||||
add_user = self.selenium.find_element_by_id('id_users_add_link')
|
||||
add_user.click()
|
||||
|
||||
|
||||
save = self.selenium.find_element_by_name('_save')
|
||||
save.submit()
|
||||
self.assertNotEqual(url, self.selenium.current_url)
|
||||
|
||||
|
||||
@snapshot_on_error
|
||||
def swap_user(self, username, username2, dbname):
|
||||
database = Database.objects.get(name=dbname, type=self.db_type)
|
||||
url = self.live_server_url + change_url(database)
|
||||
self.selenium.get(url)
|
||||
|
||||
|
||||
# remove user "username"
|
||||
user = DatabaseUser.objects.get(username=username, type=self.db_type)
|
||||
users_to = self.selenium.find_element_by_id('id_users_to')
|
||||
|
@ -320,7 +323,7 @@ class AdminDatabaseMixin(DatabaseTestMixin):
|
|||
remove_user = self.selenium.find_element_by_id('id_users_remove_link')
|
||||
remove_user.click()
|
||||
time.sleep(0.2)
|
||||
|
||||
|
||||
# add user "username2"
|
||||
user = DatabaseUser.objects.get(username=username2, type=self.db_type)
|
||||
users_from = self.selenium.find_element_by_id('id_users_from')
|
||||
|
@ -329,11 +332,11 @@ class AdminDatabaseMixin(DatabaseTestMixin):
|
|||
add_user = self.selenium.find_element_by_id('id_users_add_link')
|
||||
add_user.click()
|
||||
time.sleep(0.2)
|
||||
|
||||
|
||||
save = self.selenium.find_element_by_name('_save')
|
||||
save.submit()
|
||||
self.assertNotEqual(url, self.selenium.current_url)
|
||||
|
||||
|
||||
@snapshot_on_error
|
||||
def delete_user(self, username):
|
||||
user = DatabaseUser.objects.get(username=username)
|
||||
|
|
|
@ -1,10 +1,8 @@
|
|||
from django.contrib import admin
|
||||
from django.urls import reverse
|
||||
from django.core.urlresolvers import reverse
|
||||
from django.db import models
|
||||
from django.db.models.functions import Concat, Coalesce
|
||||
from django.templatetags.static import static
|
||||
from django.utils.html import format_html
|
||||
from django.utils.safestring import mark_safe
|
||||
from django.utils.translation import ugettext, ugettext_lazy as _
|
||||
|
||||
from orchestra.admin import ExtendedModelAdmin
|
||||
|
@ -34,18 +32,18 @@ class DomainInline(admin.TabularInline):
|
|||
readonly_fields = ('domain_link', 'display_records', 'account_link')
|
||||
extra = 0
|
||||
verbose_name_plural = _("Subdomains")
|
||||
|
||||
|
||||
domain_link = admin_link('__str__')
|
||||
domain_link.short_description = _("Name")
|
||||
account_link = admin_link('account')
|
||||
|
||||
|
||||
def display_records(self, domain):
|
||||
return ', '.join([record.type for record in domain.records.all()])
|
||||
display_records.short_description = _("Declared records")
|
||||
|
||||
|
||||
def has_add_permission(self, *args, **kwargs):
|
||||
return False
|
||||
|
||||
|
||||
def get_queryset(self, request):
|
||||
""" Order by structured name and imporve performance """
|
||||
qs = super(DomainInline, self).get_queryset(request)
|
||||
|
@ -68,23 +66,23 @@ class DomainAdmin(AccountAdminMixin, ExtendedModelAdmin):
|
|||
add_form = BatchDomainCreationAdminForm
|
||||
actions = (edit_records, set_soa, list_accounts)
|
||||
change_view_actions = (view_zone, edit_records)
|
||||
|
||||
|
||||
top_link = admin_link('top')
|
||||
|
||||
|
||||
def structured_name(self, domain):
|
||||
if domain.is_top:
|
||||
return domain.name
|
||||
return mark_safe(' '*4 + domain.name)
|
||||
return ' '*4 + domain.name
|
||||
structured_name.short_description = _("name")
|
||||
structured_name.allow_tags = True
|
||||
structured_name.admin_order_field = 'structured_name'
|
||||
|
||||
|
||||
def display_is_top(self, domain):
|
||||
return domain.is_top
|
||||
display_is_top.short_description = _("Is top")
|
||||
display_is_top.boolean = True
|
||||
display_is_top.admin_order_field = 'top'
|
||||
|
||||
@mark_safe
|
||||
|
||||
def display_websites(self, domain):
|
||||
if apps.isinstalled('orchestra.contrib.websites'):
|
||||
websites = domain.websites.all()
|
||||
|
@ -94,22 +92,22 @@ class DomainAdmin(AccountAdminMixin, ExtendedModelAdmin):
|
|||
site_link = get_on_site_link(website.get_absolute_url())
|
||||
admin_url = change_url(website)
|
||||
title = _("Edit website")
|
||||
link = format_html('<a href="{}" title="{}">{} {}</a>',
|
||||
link = '<a href="%s" title="%s">%s %s</a>' % (
|
||||
admin_url, title, website.name, site_link)
|
||||
links.append(link)
|
||||
return '<br>'.join(links)
|
||||
add_url = reverse('admin:websites_website_add')
|
||||
add_url += '?account=%i&domains=%i' % (domain.account_id, domain.pk)
|
||||
add_link = format_html(
|
||||
'<a href="{}" title="{}"><img src="{}" /></a>', add_url,
|
||||
_("Add website"), static('orchestra/images/add.png'),
|
||||
image = '<img src="%s"></img>' % static('orchestra/images/add.png')
|
||||
add_link = '<a href="%s" title="%s">%s</a>' % (
|
||||
add_url, _("Add website"), image
|
||||
)
|
||||
return _("No website %s") % (add_link)
|
||||
return '---'
|
||||
display_websites.admin_order_field = 'websites__name'
|
||||
display_websites.short_description = _("Websites")
|
||||
|
||||
@mark_safe
|
||||
display_websites.allow_tags = True
|
||||
|
||||
def display_addresses(self, domain):
|
||||
if apps.isinstalled('orchestra.contrib.mailboxes'):
|
||||
add_url = reverse('admin:mailboxes_address_add')
|
||||
|
@ -128,9 +126,10 @@ class DomainAdmin(AccountAdminMixin, ExtendedModelAdmin):
|
|||
return '---'
|
||||
display_addresses.short_description = _("Addresses")
|
||||
display_addresses.admin_order_field = 'addresses__count'
|
||||
|
||||
@mark_safe
|
||||
display_addresses.allow_tags = True
|
||||
|
||||
def implicit_records(self, domain):
|
||||
defaults = []
|
||||
types = set(domain.records.values_list('type', flat=True))
|
||||
ttl = settings.DOMAINS_DEFAULT_TTL
|
||||
lines = []
|
||||
|
@ -142,14 +141,15 @@ class DomainAdmin(AccountAdminMixin, ExtendedModelAdmin):
|
|||
value=record.value
|
||||
)
|
||||
if not domain.record_is_implicit(record, types):
|
||||
line = format_html('<strike>{}</strike>', line)
|
||||
line = '<strike>%s</strike>' % line
|
||||
if record.type is Record.SOA:
|
||||
lines.insert(0, line)
|
||||
else:
|
||||
lines.append(line)
|
||||
return '<br>'.join(lines)
|
||||
implicit_records.short_description = _("Implicit records")
|
||||
|
||||
implicit_records.allow_tags = True
|
||||
|
||||
def get_fieldsets(self, request, obj=None):
|
||||
""" Add SOA fields when domain is top """
|
||||
fieldsets = super(DomainAdmin, self).get_fieldsets(request, obj)
|
||||
|
@ -175,13 +175,13 @@ class DomainAdmin(AccountAdminMixin, ExtendedModelAdmin):
|
|||
if 'top_link' not in existing:
|
||||
fieldsets[0][1]['fields'].insert(2, 'top_link')
|
||||
return fieldsets
|
||||
|
||||
|
||||
def get_inline_instances(self, request, obj=None):
|
||||
inlines = super(DomainAdmin, self).get_inline_instances(request, obj)
|
||||
if not obj or not obj.is_top:
|
||||
return [inline for inline in inlines if type(inline) != DomainInline]
|
||||
return inlines
|
||||
|
||||
|
||||
def get_queryset(self, request):
|
||||
""" Order by structured name and imporve performance """
|
||||
qs = super(DomainAdmin, self).get_queryset(request)
|
||||
|
@ -196,7 +196,7 @@ class DomainAdmin(AccountAdminMixin, ExtendedModelAdmin):
|
|||
if apps.isinstalled('orchestra.contrib.mailboxes'):
|
||||
qs = qs.annotate(models.Count('addresses'))
|
||||
return qs
|
||||
|
||||
|
||||
def save_model(self, request, obj, form, change):
|
||||
""" batch domain creation support """
|
||||
super(DomainAdmin, self).save_model(request, obj, form, change)
|
||||
|
@ -205,7 +205,7 @@ class DomainAdmin(AccountAdminMixin, ExtendedModelAdmin):
|
|||
for name in form.extra_names:
|
||||
domain = Domain.objects.create(name=name, account_id=obj.account_id)
|
||||
self.extra_domains.append(domain)
|
||||
|
||||
|
||||
def save_related(self, request, form, formsets, change):
|
||||
""" batch domain creation support """
|
||||
super(DomainAdmin, self).save_related(request, form, formsets, change)
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
from rest_framework import viewsets
|
||||
from rest_framework.decorators import action
|
||||
from rest_framework.decorators import detail_route
|
||||
from rest_framework.response import Response
|
||||
|
||||
from orchestra.api import router
|
||||
|
@ -14,18 +14,18 @@ class DomainViewSet(AccountApiMixin, viewsets.ModelViewSet):
|
|||
serializer_class = DomainSerializer
|
||||
filter_fields = ('name',)
|
||||
queryset = Domain.objects.all()
|
||||
|
||||
|
||||
def get_queryset(self):
|
||||
qs = super(DomainViewSet, self).get_queryset()
|
||||
return qs.prefetch_related('records')
|
||||
|
||||
@action(detail=True)
|
||||
|
||||
@detail_route()
|
||||
def view_zone(self, request, pk=None):
|
||||
domain = self.get_object()
|
||||
return Response({
|
||||
'zone': domain.render_zone()
|
||||
})
|
||||
|
||||
|
||||
def options(self, request):
|
||||
metadata = super(DomainViewSet, self).options(request)
|
||||
names = ['DOMAINS_DEFAULT_A', 'DOMAINS_DEFAULT_MX', 'DOMAINS_DEFAULT_NS']
|
||||
|
|
|
@ -2,7 +2,6 @@
|
|||
from __future__ import unicode_literals
|
||||
|
||||
from django.db import models, migrations
|
||||
import django.db.models.deletion
|
||||
import orchestra.contrib.domains.utils
|
||||
import orchestra.contrib.domains.validators
|
||||
from django.conf import settings
|
||||
|
@ -21,8 +20,8 @@ class Migration(migrations.Migration):
|
|||
('id', models.AutoField(serialize=False, primary_key=True, auto_created=True, verbose_name='ID')),
|
||||
('name', models.CharField(unique=True, max_length=256, validators=[orchestra.contrib.domains.validators.validate_domain_name, orchestra.contrib.domains.validators.validate_allowed_domain], verbose_name='name', help_text='Domain or subdomain name.')),
|
||||
('serial', models.IntegerField(default=orchestra.contrib.domains.utils.generate_zone_serial, verbose_name='serial', help_text='Serial number')),
|
||||
('account', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='domains', help_text='Automatically selected for subdomains.', to=settings.AUTH_USER_MODEL, verbose_name='Account', blank=True)),
|
||||
('top', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, null=True, to='domains.Domain', editable=False, related_name='subdomain_set')),
|
||||
('account', models.ForeignKey(related_name='domains', help_text='Automatically selected for subdomains.', to=settings.AUTH_USER_MODEL, verbose_name='Account', blank=True)),
|
||||
('top', models.ForeignKey(null=True, to='domains.Domain', editable=False, related_name='subdomain_set')),
|
||||
],
|
||||
),
|
||||
migrations.CreateModel(
|
||||
|
@ -32,7 +31,7 @@ class Migration(migrations.Migration):
|
|||
('ttl', models.CharField(help_text='Record TTL, defaults to 1h', max_length=8, validators=[orchestra.contrib.domains.validators.validate_zone_interval], verbose_name='TTL', blank=True)),
|
||||
('type', models.CharField(max_length=32, verbose_name='type', choices=[('MX', 'MX'), ('NS', 'NS'), ('CNAME', 'CNAME'), ('A', 'A (IPv4 address)'), ('AAAA', 'AAAA (IPv6 address)'), ('SRV', 'SRV'), ('TXT', 'TXT'), ('SOA', 'SOA')])),
|
||||
('value', models.CharField(max_length=256, verbose_name='value')),
|
||||
('domain', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='records', to='domains.Domain', verbose_name='domain')),
|
||||
('domain', models.ForeignKey(related_name='records', to='domains.Domain', verbose_name='domain')),
|
||||
],
|
||||
),
|
||||
]
|
||||
|
|
|
@ -1,83 +0,0 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
# Generated by Django 1.10.5 on 2021-04-22 11:27
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.conf import settings
|
||||
from django.db import migrations, models
|
||||
import django.db.models.deletion
|
||||
import orchestra.contrib.domains.utils
|
||||
import orchestra.contrib.domains.validators
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
replaces = [('domains', '0001_initial'), ('domains', '0002_auto_20150715_1017'), ('domains', '0003_auto_20150720_1121'), ('domains', '0004_auto_20150720_1121'), ('domains', '0005_auto_20160219_1034'), ('domains', '0006_auto_20170528_2011'), ('domains', '0007_auto_20190805_1134'), ('domains', '0008_domain_dns2136_address_match_list'), ('domains', '0009_auto_20200204_1217'), ('domains', '0010_auto_20210330_1049')]
|
||||
|
||||
initial = True
|
||||
|
||||
dependencies = [
|
||||
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name='Domain',
|
||||
fields=[
|
||||
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('name', models.CharField(help_text='Domain or subdomain name.', max_length=256, unique=True, validators=[orchestra.contrib.domains.validators.validate_domain_name, orchestra.contrib.domains.validators.validate_allowed_domain], verbose_name='name')),
|
||||
('serial', models.IntegerField(default=orchestra.contrib.domains.utils.generate_zone_serial, help_text='Serial number', verbose_name='serial')),
|
||||
('account', models.ForeignKey(blank=True, help_text='Automatically selected for subdomains.', on_delete=django.db.models.deletion.CASCADE, related_name='domains', to=settings.AUTH_USER_MODEL, verbose_name='Account')),
|
||||
('top', models.ForeignKey(editable=False, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='subdomain_set', to='domains.Domain')),
|
||||
],
|
||||
),
|
||||
migrations.CreateModel(
|
||||
name='Record',
|
||||
fields=[
|
||||
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('ttl', models.CharField(blank=True, help_text='Record TTL, defaults to 1h', max_length=8, validators=[orchestra.contrib.domains.validators.validate_zone_interval], verbose_name='TTL')),
|
||||
('type', models.CharField(choices=[('MX', 'MX'), ('NS', 'NS'), ('CNAME', 'CNAME'), ('A', 'A (IPv4 address)'), ('AAAA', 'AAAA (IPv6 address)'), ('SRV', 'SRV'), ('TXT', 'TXT'), ('SPF', 'SPF')], max_length=32, verbose_name='type')),
|
||||
('value', models.CharField(help_text='MX, NS and CNAME records sould end with a dot.', max_length=1024, verbose_name='value')),
|
||||
('domain', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='records', to='domains.Domain', verbose_name='domain')),
|
||||
],
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='domain',
|
||||
name='serial',
|
||||
field=models.IntegerField(default=orchestra.contrib.domains.utils.generate_zone_serial, editable=False, help_text='A revision number that changes whenever this domain is updated.', verbose_name='serial'),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='domain',
|
||||
name='expire',
|
||||
field=models.CharField(blank=True, help_text='The time that a secondary server will keep trying to complete a zone transfer. If this time expires prior to a successful zone transfer, the secondary server will expire its zone file. This means the secondary will stop answering queries. The default value is <tt>4w</tt>.', max_length=16, validators=[orchestra.contrib.domains.validators.validate_zone_interval], verbose_name='expire'),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='domain',
|
||||
name='min_ttl',
|
||||
field=models.CharField(blank=True, help_text='The minimum time-to-live value applies to all resource records in the zone file. This value is supplied in query responses to inform other servers how long they should keep the data in cache. The default value is <tt>1h</tt>.', max_length=16, validators=[orchestra.contrib.domains.validators.validate_zone_interval], verbose_name='min TTL'),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='domain',
|
||||
name='refresh',
|
||||
field=models.CharField(blank=True, help_text="The time a secondary DNS server waits before querying the primary DNS server's SOA record to check for changes. When the refresh time expires, the secondary DNS server requests a copy of the current SOA record from the primary. The primary DNS server complies with this request. The secondary DNS server compares the serial number of the primary DNS server's current SOA record and the serial number in it's own SOA record. If they are different, the secondary DNS server will request a zone transfer from the primary DNS server. The default value is <tt>1d</tt>.", max_length=16, validators=[orchestra.contrib.domains.validators.validate_zone_interval], verbose_name='refresh'),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='domain',
|
||||
name='retry',
|
||||
field=models.CharField(blank=True, help_text='The time a secondary server waits before retrying a failed zone transfer. Normally, the retry time is less than the refresh time. The default value is <tt>2h</tt>.', max_length=16, validators=[orchestra.contrib.domains.validators.validate_zone_interval], verbose_name='retry'),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='domain',
|
||||
name='name',
|
||||
field=models.CharField(db_index=True, help_text='Domain or subdomain name.', max_length=256, unique=True, validators=[orchestra.contrib.domains.validators.validate_domain_name, orchestra.contrib.domains.validators.validate_allowed_domain], verbose_name='name'),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='domain',
|
||||
name='top',
|
||||
field=models.ForeignKey(editable=False, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='subdomain_set', to='domains.Domain', verbose_name='top domain'),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='domain',
|
||||
name='dns2136_address_match_list',
|
||||
field=models.CharField(blank=True, default='key pangea.key;', help_text="A bind-9 'address_match_list' that will be granted permission to perform dns2136 updates. Chiefly used to enable Let's Encrypt self-service validation.", max_length=80),
|
||||
),
|
||||
]
|
|
@ -2,7 +2,6 @@
|
|||
from __future__ import unicode_literals
|
||||
|
||||
from django.db import migrations, models
|
||||
import django.db.models.deletion
|
||||
import orchestra.contrib.domains.validators
|
||||
|
||||
|
||||
|
@ -21,7 +20,7 @@ class Migration(migrations.Migration):
|
|||
migrations.AlterField(
|
||||
model_name='domain',
|
||||
name='top',
|
||||
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, editable=False, verbose_name='top domain', related_name='subdomain_set', to='domains.Domain', null=True),
|
||||
field=models.ForeignKey(editable=False, verbose_name='top domain', related_name='subdomain_set', to='domains.Domain', null=True),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='record',
|
||||
|
|
|
@ -1,26 +0,0 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
# Generated by Django 1.10.5 on 2021-03-30 10:49
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.db import migrations, models
|
||||
import orchestra.contrib.domains.validators
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('domains', '0009_auto_20200204_1217'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AlterField(
|
||||
model_name='domain',
|
||||
name='min_ttl',
|
||||
field=models.CharField(blank=True, help_text='The minimum time-to-live value applies to all resource records in the zone file. This value is supplied in query responses to inform other servers how long they should keep the data in cache. The default value is <tt>1h</tt>.', max_length=16, validators=[orchestra.contrib.domains.validators.validate_zone_interval], verbose_name='min TTL'),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='record',
|
||||
name='ttl',
|
||||
field=models.CharField(blank=True, help_text='Record TTL, defaults to 1h', max_length=8, validators=[orchestra.contrib.domains.validators.validate_zone_interval], verbose_name='TTL'),
|
||||
),
|
||||
]
|
|
@ -31,9 +31,9 @@ class Domain(models.Model):
|
|||
validators.validate_allowed_domain
|
||||
])
|
||||
account = models.ForeignKey('accounts.Account', verbose_name=_("Account"), blank=True,
|
||||
related_name='domains', on_delete=models.CASCADE, help_text=_("Automatically selected for subdomains."))
|
||||
related_name='domains', help_text=_("Automatically selected for subdomains."))
|
||||
top = models.ForeignKey('domains.Domain', null=True, related_name='subdomain_set',
|
||||
editable=False, verbose_name=_("top domain"), on_delete=models.CASCADE)
|
||||
editable=False, verbose_name=_("top domain"))
|
||||
serial = models.IntegerField(_("serial"), default=utils.generate_zone_serial, editable=False,
|
||||
help_text=_("A revision number that changes whenever this domain is updated."))
|
||||
refresh = models.CharField(_("refresh"), max_length=16, blank=True,
|
||||
|
@ -69,16 +69,16 @@ class Domain(models.Model):
|
|||
blank=True,
|
||||
help_text="A bind-9 'address_match_list' that will be granted permission to perform "
|
||||
"dns2136 updates. Chiefly used to enable Let's Encrypt self-service validation.")
|
||||
|
||||
|
||||
objects = DomainQuerySet.as_manager()
|
||||
|
||||
|
||||
def __str__(self):
|
||||
return self.name
|
||||
|
||||
|
||||
@property
|
||||
def origin(self):
|
||||
return self.top or self
|
||||
|
||||
|
||||
@property
|
||||
def is_top(self):
|
||||
# don't cache, don't replace by top_id
|
||||
|
@ -86,14 +86,14 @@ class Domain(models.Model):
|
|||
return not bool(self.top)
|
||||
except Domain.DoesNotExist:
|
||||
return False
|
||||
|
||||
|
||||
@property
|
||||
def subdomains(self):
|
||||
return Domain.objects.filter(name__regex='\.%s$' % self.name)
|
||||
|
||||
|
||||
def clean(self):
|
||||
self.name = self.name.lower()
|
||||
|
||||
|
||||
def save(self, *args, **kwargs):
|
||||
""" create top relation """
|
||||
update = False
|
||||
|
@ -110,7 +110,7 @@ class Domain(models.Model):
|
|||
# queryset.update() is not used because we want to trigger backend to delete ex-topdomains
|
||||
domain.top = self
|
||||
domain.save(update_fields=('top',))
|
||||
|
||||
|
||||
def get_description(self):
|
||||
if self.is_top:
|
||||
num = self.subdomains.count()
|
||||
|
@ -119,21 +119,21 @@ class Domain(models.Model):
|
|||
_("top domain with %d subdomains") % num,
|
||||
num)
|
||||
return _("subdomain")
|
||||
|
||||
|
||||
def get_absolute_url(self):
|
||||
return 'http://%s' % self.name
|
||||
|
||||
|
||||
def get_declared_records(self):
|
||||
""" proxy method, needed for input validation, see helpers.domain_for_validation """
|
||||
return self.records.all()
|
||||
|
||||
|
||||
def get_subdomains(self):
|
||||
""" proxy method, needed for input validation, see helpers.domain_for_validation """
|
||||
return self.origin.subdomain_set.all().prefetch_related('records')
|
||||
|
||||
|
||||
def get_parent(self, top=False):
|
||||
return type(self).objects.get_parent(self.name, top=top)
|
||||
|
||||
|
||||
def render_zone(self):
|
||||
origin = self.origin
|
||||
zone = origin.render_records()
|
||||
|
@ -147,7 +147,7 @@ class Domain(models.Model):
|
|||
for subdomain in sorted(tail, key=lambda x: len(x.name), reverse=True):
|
||||
zone += subdomain.render_records()
|
||||
return zone.strip()
|
||||
|
||||
|
||||
def refresh_serial(self):
|
||||
""" Increases the domain serial number by one """
|
||||
serial = utils.generate_zone_serial()
|
||||
|
@ -159,7 +159,7 @@ class Domain(models.Model):
|
|||
serial = int(serial)
|
||||
self.serial = serial
|
||||
self.save(update_fields=('serial',))
|
||||
|
||||
|
||||
def get_default_soa(self):
|
||||
return ' '.join([
|
||||
"%s." % settings.DOMAINS_DEFAULT_NAME_SERVER,
|
||||
|
@ -170,7 +170,7 @@ class Domain(models.Model):
|
|||
self.expire or settings.DOMAINS_DEFAULT_EXPIRE,
|
||||
self.min_ttl or settings.DOMAINS_DEFAULT_MIN_TTL,
|
||||
])
|
||||
|
||||
|
||||
def get_default_records(self):
|
||||
defaults = []
|
||||
if self.is_top:
|
||||
|
@ -202,7 +202,7 @@ class Domain(models.Model):
|
|||
value=default_aaaa
|
||||
))
|
||||
return defaults
|
||||
|
||||
|
||||
def record_is_implicit(self, record, types):
|
||||
if record.type not in types:
|
||||
if record.type is Record.NS:
|
||||
|
@ -221,7 +221,7 @@ class Domain(models.Model):
|
|||
elif not has_a and not has_aaaa:
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
def get_records(self):
|
||||
types = set()
|
||||
records = utils.RecordStorage()
|
||||
|
@ -249,7 +249,7 @@ class Domain(models.Model):
|
|||
else:
|
||||
records.append(record)
|
||||
return records
|
||||
|
||||
|
||||
def render_records(self):
|
||||
result = ''
|
||||
for record in self.get_records():
|
||||
|
@ -273,7 +273,7 @@ class Domain(models.Model):
|
|||
value=record.value
|
||||
)
|
||||
return result
|
||||
|
||||
|
||||
def has_default_mx(self):
|
||||
records = self.get_records()
|
||||
for record in records.by_type('MX'):
|
||||
|
@ -294,7 +294,7 @@ class Record(models.Model):
|
|||
TXT = 'TXT'
|
||||
SPF = 'SPF'
|
||||
SOA = 'SOA'
|
||||
|
||||
|
||||
TYPE_CHOICES = (
|
||||
(MX, "MX"),
|
||||
(NS, "NS"),
|
||||
|
@ -305,7 +305,7 @@ class Record(models.Model):
|
|||
(TXT, "TXT"),
|
||||
(SPF, "SPF"),
|
||||
)
|
||||
|
||||
|
||||
VALIDATORS = {
|
||||
MX: (validators.validate_mx_record,),
|
||||
NS: (validators.validate_zone_label,),
|
||||
|
@ -317,8 +317,8 @@ class Record(models.Model):
|
|||
SRV: (validators.validate_srv_record,),
|
||||
SOA: (validators.validate_soa_record,),
|
||||
}
|
||||
|
||||
domain = models.ForeignKey(Domain, verbose_name=_("domain"), related_name='records', on_delete=models.CASCADE)
|
||||
|
||||
domain = models.ForeignKey(Domain, verbose_name=_("domain"), related_name='records')
|
||||
ttl = models.CharField(_("TTL"), max_length=8, blank=True,
|
||||
help_text=_("Record TTL, defaults to %s") % settings.DOMAINS_DEFAULT_TTL,
|
||||
validators=[validators.validate_zone_interval])
|
||||
|
@ -326,10 +326,10 @@ class Record(models.Model):
|
|||
# max_length bumped from 256 to 1024 (arbitrary) on August 2019.
|
||||
value = models.CharField(_("value"), max_length=1024,
|
||||
help_text=_("MX, NS and CNAME records sould end with a dot."))
|
||||
|
||||
|
||||
def __str__(self):
|
||||
return "%s %s IN %s %s" % (self.domain, self.get_ttl(), self.type, self.value)
|
||||
|
||||
|
||||
def clean(self):
|
||||
""" validates record value based on its type """
|
||||
# validate value
|
||||
|
@ -343,6 +343,6 @@ class Record(models.Model):
|
|||
raise ValidationError({
|
||||
'value': error,
|
||||
})
|
||||
|
||||
|
||||
def get_ttl(self):
|
||||
return self.ttl or settings.DOMAINS_DEFAULT_TTL
|
||||
|
|
|
@ -2,9 +2,10 @@ import os
|
|||
import time
|
||||
import socket
|
||||
from functools import partial
|
||||
from unittest import skip
|
||||
|
||||
from django.conf import settings as djsettings
|
||||
from django.urls import reverse
|
||||
from django.core.urlresolvers import reverse
|
||||
from selenium.webdriver.support.select import Select
|
||||
|
||||
from orchestra.contrib.orchestration.models import Server, Route
|
||||
|
@ -20,10 +21,10 @@ run = partial(run, display=False)
|
|||
|
||||
class DomainTestMixin(object):
|
||||
MASTER_SERVER = os.environ.get('ORCHESTRA_MASTER_SERVER', 'localhost')
|
||||
SLAVE_SERVER = os.environ.get('ORCHESTRA_SLAVE_SERVER', 'localhost')
|
||||
SLAVE_SERVER = os.environ.get('ORCHESTRA_SLAVE_SERVER', 'localhost2')
|
||||
MASTER_SERVER_ADDR = socket.gethostbyname(MASTER_SERVER)
|
||||
SLAVE_SERVER_ADDR = socket.gethostbyname(SLAVE_SERVER)
|
||||
|
||||
SLAVE_SERVER_ADDR = '127.0.0.2'
|
||||
|
||||
def setUp(self):
|
||||
djsettings.DEBUG = True
|
||||
super(DomainTestMixin, self).setUp()
|
||||
|
@ -53,19 +54,19 @@ class DomainTestMixin(object):
|
|||
(Record.CNAME, 'external.server.org.'),
|
||||
)
|
||||
self.django_domain_name = 'django%s.lan' % random_ascii(10)
|
||||
|
||||
|
||||
def add_route(self):
|
||||
raise NotImplementedError
|
||||
|
||||
|
||||
def add(self, domain_name, records):
|
||||
raise NotImplementedError
|
||||
|
||||
|
||||
def delete(self, domain_name, records):
|
||||
raise NotImplementedError
|
||||
|
||||
|
||||
def update(self, domain_name, records):
|
||||
raise NotImplementedError
|
||||
|
||||
|
||||
def validate_add(self, server_addr, domain_name):
|
||||
context = {
|
||||
'domain_name': domain_name,
|
||||
|
@ -81,7 +82,7 @@ class DomainTestMixin(object):
|
|||
self.assertEqual('%s.' % settings.DOMAINS_DEFAULT_NAME_SERVER, soa[4])
|
||||
hostmaster = utils.format_hostmaster(settings.DOMAINS_DEFAULT_HOSTMASTER)
|
||||
self.assertEqual(hostmaster, soa[5])
|
||||
|
||||
|
||||
dig_ns = 'dig @%(server_addr)s %(domain_name)s NS|grep "\sNS\s"'
|
||||
name_servers = run(dig_ns % context).stdout
|
||||
# testdomain.org. 3600 IN NS ns1.orchestra.lan.
|
||||
|
@ -95,7 +96,7 @@ class DomainTestMixin(object):
|
|||
self.assertEqual('IN', ns[2])
|
||||
self.assertEqual('NS', ns[3])
|
||||
self.assertIn(ns[4], ns_records)
|
||||
|
||||
|
||||
dig_mx = 'dig @%(server_addr)s %(domain_name)s MX|grep "\sMX\s"'
|
||||
mail_servers = run(dig_mx % context).stdout
|
||||
for mx in mail_servers.splitlines():
|
||||
|
@ -107,7 +108,7 @@ class DomainTestMixin(object):
|
|||
self.assertEqual('MX', mx[3])
|
||||
self.assertIn(mx[4], ['10', '20'])
|
||||
self.assertIn(mx[5], ['mail2.orchestra.lan.', 'mail.orchestra.lan.'])
|
||||
|
||||
|
||||
def validate_delete(self, server_addr, domain_name):
|
||||
context = {
|
||||
'domain_name': domain_name,
|
||||
|
@ -122,7 +123,7 @@ class DomainTestMixin(object):
|
|||
self.assertNotEqual('%s.' % settings.DOMAINS_DEFAULT_NAME_SERVER, soa[4])
|
||||
hostmaster = utils.format_hostmaster(settings.DOMAINS_DEFAULT_HOSTMASTER)
|
||||
self.assertNotEqual(hostmaster, soa[5])
|
||||
|
||||
|
||||
def validate_update(self, server_addr, domain_name):
|
||||
context = {
|
||||
'domain_name': domain_name,
|
||||
|
@ -138,7 +139,7 @@ class DomainTestMixin(object):
|
|||
self.assertEqual('%s.' % settings.DOMAINS_DEFAULT_NAME_SERVER, soa[4])
|
||||
hostmaster = utils.format_hostmaster(settings.DOMAINS_DEFAULT_HOSTMASTER)
|
||||
self.assertEqual(hostmaster, soa[5])
|
||||
|
||||
|
||||
dig_ns = 'dig @%(server_addr)s %(domain_name)s NS |grep "\sNS\s"'
|
||||
name_servers = run(dig_ns % context).stdout
|
||||
ns_records = ['ns1.%s.' % self.domain_name, 'ns2.%s.' % self.domain_name]
|
||||
|
@ -151,7 +152,7 @@ class DomainTestMixin(object):
|
|||
self.assertEqual('IN', ns[2])
|
||||
self.assertEqual('NS', ns[3])
|
||||
self.assertIn(ns[4], ns_records)
|
||||
|
||||
|
||||
dig_mx = 'dig @%(server_addr)s %(domain_name)s MX | grep "\sMX\s"'
|
||||
mx = run(dig_mx % context).stdout.split()
|
||||
# testdomain.org. 3600 IN MX 10 orchestra.lan.
|
||||
|
@ -161,7 +162,7 @@ class DomainTestMixin(object):
|
|||
self.assertEqual('MX', mx[3])
|
||||
self.assertIn(mx[4], ['30', '40'])
|
||||
self.assertIn(mx[5], ['mail3.orchestra.lan.', 'mail4.orchestra.lan.'])
|
||||
|
||||
|
||||
def validate_www_update(self, server_addr, domain_name):
|
||||
context = {
|
||||
'domain_name': domain_name,
|
||||
|
@ -175,7 +176,8 @@ class DomainTestMixin(object):
|
|||
self.assertEqual('IN', cname[2])
|
||||
self.assertEqual('CNAME', cname[3])
|
||||
self.assertEqual('external.server.org.', cname[4])
|
||||
|
||||
|
||||
@skip("Skip because not exists get_auth_token in orm.api.Api")
|
||||
def test_add(self):
|
||||
self.add(self.ns1_name, self.ns1_records)
|
||||
self.add(self.ns2_name, self.ns2_records)
|
||||
|
@ -184,7 +186,8 @@ class DomainTestMixin(object):
|
|||
self.validate_add(self.MASTER_SERVER_ADDR, self.domain_name)
|
||||
time.sleep(1)
|
||||
self.validate_add(self.SLAVE_SERVER_ADDR, self.domain_name)
|
||||
|
||||
|
||||
@skip("Skip because not exists get_auth_token in orm.api.Api")
|
||||
def test_delete(self):
|
||||
self.add(self.ns1_name, self.ns1_records)
|
||||
self.add(self.ns2_name, self.ns2_records)
|
||||
|
@ -193,7 +196,8 @@ class DomainTestMixin(object):
|
|||
for name in [self.domain_name, self.ns1_name, self.ns2_name]:
|
||||
self.validate_delete(self.MASTER_SERVER_ADDR, name)
|
||||
self.validate_delete(self.SLAVE_SERVER_ADDR, name)
|
||||
|
||||
|
||||
@skip("Skip because not exists get_auth_token in orm.api.Api")
|
||||
def test_update(self):
|
||||
self.add(self.ns1_name, self.ns1_records)
|
||||
self.add(self.ns2_name, self.ns2_records)
|
||||
|
@ -209,7 +213,8 @@ class DomainTestMixin(object):
|
|||
self.validate_www_update(self.MASTER_SERVER_ADDR, self.domain_name)
|
||||
time.sleep(5)
|
||||
self.validate_www_update(self.SLAVE_SERVER_ADDR, self.domain_name)
|
||||
|
||||
|
||||
@skip("Skip because not exists get_auth_token in orm.api.Api")
|
||||
def test_add_add_delete_delete(self):
|
||||
self.add(self.ns1_name, self.ns1_records)
|
||||
self.add(self.ns2_name, self.ns2_records)
|
||||
|
@ -221,7 +226,8 @@ class DomainTestMixin(object):
|
|||
self.delete(self.django_domain_name)
|
||||
self.validate_delete(self.MASTER_SERVER_ADDR, self.django_domain_name)
|
||||
self.validate_delete(self.SLAVE_SERVER_ADDR, self.django_domain_name)
|
||||
|
||||
|
||||
@skip("Skip because not exists get_auth_token in orm.api.Api")
|
||||
def test_bad_creation(self):
|
||||
self.assertRaises((self.rest.ResponseStatusError, AssertionError),
|
||||
self.add, self.domain_name, self.domain_records)
|
||||
|
@ -232,7 +238,7 @@ class AdminDomainMixin(DomainTestMixin):
|
|||
super(AdminDomainMixin, self).setUp()
|
||||
self.add_route()
|
||||
self.admin_login()
|
||||
|
||||
|
||||
def _add_records(self, records):
|
||||
self.selenium.find_element_by_link_text('Add another Record').click()
|
||||
for i, record in zip(range(0, len(records)), records):
|
||||
|
@ -244,29 +250,29 @@ class AdminDomainMixin(DomainTestMixin):
|
|||
value_input.clear()
|
||||
value_input.send_keys(value)
|
||||
return value_input
|
||||
|
||||
|
||||
@snapshot_on_error
|
||||
def add(self, domain_name, records):
|
||||
add = reverse('admin:domains_domain_add')
|
||||
url = self.live_server_url + add
|
||||
self.selenium.get(url)
|
||||
|
||||
|
||||
name = self.selenium.find_element_by_id('id_name')
|
||||
name.send_keys(domain_name)
|
||||
|
||||
|
||||
account_input = self.selenium.find_element_by_id('id_account')
|
||||
account_select = Select(account_input)
|
||||
account_select.select_by_value(str(self.account.pk))
|
||||
|
||||
|
||||
value_input = self._add_records(records)
|
||||
value_input.submit()
|
||||
self.assertNotEqual(url, self.selenium.current_url)
|
||||
|
||||
|
||||
@snapshot_on_error
|
||||
def delete(self, domain_name):
|
||||
domain = Domain.objects.get(name=domain_name)
|
||||
self.admin_delete(domain)
|
||||
|
||||
|
||||
@snapshot_on_error
|
||||
def update(self, domain_name, records):
|
||||
domain = Domain.objects.get(name=domain_name)
|
||||
|
@ -283,18 +289,18 @@ class RESTDomainMixin(DomainTestMixin):
|
|||
super(RESTDomainMixin, self).setUp()
|
||||
self.rest_login()
|
||||
self.add_route()
|
||||
|
||||
|
||||
@save_response_on_error
|
||||
def add(self, domain_name, records):
|
||||
records = [ dict(type=type, value=value) for type,value in records ]
|
||||
self.rest.domains.create(name=domain_name, records=records)
|
||||
|
||||
|
||||
@save_response_on_error
|
||||
def delete(self, domain_name):
|
||||
domain = Domain.objects.get(name=domain_name)
|
||||
domain = self.rest.domains.retrieve(id=domain.pk)
|
||||
domain.delete()
|
||||
|
||||
|
||||
@save_response_on_error
|
||||
def update(self, domain_name, records):
|
||||
records = [ dict(type=type, value=value) for type,value in records ]
|
||||
|
@ -307,7 +313,7 @@ class Bind9BackendMixin(object):
|
|||
DEPENDENCIES = (
|
||||
'orchestra.contrib.orchestration',
|
||||
)
|
||||
|
||||
|
||||
def add_route(self):
|
||||
master = Server.objects.create(name=self.MASTER_SERVER, address=self.MASTER_SERVER_ADDR)
|
||||
backend = backends.Bind9MasterDomainController.get_name()
|
||||
|
|
|
@ -1,14 +1,12 @@
|
|||
from django.contrib import admin
|
||||
from django.contrib.admin.templatetags.admin_static import static
|
||||
from django.contrib.admin.templatetags.admin_urls import add_preserved_filters
|
||||
from django.contrib.admin.utils import unquote
|
||||
from django.http import HttpResponseRedirect
|
||||
from django.urls import NoReverseMatch, reverse
|
||||
from django.utils.html import format_html
|
||||
from django.utils.safestring import mark_safe
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
from django.core.urlresolvers import reverse, NoReverseMatch
|
||||
from django.contrib.admin.templatetags.admin_urls import add_preserved_filters
|
||||
from django.http import HttpResponseRedirect
|
||||
from django.contrib.admin.utils import unquote
|
||||
from django.contrib.admin.templatetags.admin_static import static
|
||||
|
||||
from orchestra.admin.utils import admin_date, admin_link
|
||||
from orchestra.admin.utils import admin_link, admin_date
|
||||
|
||||
|
||||
class LogEntryAdmin(admin.ModelAdmin):
|
||||
|
@ -32,16 +30,15 @@ class LogEntryAdmin(admin.ModelAdmin):
|
|||
actions = None
|
||||
list_select_related = ('user', 'content_type')
|
||||
list_display_links = None
|
||||
|
||||
|
||||
user_link = admin_link('user')
|
||||
display_action_time = admin_date('action_time', short_description=_("Time"))
|
||||
|
||||
@mark_safe
|
||||
|
||||
def display_message(self, log):
|
||||
edit = format_html('<a href="{url}"><img src="{img}"></img></a>', **{
|
||||
edit = '<a href="%(url)s"><img src="%(img)s"></img></a>' % {
|
||||
'url': reverse('admin:admin_logentry_change', args=(log.pk,)),
|
||||
'img': static('admin/img/icon-changelink.svg'),
|
||||
})
|
||||
}
|
||||
if log.is_addition():
|
||||
return _('Added "%(link)s". %(edit)s') % {
|
||||
'link': self.content_object_link(log),
|
||||
|
@ -60,7 +57,8 @@ class LogEntryAdmin(admin.ModelAdmin):
|
|||
}
|
||||
display_message.short_description = _("Message")
|
||||
display_message.admin_order_field = 'action_flag'
|
||||
|
||||
display_message.allow_tags = True
|
||||
|
||||
def display_action(self, log):
|
||||
if log.is_addition():
|
||||
return _("Added")
|
||||
|
@ -69,7 +67,7 @@ class LogEntryAdmin(admin.ModelAdmin):
|
|||
return _("Deleted")
|
||||
display_action.short_description = _("Action")
|
||||
display_action.admin_order_field = 'action_flag'
|
||||
|
||||
|
||||
def content_object_link(self, log):
|
||||
ct = log.content_type
|
||||
view = 'admin:%s_%s_change' % (ct.app_label, ct.model)
|
||||
|
@ -77,10 +75,11 @@ class LogEntryAdmin(admin.ModelAdmin):
|
|||
url = reverse(view, args=(log.object_id,))
|
||||
except NoReverseMatch:
|
||||
return log.object_repr
|
||||
return format_html('<a href="{}">{}</a>', url, log.object_repr)
|
||||
return '<a href="%s">%s</a>' % (url, log.object_repr)
|
||||
content_object_link.short_description = _("Content object")
|
||||
content_object_link.admin_order_field = 'object_repr'
|
||||
|
||||
content_object_link.allow_tags = True
|
||||
|
||||
def render_change_form(self, request, context, add=False, change=False, form_url='', obj=None):
|
||||
""" Add rel_opts and object to context """
|
||||
if not add and 'edit' in request.GET.urlencode():
|
||||
|
@ -90,14 +89,14 @@ class LogEntryAdmin(admin.ModelAdmin):
|
|||
})
|
||||
return super(LogEntryAdmin, self).render_change_form(
|
||||
request, context, add, change, form_url, obj)
|
||||
|
||||
|
||||
def response_change(self, request, obj):
|
||||
""" save and continue preserve edit query string """
|
||||
response = super(LogEntryAdmin, self).response_change(request, obj)
|
||||
if 'edit' in request.GET.urlencode() and 'edit' not in response.url:
|
||||
return HttpResponseRedirect(response.url + '?edit=True')
|
||||
return response
|
||||
|
||||
|
||||
def response_post_save_change(self, request, obj):
|
||||
""" save redirect to object history """
|
||||
if 'edit' in request.GET.urlencode():
|
||||
|
@ -110,19 +109,19 @@ class LogEntryAdmin(admin.ModelAdmin):
|
|||
}, post_url)
|
||||
return HttpResponseRedirect(post_url)
|
||||
return super(LogEntryAdmin, self).response_post_save_change(request, obj)
|
||||
|
||||
|
||||
def has_add_permission(self, *args, **kwargs):
|
||||
return False
|
||||
|
||||
|
||||
def has_delete_permission(self, *args, **kwargs):
|
||||
return False
|
||||
|
||||
|
||||
def log_addition(self, *args, **kwargs):
|
||||
pass
|
||||
|
||||
|
||||
def log_change(self, *args, **kwargs):
|
||||
pass
|
||||
|
||||
|
||||
def log_deletion(self, *args, **kwargs):
|
||||
pass
|
||||
|
||||
|
|
|
@ -1,12 +1,11 @@
|
|||
from django import forms
|
||||
from django.conf.urls import url
|
||||
from django.contrib import admin
|
||||
from django.urls import reverse
|
||||
from django.core.urlresolvers import reverse
|
||||
from django.db import models
|
||||
from django.http import HttpResponse
|
||||
from django.shortcuts import get_object_or_404
|
||||
from django.utils.html import format_html, strip_tags
|
||||
from django.utils.safestring import mark_safe
|
||||
from django.utils.html import strip_tags
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
from markdown import markdown
|
||||
|
||||
|
@ -22,14 +21,14 @@ from .helpers import get_ticket_changes, markdown_formated_changes, filter_actio
|
|||
from .models import Ticket, Queue, Message
|
||||
|
||||
|
||||
PRIORITY_COLORS = {
|
||||
PRIORITY_COLORS = {
|
||||
Ticket.HIGH: 'red',
|
||||
Ticket.MEDIUM: 'darkorange',
|
||||
Ticket.LOW: 'green',
|
||||
}
|
||||
|
||||
|
||||
STATE_COLORS = {
|
||||
STATE_COLORS = {
|
||||
Ticket.NEW: 'grey',
|
||||
Ticket.IN_PROGRESS: 'darkorange',
|
||||
Ticket.FEEDBACK: 'purple',
|
||||
|
@ -45,13 +44,12 @@ class MessageReadOnlyInline(admin.TabularInline):
|
|||
can_delete = False
|
||||
fields = ('content_html',)
|
||||
readonly_fields = ('content_html',)
|
||||
|
||||
|
||||
class Media:
|
||||
css = {
|
||||
'all': ('orchestra/css/hide-inline-id.css',)
|
||||
}
|
||||
|
||||
@mark_safe
|
||||
|
||||
def content_html(self, msg):
|
||||
context = {
|
||||
'number': msg.number,
|
||||
|
@ -60,17 +58,16 @@ class MessageReadOnlyInline(admin.TabularInline):
|
|||
}
|
||||
summary = _("#%(number)i Updated by %(author)s about %(time)s") % context
|
||||
header = '<strong style="color:#666;">%s</strong><hr />' % summary
|
||||
|
||||
content = markdown(msg.content)
|
||||
content = content.replace('>\n', '>')
|
||||
content = '<div style="padding-left:20px;">%s</div>' % content
|
||||
|
||||
return header + content
|
||||
content_html.short_description = _("Content")
|
||||
|
||||
content_html.allow_tags = True
|
||||
|
||||
def has_add_permission(self, request):
|
||||
return False
|
||||
|
||||
|
||||
def has_delete_permission(self, request, obj=None):
|
||||
return False
|
||||
|
||||
|
@ -82,12 +79,12 @@ class MessageInline(admin.TabularInline):
|
|||
form = MessageInlineForm
|
||||
can_delete = False
|
||||
fields = ('content',)
|
||||
|
||||
|
||||
def get_formset(self, request, obj=None, **kwargs):
|
||||
""" hook request.user on the inline form """
|
||||
self.form.user = request.user
|
||||
return super(MessageInline, self).get_formset(request, obj, **kwargs)
|
||||
|
||||
|
||||
def get_queryset(self, request):
|
||||
""" Don't show any message """
|
||||
qs = super(MessageInline, self).get_queryset(request)
|
||||
|
@ -106,18 +103,18 @@ class TicketInline(admin.TabularInline):
|
|||
model = Ticket
|
||||
extra = 0
|
||||
max_num = 0
|
||||
|
||||
|
||||
creator_link = admin_link('creator')
|
||||
owner_link = admin_link('owner')
|
||||
created = admin_link('created_at')
|
||||
updated = admin_link('updated_at')
|
||||
colored_state = admin_colored('state', colors=STATE_COLORS, bold=False)
|
||||
colored_priority = admin_colored('priority', colors=PRIORITY_COLORS, bold=False)
|
||||
|
||||
@mark_safe
|
||||
|
||||
def ticket_id(self, instance):
|
||||
return '<b>%s</b>' % admin_link()(instance)
|
||||
ticket_id.short_description = '#'
|
||||
ticket_id.allow_tags = True
|
||||
|
||||
|
||||
class TicketAdmin(ExtendedModelAdmin):
|
||||
|
@ -138,7 +135,7 @@ class TicketAdmin(ExtendedModelAdmin):
|
|||
'owner__username'
|
||||
)
|
||||
actions = (
|
||||
mark_as_unread, mark_as_read, reject_tickets,
|
||||
mark_as_unread, mark_as_read, 'delete_selected', reject_tickets,
|
||||
resolve_tickets, close_tickets, take_tickets
|
||||
)
|
||||
sudo_actions = ('delete_selected',)
|
||||
|
@ -179,7 +176,7 @@ class TicketAdmin(ExtendedModelAdmin):
|
|||
}),
|
||||
)
|
||||
list_select_related = ('queue', 'owner', 'creator')
|
||||
|
||||
|
||||
class Media:
|
||||
css = {
|
||||
'all': ('issues/css/ticket-admin.css',)
|
||||
|
@ -187,15 +184,14 @@ class TicketAdmin(ExtendedModelAdmin):
|
|||
js = (
|
||||
'issues/js/ticket-admin.js',
|
||||
)
|
||||
|
||||
|
||||
display_creator = admin_link('creator')
|
||||
display_queue = admin_link('queue')
|
||||
display_owner = admin_link('owner')
|
||||
updated = admin_date('updated_at')
|
||||
display_state = admin_colored('state', colors=STATE_COLORS, bold=False)
|
||||
display_priority = admin_colored('priority', colors=PRIORITY_COLORS, bold=False)
|
||||
|
||||
@mark_safe
|
||||
|
||||
def display_summary(self, ticket):
|
||||
context = {
|
||||
'creator': admin_link('creator')(self, ticket) if ticket.creator else ticket.creator_name,
|
||||
|
@ -211,47 +207,50 @@ class TicketAdmin(ExtendedModelAdmin):
|
|||
context['updated'] = '. Updated by %(updater)s about %(updated)s' % context
|
||||
return '<h4>Added by %(creator)s about %(created)s%(updated)s</h4>' % context
|
||||
display_summary.short_description = 'Summary'
|
||||
|
||||
display_summary.allow_tags = True
|
||||
|
||||
def unbold_id(self, ticket):
|
||||
""" Unbold id if ticket is read """
|
||||
if ticket.is_read_by(self.user):
|
||||
return format_html('<span style="font-weight:normal;font-size:11px;">{}</span>', ticket.pk)
|
||||
return '<span style="font-weight:normal;font-size:11px;">%s</span>' % ticket.pk
|
||||
return ticket.pk
|
||||
unbold_id.allow_tags = True
|
||||
unbold_id.short_description = "#"
|
||||
unbold_id.admin_order_field = 'id'
|
||||
|
||||
|
||||
def bold_subject(self, ticket):
|
||||
""" Bold subject when tickets are unread for request.user """
|
||||
if ticket.is_read_by(self.user):
|
||||
return ticket.subject
|
||||
return format_html("<strong class='unread'>{}</strong>", ticket.subject)
|
||||
return "<strong class='unread'>%s</strong>" % ticket.subject
|
||||
bold_subject.allow_tags = True
|
||||
bold_subject.short_description = _("Subject")
|
||||
bold_subject.admin_order_field = 'subject'
|
||||
|
||||
|
||||
def formfield_for_dbfield(self, db_field, **kwargs):
|
||||
""" Make value input widget bigger """
|
||||
if db_field.name == 'subject':
|
||||
kwargs['widget'] = forms.TextInput(attrs={'size':'120'})
|
||||
return super(TicketAdmin, self).formfield_for_dbfield(db_field, **kwargs)
|
||||
|
||||
|
||||
def save_model(self, request, obj, *args, **kwargs):
|
||||
""" Define creator for new tickets """
|
||||
if not obj.pk:
|
||||
obj.creator = request.user
|
||||
super(TicketAdmin, self).save_model(request, obj, *args, **kwargs)
|
||||
obj.mark_as_read_by(request.user)
|
||||
|
||||
|
||||
def get_urls(self):
|
||||
""" add markdown preview url """
|
||||
return [
|
||||
url(r'^preview/$',
|
||||
wrap_admin_view(self, self.message_preview_view))
|
||||
] + super(TicketAdmin, self).get_urls()
|
||||
|
||||
|
||||
def add_view(self, request, form_url='', extra_context=None):
|
||||
""" Do not sow message inlines """
|
||||
return super(TicketAdmin, self).add_view(request, form_url, extra_context)
|
||||
|
||||
|
||||
def change_view(self, request, object_id, form_url='', extra_context=None):
|
||||
""" Change view actions based on ticket state """
|
||||
ticket = get_object_or_404(Ticket, pk=object_id)
|
||||
|
@ -270,12 +269,12 @@ class TicketAdmin(ExtendedModelAdmin):
|
|||
context.update(extra_context or {})
|
||||
return super(TicketAdmin, self).change_view(request, object_id, form_url=form_url,
|
||||
extra_context=context)
|
||||
|
||||
|
||||
def changelist_view(self, request, extra_context=None):
|
||||
# Hook user for bold_subject
|
||||
self.user = request.user
|
||||
return super(TicketAdmin,self).changelist_view(request, extra_context=extra_context)
|
||||
|
||||
|
||||
def message_preview_view(self, request):
|
||||
""" markdown preview render via ajax """
|
||||
data = request.POST.get("data")
|
||||
|
@ -288,20 +287,21 @@ class QueueAdmin(admin.ModelAdmin):
|
|||
actions = (set_default_queue,)
|
||||
inlines = (TicketInline,)
|
||||
ordering = ('name',)
|
||||
|
||||
|
||||
class Media:
|
||||
css = {
|
||||
'all': ('orchestra/css/hide-inline-id.css',)
|
||||
}
|
||||
|
||||
|
||||
def num_tickets(self, queue):
|
||||
num = queue.tickets__count
|
||||
url = reverse('admin:issues_ticket_changelist')
|
||||
url += '?queue=%i' % queue.pk
|
||||
return format_html('<a href="{}">{}</a>', url, num)
|
||||
return '<a href="%s">%d</a>' % (url, num)
|
||||
num_tickets.short_description = _("Tickets")
|
||||
num_tickets.admin_order_field = 'tickets__count'
|
||||
|
||||
num_tickets.allow_tags = True
|
||||
|
||||
def get_list_display(self, request):
|
||||
""" show notifications """
|
||||
list_display = list(self.list_display)
|
||||
|
@ -312,7 +312,7 @@ class QueueAdmin(admin.ModelAdmin):
|
|||
display_notify.boolean = True
|
||||
list_display.append(display_notify)
|
||||
return list_display
|
||||
|
||||
|
||||
def get_queryset(self, request):
|
||||
qs = super(QueueAdmin, self).get_queryset(request)
|
||||
qs = qs.annotate(models.Count('tickets'))
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
from rest_framework import viewsets, mixins
|
||||
from rest_framework.decorators import action
|
||||
from rest_framework.decorators import detail_route
|
||||
from rest_framework.response import Response
|
||||
|
||||
from orchestra.api import router, LogApiMixin
|
||||
|
@ -12,19 +12,19 @@ from .serializers import TicketSerializer, QueueSerializer
|
|||
class TicketViewSet(LogApiMixin, viewsets.ModelViewSet):
|
||||
queryset = Ticket.objects.all()
|
||||
serializer_class = TicketSerializer
|
||||
|
||||
@action(detail=True)
|
||||
|
||||
@detail_route()
|
||||
def mark_as_read(self, request, pk=None):
|
||||
ticket = self.get_object()
|
||||
ticket.mark_as_read_by(request.user)
|
||||
return Response({'status': 'Ticket marked as read'})
|
||||
|
||||
@action(detail=True)
|
||||
|
||||
@detail_route()
|
||||
def mark_as_unread(self, request, pk=None):
|
||||
ticket = self.get_object()
|
||||
ticket.mark_as_unread_by(request.user)
|
||||
return Response({'status': 'Ticket marked as unread'})
|
||||
|
||||
|
||||
def get_queryset(self):
|
||||
qs = super(TicketViewSet, self).get_queryset()
|
||||
qs = qs.select_related('creator', 'queue')
|
||||
|
|
|
@ -13,7 +13,7 @@ from .models import Queue, Ticket
|
|||
|
||||
class MarkDownWidget(forms.Textarea):
|
||||
""" MarkDown textarea widget with syntax preview """
|
||||
|
||||
|
||||
markdown_url = static('issues/markdown_syntax.html')
|
||||
markdown_help_text = (
|
||||
'<a href="%s" onclick=\'window.open("%s", "", "resizable=yes, '
|
||||
|
@ -21,8 +21,8 @@ class MarkDownWidget(forms.Textarea):
|
|||
'return false;\'>markdown format</a>' % (markdown_url, markdown_url)
|
||||
)
|
||||
markdown_help_text = 'HTML not allowed, you can use %s' % markdown_help_text
|
||||
|
||||
def render(self, name, value, attrs, renderer=None):
|
||||
|
||||
def render(self, name, value, attrs):
|
||||
widget_id = attrs['id'] if attrs and 'id' in attrs else 'id_%s' % name
|
||||
textarea = super(MarkDownWidget, self).render(name, value, attrs)
|
||||
preview = ('<a class="load-preview" href="#" data-field="{0}">preview</a>'\
|
||||
|
@ -35,18 +35,18 @@ class MessageInlineForm(forms.ModelForm):
|
|||
""" Add message form """
|
||||
created_on = forms.CharField(label="Created On", required=False)
|
||||
content = forms.CharField(widget=MarkDownWidget(), required=False)
|
||||
|
||||
|
||||
class Meta:
|
||||
fields = ('author', 'author_name', 'created_on', 'content')
|
||||
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super(MessageInlineForm, self).__init__(*args, **kwargs)
|
||||
self.fields['created_on'].widget = SpanWidget(display='')
|
||||
|
||||
|
||||
def clean_content(self):
|
||||
""" clean HTML tags """
|
||||
return strip_tags(self.cleaned_data['content'])
|
||||
|
||||
|
||||
def save(self, *args, **kwargs):
|
||||
if self.instance.pk is None:
|
||||
self.instance.author = self.user
|
||||
|
@ -58,7 +58,7 @@ class UsersIterator(forms.models.ModelChoiceIterator):
|
|||
def __init__(self, *args, **kwargs):
|
||||
self.ticket = kwargs.pop('ticket', False)
|
||||
super(forms.models.ModelChoiceIterator, self).__init__(*args, **kwargs)
|
||||
|
||||
|
||||
def __iter__(self):
|
||||
yield ('', '---------')
|
||||
users = get_user_model().objects.exclude(is_active=False).order_by('name')
|
||||
|
@ -73,14 +73,14 @@ class UsersIterator(forms.models.ModelChoiceIterator):
|
|||
class TicketForm(forms.ModelForm):
|
||||
display_description = forms.CharField(label=_("Description"), required=False)
|
||||
description = forms.CharField(widget=MarkDownWidget(attrs={'class':'vLargeTextField'}))
|
||||
|
||||
|
||||
class Meta:
|
||||
model = Ticket
|
||||
fields = (
|
||||
'creator', 'creator_name', 'owner', 'queue', 'subject', 'description',
|
||||
'priority', 'state', 'cc', 'display_description'
|
||||
)
|
||||
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super(TicketForm, self).__init__(*args, **kwargs)
|
||||
ticket = kwargs.get('instance', False)
|
||||
|
@ -101,7 +101,7 @@ class TicketForm(forms.ModelForm):
|
|||
description = '<div style="padding-left: 95px;">%s</div>' % description
|
||||
widget = SpanWidget(display=description)
|
||||
self.fields['display_description'].widget = widget
|
||||
|
||||
|
||||
def clean_description(self):
|
||||
""" clean HTML tags """
|
||||
return strip_tags(self.cleaned_data['description'])
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
from __future__ import unicode_literals
|
||||
|
||||
import django.db.models.deletion
|
||||
from django.db import models, migrations
|
||||
import orchestra.models.fields
|
||||
from django.conf import settings
|
||||
|
@ -21,7 +20,7 @@ class Migration(migrations.Migration):
|
|||
('author_name', models.CharField(blank=True, max_length=256, verbose_name='author name')),
|
||||
('content', models.TextField(verbose_name='content')),
|
||||
('created_on', models.DateTimeField(auto_now_add=True, verbose_name='created on')),
|
||||
('author', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='ticket_messages', to=settings.AUTH_USER_MODEL, verbose_name='author')),
|
||||
('author', models.ForeignKey(related_name='ticket_messages', to=settings.AUTH_USER_MODEL, verbose_name='author')),
|
||||
],
|
||||
options={
|
||||
'get_latest_by': 'id',
|
||||
|
@ -49,9 +48,9 @@ class Migration(migrations.Migration):
|
|||
('created_at', models.DateTimeField(auto_now_add=True, verbose_name='created')),
|
||||
('updated_at', models.DateTimeField(auto_now=True, verbose_name='modified')),
|
||||
('cc', models.TextField(blank=True, help_text='emails to send a carbon copy to', verbose_name='CC')),
|
||||
('creator', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='tickets_created', null=True, to=settings.AUTH_USER_MODEL, verbose_name='created by')),
|
||||
('owner', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, blank=True, related_name='tickets_owned', null=True, to=settings.AUTH_USER_MODEL, verbose_name='assigned to')),
|
||||
('queue', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, blank=True, related_name='tickets', null=True, to='issues.Queue')),
|
||||
('creator', models.ForeignKey(related_name='tickets_created', null=True, to=settings.AUTH_USER_MODEL, verbose_name='created by')),
|
||||
('owner', models.ForeignKey(blank=True, related_name='tickets_owned', null=True, to=settings.AUTH_USER_MODEL, verbose_name='assigned to')),
|
||||
('queue', models.ForeignKey(blank=True, related_name='tickets', null=True, to='issues.Queue')),
|
||||
],
|
||||
options={
|
||||
'ordering': ['-updated_at'],
|
||||
|
@ -61,14 +60,14 @@ class Migration(migrations.Migration):
|
|||
name='TicketTracker',
|
||||
fields=[
|
||||
('id', models.AutoField(primary_key=True, serialize=False, auto_created=True, verbose_name='ID')),
|
||||
('ticket', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='trackers', to='issues.Ticket', verbose_name='ticket')),
|
||||
('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='ticket_trackers', to=settings.AUTH_USER_MODEL, verbose_name='user')),
|
||||
('ticket', models.ForeignKey(related_name='trackers', to='issues.Ticket', verbose_name='ticket')),
|
||||
('user', models.ForeignKey(related_name='ticket_trackers', to=settings.AUTH_USER_MODEL, verbose_name='user')),
|
||||
],
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='message',
|
||||
name='ticket',
|
||||
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='messages', to='issues.Ticket', verbose_name='ticket'),
|
||||
field=models.ForeignKey(related_name='messages', to='issues.Ticket', verbose_name='ticket'),
|
||||
),
|
||||
migrations.AlterUniqueTogether(
|
||||
name='tickettracker',
|
||||
|
|
|
@ -1,114 +0,0 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
# Generated by Django 1.10.5 on 2021-04-22 11:27
|
||||
from __future__ import unicode_literals
|
||||
|
||||
import datetime
|
||||
from django.conf import settings
|
||||
from django.db import migrations, models
|
||||
import django.db.models.deletion
|
||||
from django.utils.timezone import utc
|
||||
import orchestra.models.fields
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
replaces = [('issues', '0001_initial'), ('issues', '0002_auto_20150709_1018'), ('issues', '0003_auto_20160320_1127'), ('issues', '0004_auto_20170528_2011')]
|
||||
|
||||
initial = True
|
||||
|
||||
dependencies = [
|
||||
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name='Message',
|
||||
fields=[
|
||||
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('author_name', models.CharField(blank=True, max_length=256, verbose_name='author name')),
|
||||
('content', models.TextField(verbose_name='content')),
|
||||
('created_on', models.DateTimeField(auto_now_add=True, verbose_name='created on')),
|
||||
('author', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='ticket_messages', to=settings.AUTH_USER_MODEL, verbose_name='author')),
|
||||
],
|
||||
options={
|
||||
'get_latest_by': 'id',
|
||||
},
|
||||
),
|
||||
migrations.CreateModel(
|
||||
name='Queue',
|
||||
fields=[
|
||||
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('name', models.CharField(max_length=128, unique=True, verbose_name='name')),
|
||||
('verbose_name', models.CharField(blank=True, max_length=128, verbose_name='verbose_name')),
|
||||
('default', models.BooleanField(default=False, verbose_name='default')),
|
||||
('notify', orchestra.models.fields.MultiSelectField(blank=True, choices=[('SUPPORT', 'Support tickets'), ('ADMIN', 'Administrative'), ('BILLING', 'Billing'), ('TECH', 'Technical'), ('ADDS', 'Announcements'), ('EMERGENCY', 'Emergency contact')], default=('SUPPORT', 'ADMIN', 'BILLING', 'TECH', 'ADDS', 'EMERGENCY'), help_text='Contacts to notify by email', max_length=256, verbose_name='notify')),
|
||||
],
|
||||
),
|
||||
migrations.CreateModel(
|
||||
name='Ticket',
|
||||
fields=[
|
||||
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('creator_name', models.CharField(blank=True, max_length=256, verbose_name='creator name')),
|
||||
('subject', models.CharField(max_length=256, verbose_name='subject')),
|
||||
('description', models.TextField(verbose_name='description')),
|
||||
('priority', models.CharField(choices=[('HIGH', 'High'), ('MEDIUM', 'Medium'), ('LOW', 'Low')], default='MEDIUM', max_length=32, verbose_name='priority')),
|
||||
('state', models.CharField(choices=[('NEW', 'New'), ('IN_PROGRESS', 'In Progress'), ('RESOLVED', 'Resolved'), ('FEEDBACK', 'Feedback'), ('REJECTED', 'Rejected'), ('CLOSED', 'Closed')], default='NEW', max_length=32, verbose_name='state')),
|
||||
('created_at', models.DateTimeField(auto_now_add=True, verbose_name='created')),
|
||||
('updated_at', models.DateTimeField(auto_now=True, verbose_name='modified')),
|
||||
('cc', models.TextField(blank=True, help_text='emails to send a carbon copy to', verbose_name='CC')),
|
||||
('creator', models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, related_name='tickets_created', to=settings.AUTH_USER_MODEL, verbose_name='created by')),
|
||||
('owner', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='tickets_owned', to=settings.AUTH_USER_MODEL, verbose_name='assigned to')),
|
||||
('queue', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='tickets', to='issues.Queue')),
|
||||
],
|
||||
options={
|
||||
'ordering': ['-updated_at'],
|
||||
},
|
||||
),
|
||||
migrations.CreateModel(
|
||||
name='TicketTracker',
|
||||
fields=[
|
||||
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('ticket', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='trackers', to='issues.Ticket', verbose_name='ticket')),
|
||||
('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='ticket_trackers', to=settings.AUTH_USER_MODEL, verbose_name='user')),
|
||||
],
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='message',
|
||||
name='ticket',
|
||||
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='messages', to='issues.Ticket', verbose_name='ticket'),
|
||||
),
|
||||
migrations.AlterUniqueTogether(
|
||||
name='tickettracker',
|
||||
unique_together=set([('ticket', 'user')]),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='ticket',
|
||||
name='created_at',
|
||||
field=models.DateTimeField(auto_now_add=True, db_index=True, verbose_name='created'),
|
||||
),
|
||||
migrations.RemoveField(
|
||||
model_name='message',
|
||||
name='created_on',
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='message',
|
||||
name='created_at',
|
||||
field=models.DateTimeField(auto_now_add=True, default=datetime.datetime(2016, 3, 20, 10, 27, 45, 766388, tzinfo=utc), verbose_name='created at'),
|
||||
preserve_default=False,
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='ticket',
|
||||
name='creator',
|
||||
field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='tickets_created', to=settings.AUTH_USER_MODEL, verbose_name='created by'),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='ticket',
|
||||
name='owner',
|
||||
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='tickets_owned', to=settings.AUTH_USER_MODEL, verbose_name='assigned to'),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='ticket',
|
||||
name='queue',
|
||||
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='tickets', to='issues.Queue'),
|
||||
),
|
||||
]
|
|
@ -19,10 +19,10 @@ class Queue(models.Model):
|
|||
choices=Contact.EMAIL_USAGES,
|
||||
default=contacts_settings.CONTACTS_DEFAULT_EMAIL_USAGES,
|
||||
help_text=_("Contacts to notify by email"))
|
||||
|
||||
|
||||
def __str__(self):
|
||||
return self.verbose_name or self.name
|
||||
|
||||
|
||||
def save(self, *args, **kwargs):
|
||||
""" mark as default queue if needed """
|
||||
existing_default = Queue.objects.filter(default=True)
|
||||
|
@ -48,7 +48,7 @@ class Ticket(models.Model):
|
|||
(MEDIUM, 'Medium'),
|
||||
(LOW, 'Low'),
|
||||
)
|
||||
|
||||
|
||||
NEW = 'NEW'
|
||||
IN_PROGRESS = 'IN_PROGRESS'
|
||||
RESOLVED = 'RESOLVED'
|
||||
|
@ -63,7 +63,7 @@ class Ticket(models.Model):
|
|||
(REJECTED, 'Rejected'),
|
||||
(CLOSED, 'Closed'),
|
||||
)
|
||||
|
||||
|
||||
creator = models.ForeignKey(djsettings.AUTH_USER_MODEL, verbose_name=_("created by"),
|
||||
related_name='tickets_created', null=True, on_delete=models.SET_NULL)
|
||||
creator_name = models.CharField(_("creator name"), max_length=256, blank=True)
|
||||
|
@ -79,15 +79,15 @@ class Ticket(models.Model):
|
|||
created_at = models.DateTimeField(_("created"), auto_now_add=True, db_index=True)
|
||||
updated_at = models.DateTimeField(_("modified"), auto_now=True)
|
||||
cc = models.TextField("CC", help_text=_("emails to send a carbon copy to"), blank=True)
|
||||
|
||||
|
||||
objects = TicketQuerySet.as_manager()
|
||||
|
||||
|
||||
class Meta:
|
||||
ordering = ['-updated_at']
|
||||
|
||||
|
||||
def __str__(self):
|
||||
return str(self.pk)
|
||||
|
||||
|
||||
def get_notification_emails(self):
|
||||
""" Get emails of the users related to the ticket """
|
||||
emails = list(settings.ISSUES_SUPPORT_EMAILS)
|
||||
|
@ -100,7 +100,7 @@ class Ticket(models.Model):
|
|||
for message in self.messages.distinct('author'):
|
||||
emails.append(message.author.email)
|
||||
return set(emails + self.get_cc_emails())
|
||||
|
||||
|
||||
def notify(self, message=None, content=None):
|
||||
""" Send an email to ticket stakeholders notifying an state update """
|
||||
emails = self.get_notification_emails()
|
||||
|
@ -111,7 +111,7 @@ class Ticket(models.Model):
|
|||
'ticket_message': message
|
||||
}
|
||||
send_email_template(template, context, emails, html=html_template)
|
||||
|
||||
|
||||
def save(self, *args, **kwargs):
|
||||
""" notify stakeholders of new ticket """
|
||||
new_issue = not self.pk
|
||||
|
@ -121,60 +121,60 @@ class Ticket(models.Model):
|
|||
if new_issue:
|
||||
# PK should be available for rendering the template
|
||||
self.notify()
|
||||
|
||||
|
||||
def is_involved_by(self, user):
|
||||
""" returns whether user has participated or is referenced on the ticket
|
||||
as owner or member of the group
|
||||
"""
|
||||
return Ticket.objects.filter(pk=self.pk).involved_by(user).exists()
|
||||
|
||||
|
||||
def get_cc_emails(self):
|
||||
return self.cc.split(',') if self.cc else []
|
||||
|
||||
|
||||
def mark_as_read_by(self, user):
|
||||
self.trackers.get_or_create(user=user)
|
||||
|
||||
|
||||
def mark_as_unread_by(self, user):
|
||||
self.trackers.filter(user=user).delete()
|
||||
|
||||
|
||||
def mark_as_unread(self):
|
||||
self.trackers.all().delete()
|
||||
|
||||
|
||||
def is_read_by(self, user):
|
||||
return self.trackers.filter(user=user).exists()
|
||||
|
||||
|
||||
def reject(self):
|
||||
self.state = Ticket.REJECTED
|
||||
self.save(update_fields=('state', 'updated_at'))
|
||||
|
||||
|
||||
def resolve(self):
|
||||
self.state = Ticket.RESOLVED
|
||||
self.save(update_fields=('state', 'updated_at'))
|
||||
|
||||
|
||||
def close(self):
|
||||
self.state = Ticket.CLOSED
|
||||
self.save(update_fields=('state', 'updated_at'))
|
||||
|
||||
|
||||
def take(self, user):
|
||||
self.owner = user
|
||||
self.save(update_fields=('state', 'updated_at'))
|
||||
|
||||
|
||||
class Message(models.Model):
|
||||
ticket = models.ForeignKey('issues.Ticket', on_delete=models.CASCADE,
|
||||
verbose_name=_("ticket"), related_name='messages')
|
||||
author = models.ForeignKey(djsettings.AUTH_USER_MODEL, on_delete=models.CASCADE,
|
||||
verbose_name=_("author"), related_name='ticket_messages')
|
||||
ticket = models.ForeignKey('issues.Ticket', verbose_name=_("ticket"),
|
||||
related_name='messages')
|
||||
author = models.ForeignKey(djsettings.AUTH_USER_MODEL, verbose_name=_("author"),
|
||||
related_name='ticket_messages')
|
||||
author_name = models.CharField(_("author name"), max_length=256, blank=True)
|
||||
content = models.TextField(_("content"))
|
||||
created_at = models.DateTimeField(_("created at"), auto_now_add=True)
|
||||
|
||||
|
||||
class Meta:
|
||||
get_latest_by = 'id'
|
||||
|
||||
|
||||
def __str__(self):
|
||||
return "#%i" % self.id
|
||||
|
||||
|
||||
def save(self, *args, **kwargs):
|
||||
""" notify stakeholders of ticket update """
|
||||
if not self.pk:
|
||||
|
@ -183,7 +183,7 @@ class Message(models.Model):
|
|||
self.ticket.notify(message=self)
|
||||
self.author_name = self.author.get_full_name()
|
||||
super(Message, self).save(*args, **kwargs)
|
||||
|
||||
|
||||
@property
|
||||
def number(self):
|
||||
return self.ticket.messages.filter(id__lte=self.id).count()
|
||||
|
@ -191,11 +191,10 @@ class Message(models.Model):
|
|||
|
||||
class TicketTracker(models.Model):
|
||||
""" Keeps track of user read tickets """
|
||||
ticket = models.ForeignKey(Ticket, on_delete=models.CASCADE,
|
||||
verbose_name=_("ticket"), related_name='trackers')
|
||||
user = models.ForeignKey(djsettings.AUTH_USER_MODEL, on_delete=models.CASCADE,
|
||||
verbose_name=_("user"), related_name='ticket_trackers')
|
||||
|
||||
ticket = models.ForeignKey(Ticket, verbose_name=_("ticket"), related_name='trackers')
|
||||
user = models.ForeignKey(djsettings.AUTH_USER_MODEL, verbose_name=_("user"),
|
||||
related_name='ticket_trackers')
|
||||
|
||||
class Meta:
|
||||
unique_together = (
|
||||
('ticket', 'user'),
|
||||
|
|
|
@ -3,7 +3,6 @@ from __future__ import unicode_literals
|
|||
|
||||
from django.db import models, migrations
|
||||
from django.conf import settings
|
||||
import django.db.models.deletion
|
||||
import orchestra.core.validators
|
||||
|
||||
|
||||
|
@ -23,8 +22,8 @@ class Migration(migrations.Migration):
|
|||
('address_name', models.CharField(max_length=128, validators=[orchestra.core.validators.validate_name], verbose_name='address name', blank=True)),
|
||||
('admin_email', models.EmailField(max_length=254, verbose_name='admin email', help_text='Administration email address')),
|
||||
('is_active', models.BooleanField(default=True, verbose_name='active', help_text='Designates whether this account should be treated as active. Unselect this instead of deleting accounts.')),
|
||||
('account', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='lists', to=settings.AUTH_USER_MODEL, verbose_name='Account')),
|
||||
('address_domain', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, null=True, blank=True, to='domains.Domain', verbose_name='address domain')),
|
||||
('account', models.ForeignKey(related_name='lists', to=settings.AUTH_USER_MODEL, verbose_name='Account')),
|
||||
('address_domain', models.ForeignKey(null=True, blank=True, to='domains.Domain', verbose_name='address domain')),
|
||||
],
|
||||
),
|
||||
migrations.AlterUniqueTogether(
|
||||
|
|
|
@ -1,69 +0,0 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
# Generated by Django 1.10.5 on 2021-04-22 11:27
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.conf import settings
|
||||
from django.db import migrations, models
|
||||
import django.db.models.deletion
|
||||
import orchestra.core.validators
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
replaces = [('lists', '0001_initial'), ('lists', '0002_auto_20160912_1221'), ('lists', '0003_auto_20160912_1241'), ('lists', '0004_auto_20210330_1049')]
|
||||
|
||||
initial = True
|
||||
|
||||
dependencies = [
|
||||
('domains', '0001_initial'),
|
||||
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name='List',
|
||||
fields=[
|
||||
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('name', models.CharField(help_text='Default list address <name>@lists.orchestra.lan', max_length=128, unique=True, validators=[orchestra.core.validators.validate_name], verbose_name='name')),
|
||||
('address_name', models.CharField(blank=True, max_length=128, validators=[orchestra.core.validators.validate_name], verbose_name='address name')),
|
||||
('admin_email', models.EmailField(help_text='Administration email address', max_length=254, verbose_name='admin email')),
|
||||
('is_active', models.BooleanField(default=True, help_text='Designates whether this account should be treated as active. Unselect this instead of deleting accounts.', verbose_name='active')),
|
||||
('account', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='lists', to=settings.AUTH_USER_MODEL, verbose_name='Account')),
|
||||
('address_domain', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, to='domains.Domain', verbose_name='address domain')),
|
||||
],
|
||||
),
|
||||
migrations.AlterUniqueTogether(
|
||||
name='list',
|
||||
unique_together=set([('address_name', 'address_domain')]),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='list',
|
||||
name='address_domain',
|
||||
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, to='domains.Domain', verbose_name='address domain'),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='list',
|
||||
name='address_name',
|
||||
field=models.CharField(blank=True, max_length=52, validators=[orchestra.core.validators.validate_name], verbose_name='address name'),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='list',
|
||||
name='name',
|
||||
field=models.CharField(help_text='Default list address <name>@grups.pangea.org', max_length=52, unique=True, validators=[orchestra.core.validators.validate_name], verbose_name='name'),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='list',
|
||||
name='address_name',
|
||||
field=models.CharField(blank=True, max_length=64, validators=[orchestra.core.validators.validate_name], verbose_name='address name'),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='list',
|
||||
name='name',
|
||||
field=models.CharField(help_text='Default list address <name>@grups.pangea.org', max_length=64, unique=True, validators=[orchestra.core.validators.validate_name], verbose_name='name'),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='list',
|
||||
name='name',
|
||||
field=models.CharField(help_text='Default list address <name>@lists.orchestra.lan', max_length=64, unique=True, validators=[orchestra.core.validators.validate_name], verbose_name='name'),
|
||||
),
|
||||
]
|
|
@ -1,21 +0,0 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
# Generated by Django 1.10.5 on 2021-03-30 10:49
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.db import migrations, models
|
||||
import orchestra.core.validators
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('lists', '0003_auto_20160912_1241'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AlterField(
|
||||
model_name='list',
|
||||
name='name',
|
||||
field=models.CharField(help_text='Default list address <name>@lists.orchestra.lan', max_length=64, unique=True, validators=[orchestra.core.validators.validate_name], verbose_name='name'),
|
||||
),
|
||||
]
|
|
@ -30,54 +30,54 @@ class List(models.Model):
|
|||
admin_email = models.EmailField(_("admin email"),
|
||||
help_text=_("Administration email address"))
|
||||
account = models.ForeignKey('accounts.Account', verbose_name=_("Account"),
|
||||
related_name='lists', on_delete=models.CASCADE)
|
||||
related_name='lists')
|
||||
# TODO also admin
|
||||
is_active = models.BooleanField(_("active"), default=True,
|
||||
help_text=_("Designates whether this account should be treated as active. "
|
||||
"Unselect this instead of deleting accounts."))
|
||||
password = None
|
||||
|
||||
|
||||
objects = ListQuerySet.as_manager()
|
||||
|
||||
|
||||
class Meta:
|
||||
unique_together = ('address_name', 'address_domain')
|
||||
|
||||
|
||||
def __str__(self):
|
||||
return self.name
|
||||
|
||||
|
||||
@property
|
||||
def address(self):
|
||||
if self.address_name and self.address_domain:
|
||||
return "%s@%s" % (self.address_name, self.address_domain)
|
||||
return ''
|
||||
|
||||
|
||||
@cached_property
|
||||
def active(self):
|
||||
return self.is_active and self.account.is_active
|
||||
|
||||
|
||||
def clean(self):
|
||||
if self.address_name and not self.address_domain_id:
|
||||
raise ValidationError({
|
||||
'address_domain': _("Domain should be selected for provided address name."),
|
||||
})
|
||||
|
||||
|
||||
def disable(self):
|
||||
self.is_active = False
|
||||
self.save(update_fields=('is_active',))
|
||||
|
||||
|
||||
def enable(self):
|
||||
self.is_active = False
|
||||
self.save(update_fields=('is_active',))
|
||||
|
||||
|
||||
def get_address_name(self):
|
||||
return self.address_name or self.name
|
||||
|
||||
|
||||
def get_username(self):
|
||||
return self.name
|
||||
|
||||
|
||||
def set_password(self, password):
|
||||
self.password = password
|
||||
|
||||
|
||||
def get_absolute_url(self):
|
||||
context = {
|
||||
'name': self.name
|
||||
|
|
|
@ -12,7 +12,7 @@ from .models import List
|
|||
|
||||
class RelatedDomainSerializer(AccountSerializerMixin, RelatedHyperlinkedModelSerializer):
|
||||
class Meta:
|
||||
model = List.address_domain.field.related_model
|
||||
model = List.address_domain.field.rel.to
|
||||
fields = ('url', 'id', 'name')
|
||||
|
||||
|
||||
|
@ -26,14 +26,14 @@ class ListSerializer(AccountSerializerMixin, SetPasswordHyperlinkedSerializer):
|
|||
'This value may contain any ascii character except for '
|
||||
' \'/"/\\/ characters.'), 'invalid'),
|
||||
])
|
||||
|
||||
|
||||
address_domain = RelatedDomainSerializer(required=False)
|
||||
|
||||
|
||||
class Meta:
|
||||
model = List
|
||||
fields = ('url', 'id', 'name', 'password', 'address_name', 'address_domain', 'admin_email', 'is_active',)
|
||||
postonly_fields = ('name', 'password')
|
||||
|
||||
|
||||
def validate_address_domain(self, address_name):
|
||||
if self.instance:
|
||||
address_domain = address_domain or self.instance.address_domain
|
||||
|
|
|
@ -1,26 +1,25 @@
|
|||
import os
|
||||
import smtplib
|
||||
import time
|
||||
import unittest
|
||||
import requests
|
||||
from unittest import skip
|
||||
from email.mime.text import MIMEText
|
||||
|
||||
import requests
|
||||
from django.conf import settings as djsettings
|
||||
from django.core.management.base import CommandError
|
||||
from django.urls import reverse
|
||||
from django.core.urlresolvers import reverse
|
||||
from selenium.webdriver.support.select import Select
|
||||
|
||||
from orchestra.admin.utils import change_url
|
||||
from orchestra.contrib.domains.models import Domain
|
||||
from orchestra.contrib.orchestration.models import Route, Server
|
||||
from orchestra.contrib.orchestration.models import Server, Route
|
||||
from orchestra.utils.sys import sshrun
|
||||
from orchestra.utils.tests import (BaseLiveServerTestCase, random_ascii,
|
||||
save_response_on_error, snapshot_on_error)
|
||||
from selenium.webdriver.support.select import Select
|
||||
from orchestra.utils.tests import (BaseLiveServerTestCase, random_ascii, snapshot_on_error,
|
||||
save_response_on_error)
|
||||
|
||||
from ... import backends, settings
|
||||
from ...models import List
|
||||
|
||||
TEST_REST_API = int(os.getenv('TEST_REST_API', '0'))
|
||||
|
||||
|
||||
class ListMixin(object):
|
||||
MASTER_SERVER = os.environ.get('ORCHESTRA_SLAVE_SERVER', 'localhost')
|
||||
|
@ -29,12 +28,15 @@ class ListMixin(object):
|
|||
'orchestra.contrib.domains',
|
||||
'orchestra.contrib.lists',
|
||||
)
|
||||
|
||||
|
||||
def setUp(self):
|
||||
super(ListMixin, self).setUp()
|
||||
self.add_route()
|
||||
djsettings.DEBUG = True
|
||||
|
||||
djsettings.CELERY_ALWAYS_EAGER = True
|
||||
djsettings.CELERY_TASK_ALWAYS_EAGER = True
|
||||
# import pdb; pdb.set_trace()
|
||||
self.add_route()
|
||||
|
||||
def validate_add(self, name, address=None):
|
||||
sshrun(self.MASTER_SERVER, 'list_members %s' % name, display=False)
|
||||
if not address:
|
||||
|
@ -46,11 +48,11 @@ class ListMixin(object):
|
|||
sshrun(self.MASTER_SERVER,
|
||||
'grep -v ":\|^\s\|^$\|-\|\.\|\s" /var/spool/mail/nobody | base64 -d | grep "%s"'
|
||||
% request_address, display=False)
|
||||
|
||||
|
||||
def validate_login(self, name, password):
|
||||
url = 'http://%s/cgi-bin/mailman/admin/%s' % (settings.LISTS_DEFAULT_DOMAIN, name)
|
||||
self.assertEqual(200, requests.post(url, data={'adminpw': password}).status_code)
|
||||
|
||||
|
||||
def validate_delete(self, name):
|
||||
context = {
|
||||
'name': name,
|
||||
|
@ -64,7 +66,7 @@ class ListMixin(object):
|
|||
'grep "^\s*$(domain)s\s*$" %(virtual_domain)s' % context, display=False)
|
||||
self.assertRaises(CommandError, sshrun, self.MASTER_SERVER,
|
||||
'list_lists | grep -i "^\s*%(name)s\s"' % context, display=False)
|
||||
|
||||
|
||||
def subscribe(self, subscribe_address):
|
||||
msg = MIMEText('')
|
||||
msg['To'] = subscribe_address
|
||||
|
@ -78,12 +80,13 @@ class ListMixin(object):
|
|||
server.sendmail(msg['From'], msg['To'], msg.as_string())
|
||||
finally:
|
||||
server.quit()
|
||||
|
||||
|
||||
def add_route(self):
|
||||
server = Server.objects.create(name=self.MASTER_SERVER)
|
||||
backend = backends.MailmanController.get_name()
|
||||
Route.objects.create(backend=backend, match=True, host=server)
|
||||
|
||||
|
||||
# @skip("Skip because not exists get_auth_token in orm.api.Api")
|
||||
def test_add(self):
|
||||
name = '%s_list' % random_ascii(10)
|
||||
password = '@!?%spppP001' % random_ascii(5)
|
||||
|
@ -92,7 +95,8 @@ class ListMixin(object):
|
|||
self.validate_add(name)
|
||||
self.validate_login(name, password)
|
||||
self.addCleanup(self.delete, name)
|
||||
|
||||
|
||||
@skip("Skip because not exists get_auth_token in orm.api.Api")
|
||||
def test_add_with_address(self):
|
||||
name = '%s_list' % random_ascii(10)
|
||||
password = '@!?%spppP001' % random_ascii(5)
|
||||
|
@ -104,7 +108,8 @@ class ListMixin(object):
|
|||
self.addCleanup(self.delete, name)
|
||||
# Mailman doesn't support changing the address, only the domain
|
||||
self.validate_add(name, address="%s@%s" % (address_name, address_domain))
|
||||
|
||||
|
||||
@skip("Skip because not exists get_auth_token in orm.api.Api")
|
||||
def test_change_password(self):
|
||||
name = '%s_list' % random_ascii(10)
|
||||
password = '@!?%spppP001' % random_ascii(5)
|
||||
|
@ -115,7 +120,8 @@ class ListMixin(object):
|
|||
new_password = '@!?%spppP001' % random_ascii(5)
|
||||
self.change_password(name, new_password)
|
||||
self.validate_login(name, new_password)
|
||||
|
||||
|
||||
@skip("Skip because not exists get_auth_token in orm.api.Api")
|
||||
def test_change_domain(self):
|
||||
name = '%s_list' % random_ascii(10)
|
||||
password = '@!?%spppP001' % random_ascii(5)
|
||||
|
@ -130,7 +136,8 @@ class ListMixin(object):
|
|||
address_domain = Domain.objects.create(name=domain_name, account=self.account)
|
||||
self.update_domain(name, domain_name)
|
||||
self.validate_add(name, address="%s@%s" % (address_name, address_domain))
|
||||
|
||||
|
||||
@skip("Skip because not exists get_auth_token in orm.api.Api")
|
||||
def test_change_address_name(self):
|
||||
name = '%s_list' % random_ascii(10)
|
||||
password = '@!?%spppP001' % random_ascii(5)
|
||||
|
@ -144,7 +151,8 @@ class ListMixin(object):
|
|||
address_name = '%s_name' % random_ascii(10)
|
||||
self.update_address_name(name, address_name)
|
||||
self.validate_add(name, address="%s@%s" % (address_name, address_domain))
|
||||
|
||||
|
||||
@skip("Skip because not exists get_auth_token in orm.api.Api")
|
||||
def test_delete(self):
|
||||
name = '%s_list' % random_ascii(10)
|
||||
password = '@!?%spppP001' % random_ascii(5)
|
||||
|
@ -152,7 +160,8 @@ class ListMixin(object):
|
|||
address_name = '%s_name' % random_ascii(10)
|
||||
domain_name = '%sdomain.lan' % random_ascii(10)
|
||||
address_domain = Domain.objects.create(name=domain_name, account=self.account)
|
||||
self.add(name, password, admin_email, address_name=address_name, address_domain=address_domain)
|
||||
self.add(name, password, admin_email, address_name=address_name,
|
||||
address_domain=address_domain)
|
||||
# Mailman doesn't support changing the address, only the domain
|
||||
self.validate_add(name, address="%s@%s" % (address_name, address_domain))
|
||||
self.delete(name)
|
||||
|
@ -160,12 +169,11 @@ class ListMixin(object):
|
|||
self.validate_delete(name)
|
||||
|
||||
|
||||
@unittest.skipUnless(TEST_REST_API, "REST API tests")
|
||||
class RESTListMixin(ListMixin):
|
||||
def setUp(self):
|
||||
super(RESTListMixin, self).setUp()
|
||||
self.rest_login()
|
||||
|
||||
|
||||
@save_response_on_error
|
||||
def add(self, name, password, admin_email, address_name=None, address_domain=None):
|
||||
extra = {}
|
||||
|
@ -175,22 +183,22 @@ class RESTListMixin(ListMixin):
|
|||
'address_domain': self.rest.domains.retrieve(name=address_domain.name).get(),
|
||||
})
|
||||
self.rest.lists.create(name=name, password=password, admin_email=admin_email, **extra)
|
||||
|
||||
|
||||
@save_response_on_error
|
||||
def delete(self, name):
|
||||
self.rest.lists.retrieve(name=name).delete()
|
||||
|
||||
|
||||
@save_response_on_error
|
||||
def change_password(self, name, password):
|
||||
mail_list = self.rest.lists.retrieve(name=name).get()
|
||||
mail_list.set_password(password)
|
||||
|
||||
|
||||
@save_response_on_error
|
||||
def update_domain(self, name, domain_name):
|
||||
mail_list = self.rest.lists.retrieve(name=name).get()
|
||||
domain = self.rest.domains.retrieve(name=domain_name).get()
|
||||
mail_list.update(address_domain=domain)
|
||||
|
||||
|
||||
@save_response_on_error
|
||||
def update_address_name(self, name, address_name):
|
||||
mail_list = self.rest.lists.retrieve(name=name).get()
|
||||
|
@ -228,6 +236,8 @@ class AdminListMixin(ListMixin):
|
|||
domain_select.select_by_value(str(domain.pk))
|
||||
|
||||
name_field.submit()
|
||||
# import pdb; pdb.set_trace()
|
||||
# oop = Server.objects.all()
|
||||
self.assertNotEqual(url, self.selenium.current_url)
|
||||
|
||||
@snapshot_on_error
|
||||
|
@ -245,26 +255,26 @@ class AdminListMixin(ListMixin):
|
|||
mail_list = List.objects.get(name=name)
|
||||
url = self.live_server_url + change_url(mail_list)
|
||||
self.selenium.get(url)
|
||||
|
||||
|
||||
domain = Domain.objects.get(name=domain_name)
|
||||
domain_input = self.selenium.find_element_by_id('id_address_domain')
|
||||
domain_select = Select(domain_input)
|
||||
domain_select.select_by_value(str(domain.pk))
|
||||
|
||||
|
||||
save = self.selenium.find_element_by_name('_save')
|
||||
save.submit()
|
||||
self.assertNotEqual(url, self.selenium.current_url)
|
||||
|
||||
|
||||
@snapshot_on_error
|
||||
def update_address_name(self, name, address_name):
|
||||
mail_list = List.objects.get(name=name)
|
||||
url = self.live_server_url + change_url(mail_list)
|
||||
self.selenium.get(url)
|
||||
|
||||
|
||||
address_name_field = self.selenium.find_element_by_id('id_address_name')
|
||||
address_name_field.clear()
|
||||
address_name_field.send_keys(address_name)
|
||||
|
||||
|
||||
save = self.selenium.find_element_by_name('_save')
|
||||
save.submit()
|
||||
self.assertNotEqual(url, self.selenium.current_url)
|
||||
|
|
|
@ -3,10 +3,9 @@ from urllib.parse import parse_qs
|
|||
|
||||
from django import forms
|
||||
from django.contrib import admin, messages
|
||||
from django.urls import reverse
|
||||
from django.core.urlresolvers import reverse
|
||||
from django.db.models import F, Count, Value as V
|
||||
from django.db.models.functions import Concat
|
||||
from django.utils.html import format_html, format_html_join
|
||||
from django.utils.safestring import mark_safe
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
|
||||
|
@ -29,7 +28,7 @@ from .widgets import OpenCustomFilteringOnSelect
|
|||
class AutoresponseInline(admin.StackedInline):
|
||||
model = Autoresponse
|
||||
verbose_name_plural = _("autoresponse")
|
||||
|
||||
|
||||
def formfield_for_dbfield(self, db_field, **kwargs):
|
||||
if db_field.name == 'subject':
|
||||
kwargs['widget'] = forms.TextInput(attrs={'size':'118'})
|
||||
|
@ -77,13 +76,12 @@ class MailboxAdmin(ChangePasswordAdminMixin, SelectAccountAdminMixin, ExtendedMo
|
|||
form = MailboxChangeForm
|
||||
list_prefetch_related = ('addresses__domain',)
|
||||
actions = (disable, enable, list_accounts)
|
||||
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super(MailboxAdmin, self).__init__(*args, **kwargs)
|
||||
if settings.MAILBOXES_LOCAL_DOMAIN:
|
||||
type(self).actions = self.actions + (SendMailboxEmail(),)
|
||||
|
||||
@mark_safe
|
||||
|
||||
def display_addresses(self, mailbox):
|
||||
# Get from forwards
|
||||
cache = caches.get_request_cache()
|
||||
|
@ -95,7 +93,7 @@ class MailboxAdmin(ChangePasswordAdminMixin, SelectAccountAdminMixin, ExtendedMo
|
|||
qs = qs.values_list('id', 'email', 'forward')
|
||||
for addr_id, email, mbox in qs:
|
||||
url = reverse('admin:mailboxes_address_change', args=(addr_id,))
|
||||
link = format_html('<a href="{}">{}</a>', url, email)
|
||||
link = '<a href="%s">%s</a>' % (url, email)
|
||||
try:
|
||||
cached_forwards[mbox].append(link)
|
||||
except KeyError:
|
||||
|
@ -109,29 +107,32 @@ class MailboxAdmin(ChangePasswordAdminMixin, SelectAccountAdminMixin, ExtendedMo
|
|||
addresses = []
|
||||
for addr in mailbox.addresses.all():
|
||||
url = change_url(addr)
|
||||
addresses.append(format_html('<a href="{}">{}</a>', url, addr.email))
|
||||
addresses.append('<a href="%s">%s</a>' % (url, addr.email))
|
||||
return '<br>'.join(addresses+forwards)
|
||||
display_addresses.short_description = _("Addresses")
|
||||
|
||||
display_addresses.allow_tags = True
|
||||
|
||||
def display_forwards(self, mailbox):
|
||||
forwards = mailbox.get_forwards()
|
||||
return format_html_join(
|
||||
'<br>', '<a href="{}">{}</a>',
|
||||
[(change_url(addr), addr.email) for addr in forwards]
|
||||
)
|
||||
forwards = []
|
||||
for addr in mailbox.get_forwards():
|
||||
url = change_url(addr)
|
||||
forwards.append('<a href="%s">%s</a>' % (url, addr.email))
|
||||
return '<br>'.join(forwards)
|
||||
display_forwards.short_description = _("Forward from")
|
||||
|
||||
@mark_safe
|
||||
display_forwards.allow_tags = True
|
||||
|
||||
def display_filtering(self, mailbox):
|
||||
""" becacuse of allow_tags = True """
|
||||
return mailbox.get_filtering_display()
|
||||
display_filtering.short_description = _("Filtering")
|
||||
display_filtering.admin_order_field = 'filtering'
|
||||
|
||||
display_filtering.allow_tags = True
|
||||
|
||||
def formfield_for_dbfield(self, db_field, **kwargs):
|
||||
if db_field.name == 'filtering':
|
||||
kwargs['widget'] = OpenCustomFilteringOnSelect()
|
||||
return super(MailboxAdmin, self).formfield_for_dbfield(db_field, **kwargs)
|
||||
|
||||
|
||||
def get_fieldsets(self, request, obj=None):
|
||||
fieldsets = super(MailboxAdmin, self).get_fieldsets(request, obj)
|
||||
if obj and obj.filtering == obj.CUSTOM:
|
||||
|
@ -143,31 +144,31 @@ class MailboxAdmin(ChangePasswordAdminMixin, SelectAccountAdminMixin, ExtendedMo
|
|||
fieldsets = list(copy.deepcopy(fieldsets))
|
||||
fieldsets.pop(-1)
|
||||
return fieldsets
|
||||
|
||||
|
||||
def get_form(self, *args, **kwargs):
|
||||
form = super(MailboxAdmin, self).get_form(*args, **kwargs)
|
||||
form.modeladmin = self
|
||||
return form
|
||||
|
||||
|
||||
def get_search_results(self, request, queryset, search_term):
|
||||
# Remove local domain from the search term if present (implicit local addreç)
|
||||
search_term = search_term.replace('@'+settings.MAILBOXES_LOCAL_DOMAIN, '')
|
||||
# Split address name from domain in order to support address searching
|
||||
search_term = search_term.replace('@', ' ')
|
||||
return super(MailboxAdmin, self).get_search_results(request, queryset, search_term)
|
||||
|
||||
|
||||
def render_change_form(self, request, context, add=False, change=False, form_url='', obj=None):
|
||||
if not add:
|
||||
self.check_unrelated_address(request, obj)
|
||||
self.check_matching_address(request, obj)
|
||||
return super(MailboxAdmin, self).render_change_form(
|
||||
request, context, add, change, form_url, obj)
|
||||
|
||||
|
||||
def log_addition(self, request, object, *args, **kwargs):
|
||||
self.check_unrelated_address(request, object)
|
||||
self.check_matching_address(request, object)
|
||||
return super(MailboxAdmin, self).log_addition(request, object, *args, **kwargs)
|
||||
|
||||
|
||||
def check_matching_address(self, request, obj):
|
||||
local_domain = settings.MAILBOXES_LOCAL_DOMAIN
|
||||
if obj.name and local_domain:
|
||||
|
@ -182,7 +183,7 @@ class MailboxAdmin(ChangePasswordAdminMixin, SelectAccountAdminMixin, ExtendedMo
|
|||
"selecting it makes sense.") % (obj, addr)
|
||||
if msg not in (m.message for m in messages.get_messages(request)):
|
||||
self.message_user(request, msg, level=messages.WARNING)
|
||||
|
||||
|
||||
def check_unrelated_address(self, request, obj):
|
||||
# Check if there exists an unrelated local Address for this mbox
|
||||
local_domain = settings.MAILBOXES_LOCAL_DOMAIN
|
||||
|
@ -203,7 +204,7 @@ class MailboxAdmin(ChangePasswordAdminMixin, SelectAccountAdminMixin, ExtendedMo
|
|||
# Prevent duplication (add_view+continue)
|
||||
if msg not in (m.message for m in messages.get_messages(request)):
|
||||
self.message_user(request, msg, level=messages.WARNING)
|
||||
|
||||
|
||||
def save_model(self, request, obj, form, change):
|
||||
""" save hacky mailbox.addresses and local domain clashing """
|
||||
if obj.filtering != obj.CUSTOM:
|
||||
|
@ -216,7 +217,7 @@ class MailboxAdmin(ChangePasswordAdminMixin, SelectAccountAdminMixin, ExtendedMo
|
|||
elif obj.custom_filtering:
|
||||
messages.warning(request, msg)
|
||||
super(MailboxAdmin, self).save_model(request, obj, form, change)
|
||||
obj.addresses.set(form.cleaned_data['addresses'])
|
||||
obj.addresses = form.cleaned_data['addresses']
|
||||
|
||||
|
||||
class AddressAdmin(SelectAccountAdminMixin, ExtendedModelAdmin):
|
||||
|
@ -236,37 +237,39 @@ class AddressAdmin(SelectAccountAdminMixin, ExtendedModelAdmin):
|
|||
filter_horizontal = ['mailboxes']
|
||||
form = AddressForm
|
||||
list_prefetch_related = ('mailboxes', 'domain')
|
||||
|
||||
|
||||
domain_link = admin_link('domain', order='domain__name')
|
||||
|
||||
|
||||
def display_email(self, address):
|
||||
return address.computed_email
|
||||
display_email.short_description = _("Email")
|
||||
display_email.admin_order_field = 'computed_email'
|
||||
|
||||
|
||||
def email_link(self, address):
|
||||
link = self.domain_link(address)
|
||||
return format_html("{}@{}", address.name, link)
|
||||
return "%s@%s" % (address.name, link)
|
||||
email_link.short_description = _("Email")
|
||||
|
||||
email_link.allow_tags = True
|
||||
|
||||
def display_mailboxes(self, address):
|
||||
boxes = address.mailboxes.all()
|
||||
return format_html_join(
|
||||
mark_safe('<br>'), '<a href="{}">{}</a>',
|
||||
[(change_url(mailbox), mailbox.name) for mailbox in boxes]
|
||||
)
|
||||
boxes = []
|
||||
for mailbox in address.mailboxes.all():
|
||||
url = change_url(mailbox)
|
||||
boxes.append('<a href="%s">%s</a>' % (url, mailbox.name))
|
||||
return '<br>'.join(boxes)
|
||||
display_mailboxes.short_description = _("Mailboxes")
|
||||
display_mailboxes.allow_tags = True
|
||||
display_mailboxes.admin_order_field = 'mailboxes__count'
|
||||
|
||||
|
||||
def display_all_mailboxes(self, address):
|
||||
boxes = address.get_mailboxes()
|
||||
return format_html_join(
|
||||
mark_safe('<br>'), '<a href="{}">{}</a>',
|
||||
[(change_url(mailbox), mailbox.name) for mailbox in boxes]
|
||||
)
|
||||
boxes = []
|
||||
for mailbox in address.get_mailboxes():
|
||||
url = change_url(mailbox)
|
||||
boxes.append('<a href="%s">%s</a>' % (url, mailbox.name))
|
||||
return '<br>'.join(boxes)
|
||||
display_all_mailboxes.short_description = _("Mailboxes links")
|
||||
|
||||
@mark_safe
|
||||
display_all_mailboxes.allow_tags = True
|
||||
|
||||
def display_forward(self, address):
|
||||
forward_mailboxes = {m.name: m for m in address.get_forward_mailboxes()}
|
||||
values = []
|
||||
|
@ -278,13 +281,14 @@ class AddressAdmin(SelectAccountAdminMixin, ExtendedModelAdmin):
|
|||
values.append(forward)
|
||||
return '<br>'.join(values)
|
||||
display_forward.short_description = _("Forward")
|
||||
display_forward.allow_tags = True
|
||||
display_forward.admin_order_field = 'forward'
|
||||
|
||||
|
||||
def formfield_for_dbfield(self, db_field, **kwargs):
|
||||
if db_field.name == 'forward':
|
||||
kwargs['widget'] = forms.TextInput(attrs={'size':'118'})
|
||||
return super(AddressAdmin, self).formfield_for_dbfield(db_field, **kwargs)
|
||||
|
||||
|
||||
def get_fields(self, request, obj=None):
|
||||
""" Remove mailboxes field when creating address from a popup i.e. from mailbox add form """
|
||||
fields = super(AddressAdmin, self).get_fields(request, obj)
|
||||
|
@ -293,22 +297,22 @@ class AddressAdmin(SelectAccountAdminMixin, ExtendedModelAdmin):
|
|||
fields = list(fields)
|
||||
fields.remove('mailboxes')
|
||||
return fields
|
||||
|
||||
|
||||
def get_queryset(self, request):
|
||||
qs = super(AddressAdmin, self).get_queryset(request)
|
||||
qs = qs.annotate(computed_email=Concat(F('name'), V('@'), F('domain__name')))
|
||||
return qs.annotate(Count('mailboxes'))
|
||||
|
||||
|
||||
def render_change_form(self, request, context, add=False, change=False, form_url='', obj=None):
|
||||
if not add:
|
||||
self.check_matching_mailbox(request, obj)
|
||||
return super(AddressAdmin, self).render_change_form(
|
||||
request, context, add, change, form_url, obj)
|
||||
|
||||
|
||||
def log_addition(self, request, object, *args, **kwargs):
|
||||
self.check_matching_mailbox(request, object)
|
||||
return super(AddressAdmin, self).log_addition(request, object, *args, **kwargs)
|
||||
|
||||
|
||||
def check_matching_mailbox(self, request, obj):
|
||||
# Check if new addresse matches with a mbox because of having a local domain
|
||||
if obj.name and obj.domain and obj.domain.name == settings.MAILBOXES_LOCAL_DOMAIN:
|
||||
|
|
|
@ -4,7 +4,7 @@ from orchestra.api import router, SetPasswordApiMixin, LogApiMixin
|
|||
from orchestra.contrib.accounts.api import AccountApiMixin
|
||||
|
||||
from .models import Address, Mailbox
|
||||
from .serializers import AddressSerializer, MailboxSerializer, MailboxWritableSerializer
|
||||
from .serializers import AddressSerializer, MailboxSerializer
|
||||
|
||||
|
||||
class AddressViewSet(LogApiMixin, AccountApiMixin, viewsets.ModelViewSet):
|
||||
|
@ -17,12 +17,6 @@ class MailboxViewSet(LogApiMixin, SetPasswordApiMixin, AccountApiMixin, viewsets
|
|||
queryset = Mailbox.objects.prefetch_related('addresses__domain').all()
|
||||
serializer_class = MailboxSerializer
|
||||
|
||||
def get_serializer_class(self):
|
||||
if self.request.method == 'GET':
|
||||
return self.serializer_class
|
||||
|
||||
return MailboxWritableSerializer
|
||||
|
||||
|
||||
router.register(r'mailboxes', MailboxViewSet)
|
||||
router.register(r'addresses', AddressViewSet)
|
||||
|
|
|
@ -3,7 +3,6 @@ from __future__ import unicode_literals
|
|||
|
||||
from django.db import models, migrations
|
||||
from django.conf import settings
|
||||
import django.db.models.deletion
|
||||
import orchestra.contrib.mailboxes.validators
|
||||
import django.core.validators
|
||||
|
||||
|
@ -22,8 +21,8 @@ class Migration(migrations.Migration):
|
|||
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('name', models.CharField(verbose_name='name', validators=[orchestra.contrib.mailboxes.validators.validate_emailname], blank=True, help_text='Address name, left blank for a <i>catch-all</i> address', max_length=64)),
|
||||
('forward', models.CharField(verbose_name='forward', validators=[orchestra.contrib.mailboxes.validators.validate_forward], blank=True, help_text='Space separated email addresses or mailboxes', max_length=256)),
|
||||
('account', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL, related_name='addresses', verbose_name='Account')),
|
||||
('domain', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='domains.Domain', related_name='addresses', verbose_name='domain')),
|
||||
('account', models.ForeignKey(to=settings.AUTH_USER_MODEL, related_name='addresses', verbose_name='Account')),
|
||||
('domain', models.ForeignKey(to='domains.Domain', related_name='addresses', verbose_name='domain')),
|
||||
],
|
||||
options={
|
||||
'verbose_name_plural': 'addresses',
|
||||
|
@ -36,7 +35,7 @@ class Migration(migrations.Migration):
|
|||
('subject', models.CharField(verbose_name='subject', max_length=256)),
|
||||
('message', models.TextField(verbose_name='message')),
|
||||
('enabled', models.BooleanField(verbose_name='enabled', default=False)),
|
||||
('address', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, to='mailboxes.Address', related_name='autoresponse', verbose_name='address')),
|
||||
('address', models.OneToOneField(to='mailboxes.Address', related_name='autoresponse', verbose_name='address')),
|
||||
],
|
||||
),
|
||||
migrations.CreateModel(
|
||||
|
@ -48,7 +47,7 @@ class Migration(migrations.Migration):
|
|||
('filtering', models.CharField(choices=[('CUSTOM', 'Custom filtering'), ('REDIRECT', 'Archive spam (X-Spam-Score≥9)'), ('DISABLE', 'Disable'), ('REJECT', 'Reject spam (X-Spam-Score≥9)')], max_length=16, default='REDIRECT')),
|
||||
('custom_filtering', models.TextField(verbose_name='filtering', validators=[orchestra.contrib.mailboxes.validators.validate_sieve], blank=True, help_text='Arbitrary email filtering in sieve language. This overrides any automatic junk email filtering')),
|
||||
('is_active', models.BooleanField(verbose_name='active', default=True)),
|
||||
('account', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL, related_name='mailboxes', verbose_name='account')),
|
||||
('account', models.ForeignKey(to=settings.AUTH_USER_MODEL, related_name='mailboxes', verbose_name='account')),
|
||||
],
|
||||
options={
|
||||
'verbose_name_plural': 'mailboxes',
|
||||
|
|
|
@ -1,71 +0,0 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
# Generated by Django 1.10.5 on 2021-04-22 11:27
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.conf import settings
|
||||
import django.core.validators
|
||||
from django.db import migrations, models
|
||||
import django.db.models.deletion
|
||||
import orchestra.contrib.mailboxes.validators
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
replaces = [('mailboxes', '0001_initial'), ('mailboxes', '0002_auto_20160219_1032'), ('mailboxes', '0003_auto_20170528_2011')]
|
||||
|
||||
initial = True
|
||||
|
||||
dependencies = [
|
||||
('domains', '0001_initial'),
|
||||
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name='Address',
|
||||
fields=[
|
||||
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('name', models.CharField(blank=True, help_text='Address name, left blank for a <i>catch-all</i> address', max_length=64, validators=[orchestra.contrib.mailboxes.validators.validate_emailname], verbose_name='name')),
|
||||
('forward', models.CharField(blank=True, help_text='Space separated email addresses or mailboxes', max_length=256, validators=[orchestra.contrib.mailboxes.validators.validate_forward], verbose_name='forward')),
|
||||
('account', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='addresses', to=settings.AUTH_USER_MODEL, verbose_name='Account')),
|
||||
('domain', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='addresses', to='domains.Domain', verbose_name='domain')),
|
||||
],
|
||||
options={
|
||||
'verbose_name_plural': 'addresses',
|
||||
},
|
||||
),
|
||||
migrations.CreateModel(
|
||||
name='Autoresponse',
|
||||
fields=[
|
||||
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('subject', models.CharField(max_length=256, verbose_name='subject')),
|
||||
('message', models.TextField(verbose_name='message')),
|
||||
('enabled', models.BooleanField(default=False, verbose_name='enabled')),
|
||||
('address', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, related_name='autoresponse', to='mailboxes.Address', verbose_name='address')),
|
||||
],
|
||||
),
|
||||
migrations.CreateModel(
|
||||
name='Mailbox',
|
||||
fields=[
|
||||
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('name', models.CharField(db_index=True, help_text='Required. 32 characters or fewer. Letters, digits and ./-/_ only.', max_length=32, unique=True, validators=[django.core.validators.RegexValidator('^[\\w.-]+$', 'Enter a valid mailbox name.')], verbose_name='name')),
|
||||
('password', models.CharField(max_length=128, verbose_name='password')),
|
||||
('filtering', models.CharField(choices=[('CUSTOM', 'Custom filtering'), ('DISABLE', 'Disable'), ('REDIRECT', 'Archive spam (Score≥8)'), ('REDIRECT5', 'Archive spam (Score≥5)'), ('REJECT', 'Reject spam (Score≥8)'), ('REJECT5', 'Reject spam (Score≥5)')], default='REDIRECT', max_length=16)),
|
||||
('custom_filtering', models.TextField(blank=True, help_text="Arbitrary email filtering in <a href='https://tty1.net/blog/2011/sieve-tutorial_en.html'>sieve language</a>. This overrides any automatic junk email filtering", validators=[orchestra.contrib.mailboxes.validators.validate_sieve], verbose_name='filtering')),
|
||||
('is_active', models.BooleanField(default=True, verbose_name='active')),
|
||||
('account', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='mailboxes', to=settings.AUTH_USER_MODEL, verbose_name='account')),
|
||||
],
|
||||
options={
|
||||
'verbose_name_plural': 'mailboxes',
|
||||
},
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='address',
|
||||
name='mailboxes',
|
||||
field=models.ManyToManyField(blank=True, related_name='addresses', to='mailboxes.Mailbox', verbose_name='mailboxes'),
|
||||
),
|
||||
migrations.AlterUniqueTogether(
|
||||
name='address',
|
||||
unique_together=set([('name', 'domain')]),
|
||||
),
|
||||
]
|
|
@ -13,7 +13,7 @@ from . import validators, settings
|
|||
|
||||
class Mailbox(models.Model):
|
||||
CUSTOM = 'CUSTOM'
|
||||
|
||||
|
||||
name = models.CharField(_("name"), unique=True, db_index=True,
|
||||
max_length=settings.MAILBOXES_NAME_MAX_LENGTH,
|
||||
help_text=_("Required. %s characters or fewer. Letters, digits and ./-/_ only.") %
|
||||
|
@ -23,7 +23,7 @@ class Mailbox(models.Model):
|
|||
])
|
||||
password = models.CharField(_("password"), max_length=128)
|
||||
account = models.ForeignKey('accounts.Account', verbose_name=_("account"),
|
||||
related_name='mailboxes', on_delete=models.CASCADE)
|
||||
related_name='mailboxes')
|
||||
filtering = models.CharField(max_length=16,
|
||||
default=settings.MAILBOXES_MAILBOX_DEFAULT_FILTERING,
|
||||
choices=[(k, v[0]) for k,v in sorted(settings.MAILBOXES_MAILBOX_FILTERINGS.items())])
|
||||
|
@ -33,59 +33,59 @@ class Mailbox(models.Model):
|
|||
"<a href='https://tty1.net/blog/2011/sieve-tutorial_en.html'>sieve language</a>. "
|
||||
"This overrides any automatic junk email filtering"))
|
||||
is_active = models.BooleanField(_("active"), default=True)
|
||||
|
||||
|
||||
class Meta:
|
||||
verbose_name_plural = _("mailboxes")
|
||||
|
||||
|
||||
def __str__(self):
|
||||
return self.name
|
||||
|
||||
|
||||
@cached_property
|
||||
def active(self):
|
||||
try:
|
||||
return self.is_active and self.account.is_active
|
||||
except type(self).account.field.related_model.DoesNotExist:
|
||||
except type(self).account.field.rel.to.DoesNotExist:
|
||||
return self.is_active
|
||||
|
||||
|
||||
def disable(self):
|
||||
self.is_active = False
|
||||
self.save(update_fields=('is_active',))
|
||||
|
||||
|
||||
def enable(self):
|
||||
self.is_active = False
|
||||
self.save(update_fields=('is_active',))
|
||||
|
||||
|
||||
def set_password(self, raw_password):
|
||||
self.password = make_password(raw_password)
|
||||
|
||||
|
||||
def get_home(self):
|
||||
context = {
|
||||
'name': self.name,
|
||||
'username': self.name,
|
||||
}
|
||||
return os.path.normpath(settings.MAILBOXES_HOME % context)
|
||||
|
||||
|
||||
def clean(self):
|
||||
if self.filtering == self.CUSTOM and not self.custom_filtering:
|
||||
raise ValidationError({
|
||||
'custom_filtering': _("Custom filtering is selected but not provided.")
|
||||
})
|
||||
|
||||
|
||||
def get_filtering(self):
|
||||
name, content = settings.MAILBOXES_MAILBOX_FILTERINGS[self.filtering]
|
||||
if callable(content):
|
||||
# Custom filtering
|
||||
content = content(self)
|
||||
return (name, content)
|
||||
|
||||
|
||||
def get_local_address(self):
|
||||
if not settings.MAILBOXES_LOCAL_DOMAIN:
|
||||
raise AttributeError("Mailboxes do not have a defined local address domain.")
|
||||
return '@'.join((self.name, settings.MAILBOXES_LOCAL_DOMAIN))
|
||||
|
||||
|
||||
def get_forwards(self):
|
||||
return Address.objects.filter(forward__regex=r'(^|.*\s)%s(\s.*|$)' % self.name)
|
||||
|
||||
|
||||
def get_addresses(self):
|
||||
mboxes = self.addresses.all()
|
||||
forwards = self.get_forwards()
|
||||
|
@ -97,33 +97,33 @@ class Address(models.Model):
|
|||
validators=[validators.validate_emailname],
|
||||
help_text=_("Address name, left blank for a <i>catch-all</i> address"))
|
||||
domain = models.ForeignKey(settings.MAILBOXES_DOMAIN_MODEL,
|
||||
verbose_name=_("domain"), related_name='addresses', on_delete=models.CASCADE)
|
||||
verbose_name=_("domain"), related_name='addresses')
|
||||
mailboxes = models.ManyToManyField(Mailbox, verbose_name=_("mailboxes"),
|
||||
related_name='addresses', blank=True)
|
||||
forward = models.CharField(_("forward"), max_length=256, blank=True,
|
||||
validators=[validators.validate_forward],
|
||||
help_text=_("Space separated email addresses or mailboxes"))
|
||||
account = models.ForeignKey('accounts.Account', verbose_name=_("Account"),
|
||||
related_name='addresses', on_delete=models.CASCADE)
|
||||
|
||||
related_name='addresses')
|
||||
|
||||
class Meta:
|
||||
verbose_name_plural = _("addresses")
|
||||
unique_together = ('name', 'domain')
|
||||
|
||||
|
||||
def __str__(self):
|
||||
return self.email
|
||||
|
||||
|
||||
@property
|
||||
def email(self):
|
||||
return "%s@%s" % (self.name, self.domain)
|
||||
|
||||
|
||||
@cached_property
|
||||
def destination(self):
|
||||
destinations = list(self.mailboxes.values_list('name', flat=True))
|
||||
if self.forward:
|
||||
destinations += self.forward.split()
|
||||
return ' '.join(destinations)
|
||||
|
||||
|
||||
def clean(self):
|
||||
errors = defaultdict(list)
|
||||
local_domain = settings.MAILBOXES_LOCAL_DOMAIN
|
||||
|
@ -149,7 +149,7 @@ class Address(models.Model):
|
|||
)
|
||||
if errors:
|
||||
raise ValidationError(errors)
|
||||
|
||||
|
||||
def get_forward_mailboxes(self):
|
||||
rm_local_domain = re.compile(r'@%s$' % settings.MAILBOXES_LOCAL_DOMAIN)
|
||||
mailboxes = []
|
||||
|
@ -158,7 +158,7 @@ class Address(models.Model):
|
|||
if '@' not in forward:
|
||||
mailboxes.append(forward)
|
||||
return Mailbox.objects.filter(name__in=mailboxes)
|
||||
|
||||
|
||||
def get_mailboxes(self):
|
||||
for mailbox in self.mailboxes.all():
|
||||
yield mailbox
|
||||
|
@ -168,11 +168,11 @@ class Address(models.Model):
|
|||
|
||||
class Autoresponse(models.Model):
|
||||
address = models.OneToOneField(Address, verbose_name=_("address"),
|
||||
related_name='autoresponse', on_delete=models.CASCADE)
|
||||
related_name='autoresponse')
|
||||
# TODO initial_date
|
||||
subject = models.CharField(_("subject"), max_length=256)
|
||||
message = models.TextField(_("message"))
|
||||
enabled = models.BooleanField(_("enabled"), default=False)
|
||||
|
||||
|
||||
def __str__(self):
|
||||
return self.address
|
||||
|
|
|
@ -1,4 +1,3 @@
|
|||
from django.db import transaction
|
||||
from rest_framework import serializers
|
||||
|
||||
from orchestra.api.serializers import SetPasswordHyperlinkedSerializer, RelatedHyperlinkedModelSerializer
|
||||
|
@ -9,17 +8,17 @@ from .models import Mailbox, Address
|
|||
|
||||
class RelatedDomainSerializer(AccountSerializerMixin, RelatedHyperlinkedModelSerializer):
|
||||
class Meta:
|
||||
model = Address.domain.field.related_model
|
||||
model = Address.domain.field.rel.to
|
||||
fields = ('url', 'id', 'name')
|
||||
|
||||
|
||||
class RelatedAddressSerializer(AccountSerializerMixin, serializers.HyperlinkedModelSerializer):
|
||||
domain = RelatedDomainSerializer()
|
||||
|
||||
|
||||
class Meta:
|
||||
model = Address
|
||||
fields = ('url', 'id', 'name', 'domain', 'forward')
|
||||
#
|
||||
#
|
||||
# def from_native(self, data, files=None):
|
||||
# queryset = self.opts.model.objects.filter(account=self.account)
|
||||
# return get_object_or_404(queryset, name=data['name'])
|
||||
|
@ -27,7 +26,7 @@ class RelatedAddressSerializer(AccountSerializerMixin, serializers.HyperlinkedMo
|
|||
|
||||
class MailboxSerializer(AccountSerializerMixin, SetPasswordHyperlinkedSerializer):
|
||||
addresses = RelatedAddressSerializer(many=True, read_only=True)
|
||||
|
||||
|
||||
class Meta:
|
||||
model = Mailbox
|
||||
fields = (
|
||||
|
@ -36,41 +35,6 @@ class MailboxSerializer(AccountSerializerMixin, SetPasswordHyperlinkedSerializer
|
|||
postonly_fields = ('name', 'password')
|
||||
|
||||
|
||||
class AddressRelatedField(serializers.HyperlinkedRelatedField):
|
||||
# Filter addresses by account (user)
|
||||
def get_queryset(self):
|
||||
qs = super().get_queryset()
|
||||
return qs.filter(account=self.context['account'])
|
||||
|
||||
|
||||
class MailboxWritableSerializer(AccountSerializerMixin, SetPasswordHyperlinkedSerializer):
|
||||
addresses = AddressRelatedField(many=True, view_name='address-detail', queryset=Address.objects.all())
|
||||
|
||||
class Meta:
|
||||
model = Mailbox
|
||||
fields = (
|
||||
'url', 'id', 'name', 'password', 'filtering', 'custom_filtering', 'addresses', 'is_active'
|
||||
)
|
||||
postonly_fields = ('name', 'password')
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
self.fields['addresses'].context['account'] = self.account
|
||||
|
||||
@transaction.atomic
|
||||
def create(self, validated_data):
|
||||
addresses = validated_data.pop('addresses', [])
|
||||
instance = super().create(validated_data)
|
||||
instance.addresses.set(addresses)
|
||||
return instance
|
||||
|
||||
@transaction.atomic
|
||||
def update(self, instance, validated_data):
|
||||
addresses = validated_data.pop('addresses', [])
|
||||
instance.addresses.set(addresses)
|
||||
return super().update(instance, validated_data)
|
||||
|
||||
|
||||
class RelatedMailboxSerializer(AccountSerializerMixin, RelatedHyperlinkedModelSerializer):
|
||||
class Meta:
|
||||
model = Mailbox
|
||||
|
@ -79,29 +43,14 @@ class RelatedMailboxSerializer(AccountSerializerMixin, RelatedHyperlinkedModelSe
|
|||
|
||||
class AddressSerializer(AccountSerializerMixin, serializers.HyperlinkedModelSerializer):
|
||||
domain = RelatedDomainSerializer()
|
||||
mailboxes = RelatedMailboxSerializer(many=True, required=False)
|
||||
|
||||
mailboxes = RelatedMailboxSerializer(many=True, required=False) #allow_add_remove=True
|
||||
|
||||
class Meta:
|
||||
model = Address
|
||||
fields = ('url', 'id', 'name', 'domain', 'mailboxes', 'forward')
|
||||
|
||||
|
||||
def validate(self, attrs):
|
||||
attrs = super(AddressSerializer, self).validate(attrs)
|
||||
mailboxes = attrs.get('mailboxes', [])
|
||||
forward = attrs.get('forward', '')
|
||||
if not mailboxes and not forward:
|
||||
if not attrs['mailboxes'] and not attrs['forward']:
|
||||
raise serializers.ValidationError("A mailbox or forward address should be provided.")
|
||||
return attrs
|
||||
|
||||
@transaction.atomic
|
||||
def create(self, validated_data):
|
||||
mailboxes = validated_data.pop('mailboxes', [])
|
||||
obj = super().create(validated_data)
|
||||
obj.mailboxes.set(mailboxes)
|
||||
return obj
|
||||
|
||||
@transaction.atomic
|
||||
def update(self, instance, validated_data):
|
||||
mailboxes = validated_data.pop('mailboxes', [])
|
||||
instance.mailboxes.set(mailboxes)
|
||||
return super().update(instance, validated_data)
|
||||
|
|
|
@ -27,7 +27,7 @@ def create_local_address(sender, *args, **kwargs):
|
|||
mbox = kwargs['instance']
|
||||
local_domain = settings.MAILBOXES_LOCAL_DOMAIN
|
||||
if not mbox.pk and local_domain:
|
||||
Domain = Address._meta.get_field('domain').remote_field.model
|
||||
Domain = Address._meta.get_field('domain').rel.to
|
||||
try:
|
||||
domain = Domain.objects.get(name=local_domain)
|
||||
except Domain.DoesNotExist:
|
||||
|
|
|
@ -4,14 +4,14 @@ import poplib
|
|||
import smtplib
|
||||
import time
|
||||
import textwrap
|
||||
import unittest
|
||||
from email.mime.text import MIMEText
|
||||
from unittest import skip
|
||||
|
||||
from django.apps import apps
|
||||
from django.conf import settings as djsettings
|
||||
from django.contrib.contenttypes.models import ContentType
|
||||
from django.core.management.base import CommandError
|
||||
from django.urls import reverse
|
||||
from django.core.urlresolvers import reverse
|
||||
from selenium.webdriver.support.select import Select
|
||||
|
||||
from orchestra.contrib.orchestration.models import Server, Route
|
||||
|
@ -22,8 +22,6 @@ from orchestra.utils.tests import BaseLiveServerTestCase, random_ascii, snapshot
|
|||
from ... import backends, settings
|
||||
from ...models import Mailbox
|
||||
|
||||
TEST_REST_API = int(os.getenv('TEST_REST_API', '0'))
|
||||
|
||||
|
||||
class MailboxMixin(object):
|
||||
MASTER_SERVER = os.environ.get('ORCHESTRA_SLAVE_SERVER', 'localhost')
|
||||
|
@ -32,64 +30,63 @@ class MailboxMixin(object):
|
|||
'orchestra.contrib.mails',
|
||||
'orchestra.contrib.resources',
|
||||
)
|
||||
|
||||
|
||||
def setUp(self):
|
||||
super(MailboxMixin, self).setUp()
|
||||
self.add_route()
|
||||
# clean resource relation from other tests
|
||||
apps.get_app_config('resources').reload_relations()
|
||||
djsettings.DEBUG = True
|
||||
|
||||
|
||||
def add_route(self):
|
||||
server = Server.objects.create(name=self.MASTER_SERVER)
|
||||
backend = backends.RoundcubeIdentityController.get_name()
|
||||
Route.objects.create(backend=backend, match=True, host=server)
|
||||
backend = backends.PostfixAddressController.get_name()
|
||||
Route.objects.create(backend=backend, match=True, host=server)
|
||||
|
||||
|
||||
def add_quota_resource(self):
|
||||
Resource.objects.create(
|
||||
name='disk',
|
||||
content_type=ContentType.objects.get_for_model(Mailbox),
|
||||
period=Resource.LAST,
|
||||
verbose_name='Mail quota',
|
||||
unit='MB',
|
||||
scale=10**6,
|
||||
on_demand=False,
|
||||
default_allocation=2000
|
||||
)
|
||||
|
||||
|
||||
def save(self):
|
||||
raise NotImplementedError
|
||||
|
||||
|
||||
def add(self):
|
||||
raise NotImplementedError
|
||||
|
||||
|
||||
def delete(self):
|
||||
raise NotImplementedError
|
||||
|
||||
|
||||
def update(self):
|
||||
raise NotImplementedError
|
||||
|
||||
|
||||
def disable(self):
|
||||
raise NotImplementedError
|
||||
|
||||
|
||||
def add_group(self, username, groupname):
|
||||
raise NotImplementedError
|
||||
|
||||
|
||||
def login_imap(self, username, password):
|
||||
mail = imaplib.IMAP4_SSL(self.MASTER_SERVER)
|
||||
status, msg = mail.login(username, password)
|
||||
self.assertEqual('OK', status)
|
||||
self.assertEqual(['Logged in'], msg)
|
||||
return mail
|
||||
|
||||
|
||||
def login_pop3(self, username, password):
|
||||
pop = poplib.POP3(self.MASTER_SERVER)
|
||||
pop.user(username)
|
||||
pop.pass_(password)
|
||||
return pop
|
||||
|
||||
|
||||
def send_email(self, to, token):
|
||||
msg = MIMEText(token)
|
||||
msg['To'] = to
|
||||
|
@ -103,14 +100,15 @@ class MailboxMixin(object):
|
|||
server.sendmail(msg['From'], msg['To'], msg.as_string())
|
||||
finally:
|
||||
server.quit()
|
||||
|
||||
|
||||
def validate_mailbox(self, username):
|
||||
sshrun(self.MASTER_SERVER, "doveadm search -u %s ALL" % username, display=False)
|
||||
|
||||
|
||||
def validate_email(self, username, token):
|
||||
home = Mailbox.objects.get(name=username).get_home()
|
||||
sshrun(self.MASTER_SERVER, "grep '%s' %s/Maildir/new/*" % (token, home), display=False)
|
||||
|
||||
|
||||
@skip("Skip because not exists get_auth_token in orm.api.Api")
|
||||
def test_add(self):
|
||||
username = '%s_mailbox' % random_ascii(10)
|
||||
password = '@!?%spppP001' % random_ascii(5)
|
||||
|
@ -118,7 +116,8 @@ class MailboxMixin(object):
|
|||
self.addCleanup(self.delete, username)
|
||||
imap = self.login_imap(username, password)
|
||||
self.validate_mailbox(username)
|
||||
|
||||
|
||||
@skip("Skip because not exists get_auth_token in orm.api.Api")
|
||||
def test_change_password(self):
|
||||
username = '%s_systemuser' % random_ascii(10)
|
||||
password = '@!?%spppP001' % random_ascii(5)
|
||||
|
@ -128,7 +127,8 @@ class MailboxMixin(object):
|
|||
new_password = '@!?%spppP001' % random_ascii(5)
|
||||
self.change_password(username, new_password)
|
||||
imap = self.login_imap(username, new_password)
|
||||
|
||||
|
||||
@skip("Skip because not exists get_auth_token in orm.api.Api")
|
||||
def test_quota(self):
|
||||
username = '%s_mailbox' % random_ascii(10)
|
||||
password = '@!?%spppP001' % random_ascii(5)
|
||||
|
@ -142,7 +142,8 @@ class MailboxMixin(object):
|
|||
imap = self.login_imap(username, password)
|
||||
imap_quota = int(imap.getquotaroot("INBOX")[1][1][0].split(' ')[-1].split(')')[0])
|
||||
self.assertEqual(quota*1024, imap_quota)
|
||||
|
||||
|
||||
@skip("Skip because not exists get_auth_token in orm.api.Api")
|
||||
def test_send_email(self):
|
||||
username = '%s_mailbox' % random_ascii(10)
|
||||
password = '@!?%spppP001' % random_ascii(5)
|
||||
|
@ -158,7 +159,8 @@ class MailboxMixin(object):
|
|||
server.sendmail(msg['From'], msg['To'], msg.as_string())
|
||||
finally:
|
||||
server.quit()
|
||||
|
||||
|
||||
@skip("Skip because not exists get_auth_token in orm.api.Api")
|
||||
def test_address(self):
|
||||
username = '%s_mailbox' % random_ascii(10)
|
||||
password = '@!?%spppP001' % random_ascii(5)
|
||||
|
@ -171,7 +173,8 @@ class MailboxMixin(object):
|
|||
token = random_ascii(100)
|
||||
self.send_email("%s@%s" % (name, domain), token)
|
||||
self.validate_email(username, token)
|
||||
|
||||
|
||||
@skip("Skip because not exists get_auth_token in orm.api.Api")
|
||||
def test_disable(self):
|
||||
username = '%s_systemuser' % random_ascii(10)
|
||||
password = '@!?%spppP001' % random_ascii(5)
|
||||
|
@ -181,7 +184,8 @@ class MailboxMixin(object):
|
|||
imap = self.login_imap(username, password)
|
||||
self.disable(username)
|
||||
self.assertRaises(imap.error, self.login_imap, username, password)
|
||||
|
||||
|
||||
@skip("Skip because not exists get_auth_token in orm.api.Api")
|
||||
def test_delete(self):
|
||||
username = '%s_systemuser' % random_ascii(10)
|
||||
password = '@!?%sppppP001' % random_ascii(5)
|
||||
|
@ -196,7 +200,8 @@ class MailboxMixin(object):
|
|||
self.assertRaises(imap.error, self.login_imap, username, password)
|
||||
self.assertRaises(CommandError,
|
||||
sshrun, self.MASTER_SERVER, 'ls %s' % home, display=False)
|
||||
|
||||
|
||||
@skip("Skip because not exists get_auth_token in orm.api.Api")
|
||||
def test_delete_address(self):
|
||||
username = '%s_mailbox' % random_ascii(10)
|
||||
password = '@!?%spppP001' % random_ascii(5)
|
||||
|
@ -212,14 +217,15 @@ class MailboxMixin(object):
|
|||
self.delete_address(username)
|
||||
self.send_email("%s@%s" % (name, domain), token)
|
||||
self.validate_email(username, token)
|
||||
|
||||
|
||||
@skip("Skip because not exists get_auth_token in orm.api.Api")
|
||||
def test_custom_filtering(self):
|
||||
username = '%s_mailbox' % random_ascii(10)
|
||||
password = '@!?%spppP001' % random_ascii(5)
|
||||
folder = random_ascii(5)
|
||||
filtering = textwrap.dedent("""
|
||||
require "fileinto";
|
||||
if true {
|
||||
if true {
|
||||
fileinto "%s";
|
||||
stop;
|
||||
}""" % folder)
|
||||
|
@ -238,12 +244,11 @@ class MailboxMixin(object):
|
|||
# TODO test autoreply
|
||||
|
||||
|
||||
@unittest.skipUnless(TEST_REST_API, "REST API tests")
|
||||
class RESTMailboxMixin(MailboxMixin):
|
||||
def setUp(self):
|
||||
super(RESTMailboxMixin, self).setUp()
|
||||
self.rest_login()
|
||||
|
||||
|
||||
@save_response_on_error
|
||||
def add(self, username, password, quota=None, filtering=None):
|
||||
extra = {}
|
||||
|
@ -262,28 +267,28 @@ class RESTMailboxMixin(MailboxMixin):
|
|||
'custom_filtering': filtering,
|
||||
})
|
||||
self.rest.mailboxes.create(name=username, password=password, **extra)
|
||||
|
||||
|
||||
@save_response_on_error
|
||||
def delete(self, username):
|
||||
mailbox = self.rest.mailboxes.retrieve(name=username).get()
|
||||
mailbox.delete()
|
||||
|
||||
|
||||
@save_response_on_error
|
||||
def change_password(self, username, password):
|
||||
mailbox = self.rest.mailboxes.retrieve(name=username).get()
|
||||
mailbox.change_password(password)
|
||||
|
||||
|
||||
@save_response_on_error
|
||||
def add_address(self, username, name, domain):
|
||||
mailbox = self.rest.mailboxes.retrieve(name=username).get()
|
||||
domain = self.rest.domains.retrieve(name=domain.name).get()
|
||||
self.rest.addresses.create(name=name, domain=domain, mailboxes=[mailbox])
|
||||
|
||||
|
||||
@save_response_on_error
|
||||
def delete_address(self, username):
|
||||
mailbox = self.rest.mailboxes.retrieve(name=username).get()
|
||||
self.rest.addresses.delete()
|
||||
|
||||
|
||||
@save_response_on_error
|
||||
def disable(self, username):
|
||||
mailbox = self.rest.mailboxes.retrieve(name=username).get()
|
||||
|
@ -294,30 +299,30 @@ class AdminMailboxMixin(MailboxMixin):
|
|||
def setUp(self):
|
||||
super(AdminMailboxMixin, self).setUp()
|
||||
self.admin_login()
|
||||
|
||||
|
||||
@snapshot_on_error
|
||||
def add(self, username, password, quota=None, filtering=None):
|
||||
url = self.live_server_url + reverse('admin:mailboxes_mailbox_add')
|
||||
self.selenium.get(url)
|
||||
|
||||
|
||||
# account_input = self.selenium.find_element_by_id('id_account')
|
||||
# account_select = Select(account_input)
|
||||
# account_select.select_by_value(str(self.account.pk))
|
||||
|
||||
|
||||
name_field = self.selenium.find_element_by_id('id_name')
|
||||
name_field.send_keys(username)
|
||||
|
||||
|
||||
password_field = self.selenium.find_element_by_id('id_password1')
|
||||
password_field.send_keys(password)
|
||||
password_field = self.selenium.find_element_by_id('id_password2')
|
||||
password_field.send_keys(password)
|
||||
|
||||
|
||||
if quota is not None:
|
||||
quota_id = 'id_resources-resourcedata-content_type-object_id-0-allocated'
|
||||
quota_field = self.selenium.find_element_by_id(quota_id)
|
||||
quota_field.clear()
|
||||
quota_field.send_keys(quota)
|
||||
|
||||
|
||||
if filtering is not None:
|
||||
filtering_input = self.selenium.find_element_by_id('id_filtering')
|
||||
filtering_select = Select(filtering_input)
|
||||
|
@ -327,45 +332,45 @@ class AdminMailboxMixin(MailboxMixin):
|
|||
time.sleep(0.5)
|
||||
filtering_field = self.selenium.find_element_by_id('id_custom_filtering')
|
||||
filtering_field.send_keys(filtering)
|
||||
|
||||
|
||||
name_field.submit()
|
||||
self.assertNotEqual(url, self.selenium.current_url)
|
||||
|
||||
|
||||
@snapshot_on_error
|
||||
def delete(self, username):
|
||||
mailbox = Mailbox.objects.get(name=username)
|
||||
self.admin_delete(mailbox)
|
||||
|
||||
|
||||
@snapshot_on_error
|
||||
def change_password(self, username, password):
|
||||
mailbox = Mailbox.objects.get(name=username)
|
||||
self.admin_change_password(mailbox, password)
|
||||
|
||||
|
||||
@snapshot_on_error
|
||||
def add_address(self, username, name, domain):
|
||||
url = self.live_server_url + reverse('admin:mailboxes_address_add')
|
||||
self.selenium.get(url)
|
||||
|
||||
|
||||
name_field = self.selenium.find_element_by_id('id_name')
|
||||
name_field.send_keys(name)
|
||||
|
||||
|
||||
domain_input = self.selenium.find_element_by_id('id_domain')
|
||||
domain_select = Select(domain_input)
|
||||
domain_select.select_by_value(str(domain.pk))
|
||||
|
||||
|
||||
mailboxes = self.selenium.find_element_by_id('id_mailboxes_add_all_link')
|
||||
mailboxes.click()
|
||||
time.sleep(0.5)
|
||||
name_field.submit()
|
||||
|
||||
|
||||
self.assertNotEqual(url, self.selenium.current_url)
|
||||
|
||||
|
||||
@snapshot_on_error
|
||||
def delete_address(self, username):
|
||||
mailbox = Mailbox.objects.get(name=username)
|
||||
address = mailbox.addresses.get()
|
||||
self.admin_delete(address)
|
||||
|
||||
|
||||
@snapshot_on_error
|
||||
def disable(self, username):
|
||||
mailbox = Mailbox.objects.get(name=username)
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
from django.urls import reverse
|
||||
from django.core.urlresolvers import reverse
|
||||
from django.shortcuts import redirect
|
||||
|
||||
|
||||
|
|
|
@ -3,11 +3,9 @@ import email
|
|||
|
||||
from django import forms
|
||||
from django.contrib import admin
|
||||
from django.urls import reverse
|
||||
from django.core.urlresolvers import reverse
|
||||
from django.db.models import Count
|
||||
from django.shortcuts import redirect
|
||||
from django.utils.html import format_html
|
||||
from django.utils.safestring import mark_safe
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
|
||||
from orchestra.admin import ExtendedModelAdmin
|
||||
|
@ -54,19 +52,20 @@ class MessageAdmin(ExtendedModelAdmin):
|
|||
)
|
||||
date_hierarchy = 'created_at'
|
||||
change_view_actions = (last,)
|
||||
|
||||
|
||||
colored_state = admin_colored('state', colors=COLORS)
|
||||
created_at_delta = admin_date('created_at')
|
||||
last_try_delta = admin_date('last_try')
|
||||
|
||||
|
||||
def display_subject(self, instance):
|
||||
subject = instance.subject
|
||||
if len(subject) > 64:
|
||||
return mark_safe(subject[:64] + '…')
|
||||
return subject[:64] + '…'
|
||||
return subject
|
||||
display_subject.short_description = _("Subject")
|
||||
display_subject.admin_order_field = 'subject'
|
||||
|
||||
display_subject.allow_tags = True
|
||||
|
||||
def display_retries(self, instance):
|
||||
num_logs = instance.logs__count
|
||||
if num_logs == 1:
|
||||
|
@ -75,10 +74,11 @@ class MessageAdmin(ExtendedModelAdmin):
|
|||
else:
|
||||
url = reverse('admin:mailer_smtplog_changelist')
|
||||
url += '?&message=%i' % instance.pk
|
||||
return format_html('<a href="{}" onclick="return showAddAnotherPopup(this);">{}</a>', url, instance.retries)
|
||||
return '<a href="%s" onclick="return showAddAnotherPopup(this);">%d</a>' % (url, instance.retries)
|
||||
display_retries.short_description = _("Retries")
|
||||
display_retries.admin_order_field = 'retries'
|
||||
|
||||
display_retries.allow_tags = True
|
||||
|
||||
def display_content(self, instance):
|
||||
part = email.message_from_string(instance.content)
|
||||
payload = part.get_payload()
|
||||
|
@ -99,21 +99,22 @@ class MessageAdmin(ExtendedModelAdmin):
|
|||
payload = payload.decode(charset)
|
||||
if part.get_content_type() == 'text/plain':
|
||||
payload = payload.replace('\n', '<br>').replace(' ', ' ')
|
||||
return mark_safe(payload)
|
||||
return payload
|
||||
display_content.short_description = _("Content")
|
||||
|
||||
display_content.allow_tags = True
|
||||
|
||||
def display_full_subject(self, instance):
|
||||
return instance.subject
|
||||
display_full_subject.short_description = _("Subject")
|
||||
|
||||
|
||||
def display_from(self, instance):
|
||||
return instance.from_address
|
||||
display_from.short_description = _("From")
|
||||
|
||||
|
||||
def display_to(self, instance):
|
||||
return instance.to_address
|
||||
display_to.short_description = _("To")
|
||||
|
||||
|
||||
def get_urls(self):
|
||||
from django.conf.urls import url
|
||||
urls = super().get_urls()
|
||||
|
@ -124,16 +125,16 @@ class MessageAdmin(ExtendedModelAdmin):
|
|||
name='%s_%s_send_pending' % info)
|
||||
)
|
||||
return urls
|
||||
|
||||
|
||||
def get_queryset(self, request):
|
||||
qs = super().get_queryset(request)
|
||||
return qs.annotate(Count('logs')).defer('content')
|
||||
|
||||
|
||||
def send_pending_view(self, request):
|
||||
task(send_pending).apply_async()
|
||||
self.message_user(request, _("Pending messages are being sent on the background."))
|
||||
return redirect('..')
|
||||
|
||||
|
||||
def formfield_for_dbfield(self, db_field, **kwargs):
|
||||
if db_field.name == 'subject':
|
||||
kwargs['widget'] = forms.TextInput(attrs={'size':'100'})
|
||||
|
@ -147,7 +148,7 @@ class SMTPLogAdmin(admin.ModelAdmin):
|
|||
list_filter = ('result',)
|
||||
fields = ('message_link', 'colored_result', 'date_delta', 'log_message')
|
||||
readonly_fields = fields
|
||||
|
||||
|
||||
message_link = admin_link('message')
|
||||
colored_result = admin_colored('result', colors=COLORS, bold=False)
|
||||
date_delta = admin_date('date')
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
from __future__ import unicode_literals
|
||||
|
||||
import django.db.models.deletion
|
||||
from django.db import models, migrations
|
||||
|
||||
|
||||
|
@ -33,7 +32,7 @@ class Migration(migrations.Migration):
|
|||
('result', models.CharField(choices=[('SUCCESS', 'Success'), ('FAILURE', 'Failure')], default='SUCCESS', max_length=16)),
|
||||
('date', models.DateTimeField(auto_now_add=True)),
|
||||
('log_message', models.TextField()),
|
||||
('message', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='mailer.Message', editable=False, related_name='logs')),
|
||||
('message', models.ForeignKey(to='mailer.Message', editable=False, related_name='logs')),
|
||||
],
|
||||
),
|
||||
]
|
||||
|
|
|
@ -1,89 +0,0 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
# Generated by Django 1.10.5 on 2021-04-22 11:28
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.db import migrations, models
|
||||
import django.db.models.deletion
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
replaces = [('mailer', '0001_initial'), ('mailer', '0002_auto_20150617_1021'), ('mailer', '0003_auto_20150617_1024'), ('mailer', '0004_auto_20150805_1328'), ('mailer', '0005_auto_20160219_1056')]
|
||||
|
||||
initial = True
|
||||
|
||||
dependencies = [
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name='Message',
|
||||
fields=[
|
||||
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('state', models.CharField(choices=[('QUEUED', 'Queued'), ('SENT', 'Sent'), ('DEFERRED', 'Deferred'), ('FAILED', 'Failes')], default='QUEUED', max_length=16, verbose_name='State')),
|
||||
('priority', models.PositiveIntegerField(choices=[(0, 'Critical (not queued)'), (1, 'High'), (2, 'Normal'), (3, 'Low')], default=2, verbose_name='Priority')),
|
||||
('to_address', models.CharField(max_length=256)),
|
||||
('from_address', models.CharField(max_length=256)),
|
||||
('subject', models.CharField(max_length=256, verbose_name='subject')),
|
||||
('content', models.TextField(verbose_name='content')),
|
||||
('created_at', models.DateTimeField(auto_now_add=True, verbose_name='created at')),
|
||||
('retries', models.PositiveIntegerField(default=0, verbose_name='retries')),
|
||||
('last_retry', models.DateTimeField(auto_now=True, verbose_name='last try')),
|
||||
],
|
||||
),
|
||||
migrations.CreateModel(
|
||||
name='SMTPLog',
|
||||
fields=[
|
||||
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('result', models.CharField(choices=[('SUCCESS', 'Success'), ('FAILURE', 'Failure')], default='SUCCESS', max_length=16)),
|
||||
('date', models.DateTimeField(auto_now_add=True)),
|
||||
('log_message', models.TextField()),
|
||||
('message', models.ForeignKey(editable=False, on_delete=django.db.models.deletion.CASCADE, related_name='logs', to='mailer.Message')),
|
||||
],
|
||||
),
|
||||
migrations.RenameField(
|
||||
model_name='message',
|
||||
old_name='last_retry',
|
||||
new_name='last_try',
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='message',
|
||||
name='last_try',
|
||||
field=models.DateTimeField(verbose_name='last try'),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='message',
|
||||
name='subject',
|
||||
field=models.TextField(verbose_name='subject'),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='message',
|
||||
name='last_try',
|
||||
field=models.DateTimeField(null=True, verbose_name='last try'),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='message',
|
||||
name='state',
|
||||
field=models.CharField(choices=[('QUEUED', 'Queued'), ('SENT', 'Sent'), ('DEFERRED', 'Deferred'), ('FAILED', 'Failed')], default='QUEUED', max_length=16, verbose_name='State'),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='message',
|
||||
name='last_try',
|
||||
field=models.DateTimeField(db_index=True, null=True, verbose_name='last try'),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='message',
|
||||
name='priority',
|
||||
field=models.PositiveIntegerField(choices=[(0, 'Critical (not queued)'), (1, 'High'), (2, 'Normal'), (3, 'Low')], db_index=True, default=2, verbose_name='Priority'),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='message',
|
||||
name='retries',
|
||||
field=models.PositiveIntegerField(db_index=True, default=0, verbose_name='retries'),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='message',
|
||||
name='state',
|
||||
field=models.CharField(choices=[('QUEUED', 'Queued'), ('SENT', 'Sent'), ('DEFERRED', 'Deferred'), ('FAILED', 'Failed')], db_index=True, default='QUEUED', max_length=16, verbose_name='State'),
|
||||
),
|
||||
]
|
|
@ -15,7 +15,7 @@ class Message(models.Model):
|
|||
(DEFERRED, _("Deferred")),
|
||||
(FAILED, _("Failed")),
|
||||
)
|
||||
|
||||
|
||||
CRITICAL = 0
|
||||
HIGH = 1
|
||||
NORMAL = 2
|
||||
|
@ -26,7 +26,7 @@ class Message(models.Model):
|
|||
(NORMAL, _("Normal")),
|
||||
(LOW, _("Low")),
|
||||
)
|
||||
|
||||
|
||||
state = models.CharField(_("State"), max_length=16, choices=STATES, default=QUEUED,
|
||||
db_index=True)
|
||||
priority = models.PositiveIntegerField(_("Priority"), choices=PRIORITIES, default=NORMAL,
|
||||
|
@ -38,21 +38,21 @@ class Message(models.Model):
|
|||
created_at = models.DateTimeField(_("created at"), auto_now_add=True)
|
||||
retries = models.PositiveIntegerField(_("retries"), default=0, db_index=True)
|
||||
last_try = models.DateTimeField(_("last try"), null=True, db_index=True)
|
||||
|
||||
|
||||
def __str__(self):
|
||||
return '%s to %s' % (self.subject, self.to_address)
|
||||
|
||||
|
||||
def defer(self):
|
||||
self.state = self.DEFERRED
|
||||
# Max tries
|
||||
if self.retries >= len(settings.MAILER_DEFERE_SECONDS):
|
||||
self.state = self.FAILED
|
||||
self.save(update_fields=('state',))
|
||||
|
||||
|
||||
def sent(self):
|
||||
self.state = self.SENT
|
||||
self.save(update_fields=('state',))
|
||||
|
||||
|
||||
def log(self, error):
|
||||
result = SMTPLog.SUCCESS
|
||||
if error:
|
||||
|
@ -67,7 +67,7 @@ class SMTPLog(models.Model):
|
|||
(SUCCESS, _("Success")),
|
||||
(FAILURE, _("Failure")),
|
||||
)
|
||||
message = models.ForeignKey(Message, editable=False, related_name='logs', on_delete=models.CASCADE)
|
||||
message = models.ForeignKey(Message, editable=False, related_name='logs')
|
||||
result = models.CharField(max_length=16, choices=RESULTS, default=SUCCESS)
|
||||
date = models.DateTimeField(auto_now_add=True)
|
||||
log_message = models.TextField()
|
||||
|
|
|
@ -1,8 +1,7 @@
|
|||
from django import forms
|
||||
from django.contrib import admin
|
||||
from django.urls import reverse
|
||||
from django.core.urlresolvers import reverse
|
||||
from django.db import models
|
||||
from django.utils.html import format_html
|
||||
from django.utils.safestring import mark_safe
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
|
||||
|
@ -37,17 +36,19 @@ class MiscServiceAdmin(ExtendedModelAdmin):
|
|||
prepopulated_fields = {'name': ('verbose_name',)}
|
||||
change_readonly_fields = ('name',)
|
||||
actions = (disable, enable)
|
||||
|
||||
|
||||
def display_name(self, misc):
|
||||
return format_html('<span title="{}">{}</span>', misc.description, misc.name)
|
||||
return '<span title="%s">%s</span>' % (misc.description, misc.name)
|
||||
display_name.short_description = _("name")
|
||||
display_name.allow_tags = True
|
||||
display_name.admin_order_field = 'name'
|
||||
|
||||
|
||||
def display_verbose_name(self, misc):
|
||||
return format_html('<span title="{}">{}</span>', misc.description, misc.verbose_name)
|
||||
return '<span title="%s">%s</span>' % (misc.description, misc.verbose_name)
|
||||
display_verbose_name.short_description = _("verbose name")
|
||||
display_verbose_name.allow_tags = True
|
||||
display_verbose_name.admin_order_field = 'verbose_name'
|
||||
|
||||
|
||||
def num_instances(self, misc):
|
||||
""" return num slivers as a link to slivers changelist view """
|
||||
num = misc.instances__count
|
||||
|
@ -56,11 +57,11 @@ class MiscServiceAdmin(ExtendedModelAdmin):
|
|||
return mark_safe('<a href="{0}">{1}</a>'.format(url, num))
|
||||
num_instances.short_description = _("Instances")
|
||||
num_instances.admin_order_field = 'instances__count'
|
||||
|
||||
|
||||
def get_queryset(self, request):
|
||||
qs = super(MiscServiceAdmin, self).get_queryset(request)
|
||||
return qs.annotate(models.Count('instances', distinct=True))
|
||||
|
||||
|
||||
def formfield_for_dbfield(self, db_field, **kwargs):
|
||||
""" Make value input widget bigger """
|
||||
if db_field.name == 'description':
|
||||
|
@ -82,21 +83,21 @@ class MiscellaneousAdmin(SelectPluginAdminMixin, AccountAdminMixin, ExtendedMode
|
|||
actions = (disable, enable)
|
||||
plugin_field = 'service'
|
||||
plugin = MiscServicePlugin
|
||||
|
||||
|
||||
service_link = admin_link('service')
|
||||
|
||||
|
||||
def dispaly_active(self, instance):
|
||||
return instance.active
|
||||
dispaly_active.short_description = _("Active")
|
||||
dispaly_active.boolean = True
|
||||
dispaly_active.admin_order_field = 'is_active'
|
||||
|
||||
|
||||
def get_service(self, obj):
|
||||
if obj is None:
|
||||
return self.plugin.get(self.plugin_value).related_instance
|
||||
else:
|
||||
return obj.service
|
||||
|
||||
|
||||
def get_fieldsets(self, request, obj=None):
|
||||
fieldsets = super().get_fieldsets(request, obj)
|
||||
fields = list(fieldsets[0][1]['fields'])
|
||||
|
@ -109,7 +110,7 @@ class MiscellaneousAdmin(SelectPluginAdminMixin, AccountAdminMixin, ExtendedMode
|
|||
fields.insert(2, 'identifier')
|
||||
fieldsets[0][1]['fields'] = fields
|
||||
return fieldsets
|
||||
|
||||
|
||||
def get_form(self, request, obj=None, **kwargs):
|
||||
if obj:
|
||||
plugin = self.plugin.get(obj.service.name)()
|
||||
|
@ -126,16 +127,16 @@ class MiscellaneousAdmin(SelectPluginAdminMixin, AccountAdminMixin, ExtendedMode
|
|||
validator = import_class(validator_path)
|
||||
validator(identifier)
|
||||
return identifier
|
||||
|
||||
|
||||
form.clean_identifier = clean_identifier
|
||||
return form
|
||||
|
||||
|
||||
def formfield_for_dbfield(self, db_field, **kwargs):
|
||||
""" Make value input widget bigger """
|
||||
if db_field.name == 'description':
|
||||
kwargs['widget'] = forms.Textarea(attrs={'cols': 70, 'rows': 4})
|
||||
return super(MiscellaneousAdmin, self).formfield_for_dbfield(db_field, **kwargs)
|
||||
|
||||
|
||||
def save_model(self, request, obj, form, change):
|
||||
if not change:
|
||||
plugin = self.plugin
|
||||
|
|
|
@ -2,7 +2,6 @@
|
|||
from __future__ import unicode_literals
|
||||
|
||||
from django.db import models, migrations
|
||||
import django.db.models.deletion
|
||||
import orchestra.core.validators
|
||||
from django.conf import settings
|
||||
import orchestra.models.fields
|
||||
|
@ -23,7 +22,7 @@ class Migration(migrations.Migration):
|
|||
('description', models.TextField(blank=True, verbose_name='description')),
|
||||
('amount', models.PositiveIntegerField(default=1, verbose_name='amount')),
|
||||
('is_active', models.BooleanField(default=True, help_text='Designates whether this service should be treated as active. Unselect this instead of deleting services.', verbose_name='active')),
|
||||
('account', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='miscellaneous', verbose_name='account', to=settings.AUTH_USER_MODEL)),
|
||||
('account', models.ForeignKey(related_name='miscellaneous', verbose_name='account', to=settings.AUTH_USER_MODEL)),
|
||||
],
|
||||
options={
|
||||
'verbose_name_plural': 'miscellaneous',
|
||||
|
@ -44,6 +43,6 @@ class Migration(migrations.Migration):
|
|||
migrations.AddField(
|
||||
model_name='miscellaneous',
|
||||
name='service',
|
||||
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='instances', verbose_name='service', to='miscellaneous.MiscService'),
|
||||
field=models.ForeignKey(related_name='instances', verbose_name='service', to='miscellaneous.MiscService'),
|
||||
),
|
||||
]
|
||||
|
|
|
@ -1,59 +0,0 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
# Generated by Django 1.10.5 on 2021-04-22 11:28
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.conf import settings
|
||||
from django.db import migrations, models
|
||||
import django.db.models.deletion
|
||||
import orchestra.core.validators
|
||||
import orchestra.models.fields
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
replaces = [('miscellaneous', '0001_initial'), ('miscellaneous', '0002_auto_20150723_1252')]
|
||||
|
||||
initial = True
|
||||
|
||||
dependencies = [
|
||||
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name='Miscellaneous',
|
||||
fields=[
|
||||
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('identifier', orchestra.models.fields.NullableCharField(help_text='A unique identifier for this service.', max_length=256, null=True, unique=True, verbose_name='identifier')),
|
||||
('description', models.TextField(blank=True, verbose_name='description')),
|
||||
('amount', models.PositiveIntegerField(default=1, verbose_name='amount')),
|
||||
('is_active', models.BooleanField(default=True, help_text='Designates whether this service should be treated as active. Unselect this instead of deleting services.', verbose_name='active')),
|
||||
('account', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='miscellaneous', to=settings.AUTH_USER_MODEL, verbose_name='account')),
|
||||
],
|
||||
options={
|
||||
'verbose_name_plural': 'miscellaneous',
|
||||
},
|
||||
),
|
||||
migrations.CreateModel(
|
||||
name='MiscService',
|
||||
fields=[
|
||||
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('name', models.CharField(help_text='Raw name used for internal referenciation, i.e. service match definition', max_length=32, unique=True, validators=[orchestra.core.validators.validate_name], verbose_name='name')),
|
||||
('verbose_name', models.CharField(blank=True, help_text='Human readable name', max_length=256, verbose_name='verbose name')),
|
||||
('description', models.TextField(blank=True, help_text='Optional description', verbose_name='description')),
|
||||
('has_identifier', models.BooleanField(default=True, help_text='Designates if this service has a <b>unique text</b> field that identifies it or not.', verbose_name='has identifier')),
|
||||
('has_amount', models.BooleanField(default=False, help_text='Designates whether this service has <tt>amount</tt> property or not.', verbose_name='has amount')),
|
||||
('is_active', models.BooleanField(default=True, help_text='Whether new instances of this service can be created or not. Unselect this instead of deleting services.', verbose_name='active')),
|
||||
],
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='miscellaneous',
|
||||
name='service',
|
||||
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='instances', to='miscellaneous.MiscService', verbose_name='service'),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='miscellaneous',
|
||||
name='identifier',
|
||||
field=orchestra.models.fields.NullableCharField(db_index=True, help_text='A unique identifier for this service.', max_length=256, null=True, unique=True, verbose_name='identifier'),
|
||||
),
|
||||
]
|
|
@ -22,30 +22,30 @@ class MiscService(models.Model):
|
|||
is_active = models.BooleanField(_("active"), default=True,
|
||||
help_text=_("Whether new instances of this service can be created "
|
||||
"or not. Unselect this instead of deleting services."))
|
||||
|
||||
|
||||
def __str__(self):
|
||||
return self.name
|
||||
|
||||
|
||||
def clean(self):
|
||||
self.verbose_name = self.verbose_name.strip()
|
||||
|
||||
|
||||
def get_verbose_name(self):
|
||||
return self.verbose_name or self.name
|
||||
|
||||
|
||||
def disable(self):
|
||||
self.is_active = False
|
||||
self.save(update_fields=('is_active',))
|
||||
|
||||
|
||||
def enable(self):
|
||||
self.is_active = False
|
||||
self.save(update_fields=('is_active',))
|
||||
|
||||
|
||||
class Miscellaneous(models.Model):
|
||||
service = models.ForeignKey(MiscService, on_delete=models.CASCADE,
|
||||
verbose_name=_("service"), related_name='instances')
|
||||
account = models.ForeignKey('accounts.Account', on_delete=models.CASCADE,
|
||||
verbose_name=_("account"), related_name='miscellaneous')
|
||||
service = models.ForeignKey(MiscService, verbose_name=_("service"),
|
||||
related_name='instances')
|
||||
account = models.ForeignKey('accounts.Account', verbose_name=_("account"),
|
||||
related_name='miscellaneous')
|
||||
identifier = NullableCharField(_("identifier"), max_length=256, null=True, unique=True,
|
||||
db_index=True, help_text=_("A unique identifier for this service."))
|
||||
description = models.TextField(_("description"), blank=True)
|
||||
|
@ -53,32 +53,32 @@ class Miscellaneous(models.Model):
|
|||
is_active = models.BooleanField(_("active"), default=True,
|
||||
help_text=_("Designates whether this service should be treated as "
|
||||
"active. Unselect this instead of deleting services."))
|
||||
|
||||
|
||||
class Meta:
|
||||
verbose_name_plural = _("miscellaneous")
|
||||
|
||||
|
||||
def __str__(self):
|
||||
return self.identifier or self.description[:32] or str(self.service)
|
||||
|
||||
|
||||
@cached_property
|
||||
def active(self):
|
||||
return self.is_active and self.service.is_active and self.account.is_active
|
||||
|
||||
|
||||
def get_description(self):
|
||||
return ' '.join((str(self.amount), self.service.description or self.service.verbose_name))
|
||||
|
||||
|
||||
def disable(self):
|
||||
self.is_active = False
|
||||
self.save(update_fields=('is_active',))
|
||||
|
||||
|
||||
def enable(self):
|
||||
self.is_active = False
|
||||
self.save(update_fields=('is_active',))
|
||||
|
||||
|
||||
@cached_property
|
||||
def service_class(self):
|
||||
return self.service
|
||||
|
||||
|
||||
def clean(self):
|
||||
if self.identifier:
|
||||
self.identifier = self.identifier.strip().lower()
|
||||
|
|
|
@ -15,21 +15,21 @@ class Operation():
|
|||
MONITOR = 'monitor'
|
||||
EXCEEDED = 'exceeded'
|
||||
RECOVERY = 'recovery'
|
||||
|
||||
|
||||
def __str__(self):
|
||||
return '%s.%s(%s)' % (self.backend, self.action, self.instance)
|
||||
|
||||
|
||||
def __repr__(self):
|
||||
return str(self)
|
||||
|
||||
|
||||
def __hash__(self):
|
||||
""" set() """
|
||||
return hash((self.backend, self.instance, self.action))
|
||||
|
||||
|
||||
def __eq__(self, operation):
|
||||
""" set() """
|
||||
return hash(self) == hash(operation)
|
||||
|
||||
|
||||
def __init__(self, backend, instance, action, routes=None):
|
||||
self.backend = backend
|
||||
# instance should maintain any dynamic attribute until backend execution
|
||||
|
@ -37,13 +37,13 @@ class Operation():
|
|||
self.instance = copy.deepcopy(instance)
|
||||
self.action = action
|
||||
self.routes = routes
|
||||
|
||||
|
||||
@classmethod
|
||||
def execute(cls, operations, serialize=False, run_async=None):
|
||||
def execute(cls, operations, serialize=False, async=None):
|
||||
from . import manager
|
||||
scripts, backend_serialize = manager.generate(operations)
|
||||
return manager.execute(scripts, serialize=(serialize or backend_serialize), run_async=run_async)
|
||||
|
||||
return manager.execute(scripts, serialize=(serialize or backend_serialize), async=async)
|
||||
|
||||
@classmethod
|
||||
def create_for_action(cls, instances, action):
|
||||
if not isinstance(instances, collections.Iterable):
|
||||
|
@ -56,13 +56,13 @@ class Operation():
|
|||
cls(backend_cls, instance, action)
|
||||
)
|
||||
return operations
|
||||
|
||||
|
||||
@classmethod
|
||||
def execute_action(cls, instances, action):
|
||||
""" instances can be an object or an iterable for batch processing """
|
||||
operations = cls.create_for_action(instances, action)
|
||||
return cls.execute(operations)
|
||||
|
||||
|
||||
def preload_context(self):
|
||||
"""
|
||||
Heuristic: Running get_context will prevent most of related objects do not exist errors
|
||||
|
@ -70,7 +70,7 @@ class Operation():
|
|||
if self.action == self.DELETE:
|
||||
if hasattr(self.backend, 'get_context'):
|
||||
self.backend().get_context(self.instance)
|
||||
|
||||
|
||||
def store(self, log):
|
||||
from .models import BackendOperation
|
||||
return BackendOperation.objects.create(
|
||||
|
@ -79,7 +79,7 @@ class Operation():
|
|||
instance=self.instance,
|
||||
action=self.action,
|
||||
)
|
||||
|
||||
|
||||
@classmethod
|
||||
def load(cls, operation, log=None):
|
||||
routes = None
|
||||
|
@ -88,4 +88,4 @@ class Operation():
|
|||
(operation.backend, operation.action): AttrDict(host=log.server)
|
||||
}
|
||||
return cls(operation.backend_class, operation.instance, operation.action, routes=routes)
|
||||
|
||||
|
||||
|
|
|
@ -30,40 +30,41 @@ STATE_COLORS = {
|
|||
|
||||
class RouteAdmin(ExtendedModelAdmin):
|
||||
list_display = (
|
||||
'display_backend', 'host', 'match', 'display_model', 'display_actions', 'run_async',
|
||||
'display_backend', 'host', 'match', 'display_model', 'display_actions', 'async',
|
||||
'is_active'
|
||||
)
|
||||
list_editable = ('host', 'match', 'run_async', 'is_active')
|
||||
list_filter = ('host', 'is_active', 'run_async', 'backend')
|
||||
list_editable = ('host', 'match', 'async', 'is_active')
|
||||
list_filter = ('host', 'is_active', 'async', 'backend')
|
||||
list_prefetch_related = ('host',)
|
||||
ordering = ('backend',)
|
||||
add_fields = ('backend', 'host', 'match', 'run_async', 'is_active')
|
||||
add_fields = ('backend', 'host', 'match', 'async', 'is_active')
|
||||
change_form = RouteForm
|
||||
actions = (orchestrate,)
|
||||
change_view_actions = actions
|
||||
|
||||
|
||||
BACKEND_HELP_TEXT = helpers.get_backends_help_text(ServiceBackend.get_backends())
|
||||
DEFAULT_MATCH = {
|
||||
backend.get_name(): backend.default_route_match for backend in ServiceBackend.get_backends()
|
||||
}
|
||||
|
||||
|
||||
display_backend = display_plugin_field('backend')
|
||||
|
||||
|
||||
def display_model(self, route):
|
||||
try:
|
||||
return route.backend_class.model
|
||||
return escape(route.backend_class.model)
|
||||
except KeyError:
|
||||
return mark_safe("<span style='color: red;'>NOT AVAILABLE</span>")
|
||||
return "<span style='color: red;'>NOT AVAILABLE</span>"
|
||||
display_model.short_description = _("model")
|
||||
|
||||
@mark_safe
|
||||
display_model.allow_tags = True
|
||||
|
||||
def display_actions(self, route):
|
||||
try:
|
||||
return '<br>'.join(route.backend_class.get_actions())
|
||||
except KeyError:
|
||||
return "<span style='color: red;'>NOT AVAILABLE</span>"
|
||||
display_actions.short_description = _("actions")
|
||||
|
||||
display_actions.allow_tags = True
|
||||
|
||||
def formfield_for_dbfield(self, db_field, **kwargs):
|
||||
""" Provides dynamic help text on backend form field """
|
||||
if db_field.name == 'backend':
|
||||
|
@ -78,23 +79,23 @@ class RouteAdmin(ExtendedModelAdmin):
|
|||
request._host_choices_cache = choices = list(field.choices)
|
||||
field.choices = choices
|
||||
return field
|
||||
|
||||
|
||||
def get_form(self, request, obj=None, **kwargs):
|
||||
""" Include dynamic help text for existing objects """
|
||||
form = super(RouteAdmin, self).get_form(request, obj, **kwargs)
|
||||
if obj:
|
||||
form.base_fields['backend'].help_text = self.BACKEND_HELP_TEXT.get(obj.backend, '')
|
||||
return form
|
||||
|
||||
|
||||
def show_orchestration_disabled(self, request):
|
||||
if settings.ORCHESTRATION_DISABLE_EXECUTION:
|
||||
msg = _("Orchestration execution is disabled by <tt>ORCHESTRATION_DISABLE_EXECUTION</tt> setting.")
|
||||
self.message_user(request, mark_safe(msg), messages.WARNING)
|
||||
|
||||
|
||||
def changelist_view(self, request, extra_context=None):
|
||||
self.show_orchestration_disabled(request)
|
||||
return super(RouteAdmin, self).changelist_view(request, extra_context)
|
||||
|
||||
|
||||
def changeform_view(self, request, object_id=None, form_url='', extra_context=None):
|
||||
self.show_orchestration_disabled(request)
|
||||
return super(RouteAdmin, self).changeform_view(
|
||||
|
@ -107,23 +108,24 @@ class BackendOperationInline(admin.TabularInline):
|
|||
readonly_fields = ('action', 'instance_link')
|
||||
extra = 0
|
||||
can_delete = False
|
||||
|
||||
|
||||
class Media:
|
||||
css = {
|
||||
'all': ('orchestra/css/hide-inline-id.css',)
|
||||
}
|
||||
|
||||
|
||||
def instance_link(self, operation):
|
||||
link = admin_link('instance')(self, operation)
|
||||
if link == '---':
|
||||
return _("Deleted {0}").format(operation.instance_repr or '-'.join(
|
||||
(escape(operation.content_type), escape(operation.object_id))))
|
||||
return link
|
||||
instance_link.allow_tags = True
|
||||
instance_link.short_description = _("Instance")
|
||||
|
||||
|
||||
def has_add_permission(self, *args, **kwargs):
|
||||
return False
|
||||
|
||||
|
||||
def get_queryset(self, request):
|
||||
queryset = super(BackendOperationInline, self).get_queryset(request)
|
||||
return queryset.prefetch_related('instance')
|
||||
|
@ -147,7 +149,7 @@ class BackendLogAdmin(ChangeViewActionsMixin, admin.ModelAdmin):
|
|||
readonly_fields = fields
|
||||
actions = (retry_backend,)
|
||||
change_view_actions = actions
|
||||
|
||||
|
||||
server_link = admin_link('server')
|
||||
display_created = admin_date('created_at', short_description=_("Created"))
|
||||
display_state = admin_colored('state', colors=STATE_COLORS)
|
||||
|
@ -155,17 +157,17 @@ class BackendLogAdmin(ChangeViewActionsMixin, admin.ModelAdmin):
|
|||
mono_stdout = display_mono('stdout')
|
||||
mono_stderr = display_mono('stderr')
|
||||
mono_traceback = display_mono('traceback')
|
||||
|
||||
|
||||
class Media:
|
||||
css = {
|
||||
'all': ('orchestra/css/pygments/github.css',)
|
||||
}
|
||||
|
||||
|
||||
def get_queryset(self, request):
|
||||
""" Order by structured name and imporve performance """
|
||||
qs = super(BackendLogAdmin, self).get_queryset(request)
|
||||
return qs.select_related('server').defer('script', 'stdout')
|
||||
|
||||
|
||||
def has_add_permission(self, *args, **kwargs):
|
||||
return False
|
||||
|
||||
|
@ -175,15 +177,17 @@ class ServerAdmin(ExtendedModelAdmin):
|
|||
list_filter = ('os',)
|
||||
actions = (orchestrate,)
|
||||
change_view_actions = actions
|
||||
|
||||
|
||||
def display_ping(self, instance):
|
||||
return mark_safe(self._remote_state[instance.pk][0])
|
||||
return self._remote_state[instance.pk][0]
|
||||
display_ping.short_description = _("Ping")
|
||||
|
||||
display_ping.allow_tags = True
|
||||
|
||||
def display_uptime(self, instance):
|
||||
return mark_safe(self._remote_state[instance.pk][1])
|
||||
return self._remote_state[instance.pk][1]
|
||||
display_uptime.short_description = _("Uptime")
|
||||
|
||||
display_uptime.allow_tags = True
|
||||
|
||||
def get_queryset(self, request):
|
||||
""" Order by structured name and imporve performance """
|
||||
qs = super(ServerAdmin, self).get_queryset(request)
|
||||
|
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue