diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ef83750 --- /dev/null +++ b/.gitignore @@ -0,0 +1,14 @@ +# tools, IDEs, build folders +/.coverage/ +/.eggs/ +/.idea/ +/build/ +/dist/ +/docs/build/ +/*.egg-info/ +settings.json + +# Django and Python +*.py[cod] +.pytest_cache/* +.mypy_cache diff --git a/.helm/templates/02-deployment.yaml b/.helm/templates/02-deployment.yaml deleted file mode 100644 index c8b1de5..0000000 --- a/.helm/templates/02-deployment.yaml +++ /dev/null @@ -1,37 +0,0 @@ ---- -apiVersion: apps/v1 -kind: Deployment -metadata: - name: {{ .Chart.Name }} - namespace: {{ .Chart.Name }}-{{ .Values.global.env }} - -spec: - replicas: 1 - selector: - matchLabels: - app: {{ .Chart.Name }} - template: - metadata: - labels: - app: {{ .Chart.Name }} - spec: - containers: - - name: {{ .Chart.Name }} - image: {{ .Values.DockerImage }} - ports: - - containerPort: 8888 - env: - - name: DATABASE_HOST - value: "db-postgresql" - - name: DATABASE_USER - value: "postgres" - - name: DATABASE_PASSWORD - value: {{.Values.DBPwd | required "DBPwd is required" }} - resources: - limits: - cpu: 100m - memory: 150Mi - requests: - cpu: 100m - memory: 150Mi - diff --git a/.helm/templates/03-service.yaml b/.helm/templates/03-service.yaml deleted file mode 100644 index 18c0cb1..0000000 --- a/.helm/templates/03-service.yaml +++ /dev/null @@ -1,15 +0,0 @@ ---- -apiVersion: v1 -kind: Service -metadata: - name: {{ .Chart.Name }}-service - namespace: {{ .Chart.Name }}-{{ .Values.global.env }} - -spec: - type: ClusterIP - ports: - - name: http - port: 8888 - targetPort: 8888 - selector: - app: {{ .Chart.Name }} diff --git a/.helm/templates/04-ingress.yaml b/.helm/templates/04-ingress.yaml deleted file mode 100644 index 77e28ac..0000000 --- a/.helm/templates/04-ingress.yaml +++ /dev/null @@ -1,16 +0,0 @@ -apiVersion: networking.k8s.io/v1beta1 -kind: Ingress -metadata: - name: ingress-{{ .Chart.Name }} - namespace: {{ .Chart.Name }}-{{ .Values.global.env }} - - annotations: - kubernetes.io/ingress.class: "nginx" -spec: - rules: - - http: - paths: - - path: / - backend: - serviceName: {{ .Chart.Name }}-service - servicePort: 8888 diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..0f581d6 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,27 @@ +sudo: true + +before_install: + - sudo apt-get update -qq + - sudo apt-get install -qq build-essential gettext python-dev zlib1g-dev libpq-dev xvfb libjpeg8-dev + - sudo apt-get install -qq python-setuptools python3-dev python-virtualenv python-pip + +addons: + postgresql: "9.6" + +install: + - "pip3 install pipenv" + - "pipenv install --dev" + - "pip3 install -e . --upgrade" + +language: python +python: + - "3.6" + +# Attempt to use cached versions of python deps +cache: pip + +cache: + directories: + - $HOME/.cache/pip + +script: pipenv run pytest -x -v diff --git a/Dockerfile b/Dockerfile deleted file mode 100644 index adc4433..0000000 --- a/Dockerfile +++ /dev/null @@ -1,15 +0,0 @@ -FROM python:3.6-alpine - -WORKDIR /app - -RUN apk add git gcc musl-dev postgresql-dev && \ - pip install pipenv && \ - git clone https://github.com/shacker/gtd.git /app && \ - pipenv --python 3.6 && \ - pipenv install --dev - -ADD local.py project/. -ADD entrypoint.sh . -RUN chmod +x entrypoint.sh - -CMD [ "./entrypoint.sh" ] diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..68eef42 --- /dev/null +++ b/LICENSE @@ -0,0 +1,27 @@ +Copyright (c) 2010, Scot Hacker, Birdhouse Arts and individual contributors. +All rights reserved. + +Redistribution and use in source and binary forms, with or without modification, +are permitted provided that the following conditions are met: + + 1. Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + 3. Neither the name of Birdhouse Arts nor the names of its contributors + may be used to endorse or promote products derived from this software + without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR +ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/MANIFEST.in b/MANIFEST.in new file mode 100644 index 0000000..a13f7f3 --- /dev/null +++ b/MANIFEST.in @@ -0,0 +1,4 @@ +include LICENSE +include README.rst +recursive-include todo/static * +recursive-include todo/templates * diff --git a/Pipfile b/Pipfile new file mode 100644 index 0000000..58131f2 --- /dev/null +++ b/Pipfile @@ -0,0 +1,23 @@ +[[source]] +url = "https://pypi.python.org/simple" +verify_ssl = true +name = "pypi" + +[packages] +django = "*" +django-extensions = "*" +"psycopg2-binary" = "*" +"flake8" = "*" +factory-boy = "*" +titlecase = "*" +bleach = "*" +django-autocomplete-light = "*" +html2text = "*" + +[dev-packages] +mypy = "*" +pytest = "*" +pytest-django = "*" + +[requires] +python_version = "3.6" diff --git a/Pipfile.lock b/Pipfile.lock new file mode 100644 index 0000000..63388b6 --- /dev/null +++ b/Pipfile.lock @@ -0,0 +1,289 @@ +{ + "_meta": { + "hash": { + "sha256": "c6fb601fc8a197ca280960d831a5386313c93ebe19d932afa01034d5520f2f94" + }, + "pipfile-spec": 6, + "requires": { + "python_version": "3.6" + }, + "sources": [ + { + "name": "pypi", + "url": "https://pypi.python.org/simple", + "verify_ssl": true + } + ] + }, + "default": { + "bleach": { + "hashes": [ + "sha256:213336e49e102af26d9cde77dd2d0397afabc5a6bf2fed985dc35b5d1e285a16", + "sha256:3fdf7f77adcf649c9911387df51254b813185e32b2c6619f690b593a617e19fa" + ], + "index": "pypi", + "version": "==3.1.0" + }, + "django": { + "hashes": [ + "sha256:275bec66fd2588dd517ada59b8bfb23d4a9abc5a362349139ddda3c7ff6f5ade", + "sha256:939652e9d34d7d53d74d5d8ef82a19e5f8bb2de75618f7e5360691b6e9667963" + ], + "index": "pypi", + "version": "==2.1.7" + }, + "django-autocomplete-light": { + "hashes": [ + "sha256:996cc62519a6e2e9cd1c26e57ddc5f14541209a93e62e83d7b3df3ba65c1f458" + ], + "index": "pypi", + "version": "==3.3.2" + }, + "django-extensions": { + "hashes": [ + "sha256:109004f80b6f45ad1f56addaa59debca91d94aa0dc1cb19678b9364b4fe9b6f4", + "sha256:307766e5e6c1caffe76c5d99239d8115d14ae3f7cab2cd991fcffd763dad904b" + ], + "index": "pypi", + "version": "==2.1.6" + }, + "entrypoints": { + "hashes": [ + "sha256:589f874b313739ad35be6e0cd7efde2a4e9b6fea91edcc34e58ecbb8dbe56d19", + "sha256:c70dd71abe5a8c85e55e12c19bd91ccfeec11a6e99044204511f9ed547d48451" + ], + "version": "==0.3" + }, + "factory-boy": { + "hashes": [ + "sha256:6f25cc4761ac109efd503f096e2ad99421b1159f01a29dbb917359dcd68e08ca", + "sha256:d552cb872b310ae78bd7429bf318e42e1e903b1a109e899a523293dfa762ea4f" + ], + "index": "pypi", + "version": "==2.11.1" + }, + "faker": { + "hashes": [ + "sha256:16342dca4d92bfc83bab6a7daf6650e0ab087605a66bc38f17523fdb01757910", + "sha256:d871ea315b2dcba9138b8344f2c131a76ac62d6227ca39f69b0c889fec97376c" + ], + "version": "==1.0.2" + }, + "flake8": { + "hashes": [ + "sha256:859996073f341f2670741b51ec1e67a01da142831aa1fdc6242dbf88dffbe661", + "sha256:a796a115208f5c03b18f332f7c11729812c8c3ded6c46319c59b53efd3819da8" + ], + "index": "pypi", + "version": "==3.7.7" + }, + "html2text": { + "hashes": [ + "sha256:490db40fe5b2cd79c461cf56be4d39eb8ca68191ae41ba3ba79f6cb05b7dd662", + "sha256:627514fb30e7566b37be6900df26c2c78a030cc9e6211bda604d8181233bcdd4" + ], + "index": "pypi", + "version": "==2018.1.9" + }, + "mccabe": { + "hashes": [ + "sha256:ab8a6258860da4b6677da4bd2fe5dc2c659cff31b3ee4f7f5d64e79735b80d42", + "sha256:dd8d182285a0fe56bace7f45b5e7d1a6ebcbf524e8f3bd87eb0f125271b8831f" + ], + "version": "==0.6.1" + }, + "psycopg2-binary": { + "hashes": [ + "sha256:19a2d1f3567b30f6c2bb3baea23f74f69d51f0c06c2e2082d0d9c28b0733a4c2", + "sha256:2b69cf4b0fa2716fd977aa4e1fd39af6110eb47b2bb30b4e5a469d8fbecfc102", + "sha256:2e952fa17ba48cbc2dc063ddeec37d7dc4ea0ef7db0ac1eda8906365a8543f31", + "sha256:348b49dd737ff74cfb5e663e18cb069b44c64f77ec0523b5794efafbfa7df0b8", + "sha256:3d72a5fdc5f00ca85160915eb9a973cf9a0ab8148f6eda40708bf672c55ac1d1", + "sha256:4957452f7868f43f32c090dadb4188e9c74a4687323c87a882e943c2bd4780c3", + "sha256:5138cec2ee1e53a671e11cc519505eb08aaaaf390c508f25b09605763d48de4b", + "sha256:587098ca4fc46c95736459d171102336af12f0d415b3b865972a79c03f06259f", + "sha256:5b79368bcdb1da4a05f931b62760bea0955ee2c81531d8e84625df2defd3f709", + "sha256:5cf43807392247d9bc99737160da32d3fa619e0bfd85ba24d1c78db205f472a4", + "sha256:676d1a80b1eebc0cacae8dd09b2fde24213173bf65650d22b038c5ed4039f392", + "sha256:6b0211ecda389101a7d1d3df2eba0cf7ffbdd2480ca6f1d2257c7bd739e84110", + "sha256:79cde4660de6f0bb523c229763bd8ad9a93ac6760b72c369cf1213955c430934", + "sha256:7aba9786ac32c2a6d5fb446002ed936b47d5e1f10c466ef7e48f66eb9f9ebe3b", + "sha256:7c8159352244e11bdd422226aa17651110b600d175220c451a9acf795e7414e0", + "sha256:945f2eedf4fc6b2432697eb90bb98cc467de5147869e57405bfc31fa0b824741", + "sha256:96b4e902cde37a7fc6ab306b3ac089a3949e6ce3d824eeca5b19dc0bedb9f6e2", + "sha256:9a7bccb1212e63f309eb9fab47b6eaef796f59850f169a25695b248ca1bf681b", + "sha256:a3bfcac727538ec11af304b5eccadbac952d4cca1a551a29b8fe554e3ad535dc", + "sha256:b19e9f1b85c5d6136f5a0549abdc55dcbd63aba18b4f10d0d063eb65ef2c68b4", + "sha256:b664011bb14ca1f2287c17185e222f2098f7b4c857961dbcf9badb28786dbbf4", + "sha256:bde7959ef012b628868d69c474ec4920252656d0800835ed999ba5e4f57e3e2e", + "sha256:cb095a0657d792c8de9f7c9a0452385a309dfb1bbbb3357d6b1e216353ade6ca", + "sha256:d16d42a1b9772152c1fe606f679b2316551f7e1a1ce273e7f808e82a136cdb3d", + "sha256:d444b1545430ffc1e7a24ce5a9be122ccd3b135a7b7e695c5862c5aff0b11159", + "sha256:d93ccc7bf409ec0a23f2ac70977507e0b8a8d8c54e5ee46109af2f0ec9e411f3", + "sha256:df6444f952ca849016902662e1a47abf4fa0678d75f92fd9dd27f20525f809cd", + "sha256:e63850d8c52ba2b502662bf3c02603175c2397a9acc756090e444ce49508d41e", + "sha256:ec43358c105794bc2b6fd34c68d27f92bea7102393c01889e93f4b6a70975728", + "sha256:f4c6926d9c03dadce7a3b378b40d2fea912c1344ef9b29869f984fb3d2a2420b" + ], + "index": "pypi", + "version": "==2.7.7" + }, + "pycodestyle": { + "hashes": [ + "sha256:95a2219d12372f05704562a14ec30bc76b05a5b297b21a5dfe3f6fac3491ae56", + "sha256:e40a936c9a450ad81df37f549d676d127b1b66000a6c500caa2b085bc0ca976c" + ], + "version": "==2.5.0" + }, + "pyflakes": { + "hashes": [ + "sha256:17dbeb2e3f4d772725c777fabc446d5634d1038f234e77343108ce445ea69ce0", + "sha256:d976835886f8c5b31d47970ed689944a0262b5f3afa00a5a7b4dc81e5449f8a2" + ], + "version": "==2.1.1" + }, + "python-dateutil": { + "hashes": [ + "sha256:7e6584c74aeed623791615e26efd690f29817a27c73085b78e4bad02493df2fb", + "sha256:c89805f6f4d64db21ed966fda138f8a5ed7a4fdbc1a8ee329ce1b74e3c74da9e" + ], + "version": "==2.8.0" + }, + "pytz": { + "hashes": [ + "sha256:32b0891edff07e28efe91284ed9c31e123d84bea3fd98e1f72be2508f43ef8d9", + "sha256:d5f05e487007e29e03409f9398d074e158d920d36eb82eaf66fb1136b0c5374c" + ], + "version": "==2018.9" + }, + "six": { + "hashes": [ + "sha256:3350809f0555b11f552448330d0b52d5f24c91a322ea4a15ef22629740f3761c", + "sha256:d16a0141ec1a18405cd4ce8b4613101da75da0e9a7aec5bdd4fa804d0e0eba73" + ], + "version": "==1.12.0" + }, + "text-unidecode": { + "hashes": [ + "sha256:5a1375bb2ba7968740508ae38d92e1f889a0832913cb1c447d5e2046061a396d", + "sha256:801e38bd550b943563660a91de8d4b6fa5df60a542be9093f7abf819f86050cc" + ], + "version": "==1.2" + }, + "titlecase": { + "hashes": [ + "sha256:84de7a97fb702c400e5ba11c6b30849944b39db12e20fbf4515a23c7538a0611", + "sha256:95d643a0c08097c02933aced707adfe1c275c335019e8e514dea782a465c5b84" + ], + "index": "pypi", + "version": "==0.12.0" + }, + "webencodings": { + "hashes": [ + "sha256:a0af1213f3c2226497a97e2b3aa01a7e4bee4f403f95be16fc9acd2947514a78", + "sha256:b36a1c245f2d304965eb4e0a82848379241dc04b865afcc4aab16748587e1923" + ], + "version": "==0.5.1" + } + }, + "develop": { + "atomicwrites": { + "hashes": [ + "sha256:03472c30eb2c5d1ba9227e4c2ca66ab8287fbfbbda3888aa93dc2e28fc6811b4", + "sha256:75a9445bac02d8d058d5e1fe689654ba5a6556a1dfd8ce6ec55a0ed79866cfa6" + ], + "version": "==1.3.0" + }, + "attrs": { + "hashes": [ + "sha256:69c0dbf2ed392de1cb5ec704444b08a5ef81680a61cb899dc08127123af36a79", + "sha256:f0b870f674851ecbfbbbd364d6b5cbdff9dcedbc7f3f5e18a6891057f21fe399" + ], + "version": "==19.1.0" + }, + "more-itertools": { + "hashes": [ + "sha256:0125e8f60e9e031347105eb1682cef932f5e97d7b9a1a28d9bf00c22a5daef40", + "sha256:590044e3942351a1bdb1de960b739ff4ce277960f2425ad4509446dbace8d9d1" + ], + "markers": "python_version > '2.7'", + "version": "==6.0.0" + }, + "mypy": { + "hashes": [ + "sha256:308c274eb8482fbf16006f549137ddc0d69e5a589465e37b99c4564414363ca7", + "sha256:e80fd6af34614a0e898a57f14296d0dacb584648f0339c2e000ddbf0f4cc2f8d" + ], + "index": "pypi", + "version": "==0.670" + }, + "mypy-extensions": { + "hashes": [ + "sha256:37e0e956f41369209a3d5f34580150bcacfabaa57b33a15c0b25f4b5725e0812", + "sha256:b16cabe759f55e3409a7d231ebd2841378fb0c27a5d1994719e340e4f429ac3e" + ], + "version": "==0.4.1" + }, + "pluggy": { + "hashes": [ + "sha256:19ecf9ce9db2fce065a7a0586e07cfb4ac8614fe96edf628a264b1c70116cf8f", + "sha256:84d306a647cc805219916e62aab89caa97a33a1dd8c342e87a37f91073cd4746" + ], + "version": "==0.9.0" + }, + "py": { + "hashes": [ + "sha256:64f65755aee5b381cea27766a3a147c3f15b9b6b9ac88676de66ba2ae36793fa", + "sha256:dc639b046a6e2cff5bbe40194ad65936d6ba360b52b3c3fe1d08a82dd50b5e53" + ], + "version": "==1.8.0" + }, + "pytest": { + "hashes": [ + "sha256:067a1d4bf827ffdd56ad21bd46674703fce77c5957f6c1eef731f6146bfcef1c", + "sha256:9687049d53695ad45cf5fdc7bbd51f0c49f1ea3ecfc4b7f3fde7501b541f17f4" + ], + "index": "pypi", + "version": "==4.3.0" + }, + "pytest-django": { + "hashes": [ + "sha256:30d773f1768e8f214a3106f1090e00300ce6edfcac8c55fd13b675fe1cbd1c85", + "sha256:4d3283e774fe1d40630ee58bf34929b83875e4751b525eeb07a7506996eb42ee" + ], + "index": "pypi", + "version": "==3.4.8" + }, + "six": { + "hashes": [ + "sha256:3350809f0555b11f552448330d0b52d5f24c91a322ea4a15ef22629740f3761c", + "sha256:d16a0141ec1a18405cd4ce8b4613101da75da0e9a7aec5bdd4fa804d0e0eba73" + ], + "version": "==1.12.0" + }, + "typed-ast": { + "hashes": [ + "sha256:035a54ede6ce1380599b2ce57844c6554666522e376bd111eb940fbc7c3dad23", + "sha256:037c35f2741ce3a9ac0d55abfcd119133cbd821fffa4461397718287092d9d15", + "sha256:049feae7e9f180b64efacbdc36b3af64a00393a47be22fa9cb6794e68d4e73d3", + "sha256:19228f7940beafc1ba21a6e8e070e0b0bfd1457902a3a81709762b8b9039b88d", + "sha256:2ea681e91e3550a30c2265d2916f40a5f5d89b59469a20f3bad7d07adee0f7a6", + "sha256:3a6b0a78af298d82323660df5497bcea0f0a4a25a0b003afd0ce5af049bd1f60", + "sha256:5385da8f3b801014504df0852bf83524599df890387a3c2b17b7caa3d78b1773", + "sha256:606d8afa07eef77280c2bf84335e24390055b478392e1975f96286d99d0cb424", + "sha256:69245b5b23bbf7fb242c9f8f08493e9ecd7711f063259aefffaeb90595d62287", + "sha256:6f6d839ab09830d59b7fa8fb6917023d8cb5498ee1f1dbd82d37db78eb76bc99", + "sha256:730888475f5ac0e37c1de4bd05eeb799fdb742697867f524dc8a4cd74bcecc23", + "sha256:9819b5162ffc121b9e334923c685b0d0826154e41dfe70b2ede2ce29034c71d8", + "sha256:9e60ef9426efab601dd9aa120e4ff560f4461cf8442e9c0a2b92548d52800699", + "sha256:af5fbdde0690c7da68e841d7fc2632345d570768ea7406a9434446d7b33b0ee1", + "sha256:b64efdbdf3bbb1377562c179f167f3bf301251411eb5ac77dec6b7d32bcda463", + "sha256:bac5f444c118aeb456fac1b0b5d14c6a71ea2a42069b09c176f75e9bd4c186f6", + "sha256:bda9068aafb73859491e13b99b682bd299c1b5fd50644d697533775828a28ee0", + "sha256:d659517ca116e6750101a1326107d3479028c5191f0ecee3c7203c50f5b915b0", + "sha256:eddd3fb1f3e0f82e5915a899285a39ee34ce18fd25d89582bc89fc9fb16cd2c6" + ], + "version": "==1.3.1" + } + } +} diff --git a/README.md b/README.md index 4d85b72..472d5e7 100644 --- a/README.md +++ b/README.md @@ -1,28 +1,392 @@ -# [WiP] PoC of DevOps magic +# django-todo -### Pre requires: -1. Any x86_64 Linux distribution. Tested on Debian 10, but must work on any modern Linux system -2. Internet access -3. wget -4. bash/dash shell -5. GNU awk -5. KVM +django-todo is a pluggable, multi-user, multi-group task management and +assignment application for Django, designed to be dropped into an existing site as a reusable app. django-todo can be used as a personal to-do tracker, or a group task management system, or a ticketing system for organizations (or all of these at once!) -### Known bugs and limitation: -1. Automated Keymap selection not working in the bootstrap process. You must hit Enter key. -2. Swap automatically created in the bootstrap process, I can't switch it off now. As a result, the minimum image size must be 12Gb or more. -3. It's still dirty, most exceptions during bootstrap still not handled +**The best way to learn how django-todo works is to visit the live demo site at [django-todo.org](http://django-todo.org)!** -### For start adventures: -1. just clone this repo -2. go to the repo directory -3. and type `./runme.sh` in your shell +## Features -### What happened after you run this script: -1. We bootup in-userspace VM and starting the automated installation of Debian 9 with docker and common automation tools (git, ansible, helm, werf). -2. Bootstrapping one node Kubernates cluster with Amsible (LOL we invented minicube without minicube limitations) -3. Installing Nginx.org's Ingress with Helm -4. Installing docker-registry in our shiny Kube (we are lazy of course we use Helm for it) -5. Installing Postgress into Kube 0_o Also with Helm -6. ~~Deploying test service with Helm/Werf~~ -256. ~~PROFIT!!!1~~ +* Drag and drop task prioritization +* Email task notification +* Search +* Comments on tasks +* Public-facing submission form for tickets +* Mobile-friendly (work in progress) +* Separate view for My Tasks (across lists) +* Batch-import tasks via CSV +* Multiple file attachments per task (see settings) +* Integrated mail tracking (unify a task list with an email box) + + +## Requirements + +* Django 2.0+ +* Python 3.6+ +* jQuery (full version, not "slim", for drag/drop prioritization) +* Bootstrap (to work with provided templates, though you can override them) +* bleach (`pip install bleach`) +* django-autocomplete-light (optional, required for task merging) + +## Overview + +We assume that your organization has multiple groups of employees, each with multiple users (where actual users and groups map to Django Users and Groups). Users may belong to multiple groups, and each group can have multiple todo lists. + +You must have at least one Group set up in Django admin, and that group must have at least one User as a member. This is true even if you're the sole user of django-todo. + +Users can view and modify all to-do lists belonging to their group(s). Only users with `is_staff` can add or delete lists. + +Identical list names can exist in different groups, but not in the same group. + +Emails are generated to the assigned-to person when new tasks are created. + +File attachments of a few types are allowed on tasks by default. See settings to disable or to limit filetypes. If you are concerned about file sizes, limit them in your web server configuration (not currently handled separately by django-todo). + +Comment threads can be added to tasks. Each participant in a thread receives email when new comments are added. + +django-todo is auth-only. You must set up a login system and at least one group before deploying. + +All tasks are "created by" the current user and can optionally be "assigned to" a specific user. Unassigned tickets appear as belonging to "anyone" in the UI. + +django-todo v2 makes use of features only available in Django 2.0. It will not work in previous versions. v2 is only tested against Python 3.x -- no guarantees if running it against older versions. + +## Installation + +django-todo is a Django app, not a project site. It needs a site to live in. You can either install it into an existing Django project site, or clone the django-todo [demo site (GTD)](https://github.com/shacker/gtd). + +If using your own site, be sure you have jQuery and Bootstrap wired up and working. + +django-todo views that require it will insert additional CSS/JavaScript into page heads, so your project's base templates must include: + +```jinja +{% block extrahead %}{% endblock extrahead %} +{% block extra_js %}{% endblock extra_js %} +``` + +django-todo comes with its own `todo/base.html`, which extends your master `base.html`. All content lives inside of: + +`{% block content %}{% endblock %}` + +If you use some other name for your main content area, you'll need to override and alter the provided templates. + +All views are login-required. Therefore, you must have a working user authentication system. + +For email notifications to work, make sure your site/project is [set up to send email](https://docs.djangoproject.com/en/2.0/topics/email/). + +Make sure you've installed the Django "sites" framework and have specified the default site in settings, e.g. `SITE_ID = 1` + +Put django-todo/todo somewhere on your Python path, or install via pip: + + pip install django-todo + + +Add to your settings: + +``` +INSTALLED_APPS = ( + ... + 'todo', +) +``` + +Migrate in database tables: + +`python manage.py migrate todo` + +Add to your URL conf: + +`path('todo/', include('todo.urls', namespace="todo")),` + +Add links to your site's navigation system: + +``` +Todo Lists +My Tasks +``` + +django-todo makes use of the Django `messages` system. Make sure you have something like [this](https://docs.djangoproject.com/en/2.1/ref/contrib/messages/#displaying-messages) (link) in your `base.html`. + +Log in and access `/todo`! + +### Customizing Templates + +The provided templates are fairly bare-bones, and are meant as starting points only. Unlike previous versions of django-todo, they now ship as Bootstrap examples, but feel free to override them - there is no hard dependency on Bootstrap. To override a template, create a `todo` folder in your project's `templates` dir, then copy the template you want to override from django-todo source and into that dir. + +### Filing Public Tickets + +If you wish to use the public ticket-filing system, first create the list into which those tickets should be filed, then add its slug to `TODO_DEFAULT_LIST_SLUG` in settings (more on settings below). + +## Settings + +Optional configuration params, which can be added to your project settings: + +```python +# Restrict access to ALL todo lists/views to `is_staff` users. +# If False or unset, all users can see all views (but more granular permissions are still enforced +# within views, such as requiring staff for adding and deleting lists). +TODO_STAFF_ONLY = True + +# If you use the "public" ticket filing option, to whom should these tickets be assigned? +# Must be a valid username in your system. If unset, unassigned tickets go to "Anyone." +TODO_DEFAULT_ASSIGNEE = 'johndoe' + +# If you use the "public" ticket filing option, to which list should these tickets be saved? +# Defaults to first list found, which is probably not what you want! +TODO_DEFAULT_LIST_SLUG = 'tickets' + +# If you use the "public" ticket filing option, to which *named URL* should the user be +# redirected after submitting? (since they can't see the rest of the ticket system). +# Defaults to "/" +TODO_PUBLIC_SUBMIT_REDIRECT = 'dashboard' + +# Enable or disable file attachments on Tasks +# Optionally limit list of allowed filetypes +TODO_ALLOW_FILE_ATTACHMENTS = True +TODO_ALLOWED_FILE_ATTACHMENTS = [".jpg", ".gif", ".csv", ".pdf", ".zip"] + +# additionnal classes the comment body should hold +# adding "text-monospace" makes comment monospace +TODO_COMMENT_CLASSES = [] + +# The following two settings are relevant only if you want todo to track a support mailbox - +# see Mail Tracking below. +TODO_MAIL_BACKENDS +TODO_MAIL_TRACKERS +``` + +The current django-todo version number is available from the [todo package](https://github.com/shacker/django-todo/blob/master/todo/__init__.py): + + python -c "import todo; print(todo.__version__)" + +## Importing Tasks via CSV + +django-todo has the ability to batch-import ("upsert") tasks from a specifically formatted CSV spreadsheet. This ability is provided through both a management command and a web interface. + +**Management Command** + +`./manage.py import_csv -f /path/to/file.csv` + +**Web Importer** + +Link from your navigation to `{url "todo:import_csv"}`. Follow the resulting link for the CSV web upload view. + + +### CSV Formatting + +Copy `todo/data/import_example.csv` to another location on your system and edit in a spreadsheet or directly. + +**Do not edit the header row!** + +The first four columns: `'Title', 'Group', 'Task List', 'Created By'` are required -- all others are optional and should work pretty much exactly like manual task entry via the web UI. + +Note: Internally, Tasks are keyed to TaskLists, not to Groups (TaskLists are in Gruops). However, we request the Group in the CSV +because it's possible to have multiple TaskLists with the same name in different groups; i.e. we need it for namespacing and permissions. + + +### Import Rules + +Because data entered via CSV is not going through the same view permissions enforced in the rest of django-todo, and to simplify data dependency logic, and to pre-empt disagreements between django-todo users, the importer will *not* create new users, groups, or task lists. All users, groups, and task lists referenced in your CSV must already exist, and group memberships must be correct. + +Any validation error (e.g. unparse-able dates, incorrect group memberships) **will result in that row being skipped.** + +A report of rows upserted and rows skipped (with line numbers and reasons) is provided at the end of the run. + +### Upsert Logic + +For each valid row, we need to decide whether to create a new task or update an existing one. django-todo matches on the unique combination of the four required columns. If we find a task that matches those, we *update* the rest of the columns. In other words, if you import a CSV once, then edit the Assigned To for a task and import it again, the original task will be updated with a new assignee (and same for the other columns). + +Otherwise we create a new task. + + +## Mail Tracking + +What if you could turn django-todo into a shared mailbox? Django-todo includes an optional feature that allows emails +sent to a dedicated mailbox to be pushed into todo as new tasks, and responses to be added as comments on those tasks. +This allows support teams to work with a fully unified email + bug tracking system to avoid confusion over who's seen or +responded to what. + +To enable mail tracking, you need to: + + - Define an email backend for outgoing emails + - Define an email backend for incoming emails + - Start a worker, which will wait for new emails + +In settings: + +```python +from todo.mail.producers import imap_producer +from todo.mail.consumers import tracker_consumer +from todo.mail.delivery import smtp_backend, console_backend + +# email notifications configuration +# each task list can get its own delivery method +TODO_MAIL_BACKENDS = { + # mail-queue is the name of the task list, not the worker name + "mail-queue": smtp_backend( + host="smtp.example.com", + port=465, + use_ssl=True, + username="test@example.com", + password="foobar", + # used as the From field when sending notifications. + # a username might be prepended later on + from_address="test@example.com", + # additionnal headers + headers={} + ), +} + +# incoming mail worker configuration +TODO_MAIL_TRACKERS = { + # configuration for worker "test_tracker" + "test_tracker": { + "producer": imap_producer( + host="imap.example.com", + username="text@example.com", + password="foobar", + # process_all=False, # by default, only unseen emails are processed + # preserve=False, # delete emails if False + # nap_duration=1, # duration of the pause between polling rounds + # input_folder="INBOX", # where to read emails from + ), + "consumer": tracker_consumer( + group="Mail Queuers", + task_list_slug="mail-queue", + priority=1, + task_title_format="[TEST_MAIL] {subject}", + ) + } +} +``` + +A mail worker can be started with: + +```sh +./manage.py mail_worker test_tracker +``` + +Some views and URLs were renamed in 2.0 for logical consistency. If this affects you, see source code and the demo GTD site for reference to the new URL names. + +If you want to log mail events, make sure to properly configure django logging: + +```python +LOGGING = { + 'version': 1, + 'disable_existing_loggers': False, + 'handlers': { + 'console': { + 'class': 'logging.StreamHandler', + }, + }, + 'loggers': { + '': { + 'handlers': ['console'], + 'level': 'DEBUG', + 'propagate': True, + }, + }, +} +``` + + +## Running Tests + +django-todo uses pytest exclusively for testing. The best way to run the suite is to clone django-todo into its own directory, install pytest, then: + + pip install pytest pytest-django + pip install --editable . + pytest -x -v + +## Version History + +**2.4.0** Implement optional file attachments on tasks + +**2.3.2** Update setup.py metadata + +**2.3.1** Improve error handling for badly formatted or non-existent CSV uploads. + +**2.3.0** Implement mail tracking system. Added ability to batch-import tasks via CSV. Fixed task re-ordering if task deleted behind the scenes. + +**2.2.2** Update dependencies + +**2.2.1** Convert task delete and toggle_done views to POST only + +**2.2.0** Re-instate enforcement of TODO_STAFF_ONLY setting + +**2.1.1** Correct Python version requirement in documentation to Python 3.6 + +**2.1.1** Split up views into separate modules. + +**2.1.0** December 2018: No longer allowing Javascript in task or comment bodies. Misc bug fixes. + +**2.0.3** April 2018: Bump production status in setup.py + +**2.0.2** April 2018: Improve notification email subjects and bodies + +**2.0.1** April 2018: Refactored "toggle done" and "delete" actions from list view. + +**2.0** April 2018: Major project refactor, with almost completely rewritten views, templates, and todo's first real test suite. + +**1.6.2** Added support for unicode characters in list name/slugs. + +**1.6.1** Minor bug fixes. + +**1.6** Allow unassigned ("Anyone") tasks. Clean-up / modernize templates and views. Testing infrastructure in place. + +**1.5** flake8 support, Item note no longer a required field, fix warnings for Django 1.8, Python 2/3-compatible unicode strings, simple search for tasks, get_absolute_url() for items. + +**1.4** - Removed styling from default templates. Added excludes fields from Form definitions to prevent warnings. Removed deprecated 'cycle' tags from templates. Added settings for various elements for public ticket submissions. + +**1.3** - Removed stray direct_to_template reference. Quoted all named URL references for Django 1.5 compatibility. + +**1.2** - Added CSRF protection to all sample templates. Added integrated search function. Now showing the ratio of completed/total items for each +list. Better separation of media and templates. Cleaned up Item editing form (removed extraneous fields). Re-assigning tasks now properly limits +the list of assignees. Moved project to github. + +**1.1** - Completion date was set properly when checking items off a list, but not when saving from an Item detail page. Added a save method on Item to +fix. Fixed documentation bug re: context_processors. Newly added comments are now emailed to everyone who has participated in a thread on a task. + +**1.0.1** - When viewing a single task that you want to close, it's useful to be able to comment on and close a task at the same time. We were using +django-comments so these were different models in different views. Solution was to stop using django-comments and roll our own, then rewire the +view. Apologies if you were using a previous version - you may need to port over your comments to the new system. + +**1.0.0** - Major upgrade to release version. Drag and drop task prioritization. E-mail notifications (now works more like a ticket system). More +attractive date picker. Bug fixes. + +**0.9.5** - Fixed jquery bug when editing existing events - datepicker now shows correct date. Removed that damned Django pony from base template. + +**0.9.4** - Replaced str with unicode in models. Fixed links back to lists in "My Tasks" view. + +**0.9.3** - Missing link to the individual task editing view + +**0.9.2** - Now fails gracefully when trying to add a 2nd list with the same name to the same group. - Due dates for tasks are now truly optional. - +Corrected datetime editing conflict when editing tasks - Max length of a task name has been raised from 60 to 140 chars. If upgrading, please +modify your database accordingly (field todo_item.name = maxlength 140). - Security: Users supplied with direct task URLs can no longer view/edit +tasks outside their group scope Same for list views - authorized views only. - Correct item and group counts on homepage (note - admin users see +ALL groups, not just the groups they "belong" to) + +**0.9.1** - Removed context_processors.py - leftover turdlet + +**0.9** - First release + +## Todo 2.0 Upgrade Notes + +django-todo 2.0 was rebuilt almost from the ground up, and included some radical changes, including model name changes. As a result, it is *not compatible* with data from django-todo 1.x. If you would like to upgrade an existing installation, try this: + +* Use `./manage.py dumpdata todo --indent 4 > todo.json` to export your old todo data +* Edit the dump file, replacing the old model names `Item` and `List` with the new model names (`Task` and `TaskList`) +* Delete your existing todo data +* Uninstall the old todo app and reinstall +* Migrate, then use `./manage.py loaddata todo.json` to import the edited data + +### Why not provide migrations? + +That was the plan, but unfortunately, `makemigrations` created new tables and dropped the old ones, making this a destructive update. Renaming models is unfortunately not something `makemigrations` can do, and I really didn't want to keep the badly named original models. Sorry! + +### Datepicker + +django-todo no longer references a jQuery datepicker, but defaults to native html5 browser datepicker (not supported by Safari, unforunately). Feel free to implement one of your choosing. + +### URLs + +Some views and URLs were renamed for logical consistency. If this affects you, see source code and the demo GTD site for reference to the new URL names. \ No newline at end of file diff --git a/base_urls.py b/base_urls.py new file mode 100644 index 0000000..24c9f32 --- /dev/null +++ b/base_urls.py @@ -0,0 +1,15 @@ +from django.urls import include, path + +""" +This urlconf exists so we can run tests without an actual Django project +(Django expects ROOT_URLCONF to exist.) This helps the tests remain isolated. +For your project, ignore this file and add + +`path('lists/', include('todo.urls')),` + +to your site's urlconf. +""" + +urlpatterns = [ + path('lists/', include('todo.urls')), +] diff --git a/contrib/ansible/ansible.cfg b/contrib/ansible/ansible.cfg deleted file mode 100644 index b21d257..0000000 --- a/contrib/ansible/ansible.cfg +++ /dev/null @@ -1,10 +0,0 @@ -[defaults] -allow_world_readable_tmpfiles=True -pipelining=True -retry_files_enabled = False -inventory = inventory -roles_path = roles -library = library -remote_tmp = /root/.ansible/tmp -[connection] -pipelining=True diff --git a/contrib/ansible/bootstrap-node.yml b/contrib/ansible/bootstrap-node.yml deleted file mode 100644 index 1928037..0000000 --- a/contrib/ansible/bootstrap-node.yml +++ /dev/null @@ -1,6 +0,0 @@ ---- -- name: bootstrap playbook for any k8s machine - hosts: k8s - become: yes - roles: - - bootstrap diff --git a/contrib/ansible/group_vars/all/main.yml b/contrib/ansible/group_vars/all/main.yml deleted file mode 100644 index 113cfae..0000000 --- a/contrib/ansible/group_vars/all/main.yml +++ /dev/null @@ -1,11 +0,0 @@ -k8s_version: '1.17.5' -k8s_first_master_node: 'k8s-demo' -k8s_domain: coins.k8s.demo.ix.gs -k8s_pod_network: '192.168.0.0/16' -k8s_service_network: '10.254.0.0/24' -k8s_controlplane_vip: '100.100.100.15' -k8s_controlplane_address: '{{ k8s_controlplane_vip }}:6443' -k8s_cluster_name: k8s-demo - -cloud_provider: baremetal -ha_enabled: false diff --git a/contrib/ansible/host_vars/k8s-demo.yml b/contrib/ansible/host_vars/k8s-demo.yml deleted file mode 100644 index 4e7d5ba..0000000 --- a/contrib/ansible/host_vars/k8s-demo.yml +++ /dev/null @@ -1 +0,0 @@ -k8s_node_role: 'master' diff --git a/contrib/ansible/init-cluster.yml b/contrib/ansible/init-cluster.yml deleted file mode 100644 index f2e57d6..0000000 --- a/contrib/ansible/init-cluster.yml +++ /dev/null @@ -1,7 +0,0 @@ ---- -- name: Init k8s cluster - hosts: 'k8s-demo' - become: yes - max_fail_percentage: 0 - roles: - - init-cluster diff --git a/contrib/ansible/inventory/hosts b/contrib/ansible/inventory/hosts deleted file mode 100644 index e91cbbe..0000000 --- a/contrib/ansible/inventory/hosts +++ /dev/null @@ -1,2 +0,0 @@ -[k8s] -k8s-demo ansible_connection=local diff --git a/contrib/ansible/keepalived.yml b/contrib/ansible/keepalived.yml deleted file mode 100644 index 634312f..0000000 --- a/contrib/ansible/keepalived.yml +++ /dev/null @@ -1,6 +0,0 @@ ---- -- name: bootstrap playbook for any k8s machine - hosts: k8s-masters - become: yes - roles: - - keepalived diff --git a/contrib/ansible/roles/bootstrap/defaults/main.yml b/contrib/ansible/roles/bootstrap/defaults/main.yml deleted file mode 100644 index 4f288b5..0000000 --- a/contrib/ansible/roles/bootstrap/defaults/main.yml +++ /dev/null @@ -1,6 +0,0 @@ -kubernetes_apt_release_channel: main -# Note that xenial repo is used for all Debian derivatives at this time. -kubernetes_apt_repository: "deb http://apt.kubernetes.io/ kubernetes-xenial {{ kubernetes_apt_release_channel }}" -## Calico config files -kubernetes_calico_manifest_file: https://docs.projectcalico.org/v3.10/manifests/calico.yaml - diff --git a/contrib/ansible/roles/bootstrap/handlers/main.yml b/contrib/ansible/roles/bootstrap/handlers/main.yml deleted file mode 100644 index 4a99fa9..0000000 --- a/contrib/ansible/roles/bootstrap/handlers/main.yml +++ /dev/null @@ -1,6 +0,0 @@ ---- -- name: restart kubelet - service: name=kubelet state=restarted - -- name: restart docker daemon - service: name=docker state=restarted diff --git a/contrib/ansible/roles/bootstrap/tasks/main.yml b/contrib/ansible/roles/bootstrap/tasks/main.yml deleted file mode 100644 index ed0ec24..0000000 --- a/contrib/ansible/roles/bootstrap/tasks/main.yml +++ /dev/null @@ -1,39 +0,0 @@ ---- -- name: Ensure dependencies are installed. - apt: - name: - - apt-transport-https - - ca-certificates - state: present - -- name: Add Kubernetes apt key. - apt_key: - url: https://packages.cloud.google.com/apt/doc/apt-key.gpg - state: present - -- name: Add Kubernetes repository. - apt_repository: - repo: "{{ kubernetes_apt_repository }}" - state: present - update_cache: true - -- name: Install kubeadm kubelet kubectl - apt: - pkg: - - kubeadm={{ k8s_version }}-00 - - kubelet={{ k8s_version }}-00 - - kubectl={{ k8s_version }}-00 - - kubernetes-cni=0.7.5-00 - update_cache: yes - notify: restart kubelet - -- name: Add Kubernetes apt preferences file to pin a version. - template: - src: apt-preferences-kubernetes.j2 - dest: /etc/apt/preferences.d/kubernetes - -- name: - template: - src: daemon.json - dest: /etc/docker/daemon.json - notify: restart docker daemon diff --git a/contrib/ansible/roles/bootstrap/templates/apt-preferences-kubernetes.j2 b/contrib/ansible/roles/bootstrap/templates/apt-preferences-kubernetes.j2 deleted file mode 100644 index 7709524..0000000 --- a/contrib/ansible/roles/bootstrap/templates/apt-preferences-kubernetes.j2 +++ /dev/null @@ -1,11 +0,0 @@ -Package: kubectl -Pin: version {{ k8s_version }}.* -Pin-Priority: 1000 - -Package: kubeadm -Pin: version {{ k8s_version }}.* -Pin-Priority: 1000 - -Package: kubelet -Pin: version {{ k8s_version }}.* -Pin-Priority: 1000 diff --git a/contrib/ansible/roles/bootstrap/templates/daemon.json b/contrib/ansible/roles/bootstrap/templates/daemon.json deleted file mode 100644 index 5d18abc..0000000 --- a/contrib/ansible/roles/bootstrap/templates/daemon.json +++ /dev/null @@ -1,8 +0,0 @@ -{ - "exec-opts": ["native.cgroupdriver=systemd"], - "log-driver": "json-file", - "log-opts": { - "max-size": "100m" - }, - "storage-driver": "overlay2" -} diff --git a/contrib/ansible/roles/init-cluster/tasks/main.yml b/contrib/ansible/roles/init-cluster/tasks/main.yml deleted file mode 100644 index 8a7db14..0000000 --- a/contrib/ansible/roles/init-cluster/tasks/main.yml +++ /dev/null @@ -1,52 +0,0 @@ -- name: Add Kubeadm config file - template: - src: kubeadm.conf.j2 - dest: /etc/kubeadm.conf - when: k8s_node_role == 'master' - -- name: Init cluster - command: kubeadm init --config /etc/kubeadm.conf --upload-certs --ignore-preflight-errors serviceSubnet - when: ansible_hostname == k8s_first_master_node - -- name: Create kube config directory for root - file: path=/root/.kube state=directory - when: k8s_node_role == 'master' - -- name: Copy Kubernetes admin config to home directory - copy: - src: "/etc/kubernetes/admin.conf" - dest: "/root/.kube/config" -# remote_src: yes -# when: and ansible_hostname == k8s_first_master_node - -- name: Install Calico CNI - command: kubectl apply -f "https://docs.projectcalico.org/v3.13/manifests/calico.yaml" - when: ansible_hostname == k8s_first_master_node - - -- name: Generate join token - command: kubeadm token create --print-join-command - register: join_cmd - delegate_to: '{{ k8s_first_master_node }}' - -# Эта часть не работает -# Правильная команда выглядит так: -# kubeadm join 10.129.64.60:6443 --token --discovery-token-ca-cert-hash --control-plane --certificate-key -# Предыдущий блок генерит сертификат и токен без указания ключа -# Надо пофиксить как будет время -- name: Join rest of master nodes - command: "{{ join_cmd.stdout }} --control-plane" - when: k8s_node_role == 'master' and ha_enabled and ansible_hostname != k8s_first_master_node - ignore_errors: yes - -#- name: Copy Kubernetes admin config to home directory -# copy: -# src: "/etc/kubernetes/admin.conf" -# dest: "/root/.kube/config" -# remote_src: yes -# when: k8s_node_role == 'master' - -- name: Join worker nodes - command: "{{ join_cmd.stdout }}" - when: k8s_node_role == 'worker' - diff --git a/contrib/ansible/roles/init-cluster/templates/kubeadm.conf.j2 b/contrib/ansible/roles/init-cluster/templates/kubeadm.conf.j2 deleted file mode 100644 index 84e3ddd..0000000 --- a/contrib/ansible/roles/init-cluster/templates/kubeadm.conf.j2 +++ /dev/null @@ -1,25 +0,0 @@ -apiVersion: kubelet.config.k8s.io/v1beta1 -kind: KubeletConfiguration -cgroupDriver: systemd ---- -apiVersion: kubeadm.k8s.io/v1beta2 -kind: InitConfiguration -nodeRegistration: ---- -apiVersion: kubeadm.k8s.io/v1beta2 -kind: ClusterConfiguration -kubernetesVersion: {{ k8s_version }} -certificatesDir: /etc/kubernetes/pki -clusterName: {{ k8s_cluster_name }} -controlPlaneEndpoint: {{ k8s_controlplane_address }} -dns: - type: CoreDNS -etcd: - local: - dataDir: /var/lib/etcd -imageRepository: k8s.gcr.io -networking: - dnsDomain: {{ k8s_domain }} - podSubnet: {{ k8s_pod_network }} - serviceSubnet: {{ k8s_service_network }} -scheduler: {} diff --git a/contrib/ansible/roles/keepalived/defaults/main.yml b/contrib/ansible/roles/keepalived/defaults/main.yml deleted file mode 100644 index ed97d53..0000000 --- a/contrib/ansible/roles/keepalived/defaults/main.yml +++ /dev/null @@ -1 +0,0 @@ ---- diff --git a/contrib/ansible/roles/keepalived/handlers/main.yml b/contrib/ansible/roles/keepalived/handlers/main.yml deleted file mode 100644 index 2ac9fe3..0000000 --- a/contrib/ansible/roles/keepalived/handlers/main.yml +++ /dev/null @@ -1,3 +0,0 @@ ---- -- name: restart keepalived - service: name=keepalived state=restarted diff --git a/contrib/ansible/roles/keepalived/tasks/main.yml b/contrib/ansible/roles/keepalived/tasks/main.yml deleted file mode 100644 index 4998ba8..0000000 --- a/contrib/ansible/roles/keepalived/tasks/main.yml +++ /dev/null @@ -1,14 +0,0 @@ ---- -- name: Install keepalived - apt: - pkg: - - keepalived - state: latest - -- name: Configure keepalived - template: src=keepalived.conf.j2 dest=/etc/keepalived/keepalived.conf - tags: keepalived - notify: restart keepalived - -- name: Start keepalived - service: name=keepalived state=started diff --git a/contrib/ansible/roles/keepalived/templates/keepalived.conf.j2 b/contrib/ansible/roles/keepalived/templates/keepalived.conf.j2 deleted file mode 100644 index dd127ec..0000000 --- a/contrib/ansible/roles/keepalived/templates/keepalived.conf.j2 +++ /dev/null @@ -1,31 +0,0 @@ - ! Configuration File for keepalived -global_defs { -} - -{#vrrp_script haproxy-check { - script "killall -0 haproxy" - interval 2 - weight 20 -}#} - -vrrp_instance VI_1 { - state {{ keepalived_role }} - interface {{ keepalived_shared_iface }} - virtual_router_id {{ keepalived_router_id }} - {% if keepalived_role.lower() == "master" %} - priority {{ keepalived_priority }} - {% else %} - priority {{ keepalived_backup_priority }} - {% endif %} - advert_int 1 - authentication { - auth_type PASS - auth_pass {{ keepalived_auth_pass }} - } - virtual_ipaddress { - {{ keepalived_shared_ip }} dev {{ keepalived_shared_iface }} label {{ keepalived_shared_iface }}:0 - } -{# track_script { - haproxy-check weight 20 - }#} -} diff --git a/contrib/firstboot.sh b/contrib/firstboot.sh deleted file mode 100755 index cf20fe7..0000000 --- a/contrib/firstboot.sh +++ /dev/null @@ -1,77 +0,0 @@ -#!/bin/bash -bootflag='/.manufactured' -groot='/opt/coins-demo' - -k8sDemoWA () { - printf "\033c" - echo 'Disabling swap space...' - sync && swapoff -a && sed -i '/ swap / s/^/#/' /etc/fstab - export KUBECONFIG=/root/.kube/config -} -k8sDeploy () { - echo 'Installing K8s...' - cd ${groot}/contrib/ansible && \ - ansible-playbook bootstrap-node.yml && \ - ansible-playbook init-cluster.yml - # Sometimes it's still not ready on this stage, let's check it just to be sure - while true ; do - echo "Waiting for node up..." - result=$(kubectl get nodes|awk '{print $2}'| tail -1| grep -nE '^Ready') - if [ -z "$result" ] ; then - break - fi - sleep 10 - done -} -InstallCSI () { - helm repo add rimusz https://charts.rimusz.net - helm install rimusz/hostpath-provisioner --generate-name -} -InstallRegistry () { - helm repo add harbor https://helm.goharbor.io - helm install registry harbor/harbor \ - --set expose.tls.enabled=false \ - --set expose.ingress.hosts.core="registry.k8s-demo.ix.gs" \ - --set expose.ingress.hosts.notary="notary.k8s-demo.ix.gs" \ - --set externalURL="http://registry.k8s-demo.ix.gs" \ - --set persistence.storageClass=hostpath \ - --set registry.credentials.username=k8s \ - --set registry.credentials.password=k8s \ - --set notary.enabled=false \ - --set trivy.enabled=false \ - --set clair.enabled=false \ - --set chartmuseum.enabled=false -} -InstallPGSQL () { - helm repo add bitnami https://charts.bitnami.com/bitnami - helm install db bitnami/postgresql \ - --set persistence.storageClass=hostpath \ - --set persistence.size=1Gi -} -InstallIngress () { - # Allow scheduling on our master node - kubectl taint nodes k8s-demo node-role.kubernetes.io/master- - # Installing Ingress - helm repo add nginx-stable https://helm.nginx.com/stable && \ - helm install nginx-stable/nginx-ingress --namespace kube-system --generate-name --set rbac.create=true - # Fix external IP for LB... - kubectl patch svc $(kubectl get svc -n kube-system|grep nginx-ingress|awk '{print $1}') -n kube-system --patch "$(cat ${groot}/contrib/ymls/ingress.fix.yaml)" -} -InstallApp () { - cd ${groot} - export WERF_INSECURE_REGISTRY=true - export WERF_IMAGES_REPO='http://registry.k8s-demo.ix.gs/todo' - werf build --stages-storage :local && \ - werf publish --stages-storage :local --tag-custom stable - werf deploy --stages-storage :local --tag-custom latest --env production --set 'DBPwd=$(kubectl get secret db-postgresql -o jsonpath="{.data.postgresql-password}" | base64 --decode)' -} - -if [ ! -f ${bootflag} ]; then - touch ${bootflag} - k8sDemoWA; - k8sDeploy; - InstallCSI; - InstallIngress; - InstallRegistry; - InstallPGSQL; -fi diff --git a/contrib/k8s-seed.txt b/contrib/k8s-seed.txt deleted file mode 100644 index fb89498..0000000 --- a/contrib/k8s-seed.txt +++ /dev/null @@ -1,112 +0,0 @@ -### Keyboard config -d-i debian-installer/locale string en_US.UTF-8 -d-i keyboard-configuration/variant select American English -d-i keyboard-configuration/xkb-keymap select us -d-i keyboard-configuration/toggle select No toggling - -### Network configuration -d-i netcfg/choose_interface select auto -d-i netcfg/get_hostname string unassigned-hostname -d-i netcfg/get_domain string unassigned-domain -d-i netcfg/hostname string k8s-demo -d-i netcfg/wireless_wep string -d-i netcfg/dhcp_hostname string k8s-demo -d-i hw-detect/load_firmware boolean true - -### Mirror settings -d-i mirror/country string manual -d-i mirror/http/hostname string cdn.debian.net -d-i mirror/http/directory string /debian -d-i mirror/http/proxy string - -### Account setup -d-i passwd/root-login boolean false - -# To create a normal user account. -d-i passwd/user-fullname string K8S Admin -d-i passwd/username string k8s -d-i passwd/user-password password K8Sdemo -d-i passwd/user-password-again password K8Sdemo - -# The user account will be added to some standard initial groups. To -# override that, use this. -#d-i passwd/user-default-groups string docker - -### Clock and time zone setup -d-i clock-setup/utc boolean true -d-i time/zone string UTC -d-i clock-setup/ntp boolean true -d-i clock-setup/ntp-server string clock.ix.gs - -### Partitioning -d-i partman-auto/method string regular -d-i partman-lvm/device_remove_lvm boolean true -d-i partman-md/device_remove_md boolean true -d-i partman-lvm/confirm boolean true -d-i partman-lvm/confirm_nooverwrite boolean true -d-i partman-auto/choose_recipe select atomic -d-i partman-basicfilesystems/no_swap boolean true -d-i partman-partitioning/confirm_write_new_label boolean true -d-i partman/choose_partition select finish -d-i partman/confirm boolean true -d-i partman/confirm_nooverwrite boolean true -d-i partman/default_filesystem string xfs - -### Apt setup -d-i apt-setup/non-free boolean true -d-i apt-setup/contrib boolean true -d-i debian-installer/allow_unauthenticated boolean true - - -### Package selection -tasksel tasksel/first multiselect none, ssh-server, standard - -# Individual additional packages to install -d-i pkgsel/include string \ - apt-transport-https gnupg2 ca-certificates curl \ - iptables linux-headers-amd64 git ansible wget \ - gdebi-core - -# Whether to upgrade packages after debootstrap. -# Allowed values: none, safe-upgrade, full-upgrade -d-i pkgsel/upgrade select full-upgrade -popularity-contest popularity-contest/participate boolean true - -### Boot loader installation -d-i grub-installer/only_debian boolean true -d-i grub-installer/with_other_os boolean true -d-i grub-installer/bootdev string default - -### Finishing up the installation -d-i finish-install/reboot_in_progress note -d-i debian-installer/exit/poweroff boolean true - -#### Advanced options -### Running custom commands during the installation -d-i preseed/late_command string in-target /bin/sh -c " \ - echo k8s-demo > /etc/hostname ; \ - sed -i 's/debian/k8s-demo/' /etc/hosts ; \ - update-alternatives --set iptables /usr/sbin/iptables-legacy ; \ - update-alternatives --set ip6tables /usr/sbin/ip6tables-legacy ; \ - update-alternatives --set arptables /usr/sbin/arptables-legacy ; \ - update-alternatives --set ebtables /usr/sbin/ebtables-legacy ; \ - ( systemctl disable nftables && systemctl mask nftables ) ; \ - curl -s https://download.docker.com/linux/debian/gpg | sudo apt-key add - ; \ - curl https://helm.baltorepo.com/organization/signing.asc | sudo apt-key add - ; \ - echo 'deb [arch=amd64] https://download.docker.com/linux/debian/ stretch stable' >/etc/apt/sources.list.d/docker.list ; \ - echo 'deb https://baltocdn.com/helm/stable/debian/ all main' > /etc/apt/sources.list.d/helm-stable-debian.list ; \ - apt-get update ; \ - apt-get install -y \ - docker-ce docker-ce-cli containerd.io helm ; \ - apt-get clean ; \ - curl -L https://dl.bintray.com/flant/werf/v1.1.19+fix10/werf-linux-amd64-v1.1.19+fix10 -o /usr/bin/werf ; \ - chmod +x /usr/bin/werf ; \ - git clone https://git.ix.gs/public/coins-demo.git /opt/coins-demo ; \ - cp /opt/coins-demo/contrib/mfg.service /etc/systemd/system/ ; \ - mkdir -p /lib/systemd/system/docker.service.d ; \ - sed -i 's/sock$/sock --insecure-registry="registry.k8s-demo.ix.gs"/' /lib/systemd/system/docker.service ; \ - systemctl daemon-reload ; \ - systemctl enable mfg.service ; \ - systemctl enable serial-getty@ttyS0.service ; \ - systemctl enable docker.service \ - || true" diff --git a/contrib/mfg.service b/contrib/mfg.service deleted file mode 100644 index 4aa829a..0000000 --- a/contrib/mfg.service +++ /dev/null @@ -1,18 +0,0 @@ -[Unit] -Description=Manufacturing service -After=serial-getty@ttyS0.service - -[Service] -Type=idle -ExecStart=/opt/coins-demo/contrib/firstboot.sh -StandardInput=tty -StandardOutput=tty -TTYPath=/dev/ttyS0 -TTYReset=yes -#TTYVHangup=yes -KillMode=none -TimeoutSec=0 -RemainAfterExit=yes - -[Install] -WantedBy=multi-user.target diff --git a/contrib/qemu-system-x86_64 b/contrib/qemu-system-x86_64 deleted file mode 100755 index 57598d3..0000000 Binary files a/contrib/qemu-system-x86_64 and /dev/null differ diff --git a/contrib/share/qemu/bios-256k.bin b/contrib/share/qemu/bios-256k.bin deleted file mode 100644 index fab9da2..0000000 Binary files a/contrib/share/qemu/bios-256k.bin and /dev/null differ diff --git a/contrib/share/qemu/efi-e1000.rom b/contrib/share/qemu/efi-e1000.rom deleted file mode 100644 index 776e217..0000000 Binary files a/contrib/share/qemu/efi-e1000.rom and /dev/null differ diff --git a/contrib/share/qemu/keymaps/common b/contrib/share/qemu/keymaps/common deleted file mode 100644 index adc56c7..0000000 --- a/contrib/share/qemu/keymaps/common +++ /dev/null @@ -1,157 +0,0 @@ -include modifiers - -# -# Top row -# -1 0x2 -2 0x3 -3 0x4 -4 0x5 -5 0x6 -6 0x7 -7 0x8 -8 0x9 -9 0xa -0 0xb -BackSpace 0xe - -# -# QWERTY first row -# -Tab 0xf localstate -ISO_Left_Tab 0xf shift -q 0x10 addupper -w 0x11 addupper -e 0x12 addupper -r 0x13 addupper -t 0x14 addupper -y 0x15 addupper -u 0x16 addupper -i 0x17 addupper -o 0x18 addupper -p 0x19 addupper - -# -# QWERTY second row -# -a 0x1e addupper -s 0x1f addupper -d 0x20 addupper -f 0x21 addupper -g 0x22 addupper -h 0x23 addupper -j 0x24 addupper -k 0x25 addupper -l 0x26 addupper -Return 0x1c localstate - -# -# QWERTY third row -# -z 0x2c addupper -x 0x2d addupper -c 0x2e addupper -v 0x2f addupper -b 0x30 addupper -n 0x31 addupper -m 0x32 addupper - -space 0x39 localstate - -less 0x56 -greater 0x56 shift -bar 0x56 altgr -brokenbar 0x56 shift altgr - -# -# Esc and Function keys -# -Escape 0x1 localstate -F1 0x3b localstate -F2 0x3c localstate -F3 0x3d localstate -F4 0x3e localstate -F5 0x3f localstate -F6 0x40 localstate -F7 0x41 localstate -F8 0x42 localstate -F9 0x43 localstate -F10 0x44 localstate -F11 0x57 localstate -F12 0x58 localstate - -# Printscreen, Scrollock and Pause -# Printscreen really requires four scancodes (0xe0, 0x2a, 0xe0, 0x37), -# but (0xe0, 0x37) seems to work. -Print 0xb7 localstate -Sys_Req 0xb7 localstate -Execute 0xb7 localstate -Scroll_Lock 0x46 - -# -# Insert - PgDown -# -Insert 0xd2 localstate -Delete 0xd3 localstate -Home 0xc7 localstate -End 0xcf localstate -Page_Up 0xc9 localstate -Page_Down 0xd1 localstate - -# -# Arrow keys -# -Left 0xcb localstate -Up 0xc8 localstate -Down 0xd0 localstate -Right 0xcd localstate - -# -# Numpad -# -Num_Lock 0x45 -KP_Divide 0xb5 -KP_Multiply 0x37 -KP_Subtract 0x4a -KP_Add 0x4e -KP_Enter 0x9c - -KP_Decimal 0x53 numlock -KP_Separator 0x53 numlock -KP_Delete 0x53 - -KP_0 0x52 numlock -KP_Insert 0x52 - -KP_1 0x4f numlock -KP_End 0x4f - -KP_2 0x50 numlock -KP_Down 0x50 - -KP_3 0x51 numlock -KP_Next 0x51 - -KP_4 0x4b numlock -KP_Left 0x4b - -KP_5 0x4c numlock -KP_Begin 0x4c - -KP_6 0x4d numlock -KP_Right 0x4d - -KP_7 0x47 numlock -KP_Home 0x47 - -KP_8 0x48 numlock -KP_Up 0x48 - -KP_9 0x49 numlock -KP_Prior 0x49 - -Caps_Lock 0x3a -# -# Inhibited keys -# -Multi_key 0x0 inhibit diff --git a/contrib/share/qemu/keymaps/en-us b/contrib/share/qemu/keymaps/en-us deleted file mode 100644 index f5784bb..0000000 --- a/contrib/share/qemu/keymaps/en-us +++ /dev/null @@ -1,35 +0,0 @@ -# generated from XKB map us -include common -map 0x409 -exclam 0x02 shift -at 0x03 shift -numbersign 0x04 shift -dollar 0x05 shift -percent 0x06 shift -asciicircum 0x07 shift -ampersand 0x08 shift -asterisk 0x09 shift -parenleft 0x0a shift -parenright 0x0b shift -minus 0x0c -underscore 0x0c shift -equal 0x0d -plus 0x0d shift -bracketleft 0x1a -braceleft 0x1a shift -bracketright 0x1b -braceright 0x1b shift -semicolon 0x27 -colon 0x27 shift -apostrophe 0x28 -quotedbl 0x28 shift -grave 0x29 -asciitilde 0x29 shift -backslash 0x2b -bar 0x2b shift -comma 0x33 -less 0x33 shift -period 0x34 -greater 0x34 shift -slash 0x35 -question 0x35 shift diff --git a/contrib/share/qemu/keymaps/modifiers b/contrib/share/qemu/keymaps/modifiers deleted file mode 100644 index d73b7a6..0000000 --- a/contrib/share/qemu/keymaps/modifiers +++ /dev/null @@ -1,18 +0,0 @@ -Shift_R 0x36 -Shift_L 0x2a - -Alt_R 0xb8 -Mode_switch 0xb8 -ISO_Level3_Shift 0xb8 -Alt_L 0x38 - -Control_R 0x9d -Control_L 0x1d - -# Translate Super to Windows keys. -# This is hardcoded. See documentation for details. -Super_R 0xdc -Super_L 0xdb - -# Translate Menu to the Windows Application key. -Menu 0xdd diff --git a/contrib/share/qemu/kvmvapic.bin b/contrib/share/qemu/kvmvapic.bin deleted file mode 100644 index 045f5c2..0000000 Binary files a/contrib/share/qemu/kvmvapic.bin and /dev/null differ diff --git a/contrib/share/qemu/linuxboot.bin b/contrib/share/qemu/linuxboot.bin deleted file mode 100644 index 130103f..0000000 Binary files a/contrib/share/qemu/linuxboot.bin and /dev/null differ diff --git a/contrib/share/qemu/vgabios-stdvga.bin b/contrib/share/qemu/vgabios-stdvga.bin deleted file mode 100644 index e5e5b14..0000000 Binary files a/contrib/share/qemu/vgabios-stdvga.bin and /dev/null differ diff --git a/contrib/ymls/ingress.fix.yaml b/contrib/ymls/ingress.fix.yaml deleted file mode 100644 index 691c3b1..0000000 --- a/contrib/ymls/ingress.fix.yaml +++ /dev/null @@ -1,4 +0,0 @@ -spec: - externalIPs: - - 100.100.100.15 - loadBalancerIP: 100.100.100.15 diff --git a/docs/index.md b/docs/index.md new file mode 100644 index 0000000..8860e01 --- /dev/null +++ b/docs/index.md @@ -0,0 +1,399 @@ +# django-todo + +django-todo is a pluggable, multi-user, multi-group task management and +assignment application for Django, designed to be dropped into an existing site as a reusable app. django-todo can be used as a personal to-do tracker, or a group task management system, or a ticketing system for organizations (or all of these at once!) + +**The best way to learn how django-todo works is to visit the live demo site at [django-todo.org](http://django-todo.org)!** + +## Features + +* Drag and drop task prioritization +* Email task notification +* Search +* Comments on tasks +* Public-facing submission form for tickets +* Mobile-friendly (work in progress) +* Separate view for My Tasks (across lists) +* Batch-import tasks via CSV +* Integrated mail tracking (unify a task list with an email box) + + +## Requirements + +* Django 2.0+ +* Python 3.6+ +* jQuery (full version, not "slim", for drag/drop prioritization) +* Bootstrap (to work with provided templates, though you can override them) +* bleach (`pip install bleach`) +* django-autocomplete-light (optional, required for task merging) + +## Overview + +We assume that your organization has multiple groups of employees, each with multiple users (where actual users and groups map to Django Users and Groups). Users may belong to multiple groups, and each group can have multiple todo lists. + +You must have at least one Group set up in Django admin, and that group must have at least one User as a member. This is true even if you're the sole user of django-todo. + +Users can view and modify all to-do lists belonging to their group(s). Only users with `is_staff` can add or delete lists. + +Identical list names can exist in different groups, but not in the same group. + +Emails are generated to the assigned-to person when new tasks are created. + +Comment threads can be added to tasks. Each participant in a thread receives email when new comments are added. + +django-todo is auth-only. You must set up a login system and at least one group before deploying. + +All tasks are "created by" the current user and can optionally be "assigned to" a specific user. Unassigned tickets appear as belonging to "anyone" in the UI. + +django-todo v2 makes use of features only available in Django 2.0. It will not work in previous versions. v2 is only tested against Python 3.x -- no guarantees if running it against older versions. + +## Installation + +django-todo is a Django app, not a project site. It needs a site to live in. You can either install it into an existing Django project site, or clone the django-todo [demo site (GTD)](https://github.com/shacker/gtd). + +If using your own site, be sure you have jQuery and Bootstrap wired up and working. + +django-todo views that require it will insert additional CSS/JavaScript into page heads, so your project's base templates must include: + +```jinja +{% block extrahead %}{% endblock extrahead %} +{% block extra_js %}{% endblock extra_js %} +``` + +django-todo comes with its own `todo/base.html`, which extends your master `base.html`. All content lives inside of: + +`{% block content %}{% endblock %}` + +If you use some other name for your main content area, you'll need to override and alter the provided templates. + +All views are login-required. Therefore, you must have a working user authentication system. + +For email notifications to work, make sure your site/project is [set up to send email](https://docs.djangoproject.com/en/2.0/topics/email/). + +Make sure you've installed the Django "sites" framework and have specified the default site in settings, e.g. `SITE_ID = 1` + +Put django-todo/todo somewhere on your Python path, or install via pip: + + pip install django-todo + + +Add to your settings: + +``` +INSTALLED_APPS = ( + ... + 'todo', +) +``` + +Migrate in database tables: + +`python manage.py migrate todo` + +Add to your URL conf: + +`path('todo/', include('todo.urls', namespace="todo")),` + +Add links to your site's navigation system: + +``` +Todo Lists +My Tasks +``` + +django-todo makes use of the Django `messages` system. Make sure you have something like [this](https://docs.djangoproject.com/en/2.0/ref/contrib/messages/#displaying-messages) (link) in your `base.html`. + +Log in and access `/todo`! + +### Customizing Templates + +The provided templates are fairly bare-bones, and are meant as starting points only. Unlike previous versions of django-todo, they now ship as Bootstrap examples, but feel free to override them - there is no hard dependency on Bootstrap. To override a template, create a `todo` folder in your project's `templates` dir, then copy the template you want to override from django-todo source and into that dir. + +### Filing Public Tickets + +If you wish to use the public ticket-filing system, first create the list into which those tickets should be filed, then add its slug to `TODO_DEFAULT_LIST_SLUG` in settings (more on settings below). + +## Settings + +Optional configuration params, which can be added to your project settings: + +```python +# Restrict access to ALL todo lists/views to `is_staff` users. +# If False or unset, all users can see all views (but more granular permissions are still enforced +# within views, such as requiring staff for adding and deleting lists). +TODO_STAFF_ONLY = True + +# If you use the "public" ticket filing option, to whom should these tickets be assigned? +# Must be a valid username in your system. If unset, unassigned tickets go to "Anyone." +TODO_DEFAULT_ASSIGNEE = 'johndoe' + +# If you use the "public" ticket filing option, to which list should these tickets be saved? +# Defaults to first list found, which is probably not what you want! +TODO_DEFAULT_LIST_SLUG = 'tickets' + +# If you use the "public" ticket filing option, to which *named URL* should the user be +# redirected after submitting? (since they can't see the rest of the ticket system). +# Defaults to "/" +TODO_PUBLIC_SUBMIT_REDIRECT = 'dashboard' + +# additionnal classes the comment body should hold +# adding "text-monospace" makes comment monospace +TODO_COMMENT_CLASSES = [] + +# The following two settings are relevant only if you want todo to track a support mailbox - +# see Mail Tracking below. +TODO_MAIL_BACKENDS +TODO_MAIL_TRACKERS +``` + +The current django-todo version number is available from the [todo package](https://github.com/shacker/django-todo/blob/master/todo/__init__.py): + + python -c "import todo; print(todo.__version__)" + +## Importing Tasks via CSV + +django-todo has the ability to batch-import ("upsert") tasks from a specifically formatted CSV spreadsheet. This ability is provided through both a management command and a web interface. + +**Management Command** + +`./manage.py import_csv -f /path/to/file.csv` + +**Web Importer** + +Link from your navigation to `{url "todo:import_csv"}`. Follow the resulting link for the CSV web upload view. + + +### CSV Formatting + +Copy `todo/data/import_example.csv` to another location on your system and edit in a spreadsheet or directly. + +**Do not edit the header row!** + +The first four columns: `'Title', 'Group', 'Task List', 'Created By'` are required -- all others are optional and should work pretty much exactly like manual task entry via the web UI. + +Note: Internally, Tasks are keyed to TaskLists, not to Groups (TaskLists are in Gruops). However, we request the Group in the CSV +because it's possible to have multiple TaskLists with the same name in different groups; i.e. we need it for namespacing and permissions. + + +### Import Rules + +Because data entered via CSV is not going through the same view permissions enforced in the rest of django-todo, and to simplify data dependency logic, and to pre-empt disagreements between django-todo users, the importer will *not* create new users, groups, or task lists. All users, groups, and task lists referenced in your CSV must already exist, and group memberships must be correct. + +Any validation error (e.g. unparse-able dates, incorrect group memberships) **will result in that row being skipped.** + +A report of rows upserted and rows skipped (with line numbers and reasons) is provided at the end of the run. + +### Upsert Logic + +For each valid row, we need to decide whether to create a new task or update an existing one. django-todo matches on the unique combination of the four required columns. If we find a task that matches those, we *update* the rest of the columns. In other words, if you import a CSV once, then edit the Assigned To for a task and import it again, the original task will be updated with a new assignee (and same for the other columns). + +Otherwise we create a new task. + + +## Mail Tracking + +What if you could turn django-todo into a shared mailbox? Django-todo includes an optional feature that allows emails +sent to a dedicated mailbox to be pushed into todo as new tasks, and responses to be added as comments on those tasks. +This allows support teams to work with a fully unified email + bug tracking system to avoid confusion over who's seen or +responded to what. + +To enable mail tracking, you need to: + + - Define an email backend for outgoing emails + - Define an email backend for incoming emails + - Start a worker, which will wait for new emails + +In settings: + +```python +from todo.mail.producers import imap_producer +from todo.mail.consumers import tracker_consumer +from todo.mail.delivery import smtp_backend, console_backend + +# email notifications configuration +# each task list can get its own delivery method +TODO_MAIL_BACKENDS = { + # mail-queue is the name of the task list, not the worker name + "mail-queue": smtp_backend( + host="smtp.example.com", + port=465, + use_ssl=True, + username="test@example.com", + password="foobar", + # used as the From field when sending notifications. + # a username might be prepended later on + from_address="test@example.com", + # additionnal headers + headers={} + ), +} + +# incoming mail worker configuration +TODO_MAIL_TRACKERS = { + # configuration for worker "test_tracker" + "test_tracker": { + "producer": imap_producer( + host="imap.example.com", + username="text@example.com", + password="foobar", + # process_all=False, # by default, only unseen emails are processed + # preserve=False, # delete emails if False + # nap_duration=1, # duration of the pause between polling rounds + # input_folder="INBOX", # where to read emails from + ), + "consumer": tracker_consumer( + group="Mail Queuers", + task_list_slug="mail-queue", + priority=1, + task_title_format="[TEST_MAIL] {subject}", + ) + } +} +``` + +A mail worker can be started with: + +```sh +./manage.py mail_worker test_tracker +``` + +Some views and URLs were renamed in 2.0 for logical consistency. If this affects you, see source code and the demo GTD site for reference to the new URL names. + +If you want to log mail events, make sure to properly configure django logging: + +```python +LOGGING = { + 'version': 1, + 'disable_existing_loggers': False, + 'handlers': { + 'console': { + 'class': 'logging.StreamHandler', + }, + }, + 'loggers': { + '': { + 'handlers': ['console'], + 'level': 'DEBUG', + 'propagate': True, + }, + }, +} +``` + + +## Running Tests + +django-todo uses pytest exclusively for testing. The best way to run the suite is to clone django-todo into its own directory, install pytest, then: + + pip install pytest pytest-django + pip install --editable . + pytest -x -v + +The previous `tox` system was removed with the v2 release, since we no longer aim to support older Python or Django versions. + + +## Upgrade Notes + +django-todo 2.0 was rebuilt almost from the ground up, and included some radical changes, including model name changes. As a result, it is *not compatible* with data from django-todo 1.x. If you would like to upgrade an existing installation, try this: + +* Use `./manage.py dumpdata todo --indent 4 > todo.json` to export your old todo data +* Edit the dump file, replacing the old model names `Item` and `List` with the new model names (`Task` and `TaskList`) +* Delete your existing todo data +* Uninstall the old todo app and reinstall +* Migrate, then use `./manage.py loaddata todo.json` to import the edited data + +### Why not provide migrations? + +That was the plan, but unfortunately, `makemigrations` created new tables and dropped the old ones, making this a destructive update. Renaming models is unfortunately not something `makemigrations` can do, and I really didn't want to keep the badly named original models. Sorry! + +### Datepicker + +django-todo no longer references a jQuery datepicker, but defaults to native html5 browser datepicker (not supported by Safari, unforunately). Feel free to implement one of your choosing. + +## Version History + +**2.3.0** Implement mail tracking system. Added ability to batch-import tasks via CSV. Fixed task re-ordering if task deleted behind the scenes. + +**2.2.2** Update dependencies + +**2.2.1** Convert task delete and toggle_done views to POST only + +**2.2.0** Re-instate enforcement of TODO_STAFF_ONLY setting + +**2.1.1** Correct Python version requirement in documentation to Python 3.6 + +**2.1.1** Split up views into separate modules. + +**2.1.0** December 2018: No longer allowing Javascript in task or comment bodies. Misc bug fixes. + +**2.0.3** April 2018: Bump production status in setup.py + +**2.0.2** April 2018: Improve notification email subjects and bodies + +**2.0.1** April 2018: Refactored "toggle done" and "delete" actions from list view. + +**2.0** April 2018: Major project refactor, with almost completely rewritten views, templates, and todo's first real test suite. + +**1.6.2** Added support for unicode characters in list name/slugs. + +**1.6.1** Minor bug fixes. + +**1.6** Allow unassigned ("Anyone") tasks. Clean-up / modernize templates and views. Testing infrastructure in place. + +**1.5** flake8 support, Item note no longer a required field, fix warnings for Django 1.8, Python 2/3-compatible unicode strings, simple search for tasks, get_absolute_url() for items. + +**1.4** - Removed styling from default templates. Added excludes fields from Form definitions to prevent warnings. Removed deprecated 'cycle' tags from templates. Added settings for various elements for public ticket submissions. + +**1.3** - Removed stray direct_to_template reference. Quoted all named URL references for Django 1.5 compatibility. + +**1.2** - Added CSRF protection to all sample templates. Added integrated search function. Now showing the ratio of completed/total items for each +list. Better separation of media and templates. Cleaned up Item editing form (removed extraneous fields). Re-assigning tasks now properly limits +the list of assignees. Moved project to github. + +**1.1** - Completion date was set properly when checking items off a list, but not when saving from an Item detail page. Added a save method on Item to +fix. Fixed documentation bug re: context_processors. Newly added comments are now emailed to everyone who has participated in a thread on a task. + +**1.0.1** - When viewing a single task that you want to close, it's useful to be able to comment on and close a task at the same time. We were using +django-comments so these were different models in different views. Solution was to stop using django-comments and roll our own, then rewire the +view. Apologies if you were using a previous version - you may need to port over your comments to the new system. + +**1.0.0** - Major upgrade to release version. Drag and drop task prioritization. E-mail notifications (now works more like a ticket system). More +attractive date picker. Bug fixes. + +**0.9.5** - Fixed jquery bug when editing existing events - datepicker now shows correct date. Removed that damned Django pony from base template. + +**0.9.4** - Replaced str with unicode in models. Fixed links back to lists in "My Tasks" view. + +**0.9.3** - Missing link to the individual task editing view + +**0.9.2** - Now fails gracefully when trying to add a 2nd list with the same name to the same group. - Due dates for tasks are now truly optional. - +Corrected datetime editing conflict when editing tasks - Max length of a task name has been raised from 60 to 140 chars. If upgrading, please +modify your database accordingly (field todo_item.name = maxlength 140). - Security: Users supplied with direct task URLs can no longer view/edit +tasks outside their group scope Same for list views - authorized views only. - Correct item and group counts on homepage (note - admin users see +ALL groups, not just the groups they "belong" to) + +**0.9.1** - Removed context_processors.py - leftover turdlet + +**0.9** - First release + +## Todo 2.0 Upgrade Notes + +django-todo 2.0 was rebuilt almost from the ground up, and included some radical changes, including model name changes. As a result, it is *not compatible* with data from django-todo 1.x. If you would like to upgrade an existing installation, try this: + +* Use `./manage.py dumpdata todo --indent 4 > todo.json` to export your old todo data +* Edit the dump file, replacing the old model names `Item` and `List` with the new model names (`Task` and `TaskList`) +* Delete your existing todo data +* Uninstall the old todo app and reinstall +* Migrate, then use `./manage.py loaddata todo.json` to import the edited data + +### Why not provide migrations? + +That was the plan, but unfortunately, `makemigrations` created new tables and dropped the old ones, making this a destructive update. Renaming models is unfortunately not something `makemigrations` can do, and I really didn't want to keep the badly named original models. Sorry! + +### Datepicker + +django-todo no longer references a jQuery datepicker, but defaults to native html5 browser datepicker (not supported by Safari, unforunately). Feel free to implement one of your choosing. + +### URLs + +Some views and URLs were renamed for logical consistency. If this affects you, see source code and the demo GTD site for reference to the new URL names. \ No newline at end of file diff --git a/entrypoint.sh b/entrypoint.sh deleted file mode 100644 index 64aef39..0000000 --- a/entrypoint.sh +++ /dev/null @@ -1,3 +0,0 @@ -#!/bin/sh -pipenv run python manage.py migrate todo -pipenv run python manage.py runserver 0.0.0.0:8888 diff --git a/local.py b/local.py deleted file mode 100644 index 7ef03cf..0000000 --- a/local.py +++ /dev/null @@ -1,26 +0,0 @@ -from .settings import * -import os - -DEBUG = True - -ALLOWED_HOSTS = ['*'] - -DATABASES = { - 'default': { - 'ENGINE': 'django.db.backends.postgresql', - 'NAME': os.environ['DATABASE_NAME'], - 'HOST': os.environ['DATABASE_HOST'], - 'USER': os.environ['DATABASE_USER'], - 'PASSWORD': os.environ['DATABASE_PASSWORD'], - 'PORT': '', - }, -} - -SECRET_KEY = os.environ['SECRET_KEY'] -EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend' - -# TODO-specific settings -TODO_STAFF_ONLY = False -TODO_DEFAULT_LIST_SLUG = 'tickets' -TODO_DEFAULT_ASSIGNEE = None -TODO_PUBLIC_SUBMIT_REDIRECT = '/' diff --git a/mkdocs.yml b/mkdocs.yml new file mode 100644 index 0000000..cbe1ad4 --- /dev/null +++ b/mkdocs.yml @@ -0,0 +1,2 @@ +site_name: "readthedocs" +theme: "readthedocs" diff --git a/pytest.ini b/pytest.ini new file mode 100644 index 0000000..401389d --- /dev/null +++ b/pytest.ini @@ -0,0 +1,4 @@ +[pytest] +DJANGO_SETTINGS_MODULE = test_settings +# -- recommended but optional: +python_files = tests.py test_*.py *_tests.py \ No newline at end of file diff --git a/runme.sh b/runme.sh deleted file mode 100755 index aa128fe..0000000 --- a/runme.sh +++ /dev/null @@ -1,34 +0,0 @@ -#!/bin/bash -appdir=$(cd `dirname $0` && pwd) -apptmp='/tmp' -vmsize='16G' -vmmem='4G' -vcpu=`cat /proc/cpuinfo|grep proc|tail -1|awk '{print $NF}'` -k8sdsk="${apptmp}/k8s-demo.raw" -httpboot='http://ftp.debian.org/debian/dists/stretch/main/installer-amd64/current/images/netboot/debian-installer/amd64' -seed='https://git.ix.gs/public/coins-demo/raw/branch/master/contrib/k8s-seed.txt' -qemu="${appdir}/contrib/qemu-system-x86_64 -L ${appdir}/contrib/share/qemu -net nic -net user,net=100.100.100.0/24,hostfwd=tcp::8888-:80 -m ${vmmem} -smp ${vcpu} -localtime -enable-kvm -cpu host,+nx -M pc -vga std -usbdevice tablet -k en-us -hda ${k8sdsk} -boot once=d -nographic" -wget='wget -cq4O' -### -installvm() { - echo "Allocating ${vmsize} disk..." && \ - fallocate -l ${vmsize} ${k8sdsk} && \ - echo "Downloading Linux kernel..." && \ - ${wget} ${apptmp}/kernel ${httpboot}/linux && \ - echo "Downloading Initial ramdisk..." && \ - ${wget} ${apptmp}/initrd ${httpboot}/initrd.gz && \ - echo "Preparing K8S VM..." && \ - ${qemu} -kernel ${apptmp}/kernel -initrd ${apptmp}/initrd -append "console=ttyS0,115200n8 apt-setup/proposed=true nomodeset fb=false priority=critical locale=en_US url=${seed}" && \ - echo "Housekeeping..." && \ - rm -f ${apptmp}/kernel ${apptmp}/initrd -} -runvm() { - echo "Look like we already has VM, let's go" - ${qemu} -} -### -if [ ! -f "${k8sdsk}" ]; then - installvm && runvm; -else - runvm; -fi diff --git a/setup.py b/setup.py new file mode 100755 index 0000000..d1abd2b --- /dev/null +++ b/setup.py @@ -0,0 +1,46 @@ +# Based on setup.py master example at https://github.com/pypa/sampleproject/blob/master/setup.py + +from io import open +from os import path + +from setuptools import find_packages, setup + +here = path.abspath(path.dirname(__file__)) + +# Get the long description from the README file +with open(path.join(here, "README.md"), encoding="utf-8") as f: + long_description = f.read() + +setup( + name="django-todo", + version="2.4.0", + description="A multi-user, multi-group task management and assignment system for Django.", + long_description=long_description, + long_description_content_type="text/markdown", + url="https://github.com/shacker/django-todo", + author="Scot Hacker", + # For a list of valid classifiers, see https://pypi.org/classifiers/ + classifiers=[ + "Development Status :: 5 - Production/Stable", + "Intended Audience :: Developers", + "License :: OSI Approved :: BSD License", + "Programming Language :: Python :: 3.6", + "Programming Language :: Python :: 3.7", + "Environment :: Web Environment", + "Framework :: Django", + "Operating System :: OS Independent", + "Topic :: Office/Business :: Groupware", + "Topic :: Office/Business :: Groupware", + "Topic :: Software Development :: Bug Tracking", + "Topic :: Software Development :: Bug Tracking", + ], + keywords="lists todo bug bugs tracking", + packages=find_packages(exclude=["contrib", "docs", "tests"]), + python_requires=">=3.5", + install_requires=["unidecode"], + project_urls={ + "Demo Site": "http://django-todo.org", + "Bug Reports": "https://github.com/shacker/django-todo/issues", + "Source": "https://github.com/shacker/django-todo", + }, +) diff --git a/test_settings.py b/test_settings.py new file mode 100644 index 0000000..81007c8 --- /dev/null +++ b/test_settings.py @@ -0,0 +1,92 @@ +import os + +DEBUG = (True,) +BASE_DIR = os.path.dirname(os.path.dirname(__file__)) +DATABASES = { + "default": { + "ENGINE": "django.db.backends.sqlite3" + } +} + +EMAIL_BACKEND = "django.core.mail.backends.console.EmailBackend" + +# Document +TODO_STAFF_ONLY = False +TODO_DEFAULT_LIST_SLUG = "tickets" +TODO_DEFAULT_ASSIGNEE = None +TODO_PUBLIC_SUBMIT_REDIRECT = "/" + +SECRET_KEY = "LKFSD8sdl.,8&sdf--" + +SITE_ID = 1 + +INSTALLED_APPS = ( + "django.contrib.admin", + "django.contrib.auth", + "django.contrib.contenttypes", + "django.contrib.messages", + "django.contrib.sessions", + "django.contrib.sites", + "django.contrib.staticfiles", + "todo", + "dal", + "dal_select2", +) + +ROOT_URLCONF = "base_urls" + +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", +] + +TEMPLATES = [ + { + "BACKEND": "django.template.backends.django.DjangoTemplates", + "DIRS": [os.path.join(BASE_DIR, "todo", "templates")], + "APP_DIRS": True, + "OPTIONS": { + "context_processors": [ + "django.template.context_processors.debug", + "django.template.context_processors.request", + "django.contrib.auth.context_processors.auth", + "django.template.context_processors.media", + "django.template.context_processors.static", + "django.contrib.messages.context_processors.messages", + # Your stuff: custom template context processors go here + ] + }, + } +] + +LOGGING = { + 'version': 1, + 'disable_existing_loggers': False, + 'handlers': { + 'console': { + 'class': 'logging.StreamHandler', + }, + }, + 'loggers': { + '': { + 'handlers': ['console'], + 'level': 'DEBUG', + 'propagate': True, + }, + 'django': { + 'handlers': ['console'], + 'level': 'WARNING', + 'propagate': True, + }, + 'django.request': { + 'handlers': ['console'], + 'level': 'DEBUG', + 'propagate': True, + }, + }, +} diff --git a/todo/__init__.py b/todo/__init__.py new file mode 100644 index 0000000..59a7d90 --- /dev/null +++ b/todo/__init__.py @@ -0,0 +1,12 @@ +""" +A multi-user, multi-group task management and assignment system for Django. +""" +__version__ = '2.3.1' + +__author__ = 'Scot Hacker' +__email__ = 'shacker@birdhouse.org' + +__url__ = 'https://github.com/shacker/django-todo' +__license__ = 'BSD License' + +from . import check diff --git a/todo/admin.py b/todo/admin.py new file mode 100644 index 0000000..a8e6d13 --- /dev/null +++ b/todo/admin.py @@ -0,0 +1,25 @@ +from django.contrib import admin + +from todo.models import Attachment, Comment, Task, TaskList + + +class TaskAdmin(admin.ModelAdmin): + list_display = ("title", "task_list", "completed", "priority", "due_date") + list_filter = ("task_list",) + ordering = ("priority",) + search_fields = ("title",) + + +class CommentAdmin(admin.ModelAdmin): + list_display = ("author", "date", "snippet") + + +class AttachmentAdmin(admin.ModelAdmin): + list_display = ("task", "added_by", "timestamp", "file") + autocomplete_fields = ["added_by", "task"] + + +admin.site.register(TaskList) +admin.site.register(Comment, CommentAdmin) +admin.site.register(Task, TaskAdmin) +admin.site.register(Attachment, AttachmentAdmin) diff --git a/todo/check.py b/todo/check.py new file mode 100644 index 0000000..68560ce --- /dev/null +++ b/todo/check.py @@ -0,0 +1,19 @@ +from django.core.checks import Error, register + +# the sole purpose of this warning is to prevent people who have +# django-autocomplete-light installed but not configured to start the app +@register() +def dal_check(app_configs, **kwargs): + from django.conf import settings + from todo.features import HAS_AUTOCOMPLETE + + if not HAS_AUTOCOMPLETE: + return [] + + errors = [] + missing_apps = {'dal', 'dal_select2'} - set(settings.INSTALLED_APPS) + for missing_app in missing_apps: + errors.append( + Error('{} needs to be in INSTALLED_APPS'.format(missing_app)) + ) + return errors diff --git a/todo/data/import_example.csv b/todo/data/import_example.csv new file mode 100644 index 0000000..1a68723 --- /dev/null +++ b/todo/data/import_example.csv @@ -0,0 +1,4 @@ +Title,Group,Task List,Created By,Created Date,Due Date,Completed,Assigned To,Note,Priority +Make dinner,Scuba Divers,Web project,shacker,,2019-06-14,No,,Please check with mgmt first,3 +Bake bread,Scuba Divers,Example List,mr_random,2012-03-14,,Yes,,, +Bring dessert,Scuba Divers,Web project,user1,2015-06-248,,,user1,Every generation throws a hero up the pop charts,77 \ No newline at end of file diff --git a/todo/features.py b/todo/features.py new file mode 100644 index 0000000..8cd084e --- /dev/null +++ b/todo/features.py @@ -0,0 +1,15 @@ +# The integrated mail queue functionality can enable advanced functionality if +# django-autocomplete-light is installed and configured. We can use this module +# to check for other installed dependencies in the future. + +HAS_AUTOCOMPLETE = True +try: + import dal +except ImportError: + HAS_AUTOCOMPLETE = False + +HAS_TASK_MERGE = False +if HAS_AUTOCOMPLETE: + import dal.autocomplete + if getattr(dal.autocomplete, 'Select2QuerySetView', None) is not None: + HAS_TASK_MERGE = True diff --git a/todo/forms.py b/todo/forms.py new file mode 100644 index 0000000..4793c46 --- /dev/null +++ b/todo/forms.py @@ -0,0 +1,80 @@ +from django import forms +from django.contrib.auth.models import Group +from django.forms import ModelForm +from todo.models import Task, TaskList + + +class AddTaskListForm(ModelForm): + """The picklist showing allowable groups to which a new list can be added + determines which groups the user belongs to. This queries the form object + to derive that list.""" + + def __init__(self, user, *args, **kwargs): + super(AddTaskListForm, self).__init__(*args, **kwargs) + self.fields["group"].queryset = Group.objects.filter(user=user) + self.fields["group"].widget.attrs = { + "id": "id_group", + "class": "custom-select mb-3", + "name": "group", + } + + class Meta: + model = TaskList + exclude = ["created_date", "slug"] + + +class AddEditTaskForm(ModelForm): + """The picklist showing the users to which a new task can be assigned + must find other members of the group this TaskList is attached to.""" + + def __init__(self, user, *args, **kwargs): + super().__init__(*args, **kwargs) + task_list = kwargs.get("initial").get("task_list") + members = task_list.group.user_set.all() + self.fields["assigned_to"].queryset = members + self.fields["assigned_to"].label_from_instance = lambda obj: "%s (%s)" % ( + obj.get_full_name(), + obj.username, + ) + self.fields["assigned_to"].widget.attrs = { + "id": "id_assigned_to", + "class": "custom-select mb-3", + "name": "assigned_to", + } + self.fields["task_list"].value = kwargs["initial"]["task_list"].id + + due_date = forms.DateField(widget=forms.DateInput(attrs={"type": "date"}), required=False) + + title = forms.CharField(widget=forms.widgets.TextInput()) + + note = forms.CharField(widget=forms.Textarea(), required=False) + + class Meta: + model = Task + exclude = [] + + +class AddExternalTaskForm(ModelForm): + """Form to allow users who are not part of the GTD system to file a ticket.""" + + title = forms.CharField(widget=forms.widgets.TextInput(attrs={"size": 35}), label="Summary") + note = forms.CharField(widget=forms.widgets.Textarea(), label="Problem Description") + priority = forms.IntegerField(widget=forms.HiddenInput()) + + class Meta: + model = Task + exclude = ( + "task_list", + "created_date", + "due_date", + "created_by", + "assigned_to", + "completed", + "completed_date", + ) + + +class SearchForm(forms.Form): + """Search.""" + + q = forms.CharField(widget=forms.widgets.TextInput(attrs={"size": 35})) diff --git a/todo/mail/__init__.py b/todo/mail/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/todo/mail/consumers/__init__.py b/todo/mail/consumers/__init__.py new file mode 100644 index 0000000..ea1f7d0 --- /dev/null +++ b/todo/mail/consumers/__init__.py @@ -0,0 +1,9 @@ +def tracker_consumer(**kwargs): + def tracker_factory(producer): + # the import needs to be delayed until call to enable + # using the wrapper in the django settings + from .tracker import tracker_consumer + + return tracker_consumer(producer, **kwargs) + + return tracker_factory diff --git a/todo/mail/consumers/tracker.py b/todo/mail/consumers/tracker.py new file mode 100644 index 0000000..1bf36a2 --- /dev/null +++ b/todo/mail/consumers/tracker.py @@ -0,0 +1,151 @@ +import re +import logging + +from email.charset import Charset as EMailCharset +from django.db import transaction +from django.db.models import Count +from html2text import html2text +from todo.models import Comment, Task, TaskList + +logger = logging.getLogger(__name__) + + +def part_decode(message): + charset = ("ascii", "ignore") + email_charset = message.get_content_charset() + if email_charset: + charset = (EMailCharset(email_charset).input_charset,) + + body = message.get_payload(decode=True) + return body.decode(*charset) + + +def message_find_mime(message, mime_type): + for submessage in message.walk(): + if submessage.get_content_type() == mime_type: + return submessage + return None + + +def message_text(message): + text_part = message_find_mime(message, "text/plain") + if text_part is not None: + return part_decode(text_part) + + html_part = message_find_mime(message, "text/html") + if html_part is not None: + return html2text(part_decode(html_part)) + + # TODO: find something smart to do when no text if found + return "" + + +def format_task_title(format_string, message): + return format_string.format(subject=message["subject"], author=message["from"]) + + +DJANGO_TODO_THREAD = re.compile(r"") + + +def parse_references(task_list, references): + related_messages = [] + answer_thread = None + for related_message in references.split(): + logger.info("checking reference: %r", related_message) + match = re.match(DJANGO_TODO_THREAD, related_message) + if match is None: + related_messages.append(related_message) + continue + + thread_id = int(match.group(1)) + new_answer_thread = Task.objects.filter(task_list=task_list, pk=thread_id).first() + if new_answer_thread is not None: + answer_thread = new_answer_thread + + if answer_thread is None: + logger.info("no answer thread found in references") + else: + logger.info("found an answer thread: %d", answer_thread) + return related_messages, answer_thread + + +def insert_message(task_list, message, priority, task_title_format): + if "message-id" not in message: + logger.warning("missing message id, ignoring message") + return + + if "from" not in message: + logger.warning('missing "From" header, ignoring message') + return + + if "subject" not in message: + logger.warning('missing "Subject" header, ignoring message') + return + + logger.info( + "received message:\t" + f"[Subject: {message['subject']}]\t" + f"[Message-ID: {message['message-id']}]\t" + f"[References: {message['references']}]\t" + f"[To: {message['to']}]\t" + f"[From: {message['from']}]" + ) + + # Due to limitations in MySQL wrt unique_together and TextField (grrr), + # we must use a CharField rather than TextField for message_id. + # In the unlikeley event that we get a VERY long inbound + # message_id, truncate it to the max_length of a MySQL CharField. + original_message_id = message["message-id"] + message_id = ( + (original_message_id[:252] + "...") + if len(original_message_id) > 255 + else original_message_id + ) + message_from = message["from"] + text = message_text(message) + + related_messages, answer_thread = parse_references(task_list, message.get("references", "")) + + # find the most relevant task to add a comment on. + # among tasks in the selected task list, find the task having the + # most email comments the current message references + best_task = ( + Task.objects.filter(task_list=task_list, comment__email_message_id__in=related_messages) + .annotate(num_comments=Count("comment")) + .order_by("-num_comments") + .only("id") + .first() + ) + + # if no related comment is found but a thread message-id + # (generated by django-todo) could be found, use it + if best_task is None and answer_thread is not None: + best_task = answer_thread + + with transaction.atomic(): + if best_task is None: + best_task = Task.objects.create( + priority=priority, + title=format_task_title(task_title_format, message), + task_list=task_list, + ) + logger.info("using task: %r", best_task) + + comment, comment_created = Comment.objects.get_or_create( + task=best_task, + email_message_id=message_id, + defaults={"email_from": message_from, "body": text}, + ) + logger.info("created comment: %r", comment) + + +def tracker_consumer( + producer, group=None, task_list_slug=None, priority=1, task_title_format="[MAIL] {subject}" +): + task_list = TaskList.objects.get(group__name=group, slug=task_list_slug) + for message in producer: + try: + insert_message(task_list, message, priority, task_title_format) + except Exception: + # ignore exceptions during insertion, in order to avoid + logger.exception("got exception while inserting message") diff --git a/todo/mail/delivery.py b/todo/mail/delivery.py new file mode 100644 index 0000000..33f916f --- /dev/null +++ b/todo/mail/delivery.py @@ -0,0 +1,25 @@ +import importlib + +def _declare_backend(backend_path): + backend_path = backend_path.split('.') + backend_module_name = '.'.join(backend_path[:-1]) + class_name = backend_path[-1] + + def backend(*args, headers={}, from_address=None, **kwargs): + def _backend(): + backend_module = importlib.import_module(backend_module_name) + backend = getattr(backend_module, class_name) + return backend(*args, **kwargs) + + if from_address is None: + raise ValueError("missing from_address") + + _backend.from_address = from_address + _backend.headers = headers + return _backend + return backend + + +smtp_backend = _declare_backend('django.core.mail.backends.smtp.EmailBackend') +console_backend = _declare_backend('django.core.mail.backends.console.EmailBackend') +locmem_backend = _declare_backend('django.core.mail.backends.locmem.EmailBackend') diff --git a/todo/mail/producers/__init__.py b/todo/mail/producers/__init__.py new file mode 100644 index 0000000..8ff313b --- /dev/null +++ b/todo/mail/producers/__init__.py @@ -0,0 +1,9 @@ +def imap_producer(**kwargs): + def imap_producer_factory(): + # the import needs to be delayed until call to enable + # using the wrapper in the django settings + from .imap import imap_producer + + return imap_producer(**kwargs) + + return imap_producer_factory diff --git a/todo/mail/producers/imap.py b/todo/mail/producers/imap.py new file mode 100644 index 0000000..977c76f --- /dev/null +++ b/todo/mail/producers/imap.py @@ -0,0 +1,100 @@ +import email +import email.parser +import imaplib +import logging +import time + +from email.policy import default +from contextlib import contextmanager + +logger = logging.getLogger(__name__) + + +def imap_check(command_tuple): + status, ids = command_tuple + assert status == "OK", ids + + +@contextmanager +def imap_connect(host, port, username, password): + conn = imaplib.IMAP4_SSL(host=host, port=port) + conn.login(username, password) + imap_check(conn.list()) + try: + yield conn + finally: + conn.close() + + +def parse_message(message): + for response_part in message: + if not isinstance(response_part, tuple): + continue + + message_metadata, message_content = response_part + email_parser = email.parser.BytesFeedParser(policy=default) + email_parser.feed(message_content) + return email_parser.close() + + +def search_message(conn, *filters): + status, message_ids = conn.search(None, *filters) + for message_id in message_ids[0].split(): + status, message = conn.fetch(message_id, "(RFC822)") + yield message_id, parse_message(message) + + +def imap_producer( + process_all=False, + preserve=False, + host=None, + port=993, + username=None, + password=None, + nap_duration=1, + input_folder="INBOX", +): + logger.debug("starting IMAP worker") + imap_filter = "(ALL)" if process_all else "(UNSEEN)" + + def process_batch(): + logger.debug("starting to process batch") + # reconnect each time to avoid repeated failures due to a lost connection + with imap_connect(host, port, username, password) as conn: + # select the requested folder + imap_check(conn.select(input_folder, readonly=False)) + + try: + for message_uid, message in search_message(conn, imap_filter): + logger.info(f"received message {message_uid}") + try: + yield message + except Exception: + logger.exception( + f"something went wrong while processing {message_uid}" + ) + raise + + if not preserve: + # tag the message for deletion + conn.store(message_uid, '+FLAGS', '\\Deleted') + else: + logger.debug("did not receive any message") + finally: + if not preserve: + # flush deleted messages + conn.expunge() + + while True: + try: + yield from process_batch() + except (GeneratorExit, KeyboardInterrupt): + # the generator was closed, due to the consumer + # breaking out of the loop, or an exception occuring + raise + except Exception: + logger.exception("mail fetching went wrong, retrying") + + # sleep to avoid using too much resources + # TODO: get notified when a new message arrives + time.sleep(nap_duration) diff --git a/todo/management/__init__.py b/todo/management/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/todo/management/commands/__init__.py b/todo/management/commands/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/todo/management/commands/hopper.py b/todo/management/commands/hopper.py new file mode 100644 index 0000000..933656b --- /dev/null +++ b/todo/management/commands/hopper.py @@ -0,0 +1,146 @@ +import factory +from faker import Faker +from titlecase import titlecase +import random + +from django.core.management.base import BaseCommand +from django.contrib.auth.models import Group +from django.contrib.auth import get_user_model +from django.utils.text import slugify + +from todo.models import Task, TaskList + + +num_lists = 5 + + +def gen_title(tc=True): + # faker doesn't provide a way to generate headlines in Title Case, without periods, so make our own. + # With arg `tc=True`, Title Cases The Generated Text + fake = Faker() + thestr = fake.text(max_nb_chars=32).rstrip('.') + if tc: + thestr = titlecase(thestr) + + return thestr + + +def gen_content(): + # faker provides paragraphs as a list; convert with linebreaks + fake = Faker() + grafs = fake.paragraphs() + thestr = '' + for g in grafs: + thestr += "{}\n\n".format(g) + return thestr + + +class Command(BaseCommand): + help = """Create random list and task data for a few fake users.""" + + def add_arguments(self, parser): + parser.add_argument( + "-d", + "--delete", + help="Wipe out existing content before generating new.", + action="store_true") + + def handle(self, *args, **options): + + if options.get('delete'): + # Wipe out previous contents? Cascade deletes the Tasks from the TaskLists. + TaskList.objects.all().delete() + print("Content from previous run deleted.") + print("Working...") + + fake = Faker() # Use to create user's names + + # Create users and groups, add different users to different groups. Staff user is in both groups. + sd_group, created = Group.objects.get_or_create(name='Scuba Divers') + bw_group, created = Group.objects.get_or_create(name='Basket Weavers') + + # Put user1 and user2 in one group, user3 and user4 in another + usernames = ['user1', 'user2', 'user3', 'user4', 'staffer'] + for username in usernames: + if get_user_model().objects.filter(username=username).exists(): + user = get_user_model().objects.get(username=username) + else: + user = get_user_model().objects.create_user( + username=username, + first_name=fake.first_name(), + last_name=fake.last_name(), + email="{}@example.com".format(username), + password="todo") + + if username in ['user1', 'user2']: + user.groups.add(bw_group) + + if username in ['user3', 'user4']: + user.groups.add(sd_group) + + if username == 'staffer': + user.is_staff = True + user.first_name = fake.first_name() + user.last_name = fake.last_name() + user.save() + user.groups.add(bw_group) + user.groups.add(sd_group) + + # Create lists with tasks, plus one with fixed name for externally added tasks + TaskListFactory.create_batch(5, group=bw_group) + TaskListFactory.create_batch(5, group=sd_group) + TaskListFactory.create(name="Public Tickets", slug="tickets", group=bw_group) + + print("For each of two groups, created fake tasks in each of {} fake lists.".format(num_lists)) + + +class TaskListFactory(factory.django.DjangoModelFactory): + """Group not generated here - call with group as arg.""" + + class Meta: + model = TaskList + + name = factory.LazyAttribute(lambda o: gen_title(tc=True)) + slug = factory.LazyAttribute(lambda o: slugify(o.name)) + group = None # Pass this in + + @factory.post_generation + def add_tasks(self, build, extracted, **kwargs): + num = random.randint(5, 25) + TaskFactory.create_batch(num, task_list=self) + + +class TaskFactory(factory.django.DjangoModelFactory): + """TaskList not generated here - call with TaskList as arg.""" + + class Meta: + model = Task + + title = factory.LazyAttribute(lambda o: gen_title(tc=False)) + task_list = None # Pass this in + note = factory.LazyAttribute(lambda o: gen_content()) + priority = factory.LazyAttribute(lambda o: random.randint(1, 100)) + completed = factory.Faker('boolean', chance_of_getting_true=30) + created_by = factory.LazyAttribute(lambda o: get_user_model().objects.get(username='staffer')) # Randomized in post + created_date = factory.Faker('date_this_year') + + @factory.post_generation + def add_details(self, build, extracted, **kwargs): + + fake = Faker() # Use to create user's names + taskgroup = self.task_list.group + + self.created_by = taskgroup.user_set.all().order_by('?').first() + + if self.completed: + self.completed_date = fake.date_this_year() + + # 1/3 of generated tasks have a due_date + if random.randint(1, 3) == 1: + self.due_date = fake.date_this_year() + + # 1/3 of generated tasks are assigned to someone in this tasks's group + if random.randint(1, 3) == 1: + self.assigned_to = taskgroup.user_set.all().order_by('?').first() + + self.save() diff --git a/todo/management/commands/import_csv.py b/todo/management/commands/import_csv.py new file mode 100644 index 0000000..da9398f --- /dev/null +++ b/todo/management/commands/import_csv.py @@ -0,0 +1,57 @@ +import sys +from typing import Any +from pathlib import Path + +from django.core.management.base import BaseCommand, CommandParser + +from todo.operations.csv_importer import CSVImporter + + +class Command(BaseCommand): + help = """Import specifically formatted CSV file containing incoming tasks to be loaded. + For specfic format of inbound CSV, see data/import_example.csv. + For documentation on upsert logic and required fields, see README.md. + """ + + def add_arguments(self, parser: CommandParser) -> None: + + parser.add_argument( + "-f", "--file", dest="file", default=None, help="File to to inbound CSV file." + ) + + def handle(self, *args: Any, **options: Any) -> None: + # Need a file to proceed + if not options.get("file"): + print("Sorry, we need a filename to work from.") + sys.exit(1) + + filepath = Path(options["file"]) + + if not filepath.exists(): + print(f"Sorry, couldn't find file: {filepath}") + sys.exit(1) + + # Encoding "utf-8-sig" means "ignore byte order mark (BOM), which Excel inserts when saving CSVs." + with filepath.open(mode="r", encoding="utf-8-sig") as fileobj: + importer = CSVImporter() + results = importer.upsert(fileobj, as_string_obj=True) + + # Report successes, failures and summaries + print() + if results["upserts"]: + for upsert_msg in results["upserts"]: + print(upsert_msg) + + # Stored errors has the form: + # self.errors = [{3: ["Incorrect foo", "Non-existent bar"]}, {7: [...]}] + if results["errors"]: + for error_dict in results["errors"]: + for k, error_list in error_dict.items(): + print(f"\nSkipped CSV row {k}:") + for msg in error_list: + print(f"- {msg}") + + print() + if results["summaries"]: + for summary_msg in results["summaries"]: + print(summary_msg) diff --git a/todo/management/commands/mail_worker.py b/todo/management/commands/mail_worker.py new file mode 100644 index 0000000..b0750fd --- /dev/null +++ b/todo/management/commands/mail_worker.py @@ -0,0 +1,44 @@ +import logging +import socket +import sys + +from django.core.management.base import BaseCommand +from django.conf import settings + +logger = logging.getLogger(__name__) + + +DEFAULT_IMAP_TIMEOUT = 20 + + +class Command(BaseCommand): + help = "Starts a mail worker" + + def add_arguments(self, parser): + parser.add_argument("--imap_timeout", type=int, default=30) + parser.add_argument("worker_name") + + def handle(self, *args, **options): + if not hasattr(settings, "TODO_MAIL_TRACKERS"): + logger.error("missing TODO_MAIL_TRACKERS setting") + sys.exit(1) + + worker_name = options["worker_name"] + tracker = settings.TODO_MAIL_TRACKERS.get(worker_name, None) + if tracker is None: + logger.error( + "couldn't find configuration for %r in TODO_MAIL_TRACKERS", + worker_name + ) + sys.exit(1) + + # set the default socket timeout (imaplib doesn't enable configuring it) + timeout = options["imap_timeout"] + if timeout: + socket.setdefaulttimeout(timeout) + + # run the mail polling loop + producer = tracker["producer"] + consumer = tracker["consumer"] + + consumer(producer()) diff --git a/todo/migrations/0001_initial.py b/todo/migrations/0001_initial.py new file mode 100644 index 0000000..c29c7a6 --- /dev/null +++ b/todo/migrations/0001_initial.py @@ -0,0 +1,78 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.db import models, migrations +import datetime +from django.conf import settings + + +class Migration(migrations.Migration): + + dependencies = [ + ('auth', '0001_initial'), + migrations.swappable_dependency(settings.AUTH_USER_MODEL), + ] + + operations = [ + migrations.CreateModel( + name='Comment', + fields=[ + ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)), + ('date', models.DateTimeField(default=datetime.datetime.now)), + ('body', models.TextField(blank=True)), + ('author', models.ForeignKey(to=settings.AUTH_USER_MODEL, on_delete=models.CASCADE)), + ], + options={ + }, + bases=(models.Model,), + ), + migrations.CreateModel( + name='Item', + fields=[ + ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)), + ('title', models.CharField(max_length=140)), + ('created_date', models.DateField(auto_now=True, auto_now_add=True)), + ('due_date', models.DateField(null=True, blank=True)), + ('completed', models.BooleanField(default=None)), + ('completed_date', models.DateField(null=True, blank=True)), + ('note', models.TextField(null=True, blank=True)), + ('priority', models.PositiveIntegerField(max_length=3)), + ('assigned_to', models.ForeignKey(related_name='todo_assigned_to', to=settings.AUTH_USER_MODEL, on_delete=models.CASCADE)), + ('created_by', models.ForeignKey(related_name='todo_created_by', to=settings.AUTH_USER_MODEL, on_delete=models.CASCADE)), + ], + options={ + 'ordering': ['priority'], + }, + bases=(models.Model,), + ), + migrations.CreateModel( + name='List', + fields=[ + ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)), + ('name', models.CharField(max_length=60)), + ('slug', models.SlugField(max_length=60, editable=False)), + ('group', models.ForeignKey(to='auth.Group', on_delete=models.CASCADE)), + ], + options={ + 'ordering': ['name'], + 'verbose_name_plural': 'Lists', + }, + bases=(models.Model,), + ), + migrations.AlterUniqueTogether( + name='list', + unique_together=set([('group', 'slug')]), + ), + migrations.AddField( + model_name='item', + name='list', + field=models.ForeignKey(to='todo.List', on_delete=models.CASCADE), + preserve_default=True, + ), + migrations.AddField( + model_name='comment', + name='task', + field=models.ForeignKey(to='todo.Item', on_delete=models.CASCADE), + preserve_default=True, + ), + ] diff --git a/todo/migrations/0002_auto_20150614_2339.py b/todo/migrations/0002_auto_20150614_2339.py new file mode 100644 index 0000000..a346e88 --- /dev/null +++ b/todo/migrations/0002_auto_20150614_2339.py @@ -0,0 +1,24 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.db import models, migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('todo', '0001_initial'), + ] + + operations = [ + migrations.AlterField( + model_name='item', + name='created_date', + field=models.DateField(auto_now=True), + ), + migrations.AlterField( + model_name='item', + name='priority', + field=models.PositiveIntegerField(), + ), + ] diff --git a/todo/migrations/0003_assignee_optional.py b/todo/migrations/0003_assignee_optional.py new file mode 100644 index 0000000..f5f2625 --- /dev/null +++ b/todo/migrations/0003_assignee_optional.py @@ -0,0 +1,22 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.9.5 on 2016-04-09 11:11 +from __future__ import unicode_literals + +from django.conf import settings +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('todo', '0002_auto_20150614_2339'), + ] + + operations = [ + migrations.AlterField( + model_name='item', + name='assigned_to', + field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='todo_assigned_to', to=settings.AUTH_USER_MODEL), + ), + ] diff --git a/todo/migrations/0004_rename_list_tasklist.py b/todo/migrations/0004_rename_list_tasklist.py new file mode 100644 index 0000000..65e9b04 --- /dev/null +++ b/todo/migrations/0004_rename_list_tasklist.py @@ -0,0 +1,52 @@ +# Generated by Django 2.0.2 on 2018-02-09 23:15 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('auth', '0009_alter_user_last_name_max_length'), + ('todo', '0003_assignee_optional'), + ] + + operations = [ + migrations.CreateModel( + name='TaskList', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('name', models.CharField(max_length=60)), + ('slug', models.SlugField(default='')), + ('group', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='auth.Group')), + ], + options={ + 'verbose_name_plural': 'Lists', + 'ordering': ['name'], + }, + ), + migrations.AlterUniqueTogether( + name='list', + unique_together=set(), + ), + migrations.RemoveField( + model_name='list', + name='group', + ), + migrations.RemoveField( + model_name='item', + name='list', + ), + migrations.DeleteModel( + name='List', + ), + migrations.AddField( + model_name='item', + name='task_list', + field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, to='todo.TaskList'), + ), + migrations.AlterUniqueTogether( + name='tasklist', + unique_together={('group', 'slug')}, + ), + ] diff --git a/todo/migrations/0005_auto_20180212_2325.py b/todo/migrations/0005_auto_20180212_2325.py new file mode 100644 index 0000000..5a8c466 --- /dev/null +++ b/todo/migrations/0005_auto_20180212_2325.py @@ -0,0 +1,22 @@ +# Generated by Django 2.0.2 on 2018-02-12 23:25 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('todo', '0004_rename_list_tasklist'), + ] + + operations = [ + migrations.AlterModelOptions( + name='tasklist', + options={'ordering': ['name'], 'verbose_name_plural': 'Task Lists'}, + ), + migrations.AlterField( + model_name='item', + name='completed', + field=models.BooleanField(default=False), + ), + ] diff --git a/todo/migrations/0006_rename_item_model.py b/todo/migrations/0006_rename_item_model.py new file mode 100644 index 0000000..cd9830d --- /dev/null +++ b/todo/migrations/0006_rename_item_model.py @@ -0,0 +1,19 @@ +# Generated by Django 2.0.3 on 2018-03-28 22:40 + +from django.conf import settings +from django.db import migrations + + +class Migration(migrations.Migration): + atomic = False + dependencies = [ + migrations.swappable_dependency(settings.AUTH_USER_MODEL), + ('todo', '0005_auto_20180212_2325'), + ] + + operations = [ + migrations.RenameModel( + old_name='Item', + new_name='Task', + ), + ] diff --git a/todo/migrations/0007_auto_update_created_date.py b/todo/migrations/0007_auto_update_created_date.py new file mode 100644 index 0000000..dfee145 --- /dev/null +++ b/todo/migrations/0007_auto_update_created_date.py @@ -0,0 +1,19 @@ +# Generated by Django 2.0.4 on 2018-04-05 00:24 + +from django.db import migrations, models +import django.utils.timezone + + +class Migration(migrations.Migration): + + dependencies = [ + ('todo', '0006_rename_item_model'), + ] + + operations = [ + migrations.AlterField( + model_name='task', + name='created_date', + field=models.DateField(blank=True, default=django.utils.timezone.now, null=True), + ), + ] diff --git a/todo/migrations/0008_mail_tracker.py b/todo/migrations/0008_mail_tracker.py new file mode 100644 index 0000000..31607d0 --- /dev/null +++ b/todo/migrations/0008_mail_tracker.py @@ -0,0 +1,46 @@ +# Generated by Django 2.1.7 on 2019-03-24 22:50 + +from django.conf import settings +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [("todo", "0007_auto_update_created_date")] + + operations = [ + migrations.AddField( + model_name="comment", + name="email_from", + field=models.CharField(blank=True, max_length=320, null=True), + ), + migrations.AddField( + model_name="comment", + name="email_message_id", + field=models.CharField(blank=True, max_length=255, null=True), + ), + migrations.AlterField( + model_name="comment", + name="author", + field=models.ForeignKey( + blank=True, + null=True, + on_delete=django.db.models.deletion.CASCADE, + to=settings.AUTH_USER_MODEL, + ), + ), + migrations.AlterField( + model_name="task", + name="created_by", + field=models.ForeignKey( + null=True, + on_delete=django.db.models.deletion.CASCADE, + related_name="todo_created_by", + to=settings.AUTH_USER_MODEL, + ), + ), + migrations.AlterUniqueTogether( + name="comment", unique_together={("task", "email_message_id")} + ), + ] diff --git a/todo/migrations/0009_priority_optional.py b/todo/migrations/0009_priority_optional.py new file mode 100644 index 0000000..108ec30 --- /dev/null +++ b/todo/migrations/0009_priority_optional.py @@ -0,0 +1,22 @@ +# Generated by Django 2.1.7 on 2019-03-18 23:14 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('todo', '0008_mail_tracker'), + ] + + operations = [ + migrations.AlterModelOptions( + name='task', + options={'ordering': ['priority', 'created_date']}, + ), + migrations.AlterField( + model_name='task', + name='priority', + field=models.PositiveIntegerField(blank=True, null=True), + ), + ] diff --git a/todo/migrations/0010_attachment.py b/todo/migrations/0010_attachment.py new file mode 100644 index 0000000..885411a --- /dev/null +++ b/todo/migrations/0010_attachment.py @@ -0,0 +1,28 @@ +# Generated by Django 2.2 on 2019-04-06 16:28 + +import datetime +from django.conf import settings +from django.db import migrations, models +import django.db.models.deletion +import todo.models + + +class Migration(migrations.Migration): + + dependencies = [ + migrations.swappable_dependency(settings.AUTH_USER_MODEL), + ('todo', '0009_priority_optional'), + ] + + operations = [ + migrations.CreateModel( + name='Attachment', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('timestamp', models.DateTimeField(default=datetime.datetime.now)), + ('file', models.FileField(max_length=255, upload_to=todo.models.get_attachment_upload_dir)), + ('added_by', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)), + ('task', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='todo.Task')), + ], + ), + ] diff --git a/todo/migrations/__init__.py b/todo/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/todo/models.py b/todo/models.py new file mode 100644 index 0000000..a9582dc --- /dev/null +++ b/todo/models.py @@ -0,0 +1,190 @@ +from __future__ import unicode_literals + +import datetime +import os +import textwrap + +from django.conf import settings +from django.contrib.auth.models import Group +from django.db import DEFAULT_DB_ALIAS, models +from django.db.transaction import Atomic, get_connection +from django.urls import reverse +from django.utils import timezone + + +def get_attachment_upload_dir(instance, filename): + """Determine upload dir for task attachment files. + """ + + return "/".join(["tasks", "attachments", str(instance.task.id), filename]) + + +class LockedAtomicTransaction(Atomic): + """ + modified from https://stackoverflow.com/a/41831049 + this is needed for safely merging + + Does a atomic transaction, but also locks the entire table for any transactions, for the duration of this + transaction. Although this is the only way to avoid concurrency issues in certain situations, it should be used with + caution, since it has impacts on performance, for obvious reasons... + """ + + def __init__(self, *models, using=None, savepoint=None): + if using is None: + using = DEFAULT_DB_ALIAS + super().__init__(using, savepoint) + self.models = models + + def __enter__(self): + super(LockedAtomicTransaction, self).__enter__() + + # Make sure not to lock, when sqlite is used, or you'll run into problems while running tests!!! + if settings.DATABASES[self.using]["ENGINE"] != "django.db.backends.sqlite3": + cursor = None + try: + cursor = get_connection(self.using).cursor() + for model in self.models: + cursor.execute( + "LOCK TABLE {table_name}".format( + table_name=model._meta.db_table + ) + ) + finally: + if cursor and not cursor.closed: + cursor.close() + + +class TaskList(models.Model): + name = models.CharField(max_length=60) + slug = models.SlugField(default="") + group = models.ForeignKey(Group, on_delete=models.CASCADE) + + def __str__(self): + return self.name + + class Meta: + ordering = ["name"] + verbose_name_plural = "Task Lists" + + # Prevents (at the database level) creation of two lists with the same slug in the same group + unique_together = ("group", "slug") + + +class Task(models.Model): + title = models.CharField(max_length=140) + task_list = models.ForeignKey(TaskList, on_delete=models.CASCADE, null=True) + created_date = models.DateField(default=timezone.now, blank=True, null=True) + due_date = models.DateField(blank=True, null=True) + completed = models.BooleanField(default=False) + completed_date = models.DateField(blank=True, null=True) + created_by = models.ForeignKey( + settings.AUTH_USER_MODEL, + null=True, + related_name="todo_created_by", + on_delete=models.CASCADE, + ) + assigned_to = models.ForeignKey( + settings.AUTH_USER_MODEL, + blank=True, + null=True, + related_name="todo_assigned_to", + on_delete=models.CASCADE, + ) + note = models.TextField(blank=True, null=True) + priority = models.PositiveIntegerField(blank=True, null=True) + + # Has due date for an instance of this object passed? + def overdue_status(self): + "Returns whether the Tasks's due date has passed or not." + if self.due_date and datetime.date.today() > self.due_date: + return True + + def __str__(self): + return self.title + + def get_absolute_url(self): + return reverse("todo:task_detail", kwargs={"task_id": self.id}) + + # Auto-set the Task creation / completed date + def save(self, **kwargs): + # If Task is being marked complete, set the completed_date + if self.completed: + self.completed_date = datetime.datetime.now() + super(Task, self).save() + + def merge_into(self, merge_target): + if merge_target.pk == self.pk: + raise ValueError("can't merge a task with self") + + # lock the comments to avoid concurrent additions of comments after the + # update request. these comments would be irremediably lost because of + # the cascade clause + with LockedAtomicTransaction(Comment): + Comment.objects.filter(task=self).update(task=merge_target) + self.delete() + + class Meta: + ordering = ["priority", "created_date"] + + +class Comment(models.Model): + """ + Not using Django's built-in comments because we want to be able to save + a comment and change task details at the same time. Rolling our own since it's easy. + """ + + author = models.ForeignKey( + settings.AUTH_USER_MODEL, on_delete=models.CASCADE, blank=True, null=True + ) + task = models.ForeignKey(Task, on_delete=models.CASCADE) + date = models.DateTimeField(default=datetime.datetime.now) + email_from = models.CharField(max_length=320, blank=True, null=True) + email_message_id = models.CharField(max_length=255, blank=True, null=True) + + body = models.TextField(blank=True) + + class Meta: + # an email should only appear once per task + unique_together = ("task", "email_message_id") + + @property + def author_text(self): + if self.author is not None: + return str(self.author) + + assert self.email_message_id is not None + return str(self.email_from) + + @property + def snippet(self): + body_snippet = textwrap.shorten(self.body, width=35, placeholder="...") + # Define here rather than in __str__ so we can use it in the admin list_display + return "{author} - {snippet}...".format( + author=self.author_text, snippet=body_snippet + ) + + def __str__(self): + return self.snippet + + +class Attachment(models.Model): + """ + Defines a generic file attachment for use in M2M relation with Task. + """ + + task = models.ForeignKey(Task, on_delete=models.CASCADE) + added_by = models.ForeignKey( + settings.AUTH_USER_MODEL, on_delete=models.CASCADE + ) + timestamp = models.DateTimeField(default=datetime.datetime.now) + file = models.FileField(upload_to=get_attachment_upload_dir, max_length=255) + + def filename(self): + return os.path.basename(self.file.name) + + def extension(self): + name, extension = os.path.splitext(self.file.name) + return extension + + def __str__(self): + return f"{self.task.id} - {self.file.name}" diff --git a/todo/operations/__init__.py b/todo/operations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/todo/operations/csv_importer.py b/todo/operations/csv_importer.py new file mode 100644 index 0000000..7b847fa --- /dev/null +++ b/todo/operations/csv_importer.py @@ -0,0 +1,197 @@ +import codecs +import csv +import datetime +import logging + +from django.contrib.auth import get_user_model +from django.contrib.auth.models import Group + +from todo.models import Task, TaskList + +log = logging.getLogger(__name__) + + +class CSVImporter: + """Core upsert functionality for CSV import, for re-use by `import_csv` management command, web UI and tests. + Supplies a detailed log of what was and was not imported at the end. See README for usage notes. + """ + + def __init__(self): + self.errors = [] + self.upserts = [] + self.summaries = [] + self.line_count = 0 + self.upsert_count = 0 + + def upsert(self, fileobj, as_string_obj=False): + """Expects a file *object*, not a file path. This is important because this has to work for both + the management command and the web uploader; the web uploader will pass in in-memory file + with no path! + + Header row is: + Title, Group, Task List, Created Date, Due Date, Completed, Created By, Assigned To, Note, Priority + """ + + if as_string_obj: + # fileobj comes from mgmt command + csv_reader = csv.DictReader(fileobj) + else: + # fileobj comes from browser upload (in-memory) + csv_reader = csv.DictReader(codecs.iterdecode(fileobj, "utf-8")) + + # DI check: Do we have expected header row? + header = csv_reader.fieldnames + expected = [ + "Title", + "Group", + "Task List", + "Created By", + "Created Date", + "Due Date", + "Completed", + "Assigned To", + "Note", + "Priority", + ] + if header != expected: + self.errors.append( + f"Inbound data does not have expected columns.\nShould be: {expected}" + ) + return + + for row in csv_reader: + self.line_count += 1 + + newrow = self.validate_row(row) + if newrow: + # newrow at this point is fully validated, and all FK relations exist, + # e.g. `newrow.get("Assigned To")`, is a Django User instance. + assignee = newrow.get("Assigned To") if newrow.get("Assigned To") else None + created_date = newrow.get("Created Date") if newrow.get("Created Date") else datetime.datetime.today() + due_date = newrow.get("Due Date") if newrow.get("Due Date") else None + priority = newrow.get("Priority") if newrow.get("Priority") else None + + obj, created = Task.objects.update_or_create( + created_by=newrow.get("Created By"), + task_list=newrow.get("Task List"), + title=newrow.get("Title"), + defaults={ + "assigned_to": assignee, + "completed": newrow.get("Completed"), + "created_date": created_date, + "due_date": due_date, + "note": newrow.get("Note"), + "priority": priority, + }, + ) + self.upsert_count += 1 + msg = ( + f'Upserted task {obj.id}: "{obj.title}"' + f' in list "{obj.task_list}" (group "{obj.task_list.group}")' + ) + self.upserts.append(msg) + + self.summaries.append(f"Processed {self.line_count} CSV rows") + self.summaries.append(f"Upserted {self.upsert_count} rows") + self.summaries.append(f"Skipped {self.line_count - self.upsert_count} rows") + + return {"summaries": self.summaries, "upserts": self.upserts, "errors": self.errors} + + def validate_row(self, row): + """Perform data integrity checks and set default values. Returns a valid object for insertion, or False. + Errors are stored for later display. Intentionally not broken up into separate validator functions because + there are interdpendencies, such as checking for existing `creator` in one place and then using + that creator for group membership check in others.""" + + row_errors = [] + + # ####################### + # Task creator must exist + if not row.get("Created By"): + msg = f"Missing required task creator." + row_errors.append(msg) + + creator = get_user_model().objects.filter(username=row.get("Created By")).first() + if not creator: + msg = f"Invalid task creator {row.get('Created By')}" + row_errors.append(msg) + + # ####################### + # If specified, Assignee must exist + assignee = None # Perfectly valid + if row.get("Assigned To"): + assigned = get_user_model().objects.filter(username=row.get("Assigned To")) + if assigned.exists(): + assignee = assigned.first() + else: + msg = f"Missing or invalid task assignee {row.get('Assigned To')}" + row_errors.append(msg) + + # ####################### + # Group must exist + try: + target_group = Group.objects.get(name=row.get("Group")) + except Group.DoesNotExist: + msg = f"Could not find group {row.get('Group')}." + row_errors.append(msg) + target_group = None + + # ####################### + # Task creator must be in the target group + if creator and target_group not in creator.groups.all(): + msg = f"{creator} is not in group {target_group}" + row_errors.append(msg) + + # ####################### + # Assignee must be in the target group + if assignee and target_group not in assignee.groups.all(): + msg = f"{assignee} is not in group {target_group}" + row_errors.append(msg) + + # ####################### + # Task list must exist in the target group + try: + tasklist = TaskList.objects.get(name=row.get("Task List"), group=target_group) + row["Task List"] = tasklist + except TaskList.DoesNotExist: + msg = f"Task list {row.get('Task List')} in group {target_group} does not exist" + row_errors.append(msg) + + # ####################### + # Validate Dates + datefields = ["Due Date", "Created Date"] + for datefield in datefields: + datestring = row.get(datefield) + if datestring: + valid_date = self.validate_date(datestring) + if valid_date: + row[datefield] = valid_date + else: + msg = f"Could not convert {datefield} {datestring} to valid date instance" + row_errors.append(msg) + + # ####################### + # Group membership checks have passed + row["Created By"] = creator + row["Group"] = target_group + if assignee: + row["Assigned To"] = assignee + + # Set Completed + row["Completed"] = (row["Completed"] == "Yes") + + # ####################### + if row_errors: + self.errors.append({self.line_count: row_errors}) + return False + + # No errors: + return row + + def validate_date(self, datestring): + """Inbound date string from CSV translates to a valid python date.""" + try: + date_obj = datetime.datetime.strptime(datestring, "%Y-%m-%d") + return date_obj + except ValueError: + return False diff --git a/todo/static/todo/css/styles.css b/todo/static/todo/css/styles.css new file mode 100644 index 0000000..44b68e3 --- /dev/null +++ b/todo/static/todo/css/styles.css @@ -0,0 +1,4 @@ +label { + display: block; + font-weight: bold; +} diff --git a/todo/static/todo/js/jquery.tablednd_0_5.js b/todo/static/todo/js/jquery.tablednd_0_5.js new file mode 100644 index 0000000..d4c9fec --- /dev/null +++ b/todo/static/todo/js/jquery.tablednd_0_5.js @@ -0,0 +1,382 @@ +/** + * TableDnD plug-in for JQuery, allows you to drag and drop table rows + * You can set up various options to control how the system will work + * Copyright (c) Denis Howlett + * Licensed like jQuery, see http://docs.jquery.com/License. + * + * Configuration options: + * + * onDragStyle + * This is the style that is assigned to the row during drag. There are limitations to the styles that can be + * associated with a row (such as you can't assign a border--well you can, but it won't be + * displayed). (So instead consider using onDragClass.) The CSS style to apply is specified as + * a map (as used in the jQuery css(...) function). + * onDropStyle + * This is the style that is assigned to the row when it is dropped. As for onDragStyle, there are limitations + * to what you can do. Also this replaces the original style, so again consider using onDragClass which + * is simply added and then removed on drop. + * onDragClass + * This class is added for the duration of the drag and then removed when the row is dropped. It is more + * flexible than using onDragStyle since it can be inherited by the row cells and other content. The default + * is class is tDnD_whileDrag. So to use the default, simply customise this CSS class in your + * stylesheet. + * onDrop + * Pass a function that will be called when the row is dropped. The function takes 2 parameters: the table + * and the row that was dropped. You can work out the new order of the rows by using + * table.rows. + * onDragStart + * Pass a function that will be called when the user starts dragging. The function takes 2 parameters: the + * table and the row which the user has started to drag. + * onAllowDrop + * Pass a function that will be called as a row is over another row. If the function returns true, allow + * dropping on that row, otherwise not. The function takes 2 parameters: the dragged row and the row under + * the cursor. It returns a boolean: true allows the drop, false doesn't allow it. + * scrollAmount + * This is the number of pixels to scroll if the user moves the mouse cursor to the top or bottom of the + * window. The page should automatically scroll up or down as appropriate (tested in IE6, IE7, Safari, FF2, + * FF3 beta + * dragHandle + * This is the name of a class that you assign to one or more cells in each row that is draggable. If you + * specify this class, then you are responsible for setting cursor: move in the CSS and only these cells + * will have the drag behaviour. If you do not specify a dragHandle, then you get the old behaviour where + * the whole row is draggable. + * + * Other ways to control behaviour: + * + * Add class="nodrop" to any rows for which you don't want to allow dropping, and class="nodrag" to any rows + * that you don't want to be draggable. + * + * Inside the onDrop method you can also call $.tableDnD.serialize() this returns a string of the form + * []=&[]= so that you can send this back to the server. The table must have + * an ID as must all the rows. + * + * Other methods: + * + * $("...").tableDnDUpdate() + * Will update all the matching tables, that is it will reapply the mousedown method to the rows (or handle cells). + * This is useful if you have updated the table rows using Ajax and you want to make the table draggable again. + * The table maintains the original configuration (so you don't have to specify it again). + * + * $("...").tableDnDSerialize() + * Will serialize and return the serialized string as above, but for each of the matching tables--so it can be + * called from anywhere and isn't dependent on the currentTable being set up correctly before calling + * + * Known problems: + * - Auto-scoll has some problems with IE7 (it scrolls even when it shouldn't), work-around: set scrollAmount to 0 + * + * Version 0.2: 2008-02-20 First public version + * Version 0.3: 2008-02-07 Added onDragStart option + * Made the scroll amount configurable (default is 5 as before) + * Version 0.4: 2008-03-15 Changed the noDrag/noDrop attributes to nodrag/nodrop classes + * Added onAllowDrop to control dropping + * Fixed a bug which meant that you couldn't set the scroll amount in both directions + * Added serialize method + * Version 0.5: 2008-05-16 Changed so that if you specify a dragHandle class it doesn't make the whole row + * draggable + * Improved the serialize method to use a default (and settable) regular expression. + * Added tableDnDupate() and tableDnDSerialize() to be called when you are outside the table + */ +jQuery.tableDnD = { + /** Keep hold of the current table being dragged */ + currentTable : null, + /** Keep hold of the current drag object if any */ + dragObject: null, + /** The current mouse offset */ + mouseOffset: null, + /** Remember the old value of Y so that we don't do too much processing */ + oldY: 0, + + /** Actually build the structure */ + build: function(options) { + // Set up the defaults if any + + this.each(function() { + // This is bound to each matching table, set up the defaults and override with user options + this.tableDnDConfig = jQuery.extend({ + onDragStyle: null, + onDropStyle: null, + // Add in the default class for whileDragging + onDragClass: "tDnD_whileDrag", + onDrop: null, + onDragStart: null, + scrollAmount: 5, + serializeRegexp: /[^\-]*$/, // The regular expression to use to trim row IDs + serializeParamName: null, // If you want to specify another parameter name instead of the table ID + dragHandle: null // If you give the name of a class here, then only Cells with this class will be draggable + }, options || {}); + // Now make the rows draggable + jQuery.tableDnD.makeDraggable(this); + }); + + // Now we need to capture the mouse up and mouse move event + // We can use bind so that we don't interfere with other event handlers + jQuery(document) + .bind('mousemove', jQuery.tableDnD.mousemove) + .bind('mouseup', jQuery.tableDnD.mouseup); + + // Don't break the chain + return this; + }, + + /** This function makes all the rows on the table draggable apart from those marked as "NoDrag" */ + makeDraggable: function(table) { + var config = table.tableDnDConfig; + if (table.tableDnDConfig.dragHandle) { + // We only need to add the event to the specified cells + var cells = jQuery("td."+table.tableDnDConfig.dragHandle, table); + cells.each(function() { + // The cell is bound to "this" + jQuery(this).mousedown(function(ev) { + jQuery.tableDnD.dragObject = this.parentNode; + jQuery.tableDnD.currentTable = table; + jQuery.tableDnD.mouseOffset = jQuery.tableDnD.getMouseOffset(this, ev); + if (config.onDragStart) { + // Call the onDrop method if there is one + config.onDragStart(table, this); + } + return false; + }); + }) + } else { + // For backwards compatibility, we add the event to the whole row + var rows = jQuery("tr", table); // get all the rows as a wrapped set + rows.each(function() { + // Iterate through each row, the row is bound to "this" + var row = jQuery(this); + if (! row.hasClass("nodrag")) { + row.mousedown(function(ev) { + if (ev.target.tagName == "TD") { + jQuery.tableDnD.dragObject = this; + jQuery.tableDnD.currentTable = table; + jQuery.tableDnD.mouseOffset = jQuery.tableDnD.getMouseOffset(this, ev); + if (config.onDragStart) { + // Call the onDrop method if there is one + config.onDragStart(table, this); + } + return false; + } + }).css("cursor", "move"); // Store the tableDnD object + } + }); + } + }, + + updateTables: function() { + this.each(function() { + // this is now bound to each matching table + if (this.tableDnDConfig) { + jQuery.tableDnD.makeDraggable(this); + } + }) + }, + + /** Get the mouse coordinates from the event (allowing for browser differences) */ + mouseCoords: function(ev){ + if(ev.pageX || ev.pageY){ + return {x:ev.pageX, y:ev.pageY}; + } + return { + x:ev.clientX + document.body.scrollLeft - document.body.clientLeft, + y:ev.clientY + document.body.scrollTop - document.body.clientTop + }; + }, + + /** Given a target element and a mouse event, get the mouse offset from that element. + To do this we need the element's position and the mouse position */ + getMouseOffset: function(target, ev) { + ev = ev || window.event; + + var docPos = this.getPosition(target); + var mousePos = this.mouseCoords(ev); + return {x:mousePos.x - docPos.x, y:mousePos.y - docPos.y}; + }, + + /** Get the position of an element by going up the DOM tree and adding up all the offsets */ + getPosition: function(e){ + var left = 0; + var top = 0; + /** Safari fix -- thanks to Luis Chato for this! */ + if (e.offsetHeight == 0) { + /** Safari 2 doesn't correctly grab the offsetTop of a table row + this is detailed here: + http://jacob.peargrove.com/blog/2006/technical/table-row-offsettop-bug-in-safari/ + the solution is likewise noted there, grab the offset of a table cell in the row - the firstChild. + note that firefox will return a text node as a first child, so designing a more thorough + solution may need to take that into account, for now this seems to work in firefox, safari, ie */ + e = e.firstChild; // a table cell + } + + while (e.offsetParent){ + left += e.offsetLeft; + top += e.offsetTop; + e = e.offsetParent; + } + + left += e.offsetLeft; + top += e.offsetTop; + + return {x:left, y:top}; + }, + + mousemove: function(ev) { + if (jQuery.tableDnD.dragObject == null) { + return; + } + + var dragObj = jQuery(jQuery.tableDnD.dragObject); + var config = jQuery.tableDnD.currentTable.tableDnDConfig; + var mousePos = jQuery.tableDnD.mouseCoords(ev); + var y = mousePos.y - jQuery.tableDnD.mouseOffset.y; + //auto scroll the window + var yOffset = window.pageYOffset; + if (document.all) { + // Windows version + //yOffset=document.body.scrollTop; + if (typeof document.compatMode != 'undefined' && + document.compatMode != 'BackCompat') { + yOffset = document.documentElement.scrollTop; + } + else if (typeof document.body != 'undefined') { + yOffset=document.body.scrollTop; + } + + } + + if (mousePos.y-yOffset < config.scrollAmount) { + window.scrollBy(0, -config.scrollAmount); + } else { + var windowHeight = window.innerHeight ? window.innerHeight + : document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body.clientHeight; + if (windowHeight-(mousePos.y-yOffset) < config.scrollAmount) { + window.scrollBy(0, config.scrollAmount); + } + } + + + if (y != jQuery.tableDnD.oldY) { + // work out if we're going up or down... + var movingDown = y > jQuery.tableDnD.oldY; + // update the old value + jQuery.tableDnD.oldY = y; + // update the style to show we're dragging + if (config.onDragClass) { + dragObj.addClass(config.onDragClass); + } else { + dragObj.css(config.onDragStyle); + } + // If we're over a row then move the dragged row to there so that the user sees the + // effect dynamically + var currentRow = jQuery.tableDnD.findDropTargetRow(dragObj, y); + if (currentRow) { + // TODO worry about what happens when there are multiple TBODIES + if (movingDown && jQuery.tableDnD.dragObject != currentRow) { + jQuery.tableDnD.dragObject.parentNode.insertBefore(jQuery.tableDnD.dragObject, currentRow.nextSibling); + } else if (! movingDown && jQuery.tableDnD.dragObject != currentRow) { + jQuery.tableDnD.dragObject.parentNode.insertBefore(jQuery.tableDnD.dragObject, currentRow); + } + } + } + + return false; + }, + + /** We're only worried about the y position really, because we can only move rows up and down */ + findDropTargetRow: function(draggedRow, y) { + var rows = jQuery.tableDnD.currentTable.rows; + for (var i=0; i rowY - rowHeight) && (y < (rowY + rowHeight))) { + // that's the row we're over + // If it's the same as the current row, ignore it + if (row == draggedRow) {return null;} + var config = jQuery.tableDnD.currentTable.tableDnDConfig; + if (config.onAllowDrop) { + if (config.onAllowDrop(draggedRow, row)) { + return row; + } else { + return null; + } + } else { + // If a row has nodrop class, then don't allow dropping (inspired by John Tarr and Famic) + var nodrop = jQuery(row).hasClass("nodrop"); + if (! nodrop) { + return row; + } else { + return null; + } + } + return row; + } + } + return null; + }, + + mouseup: function(e) { + if (jQuery.tableDnD.currentTable && jQuery.tableDnD.dragObject) { + var droppedRow = jQuery.tableDnD.dragObject; + var config = jQuery.tableDnD.currentTable.tableDnDConfig; + // If we have a dragObject, then we need to release it, + // The row will already have been moved to the right place so we just reset stuff + if (config.onDragClass) { + jQuery(droppedRow).removeClass(config.onDragClass); + } else { + jQuery(droppedRow).css(config.onDropStyle); + } + jQuery.tableDnD.dragObject = null; + if (config.onDrop) { + // Call the onDrop method if there is one + config.onDrop(jQuery.tableDnD.currentTable, droppedRow); + } + jQuery.tableDnD.currentTable = null; // let go of the table too + } + }, + + serialize: function() { + if (jQuery.tableDnD.currentTable) { + return jQuery.tableDnD.serializeTable(jQuery.tableDnD.currentTable); + } else { + return "Error: No Table id set, you need to set an id on your table and every row"; + } + }, + + serializeTable: function(table) { + var result = ""; + var tableId = table.id; + var rows = table.rows; + for (var i=0; i 0) result += "&"; + var rowId = rows[i].id; + if (rowId && rowId && table.tableDnDConfig && table.tableDnDConfig.serializeRegexp) { + rowId = rowId.match(table.tableDnDConfig.serializeRegexp)[0]; + } + + result += tableId + '[]=' + rowId; + } + return result; + }, + + serializeTables: function() { + var result = ""; + this.each(function() { + // this is now bound to each matching table + result += jQuery.tableDnD.serializeTable(this); + }); + return result; + } + +} + +jQuery.fn.extend( + { + tableDnD : jQuery.tableDnD.build, + tableDnDUpdate : jQuery.tableDnD.updateTables, + tableDnDSerialize: jQuery.tableDnD.serializeTables + } +); \ No newline at end of file diff --git a/todo/templates/base.html b/todo/templates/base.html new file mode 100644 index 0000000..079d860 --- /dev/null +++ b/todo/templates/base.html @@ -0,0 +1 @@ +This file not actually used by django-todo - here to satisfy the test runner. \ No newline at end of file diff --git a/todo/templates/todo/add_list.html b/todo/templates/todo/add_list.html new file mode 100644 index 0000000..610d918 --- /dev/null +++ b/todo/templates/todo/add_list.html @@ -0,0 +1,23 @@ +{% extends "todo/base.html" %} +{% block page_heading %}{% endblock %} +{% block title %}Add Todo List{% endblock %} +{% block content %} + +

Add a list:

+ +
+ {% csrf_token %} + +
+ + + The full display name for this list. +
+
+ + {{form.group}} +
+ + +
+{% endblock %} \ No newline at end of file diff --git a/todo/templates/todo/add_task_external.html b/todo/templates/todo/add_task_external.html new file mode 100644 index 0000000..8e68849 --- /dev/null +++ b/todo/templates/todo/add_task_external.html @@ -0,0 +1,47 @@ +{% extends "todo/base.html" %} +{% block page_heading %}{% endblock %} +{% block title %}File Ticket{% endblock %} + +{% block content %} + +

{{ task }}

+ +
+ {% csrf_token %} + +

File Trouble Ticket

+

+ Have a support issue? Use this form to report the difficulty - we'll get right back to you. +

+ + {% if form.errors %} + {% for error in form.errors %} +
    +
  • + The {{ error|escape }} field is required. +
  • +
+ {% endfor %} + {% endif %} + + + {% csrf_token %} +
+ + +
+
+ + + + Describe the issue. Please include essential details. + +
+ + +

+
+ +{% endblock %} diff --git a/todo/templates/todo/base.html b/todo/templates/todo/base.html new file mode 100644 index 0000000..cf146c4 --- /dev/null +++ b/todo/templates/todo/base.html @@ -0,0 +1,7 @@ +{% extends "base.html" %} +{% load static %} + +{% block extrahead %} + + +{% endblock extrahead %} diff --git a/todo/templates/todo/del_list.html b/todo/templates/todo/del_list.html new file mode 100644 index 0000000..8d9ba6d --- /dev/null +++ b/todo/templates/todo/del_list.html @@ -0,0 +1,33 @@ +{% extends "todo/base.html" %} +{% block title %}Delete list{% endblock %} + +{% block content %} + {% if user.is_staff %} +

Delete entire list: {{ task_list.name }} ?

+ +

Category tally:

+ +
    +
  • Incomplete: {{ task_count_undone }}
  • +
  • Complete: {{ task_count_done }}
  • +
  • + Total: {{ task_count_total }} +
  • +
+ +

... all of which will be irretrievably + blown away. Are you sure you want to do that?

+ +
+ {% csrf_token %} + +

+ Return to list: {{ task_list.name }} + +

+
+ + +{% else %} +

Sorry, you don't have permission to delete lists. Please contact your group administrator.

+{% endif %} {% endblock %} \ No newline at end of file diff --git a/todo/templates/todo/email/assigned_body.txt b/todo/templates/todo/email/assigned_body.txt new file mode 100644 index 0000000..deb71bc --- /dev/null +++ b/todo/templates/todo/email/assigned_body.txt @@ -0,0 +1,17 @@ +{{ task.assigned_to.first_name }} - + +A new task on the list {{ task.task_list.name }} has been assigned to you by {{ task.created_by.get_full_name }}: + +{{ task.title }} + +{% if task.note %} +{% autoescape off %} +Note: {{ task.note }} +{% endautoescape %} +{% endif %} + +Task details/comments: +http://{{ site }}{% url 'todo:task_detail' task.id %} + +List {{ task.task_list.name }}: +http://{{ site }}{% url 'todo:list_detail' task.task_list.id task.task_list.slug %} diff --git a/todo/templates/todo/email/assigned_subject.txt b/todo/templates/todo/email/assigned_subject.txt new file mode 100644 index 0000000..4e1e2b8 --- /dev/null +++ b/todo/templates/todo/email/assigned_subject.txt @@ -0,0 +1 @@ +A new task has been assigned to you - {% autoescape off %}{{ task.title }}{% endautoescape %} \ No newline at end of file diff --git a/todo/templates/todo/email/newcomment_body.txt b/todo/templates/todo/email/newcomment_body.txt new file mode 100644 index 0000000..81947ff --- /dev/null +++ b/todo/templates/todo/email/newcomment_body.txt @@ -0,0 +1,16 @@ +A new task comment has been added. + +Task: {{ task.title }} +Commenter: {{ user.first_name }} {{ user.last_name }} + +Comment: +{% autoescape off %} +{{ body }} +{% endautoescape %} + +Task details/comments: +https://{{ site }}{% url 'todo:task_detail' task.id %} + +List {{ task.task_list.name }}: +https://{{ site }}{% url 'todo:list_detail' task.task_list.id task.task_list.slug %} + diff --git a/todo/templates/todo/import_csv.html b/todo/templates/todo/import_csv.html new file mode 100644 index 0000000..9d5cb85 --- /dev/null +++ b/todo/templates/todo/import_csv.html @@ -0,0 +1,84 @@ +{% extends "todo/base.html" %} +{% load static %} + +{% block title %}Import CSV{% endblock %} + +{% block content %} +

+ Import CSV +

+ +

+ Batch-import tasks by uploading a specifically-formatted CSV. + See documentation for formatting rules. + Successs and failures will be reported here. +

+ + {% if results %} +
+
+ Results of CSV upload +
+
+ + {% if results.summaries %} +

+ Summary: +

+
    + {% for line in results.summaries %} +
  • {{ line }}
  • + {% endfor %} +
+ {% endif %} + + {% if results.upserts %} +

+ Upserts (tasks created or updated): +

+
    + {% for line in results.upserts %} +
  • {{ line }}
  • + {% endfor %} +
+ {% endif %} + + {% if results.errors %} +

+ Errors (tasks NOT created or updated): +

+
    + {% for error_row in results.errors %} + {% for k, error_list in error_row.items %} +
  • CSV row {{ k }}
  • +
      + {% for err in error_list %} +
    • {{ err }}
    • + {% endfor %} +
    + {% endfor %} + {% endfor %} +
+ {% endif %} + + +
+
+ {% endif %} + +
+
+ Upload Tasks +
+
+
+ {% csrf_token %} +
+ +
+ +
+
+
+ +{% endblock %} diff --git a/todo/templates/todo/include/task_edit.html b/todo/templates/todo/include/task_edit.html new file mode 100644 index 0000000..936d465 --- /dev/null +++ b/todo/templates/todo/include/task_edit.html @@ -0,0 +1,55 @@ +{# Form used by both Add Task and Edit Task views #} + +
+ {% csrf_token %} +
+
+ + +
+ +
+ + + + Describe the task or bug. Provide steps to reproduce the issue. + +
+ +
+ + +
+ +
+ + {# See todo.forms.AddEditTaskForm #} + {{form.assigned_to}} +
+ +
+
+ + + + Email notifications will only be sent if task is assigned to someone other than yourself. + +
+
+ + + + + +

+ +

+ +
+
diff --git a/todo/templates/todo/include/toggle_delete.html b/todo/templates/todo/include/toggle_delete.html new file mode 100644 index 0000000..80248cb --- /dev/null +++ b/todo/templates/todo/include/toggle_delete.html @@ -0,0 +1,12 @@ + + {% if list_slug != "mine" %} + {% if view_completed %} + View incomplete tasks + + {% else %} + View completed tasks + + {% endif %} + + Delete this list + {% endif %} diff --git a/todo/templates/todo/list_detail.html b/todo/templates/todo/list_detail.html new file mode 100644 index 0000000..9ac5c64 --- /dev/null +++ b/todo/templates/todo/list_detail.html @@ -0,0 +1,114 @@ +{% extends "todo/base.html" %} +{% load static %} + +{% block title %}Todo List: {{ task_list.name }}{% endblock %} + +{% block content %} + + {% if list_slug != "mine" %} + + + {# Task edit / new task form #} +
+ {% include 'todo/include/task_edit.html' %} +
+
+ {% endif %} + + {% if tasks %} + {% if list_slug == "mine" %} +

Tasks assigned to me (in all groups)

+ {% else %} +

{{ view_completed|yesno:"Completed tasks, Tasks" }} in "{{ task_list.name }}"

+

In workgroup "{{ task_list.group }}" - drag rows to set priorities.

+ {% endif %} + + + + + + + + + + + + {% for task in tasks %} + + + + + + + + + {% endfor %} +
TaskCreatedDue onOwnerAssignedMark
+ {{ task.title|truncatewords:10 }} + + {{ task.created_date|date:"m/d/Y" }} + + + {{ task.due_date|date:"m/d/Y" }} + + + {{ task.created_by }} + + {% if task.assigned_to %}{{ task.assigned_to }}{% else %}Anyone{% endif %} + +
+ {% csrf_token %} + +
+
+ + {% include 'todo/include/toggle_delete.html' %} + + {% else %} +

No tasks on this list yet (add one!)

+ {% include 'todo/include/toggle_delete.html' %} + + {% endif %} + +{% endblock %} + +{% block extra_js %} + + + +{% endblock extra_js %} diff --git a/todo/templates/todo/list_lists.html b/todo/templates/todo/list_lists.html new file mode 100644 index 0000000..a523b6c --- /dev/null +++ b/todo/templates/todo/list_lists.html @@ -0,0 +1,33 @@ +{% extends "todo/base.html" %} + +{% block title %}{{ list_title }} Todo Lists{% endblock %} + +{% block content %} +

Todo Lists

+ +

{{ task_count }} tasks in {{ list_count }} list{{ list_count|pluralize }}

+ + {% regroup lists by group as section_list %} + {% for group in section_list %} +

Group: {{ group.grouper }}

+
    + {% for task in group.list %} +
  • + {{ task.name }} + {{ task.task_set.count }} +
  • + {% endfor %} +
+ {% endfor %} + +
+ + {% if user.is_staff %} + Create new todo list + {% else %} + If you were staff, you could create a new list + {% endif %} + +
+ +{% endblock %} \ No newline at end of file diff --git a/todo/templates/todo/search_results.html b/todo/templates/todo/search_results.html new file mode 100644 index 0000000..2269f19 --- /dev/null +++ b/todo/templates/todo/search_results.html @@ -0,0 +1,30 @@ +{% extends "todo/base.html" %} + +{% block title %}Search results{% endblock %} +{% block content_title %}

Search

{% endblock %} + +{% block content %} + {% if found_tasks %} +

{{found_tasks.count}} search results for term: "{{ query_string }}"

+
+ {% for f in found_tasks %} +

+ + {{ f.title }} + +
+ + In list: + + {{ f.task_list.name }} + +
Assigned to: {% if f.assigned_to %}{{ f.assigned_to }}{% else %}Anyone{% endif %} +
Complete: {{ f.completed|yesno:"Yes,No" }} +
+

+ {% endfor %} +
+ {% else %} +

No results to show, sorry.

+ {% endif %} +{% endblock %} diff --git a/todo/templates/todo/task_detail.html b/todo/templates/todo/task_detail.html new file mode 100644 index 0000000..8eca361 --- /dev/null +++ b/todo/templates/todo/task_detail.html @@ -0,0 +1,223 @@ +{% extends "todo/base.html" %} + +{% block title %}Task:{{ task.title }}{% endblock %} + +{% block extrahead %} + +{{ form.media }} +{{ merge_form.media }} +{% endblock %} + + + +{% block content %} +
+
+
+

{{ task.title }}

+ {% if task.note %} +
{{ task.note|safe|urlize|linebreaks }}
+ {% endif %} +
+
+ +
+
    +
  • + + +
    + {% csrf_token %} +
    + +
    +
    + +
    + {% csrf_token %} +
    + +
    +
    +
  • +
  • + Assigned to: + {% if task.assigned_to %} {{ task.assigned_to.get_full_name }} {% else %} Anyone {% endif %} +
  • +
  • + Reported by: {{ task.created_by.get_full_name }} +
  • +
  • + Due date: {{ task.due_date }} +
  • + + {% if task.completed %} +
  • + Completed on: {{ task.completed_date}} +
  • + {% else %} +
  • + Completed: {{ task.completed|yesno:"Yes,No" }} +
  • + {% endif %} + +
  • + In list: + + {{ task.task_list }} + +
  • +
+
+
+ +
+ {# Task edit / new task form #} + {% include 'todo/include/task_edit.html' %} + {% if merge_form is not None %} +
+
+
Merge task
+
+
+

Merging is a destructive operation. This task will not exist anymore, and comments will be moved to the target task.

+ {% csrf_token %} + {% for field in merge_form.visible_fields %} +

+ {{ field.errors }} + {{ field }} +

+ {% endfor %} + +
+
+
+
+ {% endif %} +
+ +{% if attachments_enabled %} +
+
+ Attachments +
+ +
+ {% if task.attachment_set.count %} +
+ + + + + + + + + + + + {% for attachment in task.attachment_set.all %} + + + + + + + + {% endfor %} + +
FileUploadedByTypeRemove
{{ attachment.filename }}{{ attachment.timestamp }}{{ attachment.added_by.get_full_name }}{{ attachment.extension.lower }} +
+ {% csrf_token %} + +
+
+
+ {% endif %} + +
+ {% csrf_token %} +
+
+ + +
+
+ +
+
+
+ +
+
+{% endif %} + +
+
Add comment
+
+ {% csrf_token %} +
+ +
+ +
+
+ +
+ {% if comment_list %} +
Comments on this task
+ {% for comment in comment_list %} +
+
+
+ {% if comment.email_message_id %} + email + {% endif %} + {{ comment.author_text }} +
+ + {{ comment.date|date:"F d Y P" }} + +
+
+ {{ comment.body|safe|urlize|linebreaks }} +
+
+ {% endfor %} + {% else %} +
No comments (yet).
+ {% endif %} +
+{% endblock %} + +{% block extra_js %} + {# Support file attachment uploader #} + +{% endblock extra_js %} + diff --git a/todo/tests/__init__.py b/todo/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/todo/tests/conftest.py b/todo/tests/conftest.py new file mode 100644 index 0000000..8e6138e --- /dev/null +++ b/todo/tests/conftest.py @@ -0,0 +1,36 @@ +import pytest + +from django.contrib.auth.models import Group + +from todo.models import Task, TaskList + + +@pytest.fixture +def todo_setup(django_user_model): + # Two groups with different users, two sets of tasks. + + g1 = Group.objects.create(name="Workgroup One") + u1 = django_user_model.objects.create_user( + username="u1", password="password", email="u1@example.com" + ) + u1.groups.add(g1) + tlist1 = TaskList.objects.create(group=g1, name="Zip", slug="zip") + Task.objects.create(created_by=u1, title="Task 1", task_list=tlist1, priority=1) + Task.objects.create(created_by=u1, title="Task 2", task_list=tlist1, priority=2, completed=True) + Task.objects.create(created_by=u1, title="Task 3", task_list=tlist1, priority=3) + + g2 = Group.objects.create(name="Workgroup Two") + u2 = django_user_model.objects.create_user( + username="u2", password="password", email="u2@example.com" + ) + u2.groups.add(g2) + tlist2 = TaskList.objects.create(group=g2, name="Zap", slug="zap") + Task.objects.create(created_by=u2, title="Task 1", task_list=tlist2, priority=1) + Task.objects.create(created_by=u2, title="Task 2", task_list=tlist2, priority=2, completed=True) + Task.objects.create(created_by=u2, title="Task 3", task_list=tlist2, priority=3) + + +@pytest.fixture() +# Set up an in-memory mail server to receive test emails +def email_backend_setup(settings): + settings.EMAIL_BACKEND = "django.core.mail.backends.locmem.EmailBackend" diff --git a/todo/tests/data/csv_import_data.csv b/todo/tests/data/csv_import_data.csv new file mode 100644 index 0000000..22c0520 --- /dev/null +++ b/todo/tests/data/csv_import_data.csv @@ -0,0 +1,4 @@ +Title,Group,Task List,Created By,Created Date,Due Date,Completed,Assigned To,Note,Priority +Make dinner,Workgroup One,Zip,u1,,2019-06-14,No,u1,This is note one,3 +Bake bread,Workgroup One,Zip,u1,2012-03-14,,Yes,,, +Bring dessert,Workgroup Two,Zap,u2,2015-06-248,,,,This is note two,77 \ No newline at end of file diff --git a/todo/tests/test_import.py b/todo/tests/test_import.py new file mode 100644 index 0000000..0ab612f --- /dev/null +++ b/todo/tests/test_import.py @@ -0,0 +1,76 @@ +import datetime +from pathlib import Path + +import pytest +from django.contrib.auth import get_user_model + +from todo.models import Task, TaskList +from todo.operations.csv_importer import CSVImporter + + +""" +Exercise the "Import CSV" feature, which shares a functional module that serves +both the `import_csv` management command and the "Import CSV" web interface. +""" + + +@pytest.mark.django_db +@pytest.fixture +def import_setup(todo_setup): + app_path = Path(__file__).resolve().parent.parent + filepath = Path(app_path, "tests/data/csv_import_data.csv") + with filepath.open(mode="r", encoding="utf-8-sig") as fileobj: + importer = CSVImporter() + results = importer.upsert(fileobj, as_string_obj=True) + assert results + return {"results": results} + + +@pytest.mark.django_db +def test_setup(todo_setup): + """Confirm what we should have from conftest, prior to importing CSV.""" + assert TaskList.objects.all().count() == 2 + assert Task.objects.all().count() == 6 + + +@pytest.mark.django_db +def test_import(import_setup): + """Confirm that importing the CSV gave us two more rows (one should have been skipped)""" + assert Task.objects.all().count() == 8 # 2 out of 3 rows should have imported; one was an error + + +@pytest.mark.django_db +def test_report(import_setup): + """Confirm that importing the CSV returned expected report messaging.""" + + results = import_setup["results"] + + assert "Processed 3 CSV rows" in results["summaries"] + assert "Upserted 2 rows" in results["summaries"] + assert "Skipped 1 rows" in results["summaries"] + + assert isinstance(results["errors"], list) + assert len(results["errors"]) == 1 + assert ( + results["errors"][0].get(3)[0] + == "Could not convert Created Date 2015-06-248 to valid date instance" + ) + + assert ( + 'Upserted task 7: "Make dinner" in list "Zip" (group "Workgroup One")' in results["upserts"] + ) + assert ( + 'Upserted task 8: "Bake bread" in list "Zip" (group "Workgroup One")' in results["upserts"] + ) + + +@pytest.mark.django_db +def test_inserted_row(import_setup): + """Confirm that one inserted row is exactly right.""" + task = Task.objects.get(title="Make dinner", task_list__name="Zip") + assert task.created_by == get_user_model().objects.get(username="u1") + assert task.assigned_to == get_user_model().objects.get(username="u1") + assert not task.completed + assert task.note == "This is note one" + assert task.priority == 3 + assert task.created_date == datetime.datetime.today().date() diff --git a/todo/tests/test_tracker.py b/todo/tests/test_tracker.py new file mode 100644 index 0000000..3387d75 --- /dev/null +++ b/todo/tests/test_tracker.py @@ -0,0 +1,67 @@ +import pytest + +from django.core import mail + +from todo.models import Task, Comment +from todo.mail.consumers import tracker_consumer +from email.message import EmailMessage + + +def consumer(*args, title_format="[TEST] {subject}", **kwargs): + return tracker_consumer( + group="Workgroup One", + task_list_slug="zip", + priority=1, + task_title_format=title_format, + )(*args, **kwargs) + + +def make_message(subject, content): + msg = EmailMessage() + msg.set_content(content) + msg['Subject'] = subject + return msg + + +def test_tracker_task_creation(todo_setup, django_user_model): + msg = make_message("test1 subject", "test1 content") + msg['From'] = 'test1@example.com' + msg['Message-ID'] = '' + + # test task creation + task_count = Task.objects.count() + consumer([msg]) + + assert task_count + 1 == Task.objects.count(), "task wasn't created" + task = Task.objects.filter(title="[TEST] test1 subject").first() + assert task is not None, "task was created with the wrong name" + + # test thread answers + msg = make_message("test2 subject", "test2 content") + msg['From'] = 'test1@example.com' + msg['Message-ID'] = '' + msg['References'] = ' ' + + task_count = Task.objects.count() + consumer([msg]) + assert task_count == Task.objects.count(), "comment created another task" + Comment.objects.get( + task=task, + body__contains="test2 content", + email_message_id='' + ) + + # test notification answer + msg = make_message("test3 subject", "test3 content") + msg['From'] = 'test1@example.com' + msg['Message-ID'] = '' + msg['References'] = ' '.format(task.pk) + + task_count = Task.objects.count() + consumer([msg]) + assert task_count == Task.objects.count(), "comment created another task" + Comment.objects.get( + task=task, + body__contains="test3 content", + email_message_id='' + ) diff --git a/todo/tests/test_utils.py b/todo/tests/test_utils.py new file mode 100644 index 0000000..48f3149 --- /dev/null +++ b/todo/tests/test_utils.py @@ -0,0 +1,59 @@ +import pytest + +from django.core import mail + +from todo.models import Task, Comment +from todo.utils import send_notify_mail, send_email_to_thread_participants + + +def test_send_notify_mail_not_me(todo_setup, django_user_model, email_backend_setup): + """Assign a task to someone else, mail should be sent. + TODO: Future tests could check for email contents. + """ + + u1 = django_user_model.objects.get(username="u1") + u2 = django_user_model.objects.get(username="u2") + + task = Task.objects.filter(created_by=u1).first() + task.assigned_to = u2 + task.save() + send_notify_mail(task) + assert len(mail.outbox) == 1 + + +def test_send_notify_mail_myself(todo_setup, django_user_model, email_backend_setup): + """Assign a task to myself, no mail should be sent. + """ + + u1 = django_user_model.objects.get(username="u1") + task = Task.objects.filter(created_by=u1).first() + task.assigned_to = u1 + task.save() + send_notify_mail(task) + assert len(mail.outbox) == 0 + + +def test_send_email_to_thread_participants(todo_setup, django_user_model, email_backend_setup): + """For a given task authored by one user, add comments by two other users. + Notification email should be sent to all three users.""" + + u1 = django_user_model.objects.get(username="u1") + task = Task.objects.filter(created_by=u1).first() + + u3 = django_user_model.objects.create_user( + username="u3", password="zzz", email="u3@example.com" + ) + u4 = django_user_model.objects.create_user( + username="u4", password="zzz", email="u4@example.com" + ) + Comment.objects.create(author=u3, task=task, body="Hello") + Comment.objects.create(author=u4, task=task, body="Hello") + + send_email_to_thread_participants(task, "test body", u1) + assert len(mail.outbox) == 1 # One message to multiple recipients + assert "u1@example.com" in mail.outbox[0].recipients() + assert "u3@example.com" in mail.outbox[0].recipients() + assert "u4@example.com" in mail.outbox[0].recipients() + +# FIXME: Add tests for: +# Attachments: Test whether allowed, test multiple, test extensions \ No newline at end of file diff --git a/todo/tests/test_views.py b/todo/tests/test_views.py new file mode 100644 index 0000000..6c7188f --- /dev/null +++ b/todo/tests/test_views.py @@ -0,0 +1,256 @@ +import bleach +import pytest + +from django.contrib.auth import get_user_model +from django.contrib.auth.models import Group +from django.urls import reverse + +from todo.models import Task, TaskList + +""" +First the "smoketests" - do they respond at all for a logged in admin user? +Next permissions tests - some views should respond for staffers only. +After that, view contents and behaviors. +""" + + +@pytest.mark.django_db +def test_todo_setup(todo_setup): + assert Task.objects.all().count() == 6 + + +def test_view_list_lists(todo_setup, admin_client): + url = reverse("todo:lists") + response = admin_client.get(url) + assert response.status_code == 200 + + +def test_view_reorder(todo_setup, admin_client): + url = reverse("todo:reorder_tasks") + response = admin_client.get(url) + assert response.status_code == 201 # Special case return value expected + + +def test_view_external_add(todo_setup, admin_client, settings): + default_list = TaskList.objects.first() + settings.TODO_DEFAULT_LIST_SLUG = default_list.slug + assert settings.TODO_DEFAULT_LIST_SLUG == default_list.slug + url = reverse("todo:external_add") + response = admin_client.get(url) + assert response.status_code == 200 + + +def test_view_mine(todo_setup, admin_client): + url = reverse("todo:mine") + response = admin_client.get(url) + assert response.status_code == 200 + + +def test_view_list_completed(todo_setup, admin_client): + tlist = TaskList.objects.get(slug="zip") + url = reverse( + "todo:list_detail_completed", kwargs={"list_id": tlist.id, "list_slug": tlist.slug} + ) + response = admin_client.get(url) + assert response.status_code == 200 + + +def test_view_list(todo_setup, admin_client): + tlist = TaskList.objects.get(slug="zip") + url = reverse("todo:list_detail", kwargs={"list_id": tlist.id, "list_slug": tlist.slug}) + response = admin_client.get(url) + assert response.status_code == 200 + + +def test_del_list(todo_setup, admin_client): + tlist = TaskList.objects.get(slug="zip") + url = reverse("todo:del_list", kwargs={"list_id": tlist.id, "list_slug": tlist.slug}) + response = admin_client.get(url) + assert response.status_code == 200 + + +def test_view_add_list(todo_setup, admin_client): + url = reverse("todo:add_list") + response = admin_client.get(url) + assert response.status_code == 200 + + +def test_view_task_detail(todo_setup, admin_client): + task = Task.objects.first() + url = reverse("todo:task_detail", kwargs={"task_id": task.id}) + response = admin_client.get(url) + assert response.status_code == 200 + + +def test_del_task(todo_setup, admin_user, client): + task = Task.objects.first() + url = reverse("todo:delete_task", kwargs={"task_id": task.id}) + # View accepts POST, not GET + client.login(username="admin", password="password") + response = client.get(url) + assert response.status_code == 403 + response = client.post(url) + assert not Task.objects.filter(id=task.id).exists() + + +def test_task_toggle_done(todo_setup, admin_user, client): + task = Task.objects.first() + assert not task.completed + url = reverse("todo:task_toggle_done", kwargs={"task_id": task.id}) + # View accepts POST, not GET + client.login(username="admin", password="password") + response = client.get(url) + assert response.status_code == 403 + + client.post(url) + task.refresh_from_db() + assert task.completed + + +def test_view_search(todo_setup, admin_client): + url = reverse("todo:search") + response = admin_client.get(url) + assert response.status_code == 200 + + +@pytest.mark.django_db +def test_no_javascript_in_task_note(todo_setup, client): + task_list = TaskList.objects.first() + user = get_user_model().objects.get(username="u2") + title = "Some Unique String" + note = "foo bar" + data = { + "task_list": task_list.id, + "created_by": user.id, + "priority": 10, + "title": title, + "note": note, + "add_edit_task": "Submit", + } + + client.login(username="u2", password="password") + url = reverse("todo:list_detail", kwargs={"list_id": task_list.id, "list_slug": task_list.slug}) + + response = client.post(url, data) + assert response.status_code == 302 + + # Retrieve new task and compare notes field + task = Task.objects.get(title=title) + assert task.note != note # Should have been modified by bleach since note included javascript! + assert task.note == bleach.clean(note, strip=True) + + +@pytest.mark.django_db +def test_no_javascript_in_comments(todo_setup, client): + user = get_user_model().objects.get(username="u2") + client.login(username="u2", password="password") + + task = Task.objects.first() + task.created_by = user + task.save() + + user.groups.add(task.task_list.group) + + comment = "foo bar" + data = {"comment-body": comment, "add_comment": "Submit"} + url = reverse("todo:task_detail", kwargs={"task_id": task.id}) + + response = client.post(url, data) + assert response.status_code == 200 + + task.refresh_from_db() + newcomment = task.comment_set.last() + assert newcomment != comment # Should have been modified by bleach + assert newcomment.body == bleach.clean(comment, strip=True) + + +# ### PERMISSIONS ### + +def test_view_add_list_nonadmin(todo_setup, client): + url = reverse("todo:add_list") + client.login(username="you", password="password") + response = client.get(url) + assert response.status_code == 302 # Redirected to login + + +def test_view_del_list_nonadmin(todo_setup, client): + tlist = TaskList.objects.get(slug="zip") + url = reverse("todo:del_list", kwargs={"list_id": tlist.id, "list_slug": tlist.slug}) + client.login(username="you", password="password") + response = client.get(url) + assert response.status_code == 302 # Fedirected to login + + +def test_view_list_mine(todo_setup, client): + """View a list in a group I belong to. + """ + tlist = TaskList.objects.get(slug="zip") # User u1 is in this group's list + url = reverse("todo:list_detail", kwargs={"list_id": tlist.id, "list_slug": tlist.slug}) + client.login(username="u1", password="password") + response = client.get(url) + assert response.status_code == 200 + + +def test_view_list_not_mine(todo_setup, client): + """View a list in a group I don't belong to. + """ + tlist = TaskList.objects.get(slug="zip") # User u1 is in this group, user u2 is not. + url = reverse("todo:list_detail", kwargs={"list_id": tlist.id, "list_slug": tlist.slug}) + client.login(username="u2", password="password") + response = client.get(url) + assert response.status_code == 403 + + +def test_view_task_mine(todo_setup, client): + # Users can always view their own tasks + task = Task.objects.filter(created_by__username="u1").first() + client.login(username="u1", password="password") + url = reverse("todo:task_detail", kwargs={"task_id": task.id}) + response = client.get(url) + assert response.status_code == 200 + + +def test_view_task_my_group(todo_setup, client, django_user_model): + """User can always view tasks that are NOT theirs IF the task is in a shared group. + u1 and u2 are in different groups in the fixture - + Put them in the same group.""" + g1 = Group.objects.get(name="Workgroup One") + u2 = django_user_model.objects.get(username="u2") + u2.groups.add(g1) + + # Now u2 should be able to view one of u1's tasks. + task = Task.objects.filter(created_by__username="u1").first() + url = reverse("todo:task_detail", kwargs={"task_id": task.id}) + client.login(username="u2", password="password") + response = client.get(url) + assert response.status_code == 200 + + +def test_view_task_not_in_my_group(todo_setup, client): + # User canNOT view a task that isn't theirs if the two users are not in a shared group. + # For this we can use the fixture data as-is. + task = Task.objects.filter(created_by__username="u1").first() + url = reverse("todo:task_detail", kwargs={"task_id": task.id}) + client.login(username="u2", password="password") + response = client.get(url) + assert response.status_code == 403 + + +def test_setting_TODO_STAFF_ONLY_False(todo_setup, client, settings): + # We use Django's user_passes_test to call `staff_check` utility function on all views. + # Just testing one view here; if it works, it works for all of them. + settings.TODO_STAFF_ONLY = False + url = reverse("todo:lists") + client.login(username="u2", password="password") + response = client.get(url) + assert response.status_code == 200 + + +def test_setting_TODO_STAFF_ONLY_True(todo_setup, client, settings): + # We use Django's user_passes_test to call `staff_check` utility function on all views. + # Just testing one view here; if it works, it works for all of them. + settings.TODO_STAFF_ONLY = True + url = reverse("todo:lists") + client.login(username="u2", password="password") + response = client.get(url) + assert response.status_code == 302 # Redirected to login view diff --git a/todo/urls.py b/todo/urls.py new file mode 100644 index 0000000..a539616 --- /dev/null +++ b/todo/urls.py @@ -0,0 +1,96 @@ +from django.conf import settings +from django.urls import path + +from todo import views +from todo.features import HAS_TASK_MERGE + +app_name = 'todo' + +urlpatterns = [ + path( + '', + views.list_lists, + name="lists"), + + # View reorder_tasks is only called by JQuery for drag/drop task ordering. + path( + 'reorder_tasks/', + views.reorder_tasks, + name="reorder_tasks"), + + # Allow users to post tasks from outside django-todo (e.g. for filing tickets - see docs) + path( + 'ticket/add/', + views.external_add, + name="external_add"), + + # Three paths into `list_detail` view + path( + 'mine/', + views.list_detail, + {'list_slug': 'mine'}, + name="mine"), + + path( + '//completed/', + views.list_detail, + {'view_completed': True}, + name='list_detail_completed'), + + path( + '//', + views.list_detail, + name='list_detail'), + + path( + '//delete/', + views.del_list, + name="del_list"), + + path( + 'add_list/', + views.add_list, + name="add_list"), + + path( + 'task//', + views.task_detail, + name='task_detail'), + + path( + 'attachment/remove//', + views.remove_attachment, + name='remove_attachment'), +] + +if HAS_TASK_MERGE: + # ensure mail tracker autocomplete is optional + from todo.views.task_autocomplete import TaskAutocomplete + urlpatterns.append( + path( + 'task//autocomplete/', + TaskAutocomplete.as_view(), + name='task_autocomplete') + ) + +urlpatterns.extend([ + path( + 'toggle_done//', + views.toggle_done, + name='task_toggle_done'), + + path( + 'delete//', + views.delete_task, + name='delete_task'), + + path( + 'search/', + views.search, + name="search"), + + path( + 'import_csv/', + views.import_csv, + name="import_csv"), +]) diff --git a/todo/utils.py b/todo/utils.py new file mode 100644 index 0000000..23c0e01 --- /dev/null +++ b/todo/utils.py @@ -0,0 +1,172 @@ +import email.utils +import logging +import os +import time + +from django.conf import settings +from django.contrib.sites.models import Site +from django.core import mail +from django.template.loader import render_to_string + +from todo.models import Attachment, Comment, Task + +log = logging.getLogger(__name__) + + +def staff_check(user): + """If TODO_STAFF_ONLY is set to True, limit view access to staff users only. + # FIXME: More granular access control needed - see + https://github.com/shacker/django-todo/issues/50 + """ + + if hasattr(settings, "TODO_STAFF_ONLY") and settings.TODO_STAFF_ONLY: + return user.is_staff + else: + # If unset or False, allow all logged in users + return True + + +def user_can_read_task(task, user): + return task.task_list.group in user.groups.all() or user.is_staff + + +def todo_get_backend(task): + """Returns a mail backend for some task""" + mail_backends = getattr(settings, "TODO_MAIL_BACKENDS", None) + if mail_backends is None: + return None + + task_backend = mail_backends[task.task_list.slug] + if task_backend is None: + return None + + return task_backend + + +def todo_get_mailer(user, task): + """A mailer is a (from_address, backend) pair""" + task_backend = todo_get_backend(task) + if task_backend is None: + return (None, mail.get_connection) + + from_address = getattr(task_backend, "from_address") + from_address = email.utils.formataddr((user.username, from_address)) + return (from_address, task_backend) + + +def todo_send_mail(user, task, subject, body, recip_list): + """Send an email attached to task, triggered by user""" + references = Comment.objects.filter(task=task).only("email_message_id") + references = (ref.email_message_id for ref in references) + references = " ".join(filter(bool, references)) + + from_address, backend = todo_get_mailer(user, task) + message_hash = hash((subject, body, from_address, frozenset(recip_list), references)) + + message_id = ( + # the task_id enables attaching back notification answers + "" + ).format( + task_id=task.pk, + # avoid the -hexstring case (hashes can be negative) + message_hash=abs(message_hash), + epoch=int(time.time()), + ) + + # the thread message id is used as a common denominator between all + # notifications for some task. This message doesn't actually exist, + # it's just there to make threading possible + thread_message_id = "".format(task.pk) + references = "{} {}".format(references, thread_message_id) + + with backend() as connection: + message = mail.EmailMessage( + subject, + body, + from_address, + recip_list, + [], # Bcc + headers={ + **getattr(backend, "headers", {}), + "Message-ID": message_id, + "References": references, + "In-reply-to": thread_message_id, + }, + connection=connection, + ) + message.send() + + +def send_notify_mail(new_task): + """ + Send email to assignee if task is assigned to someone other than submittor. + Unassigned tasks should not try to notify. + """ + + if new_task.assigned_to == new_task.created_by: + return + + current_site = Site.objects.get_current() + subject = render_to_string("todo/email/assigned_subject.txt", {"task": new_task}) + body = render_to_string( + "todo/email/assigned_body.txt", {"task": new_task, "site": current_site} + ) + + recip_list = [new_task.assigned_to.email] + todo_send_mail(new_task.created_by, new_task, subject, body, recip_list) + + +def send_email_to_thread_participants(task, msg_body, user, subject=None): + """Notify all previous commentors on a Task about a new comment.""" + + current_site = Site.objects.get_current() + email_subject = subject + if not subject: + subject = render_to_string("todo/email/assigned_subject.txt", {"task": task}) + + email_body = render_to_string( + "todo/email/newcomment_body.txt", + {"task": task, "body": msg_body, "site": current_site, "user": user}, + ) + + # Get all thread participants + commenters = Comment.objects.filter(task=task) + recip_list = set(ca.author.email for ca in commenters if ca.author is not None) + for related_user in (task.created_by, task.assigned_to): + if related_user is not None: + recip_list.add(related_user.email) + recip_list = list(m for m in recip_list if m) + + todo_send_mail(user, task, email_subject, email_body, recip_list) + + +def toggle_task_completed(task_id: int) -> bool: + """Toggle the `completed` bool on Task from True to False or vice versa.""" + try: + task = Task.objects.get(id=task_id) + task.completed = not task.completed + task.save() + return True + + except Task.DoesNotExist: + log.info(f"Task {task_id} not found.") + return False + + +def remove_attachment_file(attachment_id: int) -> bool: + """Delete an Attachment object and its corresponding file from the filesystem.""" + try: + attachment = Attachment.objects.get(id=attachment_id) + if attachment.file: + if os.path.isfile(attachment.file.path): + os.remove(attachment.file.path) + + attachment.delete() + return True + + except Attachment.DoesNotExist: + log.info(f"Attachment {attachment_id} not found.") + return False diff --git a/todo/views/__init__.py b/todo/views/__init__.py new file mode 100644 index 0000000..fb6c891 --- /dev/null +++ b/todo/views/__init__.py @@ -0,0 +1,12 @@ +from todo.views.add_list import add_list # noqa: F401 +from todo.views.del_list import del_list # noqa: F401 +from todo.views.delete_task import delete_task # noqa: F401 +from todo.views.external_add import external_add # noqa: F401 +from todo.views.import_csv import import_csv # noqa: F401 +from todo.views.list_detail import list_detail # noqa: F401 +from todo.views.list_lists import list_lists # noqa: F401 +from todo.views.remove_attachment import remove_attachment # noqa: F401 +from todo.views.reorder_tasks import reorder_tasks # noqa: F401 +from todo.views.search import search # noqa: F401 +from todo.views.task_detail import task_detail # noqa: F401 +from todo.views.toggle_done import toggle_done # noqa: F401 diff --git a/todo/views/add_list.py b/todo/views/add_list.py new file mode 100644 index 0000000..8304852 --- /dev/null +++ b/todo/views/add_list.py @@ -0,0 +1,48 @@ +from django.contrib import messages +from django.contrib.auth.decorators import login_required, user_passes_test +from django.core.exceptions import PermissionDenied +from django.db import IntegrityError +from django.http import HttpResponse +from django.shortcuts import redirect, render +from django.utils.text import slugify + +from todo.forms import AddTaskListForm +from todo.utils import staff_check + + +@login_required +@user_passes_test(staff_check) +def add_list(request) -> HttpResponse: + """Allow users to add a new todo list to the group they're in. + """ + + # Only staffers can add lists, regardless of TODO_STAFF_USER setting. + if not request.user.is_staff: + raise PermissionDenied + + if request.POST: + form = AddTaskListForm(request.user, request.POST) + if form.is_valid(): + try: + newlist = form.save(commit=False) + newlist.slug = slugify(newlist.name) + newlist.save() + messages.success(request, "A new list has been added.") + return redirect("todo:lists") + + except IntegrityError: + messages.warning( + request, + "There was a problem saving the new list. " + "Most likely a list with the same name in the same group already exists.", + ) + else: + if request.user.groups.all().count() == 1: + # FIXME: Assuming first of user's groups here; better to prompt for group + form = AddTaskListForm(request.user, initial={"group": request.user.groups.all()[0]}) + else: + form = AddTaskListForm(request.user) + + context = {"form": form} + + return render(request, "todo/add_list.html", context) diff --git a/todo/views/del_list.py b/todo/views/del_list.py new file mode 100644 index 0000000..c4e044f --- /dev/null +++ b/todo/views/del_list.py @@ -0,0 +1,39 @@ +from django.contrib import messages +from django.contrib.auth.decorators import login_required, user_passes_test +from django.core.exceptions import PermissionDenied +from django.http import HttpResponse +from django.shortcuts import get_object_or_404, redirect, render + +from todo.models import Task, TaskList +from todo.utils import staff_check + + +@login_required +@user_passes_test(staff_check) +def del_list(request, list_id: int, list_slug: str) -> HttpResponse: + """Delete an entire list. Only staff members should be allowed to access this view. + """ + task_list = get_object_or_404(TaskList, id=list_id) + + # Ensure user has permission to delete list. Get the group this list belongs to, + # and check whether current user is a member of that group AND a staffer. + if task_list.group not in request.user.groups.all() and not request.user.is_staff: + raise PermissionDenied + + if request.method == "POST": + TaskList.objects.get(id=task_list.id).delete() + messages.success(request, "{list_name} is gone.".format(list_name=task_list.name)) + return redirect("todo:lists") + else: + task_count_done = Task.objects.filter(task_list=task_list.id, completed=True).count() + task_count_undone = Task.objects.filter(task_list=task_list.id, completed=False).count() + task_count_total = Task.objects.filter(task_list=task_list.id).count() + + context = { + "task_list": task_list, + "task_count_done": task_count_done, + "task_count_undone": task_count_undone, + "task_count_total": task_count_total, + } + + return render(request, "todo/del_list.html", context) diff --git a/todo/views/delete_task.py b/todo/views/delete_task.py new file mode 100644 index 0000000..526e19c --- /dev/null +++ b/todo/views/delete_task.py @@ -0,0 +1,42 @@ +from django.contrib import messages +from django.contrib.auth.decorators import login_required, user_passes_test +from django.core.exceptions import PermissionDenied +from django.http import HttpResponse +from django.shortcuts import get_object_or_404, redirect +from django.urls import reverse + +from todo.models import Task +from todo.utils import staff_check + + +@login_required +@user_passes_test(staff_check) +def delete_task(request, task_id: int) -> HttpResponse: + """Delete specified task. + Redirect to the list from which the task came. + """ + + if request.method == "POST": + task = get_object_or_404(Task, pk=task_id) + + redir_url = reverse( + "todo:list_detail", + kwargs={"list_id": task.task_list.id, "list_slug": task.task_list.slug}, + ) + + # Permissions + if not ( + (task.created_by == request.user) + or (request.user.is_superuser) + or (task.assigned_to == request.user) + or (task.task_list.group in request.user.groups.all()) + ): + raise PermissionDenied + + task.delete() + + messages.success(request, "Task '{}' has been deleted".format(task.title)) + return redirect(redir_url) + + else: + raise PermissionDenied diff --git a/todo/views/external_add.py b/todo/views/external_add.py new file mode 100644 index 0000000..2638996 --- /dev/null +++ b/todo/views/external_add.py @@ -0,0 +1,79 @@ +from django.conf import settings +from django.contrib import messages +from django.contrib.auth.decorators import login_required, user_passes_test +from django.contrib.auth.models import User +from django.contrib.sites.models import Site +from django.core.mail import send_mail +from django.http import HttpResponse +from django.shortcuts import redirect, render +from django.template.loader import render_to_string + +from todo.forms import AddExternalTaskForm +from todo.models import TaskList +from todo.utils import staff_check + + +@login_required +@user_passes_test(staff_check) +def external_add(request) -> HttpResponse: + """Allow authenticated users who don't have access to the rest of the ticket system to file a ticket + in the list specified in settings (e.g. django-todo can be used a ticket filing system for a school, where + students can file tickets without access to the rest of the todo system). + + Publicly filed tickets are unassigned unless settings.DEFAULT_ASSIGNEE exists. + """ + + if not settings.TODO_DEFAULT_LIST_SLUG: + raise RuntimeError( + "This feature requires TODO_DEFAULT_LIST_SLUG: in settings. See documentation." + ) + + if not TaskList.objects.filter(slug=settings.TODO_DEFAULT_LIST_SLUG).exists(): + raise RuntimeError( + "There is no TaskList with slug specified for TODO_DEFAULT_LIST_SLUG in settings." + ) + + if request.POST: + form = AddExternalTaskForm(request.POST) + + if form.is_valid(): + current_site = Site.objects.get_current() + task = form.save(commit=False) + task.task_list = TaskList.objects.get(slug=settings.TODO_DEFAULT_LIST_SLUG) + task.created_by = request.user + if settings.TODO_DEFAULT_ASSIGNEE: + task.assigned_to = User.objects.get(username=settings.TODO_DEFAULT_ASSIGNEE) + task.save() + + # Send email to assignee if we have one + if task.assigned_to: + email_subject = render_to_string( + "todo/email/assigned_subject.txt", {"task": task.title} + ) + email_body = render_to_string( + "todo/email/assigned_body.txt", {"task": task, "site": current_site} + ) + try: + send_mail( + email_subject, + email_body, + task.created_by.email, + [task.assigned_to.email], + fail_silently=False, + ) + except ConnectionRefusedError: + messages.warning( + request, "Task saved but mail not sent. Contact your administrator." + ) + + messages.success( + request, "Your trouble ticket has been submitted. We'll get back to you soon." + ) + return redirect(settings.TODO_PUBLIC_SUBMIT_REDIRECT) + + else: + form = AddExternalTaskForm(initial={"priority": 999}) + + context = {"form": form} + + return render(request, "todo/add_task_external.html", context) diff --git a/todo/views/import_csv.py b/todo/views/import_csv.py new file mode 100644 index 0000000..720df5e --- /dev/null +++ b/todo/views/import_csv.py @@ -0,0 +1,34 @@ +from django.contrib import messages +from django.contrib.auth.decorators import login_required, user_passes_test +from django.http import HttpResponse +from django.shortcuts import redirect, render, reverse + +from todo.operations.csv_importer import CSVImporter +from todo.utils import staff_check + + +@login_required +@user_passes_test(staff_check) +def import_csv(request) -> HttpResponse: + """Import a specifically formatted CSV into stored tasks. + """ + + ctx = {"results": None} + + if request.method == "POST": + filepath = request.FILES.get("csvfile") + + if not filepath: + messages.error(request, "You must supply a CSV file to import.") + return redirect(reverse("todo:import_csv")) + + importer = CSVImporter() + results = importer.upsert(filepath) + + if results: + ctx["results"] = results + else: + messages.error(request, "Could not parse provided CSV file.") + return redirect(reverse("todo:import_csv")) + + return render(request, "todo/import_csv.html", context=ctx) diff --git a/todo/views/list_detail.py b/todo/views/list_detail.py new file mode 100644 index 0000000..32bb9b6 --- /dev/null +++ b/todo/views/list_detail.py @@ -0,0 +1,85 @@ +import bleach +from django.contrib import messages +from django.contrib.auth.decorators import login_required, user_passes_test +from django.core.exceptions import PermissionDenied +from django.http import HttpResponse +from django.shortcuts import get_object_or_404, redirect, render +from django.utils import timezone + +from todo.forms import AddEditTaskForm +from todo.models import Task, TaskList +from todo.utils import send_notify_mail, staff_check + + +@login_required +@user_passes_test(staff_check) +def list_detail(request, list_id=None, list_slug=None, view_completed=False) -> HttpResponse: + """Display and manage tasks in a todo list. + """ + + # Defaults + task_list = None + form = None + + # Which tasks to show on this list view? + if list_slug == "mine": + tasks = Task.objects.filter(assigned_to=request.user) + + else: + # Show a specific list, ensuring permissions. + task_list = get_object_or_404(TaskList, id=list_id) + if task_list.group not in request.user.groups.all() and not request.user.is_staff: + raise PermissionDenied + tasks = Task.objects.filter(task_list=task_list.id) + + # Additional filtering + if view_completed: + tasks = tasks.filter(completed=True) + else: + tasks = tasks.filter(completed=False) + + # ###################### + # Add New Task Form + # ###################### + + if request.POST.getlist("add_edit_task"): + form = AddEditTaskForm( + request.user, + request.POST, + initial={"assigned_to": request.user.id, "priority": 999, "task_list": task_list}, + ) + + if form.is_valid(): + new_task = form.save(commit=False) + new_task.created_date = timezone.now() + new_task.note = bleach.clean(form.cleaned_data["note"], strip=True) + form.save() + + # Send email alert only if Notify checkbox is checked AND assignee is not same as the submitter + if ( + "notify" in request.POST + and new_task.assigned_to + and new_task.assigned_to != request.user + ): + send_notify_mail(new_task) + + messages.success(request, 'New task "{t}" has been added.'.format(t=new_task.title)) + return redirect(request.path) + else: + # Don't allow adding new tasks on some views + if list_slug not in ["mine", "recent-add", "recent-complete"]: + form = AddEditTaskForm( + request.user, + initial={"assigned_to": request.user.id, "priority": 999, "task_list": task_list}, + ) + + context = { + "list_id": list_id, + "list_slug": list_slug, + "task_list": task_list, + "form": form, + "tasks": tasks, + "view_completed": view_completed, + } + + return render(request, "todo/list_detail.html", context) diff --git a/todo/views/list_lists.py b/todo/views/list_lists.py new file mode 100644 index 0000000..d8b7bea --- /dev/null +++ b/todo/views/list_lists.py @@ -0,0 +1,57 @@ +import datetime + +from django.contrib import messages +from django.contrib.auth.decorators import login_required, user_passes_test +from django.http import HttpResponse +from django.shortcuts import render + +from todo.forms import SearchForm +from todo.models import Task, TaskList +from todo.utils import staff_check + + +@login_required +@user_passes_test(staff_check) +def list_lists(request) -> HttpResponse: + """Homepage view - list of lists a user can view, and ability to add a list. + """ + + thedate = datetime.datetime.now() + searchform = SearchForm(auto_id=False) + + # Make sure user belongs to at least one group. + if not request.user.groups.all().exists(): + messages.warning( + request, + "You do not yet belong to any groups. Ask your administrator to add you to one.", + ) + + # Superusers see all lists + if request.user.is_superuser: + lists = TaskList.objects.all().order_by("group", "name") + else: + lists = TaskList.objects.filter(group__in=request.user.groups.all()).order_by( + "group", "name" + ) + + list_count = lists.count() + + # superusers see all lists, so count shouldn't filter by just lists the admin belongs to + if request.user.is_superuser: + task_count = Task.objects.filter(completed=0).count() + else: + task_count = ( + Task.objects.filter(completed=0) + .filter(task_list__group__in=request.user.groups.all()) + .count() + ) + + context = { + "lists": lists, + "thedate": thedate, + "searchform": searchform, + "list_count": list_count, + "task_count": task_count, + } + + return render(request, "todo/list_lists.html", context) diff --git a/todo/views/remove_attachment.py b/todo/views/remove_attachment.py new file mode 100644 index 0000000..75b235f --- /dev/null +++ b/todo/views/remove_attachment.py @@ -0,0 +1,41 @@ +from django.contrib import messages +from django.contrib.auth.decorators import login_required +from django.core.exceptions import PermissionDenied +from django.http import HttpResponse +from django.shortcuts import get_object_or_404, redirect +from django.urls import reverse + +from todo.models import Attachment +from todo.utils import remove_attachment_file + + +@login_required +def remove_attachment(request, attachment_id: int) -> HttpResponse: + """Delete a previously posted attachment object and its corresponding file + from the filesystem, permissions allowing. + """ + + if request.method == "POST": + attachment = get_object_or_404(Attachment, pk=attachment_id) + + redir_url = reverse( + "todo:task_detail", + kwargs={"task_id": attachment.task.id}, + ) + + # Permissions + if not ( + attachment.task.task_list.group in request.user.groups.all() + or request.user.is_superuser + ): + raise PermissionDenied + + if remove_attachment_file(attachment.id): + messages.success(request, f"Attachment {attachment.id} removed.") + else: + messages.error(request, f"Sorry, there was a problem deleting attachment {attachment.id}.") + + return redirect(redir_url) + + else: + raise PermissionDenied diff --git a/todo/views/reorder_tasks.py b/todo/views/reorder_tasks.py new file mode 100644 index 0000000..04e2c1b --- /dev/null +++ b/todo/views/reorder_tasks.py @@ -0,0 +1,35 @@ +from django.contrib.auth.decorators import login_required, user_passes_test +from django.http import HttpResponse +from django.views.decorators.csrf import csrf_exempt + +from todo.models import Task +from todo.utils import staff_check + + +@csrf_exempt +@login_required +@user_passes_test(staff_check) +def reorder_tasks(request) -> HttpResponse: + """Handle task re-ordering (priorities) from JQuery drag/drop in list_detail.html + """ + newtasklist = request.POST.getlist("tasktable[]") + if newtasklist: + # First task in received list is always empty - remove it + del newtasklist[0] + + # Re-prioritize each task in list + i = 1 + for id in newtasklist: + try: + task = Task.objects.get(pk=id) + task.priority = i + task.save() + i += 1 + except Task.DoesNotExist: + # Can occur if task is deleted behind the scenes during re-ordering. + # Not easy to remove it from the UI without page refresh, but prevent crash. + pass + + # All views must return an httpresponse of some kind ... without this we get + # error 500s in the log even though things look peachy in the browser. + return HttpResponse(status=201) diff --git a/todo/views/search.py b/todo/views/search.py new file mode 100644 index 0000000..b7f5e17 --- /dev/null +++ b/todo/views/search.py @@ -0,0 +1,43 @@ +from django.contrib.auth.decorators import login_required, user_passes_test +from django.db.models import Q +from django.http import HttpResponse +from django.shortcuts import render + +from todo.models import Task +from todo.utils import staff_check + + +@login_required +@user_passes_test(staff_check) +def search(request) -> HttpResponse: + """Search for tasks user has permission to see. + """ + + query_string = "" + + if request.GET: + + found_tasks = None + if ("q" in request.GET) and request.GET["q"].strip(): + query_string = request.GET["q"] + + found_tasks = Task.objects.filter( + Q(title__icontains=query_string) | Q(note__icontains=query_string) + ) + else: + # What if they selected the "completed" toggle but didn't enter a query string? + # We still need found_tasks in a queryset so it can be "excluded" below. + found_tasks = Task.objects.all() + + if "inc_complete" in request.GET: + found_tasks = found_tasks.exclude(completed=True) + + else: + found_tasks = None + + # Only include tasks that are in groups of which this user is a member: + if not request.user.is_superuser: + found_tasks = found_tasks.filter(task_list__group__in=request.user.groups.all()) + + context = {"query_string": query_string, "found_tasks": found_tasks} + return render(request, "todo/search_results.html", context) diff --git a/todo/views/task_autocomplete.py b/todo/views/task_autocomplete.py new file mode 100644 index 0000000..0a5667e --- /dev/null +++ b/todo/views/task_autocomplete.py @@ -0,0 +1,29 @@ +from dal import autocomplete +from django.contrib.auth.decorators import login_required +from django.core.exceptions import PermissionDenied +from django.shortcuts import get_object_or_404 +from django.utils.decorators import method_decorator +from todo.models import Task +from todo.utils import user_can_read_task + + +class TaskAutocomplete(autocomplete.Select2QuerySetView): + @method_decorator(login_required) + def dispatch(self, request, task_id, *args, **kwargs): + self.task = get_object_or_404(Task, pk=task_id) + if not user_can_read_task(self.task, request.user): + raise PermissionDenied + + return super().dispatch(request, task_id, *args, **kwargs) + + def get_queryset(self): + # Don't forget to filter out results depending on the visitor ! + if not self.request.user.is_authenticated: + return Task.objects.none() + + qs = Task.objects.filter(task_list=self.task.task_list).exclude(pk=self.task.pk) + + if self.q: + qs = qs.filter(title__istartswith=self.q) + + return qs diff --git a/todo/views/task_detail.py b/todo/views/task_detail.py new file mode 100644 index 0000000..2d10844 --- /dev/null +++ b/todo/views/task_detail.py @@ -0,0 +1,159 @@ +import datetime +import os + +import bleach +from django import forms +from django.conf import settings +from django.contrib import messages +from django.contrib.auth.decorators import login_required, user_passes_test +from django.core.exceptions import PermissionDenied +from django.http import HttpResponse +from django.shortcuts import get_object_or_404, redirect, render +from django.urls import reverse + +from todo.features import HAS_TASK_MERGE +from todo.forms import AddEditTaskForm +from todo.models import Attachment, Comment, Task +from todo.utils import ( + send_email_to_thread_participants, + staff_check, + toggle_task_completed, + user_can_read_task, +) + +if HAS_TASK_MERGE: + from dal import autocomplete + + +def handle_add_comment(request, task): + if not request.POST.get("add_comment"): + return + + Comment.objects.create( + author=request.user, task=task, body=bleach.clean(request.POST["comment-body"], strip=True) + ) + + send_email_to_thread_participants( + task, + request.POST["comment-body"], + request.user, + subject='New comment posted on task "{}"'.format(task.title), + ) + + messages.success(request, "Comment posted. Notification email sent to thread participants.") + + +@login_required +@user_passes_test(staff_check) +def task_detail(request, task_id: int) -> HttpResponse: + """View task details. Allow task details to be edited. Process new comments on task. + """ + + task = get_object_or_404(Task, pk=task_id) + comment_list = Comment.objects.filter(task=task_id).order_by("-date") + + # Ensure user has permission to view task. Admins can view all tasks. + # Get the group this task belongs to, and check whether current user is a member of that group. + if not user_can_read_task(task, request.user): + raise PermissionDenied + + # Handle task merging + if not HAS_TASK_MERGE: + merge_form = None + else: + + class MergeForm(forms.Form): + merge_target = forms.ModelChoiceField( + queryset=Task.objects.all(), + widget=autocomplete.ModelSelect2( + url=reverse("todo:task_autocomplete", kwargs={"task_id": task_id}) + ), + ) + + # Handle task merging + if not request.POST.get("merge_task_into"): + merge_form = MergeForm() + else: + merge_form = MergeForm(request.POST) + if merge_form.is_valid(): + merge_target = merge_form.cleaned_data["merge_target"] + if not user_can_read_task(merge_target, request.user): + raise PermissionDenied + + task.merge_into(merge_target) + return redirect(reverse("todo:task_detail", kwargs={"task_id": merge_target.pk})) + + # Save submitted comments + handle_add_comment(request, task) + + # Save task edits + if not request.POST.get("add_edit_task"): + form = AddEditTaskForm(request.user, instance=task, initial={"task_list": task.task_list}) + else: + form = AddEditTaskForm( + request.user, request.POST, instance=task, initial={"task_list": task.task_list} + ) + + if form.is_valid(): + item = form.save(commit=False) + item.note = bleach.clean(form.cleaned_data["note"], strip=True) + item.save() + messages.success(request, "The task has been edited.") + return redirect( + "todo:list_detail", list_id=task.task_list.id, list_slug=task.task_list.slug + ) + + # Mark complete + if request.POST.get("toggle_done"): + results_changed = toggle_task_completed(task.id) + if results_changed: + messages.success(request, f"Changed completion status for task {task.id}") + + return redirect("todo:task_detail", task_id=task.id) + + if task.due_date: + thedate = task.due_date + else: + thedate = datetime.datetime.now() + + # Handle uploaded files + if request.FILES.get("attachment_file_input"): + file = request.FILES.get("attachment_file_input") + + # Validate inbound file extension against allowed filetypes + # FIXME: Move defaults to centralized module + allowed_extensions = ( + settings.TODO_ALLOWED_FILE_ATTACHMENTS + if hasattr(settings, "TODO_ALLOWED_FILE_ATTACHMENTS") + else [".jpg", ".gif", ".csv", ".pdf", ".zip"] + ) + name, extension = os.path.splitext(file.name) + if extension not in allowed_extensions: + messages.error(request, f"This site does not allow upload of {extension} files.") + return redirect("todo:task_detail", task_id=task.id) + + Attachment.objects.create( + task=task, added_by=request.user, timestamp=datetime.datetime.now(), file=file + ) + return redirect("todo:task_detail", task_id=task.id) + + # Settings for file attachments defaults to True + # FIXME: Move settings defaults to a central location? + attachments_enabled = True + if ( + hasattr(settings, "TODO_ALLOW_FILE_ATTACHMENTS") + and not settings.TODO_ALLOW_FILE_ATTACHMENTS + ): + attachments_enabled = False + + context = { + "task": task, + "comment_list": comment_list, + "form": form, + "merge_form": merge_form, + "thedate": thedate, + "comment_classes": getattr(settings, "TODO_COMMENT_CLASSES", []), + "attachments_enabled": attachments_enabled, + } + + return render(request, "todo/task_detail.html", context) diff --git a/todo/views/toggle_done.py b/todo/views/toggle_done.py new file mode 100644 index 0000000..fdc727a --- /dev/null +++ b/todo/views/toggle_done.py @@ -0,0 +1,43 @@ +from django.contrib import messages +from django.contrib.auth.decorators import login_required, user_passes_test +from django.core.exceptions import PermissionDenied +from django.http import HttpResponse +from django.shortcuts import get_object_or_404, redirect +from django.urls import reverse + +from todo.models import Task +from todo.utils import toggle_task_completed +from todo.utils import staff_check + + +@login_required +@user_passes_test(staff_check) +def toggle_done(request, task_id: int) -> HttpResponse: + """Toggle the completed status of a task from done to undone, or vice versa. + Redirect to the list from which the task came. + """ + + if request.method == "POST": + task = get_object_or_404(Task, pk=task_id) + + redir_url = reverse( + "todo:list_detail", + kwargs={"list_id": task.task_list.id, "list_slug": task.task_list.slug}, + ) + + # Permissions + if not ( + (task.created_by == request.user) + or (request.user.is_superuser) + or (task.assigned_to == request.user) + or (task.task_list.group in request.user.groups.all()) + ): + raise PermissionDenied + + toggle_task_completed(task.id) + messages.success(request, "Task status changed for '{}'".format(task.title)) + + return redirect(redir_url) + + else: + raise PermissionDenied diff --git a/werf.yml b/werf.yml deleted file mode 100644 index f8622f4..0000000 --- a/werf.yml +++ /dev/null @@ -1,5 +0,0 @@ -project: coins-demo -configVersion: 1 ---- -image: ~ -dockerfile: Dockerfile