diff --git a/.gitignore b/.gitignore index d5885822..9e6dab0f 100644 --- a/.gitignore +++ b/.gitignore @@ -10,6 +10,7 @@ docs/doctrees/* .project .pydevproject .directory +*.swp # ignore demo attachments that user might have added helpdesk/attachments/ diff --git a/.travis.yml b/.travis.yml index 8bf67021..dc7ae1cf 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,12 +2,11 @@ language: python dist: bionic # use LTS 18.04 python: - - "2.7" - "3.6" - "3.7" env: - - DJANGO=1.11.23 + - DJANGO=2.2.4 install: - pip install -q Django==$DJANGO diff --git a/LICENSE b/LICENSE index 5955bc78..86bf08e6 100644 --- a/LICENSE +++ b/LICENSE @@ -1,5 +1,5 @@ Copyright (c) 2008 Ross Poulton (Trading as Jutda), -Copyright (c) 2008-2018 django-helpdesk contributors. +Copyright (c) 2008-2019 django-helpdesk contributors. All rights reserved. Redistribution and use in source and binary forms, with or without modification, diff --git a/LICENSE.3RDPARTY b/LICENSE.3RDPARTY index ecc88b72..144220eb 100644 --- a/LICENSE.3RDPARTY +++ b/LICENSE.3RDPARTY @@ -1,22 +1,22 @@ This file contains license details for 3rd party software which is distributed with django-helpdesk. -1. License for jQuery & jQuery UI +1. License for jQuery & jQuery UI v3.3.1 2. License for jQuery UI 'Smoothness' theme 3. License for akismet.py 4. License for jqPlot -5. License for StartBootstrap SB Admin v2.0 theme +5. License for StartBootstrap SB Admin v5.0.2 theme 6. License for Raphael 7. License for Morris.js -8. License for DataTables +8. License for DataTables v1.10.18 9. License for Flot 10. License for Metis Menu -11. License for Bootstrap CSS -12. License for Font Awesome +11. License for Bootstrap CSS v4.2.1 +12. License for Font Awesome v5.6.3 ---------------------------------------------------------------------- -1. License for jQuery & jQuery UI +1. License for jQuery & jQuery UI v3.3.1 Copyright (c) 2007 John Resig, http://jquery.com/ @@ -114,11 +114,11 @@ THE SOFTWARE. ---------------------------------------------------------------------- -5. License for StartBootstrap SB Admin v2.0 theme +5. License for StartBootstrap SB Admin v5.0.2 theme The MIT License (MIT) -Copyright (c) 2013-2016 Blackrock Digital LLC. +Copyright (c) 2013-2018 Blackrock Digital LLC Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal @@ -193,25 +193,27 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ---------------------------------------------------------------------- -8. License for DataTables +8. License for DataTables v1.10.18 -Copyright (C) 2008-2016, SpryMedia Ltd. +MIT license -Permission is hereby granted, free of charge, to any person obtaining a copy of -this software and associated documentation files (the "Software"), to deal in -the Software without restriction, including without limitation the rights to -use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of -the Software, and to permit persons to whom the Software is furnished to do so, +Copyright (C) 2008-2018, SpryMedia Ltd. + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of +the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: -The above copyright notice and this permission notice shall be included in all +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS -FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR -COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER -IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR +COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER +IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ---------------------------------------------------------------------- @@ -266,9 +268,12 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ---------------------------------------------------------------------- -11. License for Bootstrap CSS +11. License for Bootstrap CSS v4.2.1 -Copyright (c) 2011-2016 Twitter, Inc. +The MIT License (MIT) + +Copyright (c) 2011-2018 Twitter, Inc. +Copyright (c) 2011-2018 The Bootstrap Authors Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal @@ -290,36 +295,39 @@ THE SOFTWARE. ---------------------------------------------------------------------- -12. License for Font Awesome +12. License for Font Awesome v5.6.3 -Font License +Font Awesome Free is free, open source, and GPL friendly. You can use it for +commercial projects, open source projects, or really almost whatever you want. +Full Font Awesome Free license: https://fontawesome.com/license/free. - Applies to all desktop and webfont files in the following directory: - font-awesome/fonts/. - License: SIL OFL 1.1 - URL: http://scripts.sil.org/OFL +# Icons: CC BY 4.0 License (https://creativecommons.org/licenses/by/4.0/) +In the Font Awesome Free download, the CC BY 4.0 license applies to all icons +packaged as SVG and JS file types. -Code License +# Fonts: SIL OFL 1.1 License (https://scripts.sil.org/OFL) +In the Font Awesome Free download, the SIL OFL license applies to all icons +packaged as web and desktop font files. - Applies to all CSS and LESS files in the following directories: - font-awesome/css/, font-awesome/less/, and font-awesome/scss/. - License: MIT License - URL: http://opensource.org/licenses/mit-license.html +# Code: MIT License (https://opensource.org/licenses/MIT) +In the Font Awesome Free download, the MIT license applies to all non-font and +non-icon files. -Documentation License +# Attribution +Attribution is required by MIT, SIL OFL, and CC BY licenses. Downloaded Font +Awesome Free files already contain embedded comments with sufficient +attribution, so you shouldn't need to do anything additional when using these +files normally. - Applies to all Font Awesome project files that are not a part of the Font or - Code licenses. - License: CC BY 3.0 - URL: http://creativecommons.org/licenses/by/3.0/ +We've kept attribution comments terse, so we ask that you do not actively work +to remove them from files, especially code. They're a great way for folks to +learn about Font Awesome. -Brand Icons - - All brand icons are trademarks of their respective owners. - The use of these trademarks does not indicate endorsement of the trademark - holder by Font Awesome, nor vice versa. - Brand icons should only be used to represent the company or product to which - they refer. +# Brand Icons +All brand icons are trademarks of their respective owners. The use of these +trademarks does not indicate endorsement of the trademark holder by Font +Awesome, nor vice versa. **Please do not use brand logos for any purpose except +to represent the company, product, or service to which they refer.** diff --git a/Makefile b/Makefile index 45e7c764..2cc1bbf7 100644 --- a/Makefile +++ b/Makefile @@ -22,11 +22,6 @@ help: develop: $(PIP) install -e . -#: develop2 - Install minimal development utilities for Python2. -.PHONY: develop2 -develop2: - pip2 install -e . - #: clean - Basic cleanup, mostly temporary files. .PHONY: clean @@ -94,31 +89,12 @@ demo: demodesk loaddata emailtemplate.json demodesk loaddata demo.json -#: demo2 - Setup demo project using Python2. -.PHONY: demo2 -demo2: - pip2 install -e . - pip2 install -e demo - demodesk migrate --noinput - # Create superuser; user will be prompted to manually set a password. - # When you get a prompt, enter a password of your choosing. - # We suggest a default of 'Test1234' for the demo project. - demodesk createsuperuser --username admin --email helpdesk@example.com - # Install fixtures (helpdesk templates as well as demo ticket data) - demodesk loaddata emailtemplate.json - demodesk loaddata demo.json - #: rundemo - Run demo server using Python3. .PHONY: rundemo rundemo: demo demodesk runserver 8080 -#: rundemo2 - Run demo server using Python2. -.PHONY: rundemo2 -rundemo2: demo2 - demodesk runserver 8080 - #: release - Tag and push to PyPI. .PHONY: release diff --git a/README.rst b/README.rst index 53b576dc..86840483 100644 --- a/README.rst +++ b/README.rst @@ -1,10 +1,10 @@ django-helpdesk - A Django powered ticket tracker for small businesses. ======================================================================= -.. image:: https://travis-ci.org/django-helpdesk/django-helpdesk.png?branch=master +.. image:: https://travis-ci.org/django-helpdesk/django-helpdesk.png?branch=develop :target: https://travis-ci.org/django-helpdesk/django-helpdesk -.. image:: https://codecov.io/gh/django-helpdesk/django-helpdesk/branch/master/graph/badge.svg +.. image:: https://codecov.io/gh/django-helpdesk/django-helpdesk/branch/develop/graph/badge.svg :target: https://codecov.io/gh/django-helpdesk/django-helpdesk Copyright 2009-2019 Ross Poulton and django-helpdesk contributors. All Rights Reserved. @@ -34,8 +34,8 @@ included packages. Demo Quickstart --------------- -django-helpdesk includes a basic demo Django project so that you may easily -get started with testing or developing django-helpdesk. The demo project +`django-helpdesk` includes a basic demo Django project so that you may easily +get started with testing or developing `django-helpdesk`. The demo project resides in the `demo/` top-level folder. It's likely that you can start up a demo project server by running @@ -62,20 +62,8 @@ Installation `django-helpdesk` requires: -* Django 1.11.x -* either Python 2.7 or 3.6+ (likely 3.4+ still works though, but unsupported) - -**NOTE REGARDING PYTHON VERSION:** -The recommended combination is Python 3.6+ with Django 1.11. -Support for Python 2 will end in the next versions of both `django-helpdesk` -and Django itself (Django 2.0), so users and developers are encouraged to begin -transitioning to Python 3 if have not already. New projects should definitely -use Python 3! - -**NOTE REGARDING DJANGO VERSION:** -The recommended release is Django 1.11. However, there initial support of -Django 2.x as of version 0.2.7 if you'd like to try it out. -Please report any bugs you find! +* Python 3.6+ +* Django 2.2 LTS (latest bugfix release highly recommended) You can quickly install the latest stable version of `django-helpdesk` app via `pip`:: diff --git a/demo/README.rst b/demo/README.rst index 4d9c9128..00a6841e 100644 --- a/demo/README.rst +++ b/demo/README.rst @@ -17,8 +17,6 @@ in the top level of the django-helpdesk directory. Running the demo ---------------- -By default, the demo is configured for Python 3. - While not recommended, the simplest way to get started is to simply install django-helpdesk to your system python package directory. @@ -59,25 +57,10 @@ before running: make rundemo -*NOTE ON USING PYTHON 2* - -By default, the demo uses Python 3, as Python 3 -will be the recommended version of python for -django-helpdesk and even Django itself in the near future. -However, if you wish to use Python 2, you can -instead run the following: - - sudo make rundemo2 - -Then navigate to the site in a browser as above. - *NOTE ON DJANGO VERISON* -The demo project was also created with Django 1.11 -in mind. If you are using an older version of Django, -slight tweaks might be necessary to make the demo work. -Please remember that we do not currently support any -version of Django other than 1.11. +The demo project was configured with Django 2.x in mind. +Django 1.11 is NOT supported. *NOTE ON ATTACHMENTS* diff --git a/demo/demodesk/config/settings.py b/demo/demodesk/config/settings.py index 2fb335c8..e1ac8106 100644 --- a/demo/demodesk/config/settings.py +++ b/demo/demodesk/config/settings.py @@ -37,8 +37,7 @@ INSTALLED_APPS = [ 'django.contrib.staticfiles', 'django.contrib.sites', 'django.contrib.humanize', - 'markdown_deux', - 'bootstrapform', + 'bootstrap4form', 'helpdesk' ] @@ -107,7 +106,6 @@ HELPDESK_REDIRECT_TO_LOGIN_BY_DEFAULT = False LOGIN_URL = '/login/' LOGIN_REDIRECT_URL = '/login/' - # Database # - by default, we use SQLite3 for the demo, but you can also # configure MySQL or PostgreSQL, see the docs for more: diff --git a/demo/setup.py b/demo/setup.py index 68caaeb0..cbe83ad8 100644 --- a/demo/setup.py +++ b/demo/setup.py @@ -13,18 +13,15 @@ project_root = os.path.dirname(here) NAME = 'django-helpdesk-demodesk' DESCRIPTION = 'A demo Django project using django-helpdesk' README = open(os.path.join(here, 'README.rst')).read() -VERSION = '0.2.18' +VERSION = '0.3.0.dev2' #VERSION = open(os.path.join(project_root, 'VERSION')).read().strip() AUTHOR = 'django-helpdesk team' URL = 'https://github.com/django-helpdesk/django-helpdesk' CLASSIFIERS = ['Development Status :: 4 - Beta', 'License :: OSI Approved :: BSD License', - 'Programming Language :: Python :: 2.7', - 'Programming Language :: Python :: 3.4', 'Programming Language :: Python :: 3.5', 'Programming Language :: Python :: 3.6', 'Programming Language :: Python :: 3.7', - 'Framework :: Django :: 1.11', 'Framework :: Django :: 2.0', 'Framework :: Django :: 2.1', 'Framework :: Django :: 2.2'] diff --git a/docs/conf.py b/docs/conf.py index 6ecfce4a..3d4ce533 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -41,16 +41,16 @@ master_doc = 'index' # General information about the project. project = u'django-helpdesk' -copyright = u'2011-2017, Ross Poulton + django-helpdesk Contributors' +copyright = u'2011-2019, Ross Poulton + django-helpdesk Contributors' # The version info for the project you're documenting, acts as replacement for # |version| and |release|, also used in various other places throughout the # built documents. # # The short X.Y version. -version = '0.2' +version = '0.3' # The full version, including alpha/beta/rc tags. -release = '0.2.0' +release = '0.3.0.dev2' # The language for content autogenerated by Sphinx. Refer to documentation # for a list of supported languages. diff --git a/docs/configuration.rst b/docs/configuration.rst index 8c30b5f9..9e10a7d2 100644 --- a/docs/configuration.rst +++ b/docs/configuration.rst @@ -3,13 +3,13 @@ Configuration Before django-helpdesk will be much use, you need to do some basic configuration. Most of this is done via the Django admin screens. -1. Visit ``http://yoursite/admin/`` and add a Helpdesk Queue. If you wish, enter your POP3 or IMAP server details. +1. Visit ``http://yoursite/admin/`` and add a Helpdesk Queue. If you wish, enter your POP3 or IMAP server details. **IMPORTANT NOTE**: Any tickets created via POP3 or IMAP mailboxes will DELETE the original e-mail from the mail server. -2. Visit ``http://yoursite/helpdesk/`` (or whatever path as defined in your ``urls.py``) +2. Visit ``http://yoursite/helpdesk/`` (or whatever path as defined in your ``urls.py``) -3. If you wish to automatically create tickets from the contents of an e-mail inbox, set up a cronjob to run the management command on a regular basis. +3. If you wish to automatically create tickets from the contents of an e-mail inbox, set up a cronjob to run the management command on a regular basis. (Or use Celery, see below) Don't forget to set the relevant Django environment variables in your crontab:: @@ -19,10 +19,12 @@ Before django-helpdesk will be much use, you need to do some basic configuration **IMPORTANT NOTE**: Any tickets created via POP3 or IMAP mailboxes will DELETE the original e-mail from the mail server. + If you wish to use `celery` instead of cron, you must add 'django_celery_beat' to `INSTALLED_APPS` and add a periodic celery task through the Django admin. + 4. If you wish to automatically escalate tickets based on their age, set up a cronjob to run the escalation command on a regular basis:: - + 0 * * * * /path/to/helpdesksite/manage.py escalate_tickets - + This will run the escalation process hourly, using the 'Escalation Days' setting for each queue to determine which tickets to escalate. 5. If you wish to exclude some days (eg, weekends) from escalation calculations, enter the dates manually via the Admin, or setup a cronjob to run a management command on a regular basis:: diff --git a/docs/custom_templates.rst b/docs/custom_templates.rst new file mode 100644 index 00000000..1a590340 --- /dev/null +++ b/docs/custom_templates.rst @@ -0,0 +1,9 @@ +Custom Templates +================ + +django-helpdesk supports custom HTML templates that can be styled with CSS. + +In particular, users can include a file named `helpdesk-customize.css` in their django project directory to provide CSS overrides easily. + +In general, entire HTML and CSS templates may be overriden by including a file of the same name in the project directory. Django automatically searches the project directory before searching for default templates included with django-helpdesk. + diff --git a/docs/install.rst b/docs/install.rst index a78ce74d..e0b0d1f8 100644 --- a/docs/install.rst +++ b/docs/install.rst @@ -9,16 +9,14 @@ Prerequisites Before getting started, ensure your system meets the following recommended dependencies: -* Python 3.6+, or Python 2.7 (Python 3.4+ may also work but unsupported) -* Django 1.11.x (Django 2.x should also work though, and recommended for new projects) +* Python 3.6+ +* Django 2.x Ensure any extra Django modules you wish to use are compatible before continuing. -**NOTE**: Python 2.7 support is deprecated in both ``django-helpdesk`` and Django. -Future releases of ``django-helpdesk`` may remove support for Python 2.7, -and Django will no longer support Python 2.7 as of the Django 2.0 release. -It is highly recommended to start new projects using Python 3.6+, or migrate -existing projects to Python 3.6+. +**NOTE**: Python 2.7 support was deprecated in ``django-helpdesk`` as of version 0.2.x +and completely removed in version 0.3.0. Users that still need Python 2 support should +remain on version 0.2.x. Getting The Code @@ -59,8 +57,7 @@ errors with trying to create User settings. 'django.contrib.sites', # Required for determining domain url for use in emails 'django.contrib.admin', # Required for helpdesk admin/maintenance 'django.contrib.humanize', # Required for elapsed time formatting - 'markdown_deux', # Required for Knowledgebase item formatting - 'bootstrapform', # Required for nicer formatting of forms with the default templates + 'bootstrap4form', # Required for nicer formatting of forms with the default templates 'helpdesk', # This is us! ) @@ -116,21 +113,17 @@ errors with trying to create User settings. Ideally, accessing http://MEDIA_URL/helpdesk/attachments/ will give you a 403 access denied error. -7. If it's not already installed, install ``markdown_deux`` and ensure it's in your ``INSTALLED_APPS``:: - - pip install django-markdown-deux - -8. If you already have a view handling your logins, then great! If not, add the following to ``settings.py`` to get your Django installation to use the login view included in ``django-helpdesk``:: +7. If you already have a view handling your logins, then great! If not, add the following to ``settings.py`` to get your Django installation to use the login view included in ``django-helpdesk``:: LOGIN_URL = '/helpdesk/login/' Alter the URL to suit your installation path. -9. Load initial e-mail templates, otherwise you will not be able to send e-mail:: +8. Load initial e-mail templates, otherwise you will not be able to send e-mail:: python manage.py loaddata emailtemplate.json -10. If you intend on using local mail directories for processing email into tickets, be sure to create the mail directory before adding it to the queue in the Django administrator interface. The default mail directory is ``/var/lib/mail/helpdesk/``. Ensure that the directory has appropriate permissions so that your Django/web server instance may read and write files from this directory. +9. If you intend on using local mail directories for processing email into tickets, be sure to create the mail directory before adding it to the queue in the Django administrator interface. The default mail directory is ``/var/lib/mail/helpdesk/``. Ensure that the directory has appropriate permissions so that your Django/web server instance may read and write files from this directory. Note that by default, any mail files placed in your local directory will be permanently deleted after being successfully processed. It is strongly recommended that you take further steps to save emails if you wish to retain backups. diff --git a/docs/integration.rst b/docs/integration.rst new file mode 100644 index 00000000..59ab0538 --- /dev/null +++ b/docs/integration.rst @@ -0,0 +1,11 @@ +Integrating django-helpdesk into your application +------------------------------------------------- + +Django-helpdesk associates an email address with each submitted ticket. If you integrate django-helpdesk directly into your django application, logged in users will automatically have their email address set when they visit the `/tickets/submit/` form. If you wish to pre-fill fields in this form, you can do so simply by setting the following query parameters: + + - `queue` + - `title` + - `body` + - `submitter_email` + +Note that these fields will continue to be user-editable despite being pre-filled. diff --git a/docs/settings.rst b/docs/settings.rst index a337e9aa..0502f0e2 100644 --- a/docs/settings.rst +++ b/docs/settings.rst @@ -89,8 +89,7 @@ These changes are visible throughout django-helpdesk - **HELPDESK_ANON_ACCESS_RAISES_404** If True, redirects user to a 404 page when attempting to reach ticket pages while not logged in, rather than redirecting to a login screen. **Default:** ``HELPDESK_ANON_ACCESS_RAISES_404 = False`` - - + Options shown on public pages ----------------------------- @@ -124,8 +123,12 @@ Options for public ticket submission form Options that change ticket updates ---------------------------------- -- **HELPDESK_ALLOW_NON_STAFF_TICKET_UPDATE** Allow non-staff users to interact with tickets? This will also change how 'staff_member_required' - in staff.py will be defined. +- **HELPDESK_ALLOW_NON_STAFF_TICKET_UPDATE** Allow non-staff users to interact with tickets? + Set to True to allow any authenticated user to manage tickets. + You can also apply a custom authorisation logic for identifying helpdesk staff members, by setting this to a callable. + In that case, the value should be a function accepting the active user as a parameter and returning True if the user is considered helpdesk staff, e.g. + + lambda u: u.is_authenticated() and u.is_active and u.groups.filter(name='helpdesk_staff').exists())) **Default:** ``HELPDESK_ALLOW_NON_STAFF_TICKET_UPDATE = False`` diff --git a/helpdesk/admin.py b/helpdesk/admin.py index ed1ab601..52318ec6 100644 --- a/helpdesk/admin.py +++ b/helpdesk/admin.py @@ -2,19 +2,28 @@ from django.contrib import admin from django.utils.translation import ugettext_lazy as _ from helpdesk.models import Queue, Ticket, FollowUp, PreSetReply, KBCategory from helpdesk.models import EscalationExclusion, EmailTemplate, KBItem -from helpdesk.models import TicketChange, Attachment, IgnoreEmail +from helpdesk.models import TicketChange, KBIAttachment, FollowUpAttachment, IgnoreEmail from helpdesk.models import CustomField @admin.register(Queue) class QueueAdmin(admin.ModelAdmin): - list_display = ('title', 'slug', 'email_address', 'locale') + list_display = ('title', 'slug', 'email_address', 'locale', 'time_spent') prepopulated_fields = {"slug": ("title",)} + def time_spent(self, q): + if q.dedicated_time: + return "{} / {}".format(q.time_spent, q.dedicated_time) + elif q.time_spent: + return q.time_spent + else: + return "-" + @admin.register(Ticket) class TicketAdmin(admin.ModelAdmin): - list_display = ('title', 'status', 'assigned_to', 'queue', 'hidden_submitter_email',) + list_display = ('title', 'status', 'assigned_to', 'queue', + 'hidden_submitter_email', 'time_spent') date_hierarchy = 'created' list_filter = ('queue', 'assigned_to', 'status') @@ -28,19 +37,30 @@ class TicketAdmin(admin.ModelAdmin): return ticket.submitter_email hidden_submitter_email.short_description = _('Submitter E-Mail') + def time_spent(self, ticket): + return ticket.time_spent + class TicketChangeInline(admin.StackedInline): model = TicketChange + extra = 0 -class AttachmentInline(admin.StackedInline): - model = Attachment +class FollowUpAttachmentInline(admin.StackedInline): + model = FollowUpAttachment + extra = 0 + + +class KBIAttachmentInline(admin.StackedInline): + model = KBIAttachment + extra = 0 @admin.register(FollowUp) class FollowUpAdmin(admin.ModelAdmin): - inlines = [TicketChangeInline, AttachmentInline] - list_display = ('ticket_get_ticket_for_url', 'title', 'date', 'ticket', 'user', 'new_status') + inlines = [TicketChangeInline, FollowUpAttachmentInline] + list_display = ('ticket_get_ticket_for_url', 'title', 'date', 'ticket', + 'user', 'new_status', 'time_spent') list_filter = ('user', 'date', 'new_status') def ticket_get_ticket_for_url(self, obj): @@ -51,6 +71,9 @@ class FollowUpAdmin(admin.ModelAdmin): @admin.register(KBItem) class KBItemAdmin(admin.ModelAdmin): list_display = ('category', 'title', 'last_updated',) + inlines = [KBIAttachmentInline] + readonly_fields = ('voted_by',) + list_display_links = ('title',) diff --git a/helpdesk/akismet.py b/helpdesk/akismet.py deleted file mode 100644 index 31564b92..00000000 --- a/helpdesk/akismet.py +++ /dev/null @@ -1,373 +0,0 @@ -# Version 0.2.0 -# 2009/06/18 - -# Copyright Michael Foord 2005-2009 -# akismet.py -# Python interface to the akismet API -# E-mail fuzzyman@voidspace.org.uk - -# http://www.voidspace.org.uk/python/modules.shtml -# http://akismet.com - -# Released subject to the BSD License -# See http://www.voidspace.org.uk/python/license.shtml - -# Updated by django-helpdesk developers, 2018 -# to be compatible with python 3 - - -""" -A python interface to the `Akismet `_ API. -This is a web service for blocking SPAM comments to blogs - or other online -services. - -You will need a Wordpress API key, from `wordpress.com `_. - -You should pass in the keyword argument 'agent' to the name of your program, -when you create an Akismet instance. This sets the ``user-agent`` to a useful -value. - -The default is:: - - Python Interface by Fuzzyman | akismet.py/0.2.0 - -Whatever you pass in, will replace the *Python Interface by Fuzzyman* part. -**0.2.0** will change with the version of this interface. - -Usage example:: - - from akismet import Akismet - - api = Akismet(agent='Test Script') - # if apikey.txt is in place, - # the key will automatically be set - # or you can call api.setAPIKey() - # - if api.key is None: - print >> sys.stderr, "No 'apikey.txt' file." - elif not api.verify_key(): - print >> sys.stderr, "The API key is invalid." - else: - # data should be a dictionary of values - # They can all be filled in with defaults - # from a CGI environment - if api.comment_check(comment, data): - print >> sys.stderr, 'This comment is spam.' - else: - print >> sys.stderr, 'This comment is ham.' -""" - - -import os -try: - from urllib import urlencode # python2 -except ImportError: - from urllib.parse import urlencode # python3 - -import socket -if hasattr(socket, 'setdefaulttimeout'): - # Set the default timeout on sockets to 5 seconds - socket.setdefaulttimeout(5) - -__version__ = '0.2.0' - -__all__ = ( - '__version__', - 'Akismet', - 'AkismetError', - 'APIKeyError', -) - -__author__ = 'Michael Foord ' - -__docformat__ = "restructuredtext en" - -user_agent = "%s | akismet.py/%s" -DEFAULTAGENT = 'Python Interface by Fuzzyman/%s' - -isfile = os.path.isfile - -urllib2 = None -try: - from google.appengine.api import urlfetch -except ImportError: - import urllib2 - -if urllib2 is None: - def _fetch_url(url, data, headers): - req = urlfetch.fetch(url=url, payload=data, method=urlfetch.POST, headers=headers) - if req.status_code == 200: - return req.content - raise Exception('Could not fetch Akismet URL: %s Response code: %s' % - (url, req.status_code)) -else: - def _fetch_url(url, data, headers): - req = urllib2.Request(url, data, headers) - h = urllib2.urlopen(req) - resp = h.read() - return resp - - -class AkismetError(Exception): - """Base class for all akismet exceptions.""" - pass - - -class APIKeyError(AkismetError): - """Invalid API key.""" - pass - - -class Akismet(object): - """A class for working with the akismet API""" - - baseurl = 'rest.akismet.com/1.1/' - - def __init__(self, key=None, blog_url=None, agent=None): - """Automatically calls ``setAPIKey``.""" - if agent is None: - agent = DEFAULTAGENT % __version__ - self.user_agent = user_agent % (agent, __version__) - self.setAPIKey(key, blog_url) - - def _getURL(self): - """ - Fetch the url to make requests to. - - This comprises of api key plus the baseurl. - """ - return 'http://%s.%s' % (self.key, self.baseurl) - - def _safeRequest(self, url, data, headers): - try: - resp = _fetch_url(url, data, headers) - except Exception as e: - raise AkismetError(str(e)) - return resp - - def setAPIKey(self, key=None, blog_url=None): - """ - Set the wordpress API key for all transactions. - - If you don't specify an explicit API ``key`` and ``blog_url`` it will - attempt to load them from a file called ``apikey.txt`` in the current - directory. - - This method is *usually* called automatically when you create a new - ``Akismet`` instance. - """ - if key is None and isfile('apikey.txt'): - the_file = [l.strip() for l in open('apikey.txt').readlines() - if l.strip() and not l.strip().startswith('#')] - try: - self.key = the_file[0] - self.blog_url = the_file[1] - except IndexError: - raise APIKeyError("Your 'apikey.txt' is invalid.") - else: - self.key = key - self.blog_url = blog_url - - def verify_key(self): - """ - This equates to the ``verify-key`` call against the akismet API. - - It returns ``True`` if the key is valid. - - The docs state that you *ought* to call this at the start of the - transaction. - - It raises ``APIKeyError`` if you have not yet set an API key. - - If the connection to akismet fails, it allows the normal ``HTTPError`` - or ``URLError`` to be raised. - (*akismet.py* uses `urllib2 `_) - """ - if self.key is None: - raise APIKeyError("Your have not set an API key.") - data = {'key': self.key, 'blog': self.blog_url} - # this function *doesn't* use the key as part of the URL - url = 'http://%sverify-key' % self.baseurl - # we *don't* trap the error here - # so if akismet is down it will raise an HTTPError or URLError - headers = {'User-Agent': self.user_agent} - resp = self._safeRequest(url, urlencode(data), headers) - if resp.lower() == 'valid': - return True - else: - return False - - def _build_data(self, comment, data): - """ - This function builds the data structure required by ``comment_check``, - ``submit_spam``, and ``submit_ham``. - - It modifies the ``data`` dictionary you give it in place. (and so - doesn't return anything) - - It raises an ``AkismetError`` if the user IP or user-agent can't be - worked out. - """ - data['comment_content'] = comment - if 'user_ip' not in data: - try: - val = os.environ['REMOTE_ADDR'] - except KeyError: - raise AkismetError("No 'user_ip' supplied") - data['user_ip'] = val - if 'user_agent' not in data: - try: - val = os.environ['HTTP_USER_AGENT'] - except KeyError: - raise AkismetError("No 'user_agent' supplied") - data['user_agent'] = val - # - data.setdefault('referrer', os.environ.get('HTTP_REFERER', 'unknown')) - data.setdefault('permalink', '') - data.setdefault('comment_type', 'comment') - data.setdefault('comment_author', '') - data.setdefault('comment_author_email', '') - data.setdefault('comment_author_url', '') - data.setdefault('SERVER_ADDR', os.environ.get('SERVER_ADDR', '')) - data.setdefault('SERVER_ADMIN', os.environ.get('SERVER_ADMIN', '')) - data.setdefault('SERVER_NAME', os.environ.get('SERVER_NAME', '')) - data.setdefault('SERVER_PORT', os.environ.get('SERVER_PORT', '')) - data.setdefault('SERVER_SIGNATURE', os.environ.get('SERVER_SIGNATURE', '')) - data.setdefault('SERVER_SOFTWARE', os.environ.get('SERVER_SOFTWARE', '')) - data.setdefault('HTTP_ACCEPT', os.environ.get('HTTP_ACCEPT', '')) - data.setdefault('blog', self.blog_url) - - def comment_check(self, comment, data=None, build_data=True, DEBUG=False): - """ - This is the function that checks comments. - - It returns ``True`` for spam and ``False`` for ham. - - If you set ``DEBUG=True`` then it will return the text of the response, - instead of the ``True`` or ``False`` object. - - It raises ``APIKeyError`` if you have not yet set an API key. - - If the connection to Akismet fails then the ``HTTPError`` or - ``URLError`` will be propogated. - - As a minimum it requires the body of the comment. This is the - ``comment`` argument. - - Akismet requires some other arguments, and allows some optional ones. - The more information you give it, the more likely it is to be able to - make an accurate diagnosise. - - You supply these values using a mapping object (dictionary) as the - ``data`` argument. - - If ``build_data`` is ``True`` (the default), then *akismet.py* will - attempt to fill in as much information as possible, using default - values where necessary. This is particularly useful for programs - running in a {acro;CGI} environment. A lot of useful information - can be supplied from evironment variables (``os.environ``). See below. - - You *only* need supply values for which you don't want defaults filled - in for. All values must be strings. - - There are a few required values. If they are not supplied, and - defaults can't be worked out, then an ``AkismetError`` is raised. - - If you set ``build_data=False`` and a required value is missing an - ``AkismetError`` will also be raised. - - The normal values (and defaults) are as follows : :: - - 'user_ip': os.environ['REMOTE_ADDR'] (*) - 'user_agent': os.environ['HTTP_USER_AGENT'] (*) - 'referrer': os.environ.get('HTTP_REFERER', 'unknown') [#]_ - 'permalink': '' - 'comment_type': 'comment' [#]_ - 'comment_author': '' - 'comment_author_email': '' - 'comment_author_url': '' - 'SERVER_ADDR': os.environ.get('SERVER_ADDR', '') - 'SERVER_ADMIN': os.environ.get('SERVER_ADMIN', '') - 'SERVER_NAME': os.environ.get('SERVER_NAME', '') - 'SERVER_PORT': os.environ.get('SERVER_PORT', '') - 'SERVER_SIGNATURE': os.environ.get('SERVER_SIGNATURE', '') - 'SERVER_SOFTWARE': os.environ.get('SERVER_SOFTWARE', '') - 'HTTP_ACCEPT': os.environ.get('HTTP_ACCEPT', '') - - (*) Required values - - You may supply as many additional 'HTTP_*' type values as you wish. - These should correspond to the http headers sent with the request. - - .. [#] Note the spelling "referrer". This is a required value by the - akismet api - however, referrer information is not always - supplied by the browser or server. In fact the HTTP protocol - forbids relying on referrer information for functionality in - programs. - .. [#] The `API docs `_ state that this value - can be " *blank, comment, trackback, pingback, or a made up value* - *like 'registration'* ". - """ - if self.key is None: - raise APIKeyError("Your have not set an API key.") - if data is None: - data = {} - if build_data: - self._build_data(comment, data) - if 'blog' not in data: - data['blog'] = self.blog_url - url = '%scomment-check' % self._getURL() - # we *don't* trap the error here - # so if akismet is down it will raise an HTTPError or URLError - headers = {'User-Agent': self.user_agent} - resp = self._safeRequest(url, urlencode(data), headers) - if DEBUG: - return resp - resp = resp.lower() - if resp == 'true': - return True - elif resp == 'false': - return False - else: - # NOTE: Happens when you get a 'howdy wilbur' response ! - raise AkismetError('missing required argument.') - - def submit_spam(self, comment, data=None, build_data=True): - """ - This function is used to tell akismet that a comment it marked as ham, - is really spam. - - It takes all the same arguments as ``comment_check``, except for - *DEBUG*. - """ - if self.key is None: - raise APIKeyError("Your have not set an API key.") - if data is None: - data = {} - if build_data: - self._build_data(comment, data) - url = '%ssubmit-spam' % self._getURL() - # we *don't* trap the error here - # so if akismet is down it will raise an HTTPError or URLError - headers = {'User-Agent': self.user_agent} - self._safeRequest(url, urlencode(data), headers) - - def submit_ham(self, comment, data=None, build_data=True): - """ - This function is used to tell akismet that a comment it marked as spam, - is really ham. - - It takes all the same arguments as ``comment_check``, except for - *DEBUG*. - """ - if self.key is None: - raise APIKeyError("Your have not set an API key.") - if data is None: - data = {} - if build_data: - self._build_data(comment, data) - url = '%ssubmit-ham' % self._getURL() - # we *don't* trap the error here - # so if akismet is down it will raise an HTTPError or URLError - headers = {'User-Agent': self.user_agent} - self._safeRequest(url, urlencode(data), headers) diff --git a/helpdesk/decorators.py b/helpdesk/decorators.py index 3ef8dfec..28dfe2aa 100644 --- a/helpdesk/decorators.py +++ b/helpdesk/decorators.py @@ -5,11 +5,44 @@ from django.http import Http404 from django.shortcuts import redirect from django.utils.decorators import available_attrs +from django.contrib.auth.decorators import user_passes_test from helpdesk import settings as helpdesk_settings +def check_staff_status(check_staff=False): + """ + Somewhat ridiculous currying to check user permissions without using lambdas. + The function most only take one User parameter at the end for use with + the Django function user_passes_test. + """ + def check_superuser_status(check_superuser): + def check_user_status(u): + is_ok = u.is_authenticated and u.is_active + if check_staff: + return is_ok and u.is_staff + elif check_superuser: + return is_ok and u.is_superuser + else: + return is_ok + return check_user_status + return check_superuser_status + + +if callable(helpdesk_settings.HELPDESK_ALLOW_NON_STAFF_TICKET_UPDATE): + # apply a custom user validation condition + is_helpdesk_staff = helpdesk_settings.HELPDESK_ALLOW_NON_STAFF_TICKET_UPDATE +elif helpdesk_settings.HELPDESK_ALLOW_NON_STAFF_TICKET_UPDATE: + # treat 'normal' users like 'staff' + is_helpdesk_staff = check_staff_status(False)(False) +else: + is_helpdesk_staff = check_staff_status(True)(False) + +helpdesk_staff_member_required = user_passes_test(is_helpdesk_staff) +helpdesk_superuser_required = user_passes_test(check_staff_status(False)(True)) + + def protect_view(view_func): """ Decorator for protecting the views checking user, redirecting diff --git a/helpdesk/email.py b/helpdesk/email.py new file mode 100644 index 00000000..5458e022 --- /dev/null +++ b/helpdesk/email.py @@ -0,0 +1,547 @@ +""" +Django Helpdesk - A Django powered ticket tracker for small enterprise. + +(c) Copyright 2008 Jutda. Copyright 2018 Timothy Hobbs. All Rights Reserved. +See LICENSE for details. +""" +from django.core.exceptions import ValidationError +from django.core.files.base import ContentFile +from django.core.files.uploadedfile import SimpleUploadedFile +from django.core.management.base import BaseCommand +from django.db.models import Q +from django.utils.translation import ugettext as _ +from django.utils import encoding, timezone +from django.contrib.auth import get_user_model + +from helpdesk import settings +from helpdesk.lib import safe_template_context, process_attachments +from helpdesk.models import Queue, Ticket, TicketCC, FollowUp, IgnoreEmail + +from datetime import timedelta +import base64 +import binascii +import email +from email.header import decode_header +from email.utils import getaddresses, parseaddr, collapse_rfc2231_value +import imaplib +import mimetypes +from os import listdir, unlink +from os.path import isfile, join +import poplib +import re +import socket +import ssl +import sys +from time import ctime +from optparse import make_option + +from bs4 import BeautifulSoup + +from email_reply_parser import EmailReplyParser + +import logging + +# import User model, which may be a custom model +User = get_user_model() + + +STRIPPED_SUBJECT_STRINGS = [ + "Re: ", + "Fw: ", + "RE: ", + "FW: ", + "Automatic reply: ", +] + + +def process_email(quiet=False): + for q in Queue.objects.filter( + email_box_type__isnull=False, + allow_email_submission=True): + + logger = logging.getLogger('django.helpdesk.queue.' + q.slug) + logging_types = { + 'info': logging.INFO, + 'warn': logging.WARN, + 'error': logging.ERROR, + 'crit': logging.CRITICAL, + 'debug': logging.DEBUG, + } + if q.logging_type in logging_types: + logger.setLevel(logging_types[q.logging_type]) + elif not q.logging_type or q.logging_type == 'none': + logging.disable(logging.CRITICAL) # disable all messages + if quiet: + logger.propagate = False # do not propagate to root logger that would log to console + logdir = q.logging_dir or '/var/log/helpdesk/' + handler = logging.FileHandler(join(logdir, q.slug + '_get_email.log')) + logger.addHandler(handler) + + if not q.email_box_last_check: + q.email_box_last_check = timezone.now() - timedelta(minutes=30) + + queue_time_delta = timedelta(minutes=q.email_box_interval or 0) + + if (q.email_box_last_check + queue_time_delta) < timezone.now(): + process_queue(q, logger=logger) + q.email_box_last_check = timezone.now() + q.save() + + +def pop3_sync(q, logger, server): + server.getwelcome() + server.user(q.email_box_user or settings.QUEUE_EMAIL_BOX_USER) + server.pass_(q.email_box_pass or settings.QUEUE_EMAIL_BOX_PASSWORD) + + messagesInfo = server.list()[1] + logger.info("Received %d messages from POP3 server" % len(messagesInfo)) + + for msgRaw in messagesInfo: + if type(msgRaw) is bytes: + try: + msg = msgRaw.decode("utf-8") + except UnicodeError: + # if couldn't decode easily, just leave it raw + msg = msgRaw + else: + # already a str + msg = msgRaw + msgNum = msg.split(" ")[0] + logger.info("Processing message %s" % msgNum) + + raw_content = server.retr(msgNum)[1] + if type(raw_content[0]) is bytes: + full_message = "\n".join([elm.decode('utf-8') for elm in raw_content]) + else: + full_message = encoding.force_text("\n".join(raw_content), errors='replace') + ticket = object_from_message(message=full_message, queue=q, logger=logger) + + if ticket: + server.dele(msgNum) + logger.info("Successfully processed message %s, deleted from POP3 server" % msgNum) + else: + logger.warn("Message %s was not successfully processed, and will be left on POP3 server" % msgNum) + + server.quit() + + +def imap_sync(q, logger, server): + try: + server.login(q.email_box_user or + settings.QUEUE_EMAIL_BOX_USER, + q.email_box_pass or + settings.QUEUE_EMAIL_BOX_PASSWORD) + server.select(q.email_box_imap_folder) + except imaplib.IMAP4.abort: + logger.error("IMAP login failed. Check that the server is accessible and that the username and password are correct.") + server.logout() + sys.exit() + except ssl.SSLError: + logger.error("IMAP login failed due to SSL error. This is often due to a timeout. Please check your connection and try again.") + server.logout() + sys.exit() + + try: + status, data = server.search(None, 'NOT', 'DELETED') + except imaplib.IMAP4.error: + logger.error("IMAP retrieve failed. Is the folder '%s' spelled correctly, and does it exist on the server?" % q.email_box_imap_folder) + if data: + msgnums = data[0].split() + logger.info("Received %d messages from IMAP server" % len(msgnums)) + for num in msgnums: + logger.info("Processing message %s" % num) + status, data = server.fetch(num, '(RFC822)') + full_message = encoding.force_text(data[0][1], errors='replace') + try: + ticket = object_from_message(message=full_message, queue=q, logger=logger) + except TypeError: + ticket = None # hotfix. Need to work out WHY. + if ticket: + server.store(num, '+FLAGS', '\\Deleted') + logger.info("Successfully processed message %s, deleted from IMAP server" % num) + else: + logger.warn("Message %s was not successfully processed, and will be left on IMAP server" % num) + + server.expunge() + server.close() + server.logout() + + +def process_queue(q, logger): + logger.info("***** %s: Begin processing mail for django-helpdesk" % ctime()) + + if q.socks_proxy_type and q.socks_proxy_host and q.socks_proxy_port: + try: + import socks + except ImportError: + no_socks_msg = "Queue has been configured with proxy settings, " \ + "but no socks library was installed. Try to " \ + "install PySocks via PyPI." + logger.error(no_socks_msg) + raise ImportError(no_socks_msg) + + proxy_type = { + 'socks4': socks.SOCKS4, + 'socks5': socks.SOCKS5, + }.get(q.socks_proxy_type) + + socks.set_default_proxy(proxy_type=proxy_type, + addr=q.socks_proxy_host, + port=q.socks_proxy_port) + socket.socket = socks.socksocket + + email_box_type = settings.QUEUE_EMAIL_BOX_TYPE or q.email_box_type + + mail_defaults = { + 'pop3': { + 'ssl': { + 'port': 995, + 'init': poplib.POP3_SSL, + }, + 'insecure': { + 'port': 110, + 'init': poplib.POP3, + }, + 'sync': pop3_sync, + }, + 'imap': { + 'ssl': { + 'port': 993, + 'init': imaplib.IMAP4_SSL, + }, + 'insecure': { + 'port': 143, + 'init': imaplib.IMAP4, + }, + 'sync': imap_sync + } + } + if email_box_type in mail_defaults: + encryption = 'insecure' + if q.email_box_ssl or settings.QUEUE_EMAIL_BOX_SSL: + encryption = 'ssl' + if not q.email_box_port: + q.email_box_port = mail_defaults[email_box_type][encryption]['port'] + + server = mail_defaults[email_box_type][encryption]['init']( + q.email_box_host or settings.QUEUE_EMAIL_BOX_HOST, + int(q.email_box_port) + ) + logger.info("Attempting %s server login" % email_box_type.upper()) + mail_defaults[email_box_type]['sync'](q, logger, server) + + elif email_box_type == 'local': + mail_dir = q.email_box_local_dir or '/var/lib/mail/helpdesk/' + mail = [join(mail_dir, f) for f in listdir(mail_dir) if isfile(join(mail_dir, f))] + logger.info("Found %d messages in local mailbox directory" % len(mail)) + + logger.info("Found %d messages in local mailbox directory" % len(mail)) + for i, m in enumerate(mail, 1): + logger.info("Processing message %d" % i) + with open(m, 'r') as f: + full_message = encoding.force_text(f.read(), errors='replace') + ticket = object_from_message(message=full_message, queue=q, logger=logger) + if ticket: + logger.info("Successfully processed message %d, ticket/comment created." % i) + try: + unlink(m) # delete message file if ticket was successful + except OSError: + logger.error("Unable to delete message %d." % i) + else: + logger.info("Successfully deleted message %d." % i) + else: + logger.warn("Message %d was not successfully processed, and will be left in local directory" % i) + + +def decodeUnknown(charset, string): + if type(string) is not str: + if not charset: + try: + return str(string, encoding='utf-8', errors='replace') + except UnicodeError: + return str(string, encoding='iso8859-1', errors='replace') + return str(string, encoding=charset, errors='replace') + return string + + +def decode_mail_headers(string): + decoded = email.header.decode_header(string) + return u' '.join([str(msg, encoding=charset, errors='replace') if charset else str(msg) for msg, charset in decoded]) + + +def create_ticket_cc(ticket, cc_list): + + if not cc_list: + return [] + + # Local import to deal with non-defined / circular reference problem + from helpdesk.views.staff import User, subscribe_to_ticket_updates + + new_ticket_ccs = [] + for cced_name, cced_email in cc_list: + + cced_email = cced_email.strip() + if cced_email == ticket.queue.email_address: + continue + + user = None + + try: + user = User.objects.get(email=cced_email) + except User.DoesNotExist: + pass + + try: + ticket_cc = subscribe_to_ticket_updates(ticket=ticket, user=user, email=cced_email) + new_ticket_ccs.append(ticket_cc) + except ValidationError as err: + pass + + return new_ticket_ccs + + +def create_object_from_email_message(message, ticket_id, payload, files, logger): + + ticket, previous_followup, new = None, None, False + now = timezone.now() + + queue = payload['queue'] + sender_email = payload['sender_email'] + + to_list = getaddresses(message.get_all('To', [])) + cc_list = getaddresses(message.get_all('Cc', [])) + + message_id = message.get('Message-Id') + in_reply_to = message.get('In-Reply-To') + + if in_reply_to is not None: + try: + queryset = FollowUp.objects.filter(message_id=in_reply_to).order_by('-date') + if queryset.count() > 0: + previous_followup = queryset.first() + ticket = previous_followup.ticket + except FollowUp.DoesNotExist: + pass # play along. The header may be wrong + + if previous_followup is None and ticket_id is not None: + try: + ticket = Ticket.objects.get(id=ticket_id) + new = False + except Ticket.DoesNotExist: + ticket = None + + # New issue, create a new instance + if ticket is None: + if not settings.QUEUE_EMAIL_BOX_UPDATE_ONLY: + ticket = Ticket.objects.create( + title=payload['subject'], + queue=queue, + submitter_email=sender_email, + created=now, + description=payload['body'], + priority=payload['priority'], + ) + ticket.save() + logger.debug("Created new ticket %s-%s" % (ticket.queue.slug, ticket.id)) + + new = True + update = '' + + # Old issue being re-opened + elif ticket.status == Ticket.CLOSED_STATUS: + ticket.status = Ticket.REOPENED_STATUS + ticket.save() + + f = FollowUp( + ticket=ticket, + title=_('E-Mail Received from %(sender_email)s' % {'sender_email': sender_email}), + date=now, + public=True, + comment=payload['body'], + message_id=message_id + ) + + if ticket.status == Ticket.REOPENED_STATUS: + f.new_status = Ticket.REOPENED_STATUS + f.title = _('Ticket Re-Opened by E-Mail Received from %(sender_email)s' % {'sender_email': sender_email}) + + f.save() + logger.debug("Created new FollowUp for Ticket") + + logger.info("[%s-%s] %s" % (ticket.queue.slug, ticket.id, ticket.title,)) + + attached = process_attachments(f, files) + for att_file in attached: + logger.info("Attachment '%s' (with size %s) successfully added to ticket from email." % (att_file[0], att_file[1].size)) + + context = safe_template_context(ticket) + + new_ticket_ccs = [] + new_ticket_ccs.append(create_ticket_cc(ticket, to_list + cc_list)) + + notifications_to_be_sent = [sender_email] + + if queue.enable_notifications_on_email_events and len(notifications_to_be_sent): + + ticket_cc_list = TicketCC.objects.filter(ticket=ticket).all().values_list('email', flat=True) + + for email in ticket_cc_list: + notifications_to_be_sent.append(email) + + # send mail to appropriate people now depending on what objects + # were created and who was CC'd + if new: + ticket.send( + {'submitter': ('newticket_submitter', context), + 'new_ticket_cc': ('newticket_cc', context), + 'ticket_cc': ('newticket_cc', context)}, + fail_silently=True, + extra_headers={'In-Reply-To': message_id}, + ) + else: + context.update(comment=f.comment) + ticket.send( + {'submitter': ('newticket_submitter', context), + 'assigned_to': ('updated_owner', context)}, + fail_silently=True, + extra_headers={'In-Reply-To': message_id}, + ) + if queue.enable_notifications_on_email_events: + ticket.send( + {'ticket_cc': ('updated_cc', context)}, + fail_silently=True, + extra_headers={'In-Reply-To': message_id}, + ) + + return ticket + + +def object_from_message(message, queue, logger): + # 'message' must be an RFC822 formatted message. + message = email.message_from_string(message) + + subject = message.get('subject', _('Comment from e-mail')) + subject = decode_mail_headers(decodeUnknown(message.get_charset(), subject)) + for affix in STRIPPED_SUBJECT_STRINGS: + subject = subject.replace(affix, "") + subject = subject.strip() + + sender = message.get('from', _('Unknown Sender')) + sender = decode_mail_headers(decodeUnknown(message.get_charset(), sender)) + sender_email = email.utils.parseaddr(sender)[1] + + body_plain, body_html = '', '' + + cc = message.get_all('cc', None) + if cc: + # first, fixup the encoding if necessary + cc = [decode_mail_headers(decodeUnknown(message.get_charset(), x)) for x in cc] + # get_all checks if multiple CC headers, but individual emails may be comma separated too + tempcc = [] + for hdr in cc: + tempcc.extend(hdr.split(',')) + # use a set to ensure no duplicates + cc = set([x.strip() for x in tempcc]) + + for ignore in IgnoreEmail.objects.filter(Q(queues=queue) | Q(queues__isnull=True)): + if ignore.test(sender_email): + if ignore.keep_in_mailbox: + # By returning 'False' the message will be kept in the mailbox, + # and the 'True' will cause the message to be deleted. + return False + return True + + matchobj = re.match(r".*\[" + queue.slug + r"-(?P\d+)\]", subject) + if matchobj: + # This is a reply or forward. + ticket = matchobj.group('id') + logger.info("Matched tracking ID %s-%s" % (queue.slug, ticket)) + else: + logger.info("No tracking ID matched.") + ticket = None + + body = None + counter = 0 + files = [] + + for part in message.walk(): + if part.get_content_maintype() == 'multipart': + continue + + name = part.get_param("name") + if name: + name = email.utils.collapse_rfc2231_value(name) + + if part.get_content_maintype() == 'text' and name is None: + if part.get_content_subtype() == 'plain': + body = part.get_payload(decode=True) + # https://github.com/django-helpdesk/django-helpdesk/issues/732 + if part['Content-Transfer-Encoding'] == '8bit' and part.get_content_charset() == 'utf-8': + body = body.decode('unicode_escape') + body = decodeUnknown(part.get_content_charset(), body) + body = EmailReplyParser.parse_reply(body) + # workaround to get unicode text out rather than escaped text + try: + body = body.encode('ascii').decode('unicode_escape') + except UnicodeEncodeError: + body.encode('utf-8') + logger.debug("Discovered plain text MIME part") + else: + payload = """ + + + + +%s +""" % encoding.smart_text(part.get_payload(decode=True)) + files.append( + SimpleUploadedFile(_("email_html_body.html"), payload.encode("utf-8"), 'text/html') + ) + logger.debug("Discovered HTML MIME part") + else: + if not name: + ext = mimetypes.guess_extension(part.get_content_type()) + name = "part-%i%s" % (counter, ext) + payload = part.get_payload() + if isinstance(payload, list): + payload = payload.pop().as_string() + payloadToWrite = payload + # check version of python to ensure use of only the correct error type + non_b64_err = TypeError + try: + logger.debug("Try to base64 decode the attachment payload") + payloadToWrite = base64.decodebytes(payload) + except non_b64_err: + logger.debug("Payload was not base64 encoded, using raw bytes") + payloadToWrite = payload + files.append(SimpleUploadedFile(name, part.get_payload(decode=True), mimetypes.guess_type(name)[0])) + logger.debug("Found MIME attachment %s" % name) + + counter += 1 + + if not body: + mail = BeautifulSoup(str(message), "html.parser") + beautiful_body = mail.find('body') + if beautiful_body: + try: + body = beautiful_body.text + except AttributeError: + pass + if not body: + body = "" + + smtp_priority = message.get('priority', '') + smtp_importance = message.get('importance', '') + high_priority_types = {'high', 'important', '1', 'urgent'} + priority = 2 if high_priority_types & {smtp_priority, smtp_importance} else 3 + + payload = { + 'body': body, + 'subject': subject, + 'queue': queue, + 'sender_email': sender_email, + 'priority': priority, + 'files': files, + } + + return create_object_from_email_message(message, ticket, payload, files, logger=logger) diff --git a/helpdesk/forms.py b/helpdesk/forms.py index e9d4ef64..bd6b4603 100644 --- a/helpdesk/forms.py +++ b/helpdesk/forms.py @@ -9,7 +9,6 @@ forms.py - Definitions of newforms-based forms for creating and maintaining from django.core.exceptions import ObjectDoesNotExist -from django.utils.six import StringIO from django import forms from django.forms import widgets from django.conf import settings @@ -17,9 +16,9 @@ from django.utils.translation import ugettext_lazy as _ from django.contrib.auth import get_user_model from django.utils import timezone -from helpdesk.lib import send_templated_mail, safe_template_context, process_attachments -from helpdesk.models import (Ticket, Queue, FollowUp, Attachment, IgnoreEmail, TicketCC, - CustomField, TicketCustomFieldValue, TicketDependency) +from helpdesk.lib import safe_template_context, process_attachments +from helpdesk.models import (Ticket, Queue, FollowUp, IgnoreEmail, TicketCC, + CustomField, TicketCustomFieldValue, TicketDependency, UserSettings) from helpdesk import settings as helpdesk_settings User = get_user_model() @@ -167,10 +166,12 @@ class AbstractTicketForm(CustomFieldMixin, forms.Form): due_date = forms.DateTimeField( widget=forms.TextInput(attrs={'class': 'form-control'}), required=False, + input_formats=['%d/%m/%Y', '%m/%d/%Y', "%d.%m.%Y", ], label=_('Due on'), ) attachment = forms.FileField( + widget=forms.FileInput(attrs={'class': 'form-control-file'}), required=False, label=_('Attach File'), help_text=_('You can attach a file such as a document or screenshot to this ticket.'), @@ -238,56 +239,16 @@ class AbstractTicketForm(CustomFieldMixin, forms.Form): context = safe_template_context(ticket) context['comment'] = followup.comment - messages_sent_to = [] - - if ticket.submitter_email: - send_templated_mail( - 'newticket_submitter', - context, - recipients=ticket.submitter_email, - sender=queue.from_address, - fail_silently=True, - files=files, - ) - messages_sent_to.append(ticket.submitter_email) - - if ticket.assigned_to and \ - ticket.assigned_to != user and \ - ticket.assigned_to.usersettings_helpdesk.settings.get('email_on_ticket_assign', False) and \ - ticket.assigned_to.email and \ - ticket.assigned_to.email not in messages_sent_to: - send_templated_mail( - 'assigned_owner', - context, - recipients=ticket.assigned_to.email, - sender=queue.from_address, - fail_silently=True, - files=files, - ) - messages_sent_to.append(ticket.assigned_to.email) - - if queue.new_ticket_cc and queue.new_ticket_cc not in messages_sent_to: - send_templated_mail( - 'newticket_cc', - context, - recipients=queue.new_ticket_cc, - sender=queue.from_address, - fail_silently=True, - files=files, - ) - messages_sent_to.append(queue.new_ticket_cc) - - if queue.updated_ticket_cc and \ - queue.updated_ticket_cc != queue.new_ticket_cc and \ - queue.updated_ticket_cc not in messages_sent_to: - send_templated_mail( - 'newticket_cc', - context, - recipients=queue.updated_ticket_cc, - sender=queue.from_address, - fail_silently=True, - files=files, - ) + roles = {'submitter': ('newticket_submitter', context), + 'new_ticket_cc': ('newticket_cc', context), + 'ticket_cc': ('newticket_cc', context)} + if ticket.assigned_to and ticket.assigned_to.usersettings_helpdesk.email_on_ticket_assign: + roles['assigned_to'] = ('assigned_owner', context) + ticket.send( + roles, + fail_silently=True, + files=files, + ) class TicketForm(AbstractTicketForm): @@ -297,25 +258,34 @@ class TicketForm(AbstractTicketForm): submitter_email = forms.EmailField( required=False, label=_('Submitter E-Mail Address'), - widget=forms.TextInput(attrs={'class': 'form-control'}), + widget=forms.TextInput(attrs={'class': 'form-control', 'type': 'email'}), help_text=_('This e-mail address will receive copies of all public ' 'updates to this ticket.'), ) - assigned_to = forms.ChoiceField( - widget=forms.Select(attrs={'class': 'form-control'}), - choices=(), + widget=forms.Select(attrs={'class': 'form-control'}) if not helpdesk_settings.HELPDESK_CREATE_TICKET_HIDE_ASSIGNED_TO else forms.HiddenInput(), required=False, label=_('Case owner'), help_text=_('If you select an owner other than yourself, they\'ll be ' 'e-mailed details of this ticket immediately.'), + + choices=() ) def __init__(self, *args, **kwargs): """ Add any custom fields that are defined to the form. """ - super(TicketForm, self).__init__(*args, **kwargs) + queue_choices = kwargs.pop("queue_choices") + + super().__init__(*args, **kwargs) + + self.fields['queue'].choices = queue_choices + if helpdesk_settings.HELPDESK_STAFF_ONLY_TICKET_OWNERS: + assignable_users = User.objects.filter(is_active=True, is_staff=True).order_by(User.USERNAME_FIELD) + else: + assignable_users = User.objects.filter(is_active=True).order_by(User.USERNAME_FIELD) + self.fields['assigned_to'].choices = [('', '--------')] + [(u.id, u.get_username()) for u in assignable_users] self._add_form_custom_fields() def save(self, user=None): @@ -357,7 +327,7 @@ class PublicTicketForm(AbstractTicketForm): Ticket Form creation for all users (public-facing). """ submitter_email = forms.EmailField( - widget=forms.TextInput(attrs={'class': 'form-control'}), + widget=forms.TextInput(attrs={'class': 'form-control', 'type': 'email'}), required=True, label=_('Your E-Mail Address'), help_text=_('We will e-mail you when your ticket is updated.'), @@ -375,8 +345,8 @@ class PublicTicketForm(AbstractTicketForm): self.fields['priority'].widget = forms.HiddenInput() if hasattr(settings, 'HELPDESK_PUBLIC_TICKET_DUE_DATE'): self.fields['due_date'].widget = forms.HiddenInput() - - self._add_form_custom_fields(False) + self.fields['queue'].choices = [('', '--------')] + [ + (q.id, q.title) for q in Queue.objects.filter(allow_public_submission=True)] def save(self): """ @@ -400,40 +370,11 @@ class PublicTicketForm(AbstractTicketForm): return ticket -class UserSettingsForm(forms.Form): - login_view_ticketlist = forms.BooleanField( - label=_('Show Ticket List on Login?'), - help_text=_('Display the ticket list upon login? Otherwise, the dashboard is shown.'), - required=False, - ) +class UserSettingsForm(forms.ModelForm): - email_on_ticket_change = forms.BooleanField( - label=_('E-mail me on ticket change?'), - help_text=_('If you\'re the ticket owner and the ticket is changed via the web by somebody else, do you want to receive an e-mail?'), - required=False, - ) - - email_on_ticket_assign = forms.BooleanField( - label=_('E-mail me when assigned a ticket?'), - help_text=_('If you are assigned a ticket via the web, do you want to receive an e-mail?'), - required=False, - ) - - tickets_per_page = forms.ChoiceField( - label=_('Number of tickets to show per page'), - help_text=_('How many tickets do you want to see on the Ticket List page?'), - required=False, - choices=((10, '10'), (25, '25'), (50, '50'), (100, '100')), - ) - - use_email_as_submitter = forms.BooleanField( - label=_('Use my e-mail address when submitting tickets?'), - help_text=_('When you submit a ticket, do you want to automatically ' - 'use your e-mail address as the submitter address? You ' - 'can type a different e-mail address when entering the ' - 'ticket if needed, this option only changes the default.'), - required=False, - ) + class Meta: + model = UserSettings + exclude = ['user', 'settings_pickled'] class EmailIgnoreForm(forms.ModelForm): diff --git a/helpdesk/lib.py b/helpdesk/lib.py index 7269e505..9a48af5b 100644 --- a/helpdesk/lib.py +++ b/helpdesk/lib.py @@ -9,207 +9,17 @@ lib.py - Common functions (eg multipart e-mail) import logging import mimetypes import os -from smtplib import SMTPException - -try: - # Python 2 support - from base64 import urlsafe_b64encode as b64encode -except ImportError: - # Python 3 support - from base64 import encodebytes as b64encode -try: - # Python 2 support - from base64 import urlsafe_b64decode as b64decode -except ImportError: - # Python 3 support - from base64 import decodebytes as b64decode from django.conf import settings -from django.db.models import Q -from django.utils import six -from django.utils.encoding import smart_text +from django.utils.encoding import smart_text, smart_str from django.utils.safestring import mark_safe -from helpdesk.models import Attachment, EmailTemplate +from helpdesk.models import FollowUpAttachment, EmailTemplate + logger = logging.getLogger('helpdesk') -def send_templated_mail(template_name, - context, - recipients, - sender=None, - bcc=None, - fail_silently=False, - files=None): - """ - send_templated_mail() is a wrapper around Django's e-mail routines that - allows us to easily send multipart (text/plain & text/html) e-mails using - templates that are stored in the database. This lets the admin provide - both a text and a HTML template for each message. - - template_name is the slug of the template to use for this message (see - models.EmailTemplate) - - context is a dictionary to be used when rendering the template - - recipients can be either a string, eg 'a@b.com', or a list of strings. - - sender should contain a string, eg 'My Site '. If you leave it - blank, it'll use settings.DEFAULT_FROM_EMAIL as a fallback. - - bcc is an optional list of addresses that will receive this message as a - blind carbon copy. - - fail_silently is passed to Django's mail routine. Set to 'True' to ignore - any errors at send time. - - files can be a list of tuples. Each tuple should be a filename to attach, - along with the File objects to be read. files can be blank. - - """ - from django.core.mail import EmailMultiAlternatives - from django.template import engines - from_string = engines['django'].from_string - - from helpdesk.models import EmailTemplate - from helpdesk.settings import HELPDESK_EMAIL_SUBJECT_TEMPLATE, \ - HELPDESK_EMAIL_FALLBACK_LOCALE - - locale = context['queue'].get('locale') or HELPDESK_EMAIL_FALLBACK_LOCALE - - try: - t = EmailTemplate.objects.get(template_name__iexact=template_name, locale=locale) - except EmailTemplate.DoesNotExist: - try: - t = EmailTemplate.objects.get(template_name__iexact=template_name, locale__isnull=True) - except EmailTemplate.DoesNotExist: - logger.warning('template "%s" does not exist, no mail sent', template_name) - return # just ignore if template doesn't exist - - subject_part = from_string( - HELPDESK_EMAIL_SUBJECT_TEMPLATE % { - "subject": t.subject - }).render(context).replace('\n', '').replace('\r', '') - - footer_file = os.path.join('helpdesk', locale, 'email_text_footer.txt') - - text_part = from_string( - "%s{%% include '%s' %%}" % (t.plain_text, footer_file) - ).render(context) - - email_html_base_file = os.path.join('helpdesk', locale, 'email_html_base.html') - # keep new lines in html emails - if 'comment' in context: - context['comment'] = mark_safe(context['comment'].replace('\r\n', '
')) - - html_part = from_string( - "{%% extends '%s' %%}{%% block title %%}" - "%s" - "{%% endblock %%}{%% block content %%}%s{%% endblock %%}" % - (email_html_base_file, t.heading, t.html) - ).render(context) - - if isinstance(recipients, str): - if recipients.find(','): - recipients = recipients.split(',') - elif type(recipients) != list: - recipients = [recipients] - - msg = EmailMultiAlternatives(subject_part, text_part, - sender or settings.DEFAULT_FROM_EMAIL, - recipients, bcc=bcc) - msg.attach_alternative(html_part, "text/html") - - if files: - for filename, filefield in files: - mime = mimetypes.guess_type(filename) - if mime[0] is not None and mime[0] == "text/plain": - with open(filefield.path, 'r') as attachedfile: - content = attachedfile.read() - msg.attach(filename, content) - else: - if six.PY3: - msg.attach_file(filefield.path) - else: - with open(filefield.path, 'rb') as attachedfile: - content = attachedfile.read() - msg.attach(filename, content) - - logger.debug('Sending email to: {!r}'.format(recipients)) - - try: - return msg.send() - except SMTPException as e: - logger.exception('SMTPException raised while sending email to {}'.format(recipients)) - if not fail_silently: - raise e - return 0 - - -def query_to_dict(results, descriptions): - """ - Replacement method for cursor.dictfetchall() as that method no longer - exists in psycopg2, and I'm guessing in other backends too. - - Converts the results of a raw SQL query into a list of dictionaries, suitable - for use in templates etc. - """ - - output = [] - for data in results: - row = {} - i = 0 - for column in descriptions: - row[column[0]] = data[i] - i += 1 - - output.append(row) - return output - - -def apply_query(queryset, params): - """ - Apply a dict-based set of filters & parameters to a queryset. - - queryset is a Django queryset, eg MyModel.objects.all() or - MyModel.objects.filter(user=request.user) - - params is a dictionary that contains the following: - filtering: A dict of Django ORM filters, eg: - {'user__id__in': [1, 3, 103], 'title__contains': 'foo'} - - search_string: A freetext search string - - sorting: The name of the column to sort by - """ - for key in params['filtering'].keys(): - filter = {key: params['filtering'][key]} - queryset = queryset.filter(**filter) - - search = params.get('search_string', None) - if search: - qset = ( - Q(title__icontains=search) | - Q(description__icontains=search) | - Q(resolution__icontains=search) | - Q(submitter_email__icontains=search) | - Q(ticketcustomfieldvalue__value__icontains=search) - ) - - # Distinct works, when there are multiple custom fields - queryset = queryset.filter(qset).distinct() - - sorting = params.get('sorting', None) - if sorting: - sortreverse = params.get('sortreverse', None) - if sortreverse: - sorting = "-%s" % sorting - queryset = queryset.order_by(sorting) - - return queryset - - def ticket_template_context(ticket): context = {} @@ -275,7 +85,7 @@ def text_is_spam(text, request): from django.contrib.sites.models import Site from django.core.exceptions import ImproperlyConfigured try: - from helpdesk.akismet import Akismet + from akismet import Akismet except ImportError: return False try: @@ -283,19 +93,27 @@ def text_is_spam(text, request): except ImproperlyConfigured: site = Site(domain='configure-django-sites.com') - ak = Akismet( - blog_url='http://%s/' % site.domain, - agent='django-helpdesk', - ) + # see https://akismet.readthedocs.io/en/latest/overview.html#using-akismet + + apikey = None if hasattr(settings, 'TYPEPAD_ANTISPAM_API_KEY'): - ak.setAPIKey(key=settings.TYPEPAD_ANTISPAM_API_KEY) + apikey = settings.TYPEPAD_ANTISPAM_API_KEY ak.baseurl = 'api.antispam.typepad.com/1.1/' + elif hasattr(settings, 'PYTHON_AKISMET_API_KEY'): + # new env var expected by python-akismet package + apikey = settings.PYTHON_AKISMET_API_KEY elif hasattr(settings, 'AKISMET_API_KEY'): - ak.setAPIKey(key=settings.AKISMET_API_KEY) + # deprecated, but kept for backward compatibility + apikey = settings.AKISMET_API_KEY else: return False + ak = Akismet( + blog_url='http://%s/' % site.domain, + key=apikey, + ) + if ak.verify_key(): ak_data = { 'user_ip': request.META.get('REMOTE_ADDR', '127.0.0.1'), @@ -315,9 +133,10 @@ def process_attachments(followup, attached_files): attachments = [] for attached in attached_files: + if attached.size: filename = smart_text(attached.name) - att = Attachment( + att = FollowUpAttachment( followup=followup, file=attached, filename=filename, @@ -334,3 +153,18 @@ def process_attachments(followup, attached_files): attachments.append([filename, att.file]) return attachments + + +def format_time_spent(time_spent): + """Format time_spent attribute to "[H]HHh:MMm" text string to be allign in + all graphical outputs + """ + + if time_spent: + time_spent = "{0:02d}h:{0:02d}m".format( + int(time_spent.total_seconds() // 3600), + int((time_spent.total_seconds() % 3600) / 60) + ) + else: + time_spent = "" + return time_spent diff --git a/helpdesk/management/commands/create_escalation_exclusions.py b/helpdesk/management/commands/create_escalation_exclusions.py index fcda257d..8801e0f0 100644 --- a/helpdesk/management/commands/create_escalation_exclusions.py +++ b/helpdesk/management/commands/create_escalation_exclusions.py @@ -8,17 +8,15 @@ scripts/create_escalation_exclusion.py - Easy way to routinely add particular days to the list of days on which no escalation should take place. """ -from __future__ import print_function +from django.core.management.base import BaseCommand, CommandError + +from helpdesk.models import EscalationExclusion, Queue from datetime import timedelta, date import getopt from optparse import make_option import sys -from django.core.management.base import BaseCommand, CommandError - -from helpdesk.models import EscalationExclusion, Queue - class Command(BaseCommand): diff --git a/helpdesk/management/commands/create_usersettings.py b/helpdesk/management/commands/create_usersettings.py index 46280159..9e5ced07 100644 --- a/helpdesk/management/commands/create_usersettings.py +++ b/helpdesk/management/commands/create_usersettings.py @@ -29,5 +29,4 @@ class Command(BaseCommand): def handle(self, *args, **options): """handle command line""" for u in User.objects.all(): - UserSettings.objects.get_or_create(user=u, - defaults={'settings': DEFAULT_USER_SETTINGS}) + UserSettings.objects.get_or_create(user=u) diff --git a/helpdesk/management/commands/escalate_tickets.py b/helpdesk/management/commands/escalate_tickets.py index f7468182..122333f0 100644 --- a/helpdesk/management/commands/escalate_tickets.py +++ b/helpdesk/management/commands/escalate_tickets.py @@ -7,7 +7,6 @@ django-helpdesk - A Django powered ticket tracker for small enterprise. scripts/escalate_tickets.py - Easy way to escalate tickets based on their age, designed to be run from Cron or similar. """ -from __future__ import print_function from datetime import timedelta, date import getopt @@ -17,14 +16,10 @@ import sys from django.core.management.base import BaseCommand, CommandError from django.db.models import Q from django.utils.translation import ugettext as _ - -try: - from django.utils import timezone -except ImportError: - from datetime import datetime as timezone +from django.utils import timezone from helpdesk.models import Queue, Ticket, FollowUp, EscalationExclusion, TicketChange -from helpdesk.lib import send_templated_mail, safe_template_context +from helpdesk.lib import safe_template_context class Command(BaseCommand): @@ -107,32 +102,12 @@ def escalate_tickets(queues, verbose): context = safe_template_context(t) - if t.submitter_email: - send_templated_mail( - 'escalated_submitter', - context, - recipients=t.submitter_email, - sender=t.queue.from_address, - fail_silently=True, - ) - - if t.queue.updated_ticket_cc: - send_templated_mail( - 'escalated_cc', - context, - recipients=t.queue.updated_ticket_cc, - sender=t.queue.from_address, - fail_silently=True, - ) - - if t.assigned_to: - send_templated_mail( - 'escalated_owner', - context, - recipients=t.assigned_to.email, - sender=t.queue.from_address, - fail_silently=True, - ) + t.send( + {'submitter': ('escalated_submitter', context), + 'ticket_cc': ('escalated_cc', context), + 'assigned_to': ('escalated_owner', context)} + fail_silently=True, + ) if verbose: print(" - Esclating %s from %s>%s" % ( diff --git a/helpdesk/management/commands/get_email.py b/helpdesk/management/commands/get_email.py index 273b7fd5..ce344bd5 100755 --- a/helpdesk/management/commands/get_email.py +++ b/helpdesk/management/commands/get_email.py @@ -10,52 +10,9 @@ scripts/get_email.py - Designed to be run from cron, this script checks the helpdesk, creating tickets from the new messages (or adding to existing tickets if needed) """ -from __future__ import unicode_literals - -from datetime import timedelta -import base64 -import binascii -import email -import imaplib -import mimetypes -from os import listdir, unlink -from os.path import isfile, join -import poplib -import re -import socket -import ssl -import sys -from time import ctime - -from bs4 import BeautifulSoup - -from email_reply_parser import EmailReplyParser - - -from django.contrib.auth import get_user_model -from django.core.files.base import ContentFile -from django.core.files.uploadedfile import SimpleUploadedFile from django.core.management.base import BaseCommand -from django.db.models import Q -from django.utils.translation import ugettext as _ -from django.utils import encoding, six, timezone -from helpdesk import settings -from helpdesk.lib import send_templated_mail, safe_template_context, process_attachments -from helpdesk.models import Queue, Ticket, TicketCC, FollowUp, IgnoreEmail - -import logging - -User = get_user_model() - - -STRIPPED_SUBJECT_STRINGS = [ - "Re: ", - "Fw: ", - "RE: ", - "FW: ", - "Automatic reply: ", -] +from helpdesk.email import process_email class Command(BaseCommand): @@ -80,488 +37,5 @@ class Command(BaseCommand): process_email(quiet=quiet) -def process_email(quiet=False): - for q in Queue.objects.filter( - email_box_type__isnull=False, - allow_email_submission=True): - - logger = logging.getLogger('django.helpdesk.queue.' + q.slug) - if not q.logging_type or q.logging_type == 'none': - logging.disable(logging.CRITICAL) # disable all messages - elif q.logging_type == 'info': - logger.setLevel(logging.INFO) - elif q.logging_type == 'warn': - logger.setLevel(logging.WARN) - elif q.logging_type == 'error': - logger.setLevel(logging.ERROR) - elif q.logging_type == 'crit': - logger.setLevel(logging.CRITICAL) - elif q.logging_type == 'debug': - logger.setLevel(logging.DEBUG) - if quiet: - logger.propagate = False # do not propagate to root logger that would log to console - logdir = q.logging_dir or '/var/log/helpdesk/' - handler = logging.FileHandler(join(logdir, q.slug + '_get_email.log')) - logger.addHandler(handler) - - if not q.email_box_last_check: - q.email_box_last_check = timezone.now() - timedelta(minutes=30) - - queue_time_delta = timedelta(minutes=q.email_box_interval or 0) - - if (q.email_box_last_check + queue_time_delta) < timezone.now(): - process_queue(q, logger=logger) - q.email_box_last_check = timezone.now() - q.save() - - -def process_queue(q, logger): - logger.info("***** %s: Begin processing mail for django-helpdesk" % ctime()) - - if q.socks_proxy_type and q.socks_proxy_host and q.socks_proxy_port: - try: - import socks - except ImportError: - no_socks_msg = "Queue has been configured with proxy settings, " \ - "but no socks library was installed. Try to " \ - "install PySocks via PyPI." - logger.error(no_socks_msg) - raise ImportError(no_socks_msg) - - proxy_type = { - 'socks4': socks.SOCKS4, - 'socks5': socks.SOCKS5, - }.get(q.socks_proxy_type) - - socks.set_default_proxy(proxy_type=proxy_type, - addr=q.socks_proxy_host, - port=q.socks_proxy_port) - socket.socket = socks.socksocket - elif six.PY2: - socket.socket = socket._socketobject - - email_box_type = settings.QUEUE_EMAIL_BOX_TYPE or q.email_box_type - - if email_box_type == 'pop3': - if q.email_box_ssl or settings.QUEUE_EMAIL_BOX_SSL: - if not q.email_box_port: - q.email_box_port = 995 - server = poplib.POP3_SSL(q.email_box_host or - settings.QUEUE_EMAIL_BOX_HOST, - int(q.email_box_port)) - else: - if not q.email_box_port: - q.email_box_port = 110 - server = poplib.POP3(q.email_box_host or - settings.QUEUE_EMAIL_BOX_HOST, - int(q.email_box_port)) - - logger.info("Attempting POP3 server login") - - server.getwelcome() - server.user(q.email_box_user or settings.QUEUE_EMAIL_BOX_USER) - server.pass_(q.email_box_pass or settings.QUEUE_EMAIL_BOX_PASSWORD) - - messagesInfo = server.list()[1] - logger.info("Received %d messages from POP3 server" % len(messagesInfo)) - - for msgRaw in messagesInfo: - if six.PY3 and type(msgRaw) is bytes: - # in py3, msgRaw may be a bytes object, decode to str - try: - msg = msgRaw.decode("utf-8") - except UnicodeError: - # if couldn't decode easily, just leave it raw - msg = msgRaw - else: - # already a str - msg = msgRaw - msgNum = msg.split(" ")[0] - logger.info("Processing message %s" % msgNum) - - if six.PY2: - full_message = encoding.force_text("\n".join(server.retr(msgNum)[1]), errors='replace') - else: - raw_content = server.retr(msgNum)[1] - if type(raw_content[0]) is bytes: - full_message = "\n".join([elm.decode('utf-8') for elm in raw_content]) - else: - full_message = encoding.force_text("\n".join(raw_content), errors='replace') - ticket = ticket_from_message(message=full_message, queue=q, logger=logger) - - if ticket: - server.dele(msgNum) - logger.info("Successfully processed message %s, deleted from POP3 server" % msgNum) - else: - logger.warn("Message %s was not successfully processed, and will be left on POP3 server" % msgNum) - - server.quit() - - elif email_box_type == 'imap': - if q.email_box_ssl or settings.QUEUE_EMAIL_BOX_SSL: - if not q.email_box_port: - q.email_box_port = 993 - server = imaplib.IMAP4_SSL(q.email_box_host or - settings.QUEUE_EMAIL_BOX_HOST, - int(q.email_box_port)) - else: - if not q.email_box_port: - q.email_box_port = 143 - server = imaplib.IMAP4(q.email_box_host or - settings.QUEUE_EMAIL_BOX_HOST, - int(q.email_box_port)) - - logger.info("Attempting IMAP server login") - - try: - server.login(q.email_box_user or - settings.QUEUE_EMAIL_BOX_USER, - q.email_box_pass or - settings.QUEUE_EMAIL_BOX_PASSWORD) - server.select(q.email_box_imap_folder) - except imaplib.IMAP4.abort: - logger.error("IMAP login failed. Check that the server is accessible and that the username and password are correct.") - server.logout() - sys.exit() - except ssl.SSLError: - logger.error("IMAP login failed due to SSL error. This is often due to a timeout. Please check your connection and try again.") - server.logout() - sys.exit() - - try: - status, data = server.search(None, 'NOT', 'DELETED') - except imaplib.IMAP4.error: - logger.error("IMAP retrieve failed. Is the folder '%s' spelled correctly, and does it exist on the server?" % q.email_box_imap_folder) - if data: - msgnums = data[0].split() - logger.info("Received %d messages from IMAP server" % len(msgnums)) - for num in msgnums: - logger.info("Processing message %s" % num) - status, data = server.fetch(num, '(RFC822)') - full_message = encoding.force_text(data[0][1], errors='replace') - try: - ticket = ticket_from_message(message=full_message, queue=q, logger=logger) - except TypeError: - ticket = None # hotfix. Need to work out WHY. - if ticket: - server.store(num, '+FLAGS', '\\Deleted') - logger.info("Successfully processed message %s, deleted from IMAP server" % num) - else: - logger.warn("Message %s was not successfully processed, and will be left on IMAP server" % num) - - server.expunge() - server.close() - server.logout() - - elif email_box_type == 'local': - mail_dir = q.email_box_local_dir or '/var/lib/mail/helpdesk/' - mail = [join(mail_dir, f) for f in listdir(mail_dir) if isfile(join(mail_dir, f))] - logger.info("Found %d messages in local mailbox directory" % len(mail)) - - logger.info("Found %d messages in local mailbox directory" % len(mail)) - for i, m in enumerate(mail, 1): - logger.info("Processing message %d" % i) - with open(m, 'r') as f: - full_message = encoding.force_text(f.read(), errors='replace') - ticket = ticket_from_message(message=full_message, queue=q, logger=logger) - if ticket: - logger.info("Successfully processed message %d, ticket/comment created." % i) - try: - unlink(m) # delete message file if ticket was successful - except OSError: - logger.error("Unable to delete message %d." % i) - else: - logger.info("Successfully deleted message %d." % i) - else: - logger.warn("Message %d was not successfully processed, and will be left in local directory" % i) - - -def decodeUnknown(charset, string): - if six.PY2: - if not charset: - try: - return string.decode('utf-8', 'replace') - except UnicodeError: - return string.decode('iso8859-1', 'replace') - return unicode(string, charset) - elif six.PY3: - if type(string) is not str: - if not charset: - try: - return str(string, encoding='utf-8', errors='replace') - except UnicodeError: - return str(string, encoding='iso8859-1', errors='replace') - return str(string, encoding=charset, errors='replace') - return string - - -def decode_mail_headers(string): - decoded = email.header.decode_header(string) if six.PY3 else email.header.decode_header(string.encode('utf-8')) - if six.PY2: - return u' '.join([unicode(msg, charset or 'utf-8') for msg, charset in decoded]) - elif six.PY3: - return u' '.join([str(msg, encoding=charset, errors='replace') if charset else str(msg) for msg, charset in decoded]) - - -def ticket_from_message(message, queue, logger): - # 'message' must be an RFC822 formatted message. - message = email.message_from_string(message) if six.PY3 else email.message_from_string(message.encode('utf-8')) - subject = message.get('subject', _('Comment from e-mail')) - subject = decode_mail_headers(decodeUnknown(message.get_charset(), subject)) - for affix in STRIPPED_SUBJECT_STRINGS: - subject = subject.replace(affix, "") - subject = subject.strip() - - sender = message.get('from', _('Unknown Sender')) - sender = decode_mail_headers(decodeUnknown(message.get_charset(), sender)) - sender_email = email.utils.parseaddr(sender)[1] - - cc = message.get_all('cc', None) - if cc: - # first, fixup the encoding if necessary - cc = [decode_mail_headers(decodeUnknown(message.get_charset(), x)) for x in cc] - # get_all checks if multiple CC headers, but individual emails may be comma separated too - tempcc = [] - for hdr in cc: - tempcc.extend(hdr.split(',')) - # use a set to ensure no duplicates - cc = set([x.strip() for x in tempcc]) - - for ignore in IgnoreEmail.objects.filter(Q(queues=queue) | Q(queues__isnull=True)): - if ignore.test(sender_email): - if ignore.keep_in_mailbox: - # By returning 'False' the message will be kept in the mailbox, - # and the 'True' will cause the message to be deleted. - return False - return True - - matchobj = re.match(r".*\[" + queue.slug + r"-(?P\d+)\]", subject) - if matchobj: - # This is a reply or forward. - ticket = matchobj.group('id') - logger.info("Matched tracking ID %s-%s" % (queue.slug, ticket)) - else: - logger.info("No tracking ID matched.") - ticket = None - - body = None - counter = 0 - files = [] - - for part in message.walk(): - if part.get_content_maintype() == 'multipart': - continue - - name = part.get_param("name") - if name: - name = email.utils.collapse_rfc2231_value(name) - - if part.get_content_maintype() == 'text' and name is None: - if part.get_content_subtype() == 'plain': - body = EmailReplyParser.parse_reply( - decodeUnknown(part.get_content_charset(), part.get_payload(decode=True)) - ) - # workaround to get unicode text out rather than escaped text - try: - body = body.encode('ascii').decode('unicode_escape') - except UnicodeEncodeError: - body.encode('utf-8') - logger.debug("Discovered plain text MIME part") - else: - files.append( - SimpleUploadedFile(_("email_html_body.html"), encoding.smart_bytes(part.get_payload()), 'text/html') - ) - logger.debug("Discovered HTML MIME part") - else: - if not name: - ext = mimetypes.guess_extension(part.get_content_type()) - name = "part-%i%s" % (counter, ext) - payload = part.get_payload() - if isinstance(payload, list): - payload = payload.pop().as_string() - payloadToWrite = payload - # check version of python to ensure use of only the correct error type - if six.PY2: - non_b64_err = binascii.Error - else: - non_b64_err = TypeError - try: - logger.debug("Try to base64 decode the attachment payload") - if six.PY2: - payloadToWrite = base64.decodestring(payload) - else: - payloadToWrite = base64.decodebytes(payload) - except non_b64_err: - logger.debug("Payload was not base64 encoded, using raw bytes") - payloadToWrite = payload - files.append(SimpleUploadedFile(name, part.get_payload(decode=True), mimetypes.guess_type(name)[0])) - logger.debug("Found MIME attachment %s" % name) - - counter += 1 - - if not body: - mail = BeautifulSoup(part.get_payload(), "lxml") - if ">" in mail.text: - body = mail.find('body') - body = body.text - body = body.encode('ascii', errors='ignore') - else: - body = mail.text - - if ticket: - try: - t = Ticket.objects.get(id=ticket) - except Ticket.DoesNotExist: - logger.info("Tracking ID %s-%s not associated with existing ticket. Creating new ticket." % (queue.slug, ticket)) - ticket = None - else: - logger.info("Found existing ticket with Tracking ID %s-%s" % (t.queue.slug, t.id)) - if t.status == Ticket.CLOSED_STATUS: - t.status = Ticket.REOPENED_STATUS - t.save() - new = False - - smtp_priority = message.get('priority', '') - smtp_importance = message.get('importance', '') - high_priority_types = {'high', 'important', '1', 'urgent'} - priority = 2 if high_priority_types & {smtp_priority, smtp_importance} else 3 - - if ticket is None: - if settings.QUEUE_EMAIL_BOX_UPDATE_ONLY: - return None - new = True - t = Ticket.objects.create( - title=subject, - queue=queue, - submitter_email=sender_email, - created=timezone.now(), - description=body, - priority=priority, - ) - logger.debug("Created new ticket %s-%s" % (t.queue.slug, t.id)) - - if cc: - # get list of currently CC'd emails - current_cc = TicketCC.objects.filter(ticket=ticket) - current_cc_emails = [x.email for x in current_cc if x.email] - # get emails of any Users CC'd to email, if defined - # (some Users may not have an associated email, e.g, when using LDAP) - current_cc_users = [x.user.email for x in current_cc if x.user and x.user.email] - # ensure submitter, assigned user, queue email not added - other_emails = [queue.email_address] - if t.submitter_email: - other_emails.append(t.submitter_email) - if t.assigned_to: - other_emails.append(t.assigned_to.email) - current_cc = set(current_cc_emails + current_cc_users + other_emails) - # first, add any User not previously CC'd (as identified by User's email) - all_users = User.objects.all() - all_user_emails = set([x.email for x in all_users]) - users_not_currently_ccd = all_user_emails.difference(set(current_cc)) - users_to_cc = cc.intersection(users_not_currently_ccd) - for user in users_to_cc: - tcc = TicketCC.objects.create( - ticket=t, - user=User.objects.get(email=user), - can_view=True, - can_update=False - ) - tcc.save() - # then add remaining emails alphabetically, makes testing easy - new_cc = cc.difference(current_cc).difference(all_user_emails) - new_cc = sorted(list(new_cc)) - for ccemail in new_cc: - tcc = TicketCC.objects.create( - ticket=t, - email=ccemail.replace('\n', ' ').replace('\r', ' '), - can_view=True, - can_update=False - ) - tcc.save() - - f = FollowUp( - ticket=t, - title=_('E-Mail Received from %(sender_email)s' % {'sender_email': sender_email}), - date=timezone.now(), - public=True, - comment=body, - ) - - if t.status == Ticket.REOPENED_STATUS: - f.new_status = Ticket.REOPENED_STATUS - f.title = _('Ticket Re-Opened by E-Mail Received from %(sender_email)s' % {'sender_email': sender_email}) - - f.save() - logger.debug("Created new FollowUp for Ticket") - - if six.PY2: - logger.info(("[%s-%s] %s" % (t.queue.slug, t.id, t.title,)).encode('ascii', 'replace')) - elif six.PY3: - logger.info("[%s-%s] %s" % (t.queue.slug, t.id, t.title,)) - - attached = process_attachments(f, files) - for att_file in attached: - logger.info("Attachment '%s' (with size %s) successfully added to ticket from email." % (att_file[0], att_file[1].size)) - - context = safe_template_context(t) - - if new: - if sender_email: - send_templated_mail( - 'newticket_submitter', - context, - recipients=sender_email, - sender=queue.from_address, - fail_silently=True, - ) - if queue.new_ticket_cc: - send_templated_mail( - 'newticket_cc', - context, - recipients=queue.new_ticket_cc, - sender=queue.from_address, - fail_silently=True, - ) - if queue.updated_ticket_cc and queue.updated_ticket_cc != queue.new_ticket_cc: - send_templated_mail( - 'newticket_cc', - context, - recipients=queue.updated_ticket_cc, - sender=queue.from_address, - fail_silently=True, - ) - else: - context.update(comment=f.comment) - if t.assigned_to: - send_templated_mail( - 'updated_owner', - context, - recipients=t.assigned_to.email, - sender=queue.from_address, - fail_silently=True, - ) - if queue.updated_ticket_cc: - send_templated_mail( - 'updated_cc', - context, - recipients=queue.updated_ticket_cc, - sender=queue.from_address, - fail_silently=True, - ) - # copy email to all those CC'd to this particular ticket - for cc in t.ticketcc_set.all(): - # don't duplicate email to assignee - if t.assigned_to.email != cc.email_address: - send_templated_mail( - 'updated_cc', - context, - recipients=cc.email_address, - sender=queue.from_address, - fail_silently=True, - ) - - return t - - if __name__ == '__main__': process_email() diff --git a/helpdesk/migrations/0001_initial.py b/helpdesk/migrations/0001_initial.py index 3bd49746..8ae60e4a 100644 --- a/helpdesk/migrations/0001_initial.py +++ b/helpdesk/migrations/0001_initial.py @@ -1,9 +1,8 @@ # -*- coding: utf-8 -*- -from __future__ import unicode_literals - from django.db import models, migrations import django.utils.timezone from django.conf import settings + import helpdesk.models diff --git a/helpdesk/migrations/0002_populate_usersettings.py b/helpdesk/migrations/0002_populate_usersettings.py index ba2979be..b35bfc80 100644 --- a/helpdesk/migrations/0002_populate_usersettings.py +++ b/helpdesk/migrations/0002_populate_usersettings.py @@ -1,19 +1,17 @@ # -*- coding: utf-8 -*- -from __future__ import unicode_literals - from django.contrib.auth import get_user_model from django.db import models, migrations from helpdesk.settings import DEFAULT_USER_SETTINGS -def picke_settings(data): +def pickle_settings(data): """Pickling as defined at migration's creation time""" try: import pickle except ImportError: import cPickle as pickle - from helpdesk.lib import b64encode + from helpdesk.query import b64encode return b64encode(pickle.dumps(data)) @@ -29,7 +27,7 @@ def populate_usersettings(apps, schema_editor): # Import historical version of models UserSettings = apps.get_model("helpdesk", "UserSettings") - settings_pickled = picke_settings(DEFAULT_USER_SETTINGS) + settings_pickled = pickle_settings(DEFAULT_USER_SETTINGS) for u in User.objects.all(): try: diff --git a/helpdesk/migrations/0003_initial_data_import.py b/helpdesk/migrations/0003_initial_data_import.py index cc478377..c13bb47c 100644 --- a/helpdesk/migrations/0003_initial_data_import.py +++ b/helpdesk/migrations/0003_initial_data_import.py @@ -1,6 +1,4 @@ # -*- coding: utf-8 -*- -from __future__ import unicode_literals - import os from sys import path diff --git a/helpdesk/migrations/0004_add_per_queue_staff_membership.py b/helpdesk/migrations/0004_add_per_queue_staff_membership.py index 1b96b3bc..d3a444f4 100644 --- a/helpdesk/migrations/0004_add_per_queue_staff_membership.py +++ b/helpdesk/migrations/0004_add_per_queue_staff_membership.py @@ -1,6 +1,4 @@ # -*- coding: utf-8 -*- -from __future__ import unicode_literals - from django.db import models, migrations from django.conf import settings diff --git a/helpdesk/migrations/0005_queues_no_null.py b/helpdesk/migrations/0005_queues_no_null.py index 8a678635..e7e2ba76 100644 --- a/helpdesk/migrations/0005_queues_no_null.py +++ b/helpdesk/migrations/0005_queues_no_null.py @@ -1,6 +1,4 @@ # -*- coding: utf-8 -*- -from __future__ import unicode_literals - from django.db import models, migrations diff --git a/helpdesk/migrations/0006_email_maxlength.py b/helpdesk/migrations/0006_email_maxlength.py index 7c50b32a..e1e37927 100644 --- a/helpdesk/migrations/0006_email_maxlength.py +++ b/helpdesk/migrations/0006_email_maxlength.py @@ -1,6 +1,4 @@ # -*- coding: utf-8 -*- -from __future__ import unicode_literals - from django.db import models, migrations diff --git a/helpdesk/migrations/0007_max_length_by_integer.py b/helpdesk/migrations/0007_max_length_by_integer.py index 98b404a8..5b8deba3 100644 --- a/helpdesk/migrations/0007_max_length_by_integer.py +++ b/helpdesk/migrations/0007_max_length_by_integer.py @@ -1,6 +1,4 @@ # -*- coding: utf-8 -*- -from __future__ import unicode_literals - from django.db import models, migrations diff --git a/helpdesk/migrations/0008_extra_for_permissions.py b/helpdesk/migrations/0008_extra_for_permissions.py index ac6d1433..d9a75aa9 100644 --- a/helpdesk/migrations/0008_extra_for_permissions.py +++ b/helpdesk/migrations/0008_extra_for_permissions.py @@ -1,6 +1,4 @@ # -*- coding: utf-8 -*- -from __future__ import unicode_literals - from django.db import models, migrations diff --git a/helpdesk/migrations/0009_migrate_queuemembership.py b/helpdesk/migrations/0009_migrate_queuemembership.py index 10d27cd0..09e2a60f 100644 --- a/helpdesk/migrations/0009_migrate_queuemembership.py +++ b/helpdesk/migrations/0009_migrate_queuemembership.py @@ -1,6 +1,4 @@ # -*- coding: utf-8 -*- -from __future__ import unicode_literals - from django.core.exceptions import ObjectDoesNotExist from django.db import migrations from django.db.utils import IntegrityError diff --git a/helpdesk/migrations/0010_remove_queuemembership.py b/helpdesk/migrations/0010_remove_queuemembership.py index 3b097365..b0df7ee4 100644 --- a/helpdesk/migrations/0010_remove_queuemembership.py +++ b/helpdesk/migrations/0010_remove_queuemembership.py @@ -1,6 +1,4 @@ # -*- coding: utf-8 -*- -from __future__ import unicode_literals - from django.db import models, migrations diff --git a/helpdesk/migrations/0011_admin_related_improvements.py b/helpdesk/migrations/0011_admin_related_improvements.py index a36ff1fb..1ac25317 100644 --- a/helpdesk/migrations/0011_admin_related_improvements.py +++ b/helpdesk/migrations/0011_admin_related_improvements.py @@ -1,6 +1,4 @@ # -*- coding: utf-8 -*- -from __future__ import unicode_literals - from django.db import models, migrations diff --git a/helpdesk/migrations/0012_queue_default_owner.py b/helpdesk/migrations/0012_queue_default_owner.py index 7a731e00..aa8af6f4 100644 --- a/helpdesk/migrations/0012_queue_default_owner.py +++ b/helpdesk/migrations/0012_queue_default_owner.py @@ -1,7 +1,5 @@ # -*- coding: utf-8 -*- # Generated by Django 1.9.2 on 2016-02-15 21:37 -from __future__ import unicode_literals - from django.conf import settings from django.db import migrations, models import django.db.models.deletion diff --git a/helpdesk/migrations/0013_email_box_local_dir_and_logging.py b/helpdesk/migrations/0013_email_box_local_dir_and_logging.py index 71ba784e..6fc936ca 100644 --- a/helpdesk/migrations/0013_email_box_local_dir_and_logging.py +++ b/helpdesk/migrations/0013_email_box_local_dir_and_logging.py @@ -1,7 +1,5 @@ # -*- coding: utf-8 -*- # Generated by Django 1.10.1 on 2016-09-14 23:47 -from __future__ import unicode_literals - from django.db import migrations, models diff --git a/helpdesk/migrations/0014_usersettings_related_name.py b/helpdesk/migrations/0014_usersettings_related_name.py index ab58ee01..857e0293 100644 --- a/helpdesk/migrations/0014_usersettings_related_name.py +++ b/helpdesk/migrations/0014_usersettings_related_name.py @@ -1,6 +1,4 @@ # -*- coding: utf-8 -*- -from __future__ import unicode_literals - from django.db import migrations, models from django.conf import settings diff --git a/helpdesk/migrations/0015_expand_permission_name_size.py b/helpdesk/migrations/0015_expand_permission_name_size.py index 5feecb38..b5aa3a1d 100644 --- a/helpdesk/migrations/0015_expand_permission_name_size.py +++ b/helpdesk/migrations/0015_expand_permission_name_size.py @@ -1,7 +1,5 @@ # -*- coding: utf-8 -*- # Generated by Django 1.10.5 on 2017-02-10 19:27 -from __future__ import unicode_literals - from django.db import migrations, models diff --git a/helpdesk/migrations/0016_alter_model_options.py b/helpdesk/migrations/0016_alter_model_options.py index a265317a..426b5864 100644 --- a/helpdesk/migrations/0016_alter_model_options.py +++ b/helpdesk/migrations/0016_alter_model_options.py @@ -1,7 +1,5 @@ # -*- coding: utf-8 -*- # Generated by Django 1.10.2 on 2017-03-08 17:51 -from __future__ import unicode_literals - from django.db import migrations diff --git a/helpdesk/migrations/0017_default_owner_on_delete_null.py b/helpdesk/migrations/0017_default_owner_on_delete_null.py index 54591f64..ee8cad36 100644 --- a/helpdesk/migrations/0017_default_owner_on_delete_null.py +++ b/helpdesk/migrations/0017_default_owner_on_delete_null.py @@ -1,8 +1,5 @@ # -*- coding: utf-8 -*- # Generated by Django 1.11.2 on 2018-01-19 09:48 - -from __future__ import unicode_literals - from django.conf import settings from django.db import migrations, models import django.db.models.deletion diff --git a/helpdesk/migrations/0018_ticket_secret_key.py b/helpdesk/migrations/0018_ticket_secret_key.py new file mode 100644 index 00000000..bafd807c --- /dev/null +++ b/helpdesk/migrations/0018_ticket_secret_key.py @@ -0,0 +1,27 @@ +# Generated by Django 2.0.1 on 2018-09-07 21:22 +from django.db import migrations, models +import helpdesk.models + + +def clear_secret_keys(apps, schema_editor): + Ticket = apps.get_model("helpdesk", "Ticket") + db_alias = schema_editor.connection.alias + + for ticket in Ticket.objects.using(db_alias).all(): + ticket.secret_key='' + ticket.save() + +class Migration(migrations.Migration): + + dependencies = [ + ('helpdesk', '0017_default_owner_on_delete_null'), + ] + + operations = [ + migrations.AddField( + model_name='ticket', + name='secret_key', + field=models.CharField(default=helpdesk.models.mk_secret, max_length=36, null=True, verbose_name='Secret key needed for viewing/editing ticket by non-logged in users'), + ), + migrations.RunPython(clear_secret_keys), + ] diff --git a/helpdesk/migrations/0019_ticket_secret_key.py b/helpdesk/migrations/0019_ticket_secret_key.py new file mode 100644 index 00000000..ed3847da --- /dev/null +++ b/helpdesk/migrations/0019_ticket_secret_key.py @@ -0,0 +1,18 @@ +# Generated by Django 2.0.1 on 2018-09-07 21:22 +from django.db import migrations, models +import helpdesk.models + + +class Migration(migrations.Migration): + + dependencies = [ + ('helpdesk', '0018_ticket_secret_key'), + ] + + operations = [ + migrations.AlterField( + model_name='ticket', + name='secret_key', + field=models.CharField(default=helpdesk.models.mk_secret, max_length=36, verbose_name='Secret key needed for viewing/editing ticket by non-logged in users'), + ), + ] diff --git a/helpdesk/migrations/0020_depickle_user_settings.py b/helpdesk/migrations/0020_depickle_user_settings.py new file mode 100644 index 00000000..4315c8fd --- /dev/null +++ b/helpdesk/migrations/0020_depickle_user_settings.py @@ -0,0 +1,68 @@ +# Generated by Django 2.0.7 on 2018-10-19 14:11 +from django.db import migrations, models +import helpdesk.models + + +def unpickle_settings(settings_pickled): + # return a python dictionary representing the pickled data. + try: + import pickle + except ImportError: + import cPickle as pickle + from helpdesk.lib import b64decode + try: + return pickle.loads(b64decode(settings_pickled.encode('utf-8'))) + except Exception: + return {} + + +def move_old_values(apps, schema_editor): + UserSettings = apps.get_model("helpdesk", "UserSettings") + db_alias = schema_editor.connection.alias + + for user_settings in UserSettings.objects.using(db_alias).all(): + if user_settings.settings_pickled: + settings_dict = unpickle_settings(user_settings.settings_pickled) + for setting, value in settings_dict.items(): + user_settings.__setattr__(setting, value) + + +class Migration(migrations.Migration): + + dependencies = [ + ('helpdesk', '0019_ticket_secret_key'), + ] + + operations = [ + migrations.AddField( + model_name='usersettings', + name='email_on_ticket_assign', + field=models.BooleanField(default=helpdesk.models.email_on_ticket_assign_default, help_text='If you are assigned a ticket via the web, do you want to receive an e-mail?', verbose_name='E-mail me when assigned a ticket?'), + ), + migrations.AddField( + model_name='usersettings', + name='email_on_ticket_change', + field=models.BooleanField(default=helpdesk.models.email_on_ticket_change_default, help_text="If you're the ticket owner and the ticket is changed via the web by somebody else, do you want to receive an e-mail?", verbose_name='E-mail me on ticket change?'), + ), + migrations.AddField( + model_name='usersettings', + name='login_view_ticketlist', + field=models.BooleanField(default=helpdesk.models.login_view_ticketlist_default, help_text='Display the ticket list upon login? Otherwise, the dashboard is shown.', verbose_name='Show Ticket List on Login?'), + ), + migrations.AddField( + model_name='usersettings', + name='tickets_per_page', + field=models.IntegerField(choices=[(10, '10'), (25, '25'), (50, '50'), (100, '100')], default=helpdesk.models.tickets_per_page_default, help_text='How many tickets do you want to see on the Ticket List page?', verbose_name='Number of tickets to show per page'), + ), + migrations.AddField( + model_name='usersettings', + name='use_email_as_submitter', + field=models.BooleanField(default=helpdesk.models.use_email_as_submitter_default, help_text='When you submit a ticket, do you want to automatically use your e-mail address as the submitter address? You can type a different e-mail address when entering the ticket if needed, this option only changes the default.', verbose_name='Use my e-mail address when submitting tickets?'), + ), + migrations.AlterField( + model_name='usersettings', + name='settings_pickled', + field=models.TextField(blank=True, help_text='DEPRECATED! This is a base64-encoded representation of a pickled Python dictionary. Do not change this field via the admin.', null=True, verbose_name='DEPRECATED! Settings Dictionary DEPRECATED!'), + ), + migrations.RunPython(move_old_values), + ] diff --git a/helpdesk/migrations/0021_voting_tracker.py b/helpdesk/migrations/0021_voting_tracker.py new file mode 100644 index 00000000..691977a2 --- /dev/null +++ b/helpdesk/migrations/0021_voting_tracker.py @@ -0,0 +1,64 @@ +# Generated by Django 2.1.2 on 2018-10-17 17:37 +from django.conf import settings +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + migrations.swappable_dependency(settings.AUTH_USER_MODEL), + ('helpdesk', '0020_depickle_user_settings'), + ] + + operations = [ + migrations.AddField( + model_name='kbitem', + name='voted_by', + field=models.ManyToManyField(to=settings.AUTH_USER_MODEL), + ), + migrations.AlterField( + model_name='followup', + name='public', + field=models.BooleanField(blank=True, default=False, help_text='Public tickets are viewable by the submitter and all staff, but non-public tickets can only be seen by staff.', verbose_name='Public'), + ), + migrations.AlterField( + model_name='ignoreemail', + name='keep_in_mailbox', + field=models.BooleanField(blank=True, default=False, help_text='Do you want to save emails from this address in the mailbox? If this is unticked, emails from this address will be deleted.', verbose_name='Save Emails in Mailbox?'), + ), + migrations.AlterField( + model_name='queue', + name='allow_email_submission', + field=models.BooleanField(blank=True, default=False, help_text='Do you want to poll the e-mail box below for new tickets?', verbose_name='Allow E-Mail Submission?'), + ), + migrations.AlterField( + model_name='queue', + name='allow_public_submission', + field=models.BooleanField(blank=True, default=False, help_text='Should this queue be listed on the public submission form?', verbose_name='Allow Public Submission?'), + ), + migrations.AlterField( + model_name='queue', + name='email_box_ssl', + field=models.BooleanField(blank=True, default=False, help_text='Whether to use SSL for IMAP or POP3 - the default ports when using SSL are 993 for IMAP and 995 for POP3.', verbose_name='Use SSL for E-Mail?'), + ), + migrations.AlterField( + model_name='savedsearch', + name='shared', + field=models.BooleanField(blank=True, default=False, help_text='Should other users see this query?', verbose_name='Shared With Other Users?'), + ), + migrations.AlterField( + model_name='ticket', + name='on_hold', + field=models.BooleanField(blank=True, default=False, help_text='If a ticket is on hold, it will not automatically be escalated.', verbose_name='On Hold'), + ), + migrations.AlterField( + model_name='ticketcc', + name='can_update', + field=models.BooleanField(blank=True, default=False, help_text='Can this CC login and update the ticket?', verbose_name='Can Update Ticket?'), + ), + migrations.AlterField( + model_name='ticketcc', + name='can_view', + field=models.BooleanField(blank=True, default=False, help_text='Can this CC login to view the ticket details?', verbose_name='Can View Ticket?'), + ), + ] diff --git a/helpdesk/migrations/0022_add_submitter_email_id_field_to_ticket.py b/helpdesk/migrations/0022_add_submitter_email_id_field_to_ticket.py new file mode 100644 index 00000000..614204b2 --- /dev/null +++ b/helpdesk/migrations/0022_add_submitter_email_id_field_to_ticket.py @@ -0,0 +1,18 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.9.1 on 2016-02-07 19:51 +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('helpdesk', '0021_voting_tracker'), + ] + + operations = [ + migrations.AddField( + model_name='followup', + name='message_id', + field=models.CharField(blank=True, editable=False, help_text="The Message ID of the submitter's email.", max_length=256, null=True, verbose_name='E-Mail ID'), + ), + ] diff --git a/helpdesk/migrations/0023_add_enable_notifications_on_email_events_to_ticket.py b/helpdesk/migrations/0023_add_enable_notifications_on_email_events_to_ticket.py new file mode 100644 index 00000000..18a10d77 --- /dev/null +++ b/helpdesk/migrations/0023_add_enable_notifications_on_email_events_to_ticket.py @@ -0,0 +1,18 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.9.1 on 2016-03-01 19:43 +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('helpdesk', '0022_add_submitter_email_id_field_to_ticket'), + ] + + operations = [ + migrations.AddField( + model_name='queue', + name='enable_notifications_on_email_events', + field=models.BooleanField(default=False, help_text='When an email arrives to either create a ticket or to interact with an existing discussion. Should email notifications be sent ? Note: the new_ticket_cc and updated_ticket_cc work independently of this feature', verbose_name='Notify contacts when email updates arrive'), + ), + ] diff --git a/helpdesk/migrations/0024_time_spent.py b/helpdesk/migrations/0024_time_spent.py new file mode 100644 index 00000000..bbb0f22f --- /dev/null +++ b/helpdesk/migrations/0024_time_spent.py @@ -0,0 +1,18 @@ +# Generated by Django 2.0.5 on 2019-02-06 13:24 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('helpdesk', '0023_add_enable_notifications_on_email_events_to_ticket'), + ] + + operations = [ + migrations.AddField( + model_name='followup', + name='time_spent', + field=models.DurationField(blank=True, help_text='Time spent on this follow up', null=True), + ), + ] diff --git a/helpdesk/migrations/0025_queue_dedicated_time.py b/helpdesk/migrations/0025_queue_dedicated_time.py new file mode 100644 index 00000000..d3dfd8d3 --- /dev/null +++ b/helpdesk/migrations/0025_queue_dedicated_time.py @@ -0,0 +1,18 @@ +# Generated by Django 2.0.5 on 2019-02-19 21:53 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('helpdesk', '0024_time_spent'), + ] + + operations = [ + migrations.AddField( + model_name='queue', + name='dedicated_time', + field=models.DurationField(blank=True, help_text='Time to be spent on this Queue in total', null=True), + ), + ] diff --git a/helpdesk/migrations/0026_kbitem_attachments.py b/helpdesk/migrations/0026_kbitem_attachments.py new file mode 100644 index 00000000..810672c5 --- /dev/null +++ b/helpdesk/migrations/0026_kbitem_attachments.py @@ -0,0 +1,36 @@ +# Generated by Django 2.0.5 on 2019-03-07 20:30 + +from django.db import migrations, models +import django.db.models.deletion +import helpdesk.models + + +class Migration(migrations.Migration): + + dependencies = [ + ('helpdesk', '0025_queue_dedicated_time'), + ] + + operations = [ + migrations.CreateModel( + name='KBIAttachment', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('file', models.FileField(max_length=1000, upload_to=helpdesk.models.attachment_path, verbose_name='File')), + ('filename', models.CharField(max_length=1000, verbose_name='Filename')), + ('mime_type', models.CharField(max_length=255, verbose_name='MIME Type')), + ('size', models.IntegerField(help_text='Size of this file in bytes', verbose_name='Size')), + ('kbitem', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='helpdesk.KBItem', verbose_name='Knowledge base item')), + ], + options={ + 'verbose_name': 'Attachment', + 'verbose_name_plural': 'Attachments', + 'ordering': ('filename',), + 'abstract': False, + }, + ), + migrations.RenameModel( + old_name='Attachment', + new_name='FollowUpAttachment', + ), + ] diff --git a/helpdesk/models.py b/helpdesk/models.py index 177574ce..339f4c9f 100644 --- a/helpdesk/models.py +++ b/helpdesk/models.py @@ -7,20 +7,64 @@ models.py - Model (and hence database) definitions. This is the core of the helpdesk structure. """ -from __future__ import unicode_literals from django.contrib.auth.models import Permission +from django.contrib.auth import get_user_model from django.contrib.contenttypes.models import ContentType from django.core.exceptions import ObjectDoesNotExist from django.db import models from django.conf import settings from django.utils import timezone -from django.utils import six from django.utils.translation import ugettext_lazy as _, ugettext -from django.utils.encoding import python_2_unicode_compatible +from io import StringIO import re +import os +import mimetypes +import datetime + +from django.utils.safestring import mark_safe +from markdown import markdown +from markdown.extensions import Extension + + +import uuid + +from helpdesk import settings as helpdesk_settings + +from .templated_email import send_templated_mail + + +def format_time_spent(time_spent): + if time_spent: + time_spent = "{0:02d}h:{0:02d}m".format( + int(time_spent.total_seconds() // (3600)), + int((time_spent.total_seconds() % 3600) / 60) + ) + else: + time_spent = "" + return time_spent + + +class EscapeHtml(Extension): + def extendMarkdown(self, md, md_globals): + del md.preprocessors['html_block'] + del md.inlinePatterns['html'] + + +def get_markdown(text): + if not text: + return "" + + return mark_safe( + markdown( + text, + extensions=[ + EscapeHtml(), 'markdown.extensions.nl2br', + 'markdown.extensions.fenced_code' + ] + ) + ) -@python_2_unicode_compatible class Queue(models.Model): """ A queue is a collection of tickets into what would generally be business @@ -106,6 +150,15 @@ class Queue(models.Model): 'multiple addresses with a comma.'), ) + enable_notifications_on_email_events = models.BooleanField( + _('Notify contacts when email updates arrive'), + blank=True, + default=False, + help_text=_('When an email arrives to either create a ticket or to ' + 'interact with an existing discussion. Should email notifications be sent ? ' + 'Note: the new_ticket_cc and updated_ticket_cc work independently of this feature'), + ) + email_box_type = models.CharField( _('E-Mail Box Type'), max_length=5, @@ -264,6 +317,11 @@ class Queue(models.Model): verbose_name=_('Default owner'), ) + dedicated_time = models.DurationField( + help_text=_("Time to be spent on this Queue in total"), + blank=True, null=True + ) + def __str__(self): return "%s" % self.title @@ -290,6 +348,21 @@ class Queue(models.Model): return u'%s <%s>' % (self.title, self.email_address) from_address = property(_from_address) + @property + def time_spent(self): + """Return back total time spent on the ticket. This is calculated value + based on total sum from all FollowUps + """ + total = datetime.timedelta(0) + for val in self.ticket_set.all(): + if val.time_spent: + total = total + val.time_spent + return total + + @property + def time_spent_formated(self): + return format_time_spent(self.time_spent) + def prepare_permission_name(self): """Prepare internally the codename for the permission and store it in permission_name. :return: The codename that can be used to create a new Permission object. @@ -348,7 +421,10 @@ class Queue(models.Model): pass -@python_2_unicode_compatible +def mk_secret(): + return str(uuid.uuid4()) + + class Ticket(models.Model): """ To allow a ticket to be entered as quickly as possible, only the @@ -477,6 +553,76 @@ class Ticket(models.Model): 'automatically by management/commands/escalate_tickets.py.'), ) + secret_key = models.CharField( + _("Secret key needed for viewing/editing ticket by non-logged in users"), + max_length=36, + default=mk_secret, + ) + + @property + def time_spent(self): + """Return back total time spent on the ticket. This is calculated value + based on total sum from all FollowUps + """ + total = datetime.timedelta(0) + for val in self.followup_set.all(): + if val.time_spent: + total = total + val.time_spent + return total + + @property + def time_spent_formated(self): + return format_time_spent(self.time_spent) + + def send(self, roles, dont_send_to=None, **kwargs): + """ + Send notifications to everyone interested in this ticket. + + The the roles argument is a dictionary mapping from roles to (template, context) pairs. + If a role is not present in the dictionary, users of that type will not receive the notification. + + The following roles exist: + + - 'submitter' + - 'new_ticket_cc' + - 'ticket_cc' + - 'assigned_to' + + Here is an example roles dictionary: + + { + 'submitter': (template_name, context), + 'assigned_to': (template_name2, context), + } + + **kwargs are passed to send_templated_mail defined in templated_mail.py + + returns the set of email addresses the notification was delivered to. + + """ + recipients = set() + + if dont_send_to is not None: + recipients.update(dont_send_to) + + def should_receive(email): + return email and email not in recipients + + def send(role, recipient): + if recipient and recipient not in recipients and role in roles: + template, context = roles[role] + send_templated_mail(template, context, recipient, sender=self.queue.from_address, **kwargs) + recipients.add(recipient) + send('submitter', self.submitter_email) + send('ticket_cc', self.queue.updated_ticket_cc) + send('new_ticket_cc', self.queue.new_ticket_cc) + if self.assigned_to: + send('assigned_to', self.assigned_to.email) + if self.queue.enable_notifications_on_email_events: + for cc in self.ticketcc_set.all(): + send('ticket_cc', cc.email_address) + return recipients + def _get_assigned_to(self): """ Custom property to allow us to easily print 'Unassigned' if a ticket has no owner, or the users name if it's assigned. If the user @@ -541,11 +687,17 @@ class Ticket(models.Model): site = Site.objects.get_current() except ImproperlyConfigured: site = Site(domain='configure-django-sites.com') - return u"http://%s%s?ticket=%s&email=%s" % ( + if helpdesk_settings.HELPDESK_USE_HTTPS_IN_EMAIL_LINK: + protocol = 'https' + else: + protocol = 'http' + return u"%s://%s%s?ticket=%s&email=%s&key=%s" % ( + protocol, site.domain, reverse('helpdesk:public_view'), self.ticket_for_url, - self.submitter_email + self.submitter_email, + self.secret_key ) ticket_url = property(_get_ticket_url) @@ -561,7 +713,12 @@ class Ticket(models.Model): site = Site.objects.get_current() except ImproperlyConfigured: site = Site(domain='configure-django-sites.com') - return u"http://%s%s" % ( + if helpdesk_settings.HELPDESK_USE_HTTPS_IN_EMAIL_LINK: + protocol = 'https' + else: + protocol = 'http' + return u"%s://%s%s" % ( + protocol, site.domain, reverse('helpdesk:view', args=[self.id]) @@ -579,6 +736,13 @@ class Ticket(models.Model): depends_on__status__in=OPEN_STATUSES).count() == 0 can_be_resolved = property(_can_be_resolved) + def get_submitter_userprofile(self): + User = get_user_model() + try: + return User.objects.get(email=self.submitter_email) + except User.DoesNotExist: + return None + class Meta: get_latest_by = "created" ordering = ('id',) @@ -612,6 +776,13 @@ class Ticket(models.Model): queue = '-'.join(parts[0:-1]) return queue, parts[-1] + def get_markdown(self): + return get_markdown(self.description) + + @property + def get_resolution_markdown(self): + return get_markdown(self.resolution) + class FollowUpManager(models.Manager): @@ -622,7 +793,6 @@ class FollowUpManager(models.Manager): return self.filter(public=True) -@python_2_unicode_compatible class FollowUp(models.Model): """ A FollowUp is a comment and/or change to a ticket. We keep a simple @@ -664,8 +834,10 @@ class FollowUp(models.Model): _('Public'), blank=True, default=False, - help_text=_('Public tickets are viewable by the submitter and all ' - 'staff, but non-public tickets can only be seen by staff.'), + help_text=_( + 'Public tickets are viewable by the submitter and all ' + 'staff, but non-public tickets can only be seen by staff.' + ), ) user = models.ForeignKey( @@ -684,8 +856,22 @@ class FollowUp(models.Model): help_text=_('If the status was changed, what was it changed to?'), ) + message_id = models.CharField( + _('E-Mail ID'), + max_length=256, + blank=True, + null=True, + help_text=_("The Message ID of the submitter's email."), + editable=False, + ) + objects = FollowUpManager() + time_spent = models.DurationField( + help_text=_("Time spent on this follow up"), + blank=True, null=True + ) + class Meta: ordering = ('date',) verbose_name = _('Follow-up') @@ -703,8 +889,14 @@ class FollowUp(models.Model): t.save() super(FollowUp, self).save(*args, **kwargs) + def get_markdown(self): + return get_markdown(self.comment) + + @property + def time_spent_formated(self): + return format_time_spent(self.time_spent) + -@python_2_unicode_compatible class TicketChange(models.Model): """ For each FollowUp, any changes to the parent ticket (eg Title, Priority, @@ -753,33 +945,16 @@ class TicketChange(models.Model): def attachment_path(instance, filename): - """ - Provide a file path that will help prevent files being overwritten, by - putting attachments in a folder off attachments for ticket/followup_id/. - """ - import os - os.umask(0) - path = 'helpdesk/attachments/%s/%s' % (instance.followup.ticket.ticket_for_url, instance.followup.id) - att_path = os.path.join(settings.MEDIA_ROOT, path) - if settings.DEFAULT_FILE_STORAGE == "django.core.files.storage.FileSystemStorage": - if not os.path.exists(att_path): - os.makedirs(att_path, 0o777) - return os.path.join(path, filename) + """Just bridge""" + return instance.attachment_path(filename) -@python_2_unicode_compatible class Attachment(models.Model): """ Represents a file attached to a follow-up. This could come from an e-mail attachment, or it could be uploaded via the web interface. """ - followup = models.ForeignKey( - FollowUp, - on_delete=models.CASCADE, - verbose_name=_('Follow-up'), - ) - file = models.FileField( _('File'), upload_to=attachment_path, @@ -788,29 +963,104 @@ class Attachment(models.Model): filename = models.CharField( _('Filename'), + blank=True, max_length=1000, ) mime_type = models.CharField( _('MIME Type'), + blank=True, max_length=255, ) size = models.IntegerField( _('Size'), + blank=True, help_text=_('Size of this file in bytes'), ) def __str__(self): return '%s' % self.filename + def save(self, *args, **kwargs): + + if not self.size: + self.size = self.get_size() + + if not self.filename: + self.filename = self.get_filename() + + if not self.mime_type: + self.mime_type = \ + mimetypes.guess_type(self.filename, strict=False)[0] or \ + 'application/octet-stream' + + return super(Attachment, self).save(*args, **kwargs) + + def get_filename(self): + return str(self.file) + + def get_size(self): + return self.file.file.size + + def attachment_path(self, filename): + """Provide a file path that will help prevent files being overwritten, by + putting attachments in a folder off attachments for ticket/followup_id/. + """ + assert NotImplementedError( + "This method is to be implemented by Attachment classes" + ) + class Meta: ordering = ('filename',) verbose_name = _('Attachment') verbose_name_plural = _('Attachments') + abstract = True + + +class FollowUpAttachment(Attachment): + + followup = models.ForeignKey( + FollowUp, + on_delete=models.CASCADE, + verbose_name=_('Follow-up'), + ) + + def attachment_path(self, filename): + + os.umask(0) + path = 'helpdesk/attachments/{ticket_for_url}-{secret_key}/{id_}'.format( + ticket_for_url=self.followup.ticket.ticket_for_url, + secret_key=self.followup.ticket.secret_key, + id_=self.followup.id) + att_path = os.path.join(settings.MEDIA_ROOT, path) + if settings.DEFAULT_FILE_STORAGE == "django.core.files.storage.FileSystemStorage": + if not os.path.exists(att_path): + os.makedirs(att_path, 0o777) + return os.path.join(path, filename) + + +class KBIAttachment(Attachment): + + kbitem = models.ForeignKey( + "KBItem", + on_delete=models.CASCADE, + verbose_name=_('Knowledge base item'), + ) + + def attachment_path(self, filename): + + os.umask(0) + path = 'helpdesk/attachments/kb/{category}/{kbi}'.format( + category=self.kbitem.category, + kbi=self.kbitem.id) + att_path = os.path.join(settings.MEDIA_ROOT, path) + if settings.DEFAULT_FILE_STORAGE == "django.core.files.storage.FileSystemStorage": + if not os.path.exists(att_path): + os.makedirs(att_path, 0o777) + return os.path.join(path, filename) -@python_2_unicode_compatible class PreSetReply(models.Model): """ We can allow the admin to define a number of pre-set replies, used to @@ -852,7 +1102,6 @@ class PreSetReply(models.Model): return '%s' % self.name -@python_2_unicode_compatible class EscalationExclusion(models.Model): """ An 'EscalationExclusion' lets us define a date on which escalation should @@ -889,7 +1138,6 @@ class EscalationExclusion(models.Model): verbose_name_plural = _('Escalation exclusions') -@python_2_unicode_compatible class EmailTemplate(models.Model): """ Since these are more likely to be changed than other templates, we store @@ -949,7 +1197,6 @@ class EmailTemplate(models.Model): verbose_name_plural = _('e-mail templates') -@python_2_unicode_compatible class KBCategory(models.Model): """ Lets help users help themselves: the Knowledge Base is a categorised @@ -982,12 +1229,12 @@ class KBCategory(models.Model): return reverse('helpdesk:kb_category', kwargs={'slug': self.slug}) -@python_2_unicode_compatible class KBItem(models.Model): """ An item within the knowledgebase. Very straightforward question/answer style system. """ + voted_by = models.ManyToManyField(settings.AUTH_USER_MODEL) category = models.ForeignKey( KBCategory, on_delete=models.CASCADE, @@ -1049,8 +1296,10 @@ class KBItem(models.Model): from django.urls import reverse return reverse('helpdesk:kb_item', args=(self.id,)) + def get_markdown(self): + return get_markdown(self.answer) + -@python_2_unicode_compatible class SavedSearch(models.Model): """ Allow a user to save a ticket search, eg their filtering and sorting @@ -1097,15 +1346,38 @@ class SavedSearch(models.Model): verbose_name_plural = _('Saved searches') -@python_2_unicode_compatible +def get_default_setting(setting): + from helpdesk.settings import DEFAULT_USER_SETTINGS + return DEFAULT_USER_SETTINGS[setting] + + +def login_view_ticketlist_default(): + return get_default_setting('login_view_ticketlist') + + +def email_on_ticket_change_default(): + return get_default_setting('email_on_ticket_change') + + +def email_on_ticket_assign_default(): + return get_default_setting('email_on_ticket_assign') + + +def tickets_per_page_default(): + return get_default_setting('tickets_per_page') + + +def use_email_as_submitter_default(): + return get_default_setting('use_email_as_submitter') + + class UserSettings(models.Model): """ A bunch of user-specific settings that we want to be able to define, such as notification preferences and other things that should probably be configurable. - - We should always refer to user.usersettings_helpdesk.settings['setting_name']. """ + PAGE_SIZES = ((10, '10'), (25, '25'), (50, '50'), (100, '100')) user = models.OneToOneField( settings.AUTH_USER_MODEL, @@ -1113,41 +1385,46 @@ class UserSettings(models.Model): related_name="usersettings_helpdesk") settings_pickled = models.TextField( - _('Settings Dictionary'), - help_text=_('This is a base64-encoded representation of a pickled Python dictionary. ' + _('DEPRECATED! Settings Dictionary DEPRECATED!'), + help_text=_('DEPRECATED! This is a base64-encoded representation of a pickled Python dictionary. ' 'Do not change this field via the admin.'), blank=True, null=True, ) - def _set_settings(self, data): - # data should always be a Python dictionary. - try: - import pickle - except ImportError: - import cPickle as pickle - from helpdesk.lib import b64encode - if six.PY2: - self.settings_pickled = b64encode(pickle.dumps(data)) - else: - self.settings_pickled = b64encode(pickle.dumps(data)).decode() + login_view_ticketlist = models.BooleanField( + verbose_name=_('Show Ticket List on Login?'), + help_text=_('Display the ticket list upon login? Otherwise, the dashboard is shown.'), + default=login_view_ticketlist_default, + ) - def _get_settings(self): - # return a python dictionary representing the pickled data. - try: - import pickle - except ImportError: - import cPickle as pickle - from helpdesk.lib import b64decode - try: - if six.PY2: - return pickle.loads(b64decode(str(self.settings_pickled))) - else: - return pickle.loads(b64decode(self.settings_pickled.encode('utf-8'))) - except pickle.UnpicklingError: - return {} + email_on_ticket_change = models.BooleanField( + verbose_name=_('E-mail me on ticket change?'), + help_text=_('If you\'re the ticket owner and the ticket is changed via the web by somebody else, do you want to receive an e-mail?'), + default=email_on_ticket_change_default, + ) - settings = property(_get_settings, _set_settings) + email_on_ticket_assign = models.BooleanField( + verbose_name=_('E-mail me when assigned a ticket?'), + help_text=_('If you are assigned a ticket via the web, do you want to receive an e-mail?'), + default=email_on_ticket_assign_default, + ) + + tickets_per_page = models.IntegerField( + verbose_name=_('Number of tickets to show per page'), + help_text=_('How many tickets do you want to see on the Ticket List page?'), + default=tickets_per_page_default, + choices=PAGE_SIZES, + ) + + use_email_as_submitter = models.BooleanField( + verbose_name=_('Use my e-mail address when submitting tickets?'), + help_text=_('When you submit a ticket, do you want to automatically ' + 'use your e-mail address as the submitter address? You ' + 'can type a different e-mail address when entering the ' + 'ticket if needed, this option only changes the default.'), + default=use_email_as_submitter_default, + ) def __str__(self): return 'Preferences for %s' % self.user @@ -1166,15 +1443,13 @@ def create_usersettings(sender, instance, created, **kwargs): If we end up with users with no UserSettings, then we get horrible 'DoesNotExist: UserSettings matching query does not exist.' errors. """ - from helpdesk.settings import DEFAULT_USER_SETTINGS if created: - UserSettings.objects.create(user=instance, settings=DEFAULT_USER_SETTINGS) + UserSettings.objects.create(user=instance) models.signals.post_save.connect(create_usersettings, sender=settings.AUTH_USER_MODEL) -@python_2_unicode_compatible class IgnoreEmail(models.Model): """ This model lets us easily ignore e-mails from certain senders when @@ -1261,7 +1536,6 @@ class IgnoreEmail(models.Model): return False -@python_2_unicode_compatible class TicketCC(models.Model): """ Often, there are people who wish to follow a ticket who aren't the @@ -1332,7 +1606,6 @@ class CustomFieldManager(models.Manager): return super(CustomFieldManager, self).get_queryset().order_by('ordering') -@python_2_unicode_compatible class CustomField(models.Model): """ Definitions for custom fields that are glued onto each ticket. @@ -1416,7 +1689,6 @@ class CustomField(models.Model): ) def _choices_as_array(self): - from django.utils.six import StringIO valuebuffer = StringIO(self.list_values) choices = [[item.strip(), item.strip()] for item in valuebuffer.readlines()] valuebuffer.close() @@ -1446,7 +1718,6 @@ class CustomField(models.Model): verbose_name_plural = _('Custom fields') -@python_2_unicode_compatible class TicketCustomFieldValue(models.Model): ticket = models.ForeignKey( Ticket, @@ -1471,7 +1742,6 @@ class TicketCustomFieldValue(models.Model): verbose_name_plural = _('Ticket custom field values') -@python_2_unicode_compatible class TicketDependency(models.Model): """ The ticket identified by `ticket` cannot be resolved until the ticket in `depends_on` has been resolved. diff --git a/helpdesk/query.py b/helpdesk/query.py new file mode 100644 index 00000000..a2263326 --- /dev/null +++ b/helpdesk/query.py @@ -0,0 +1,156 @@ +from django.db.models import Q +from django.core.cache import cache + +from model_utils import Choices + +from base64 import b64encode +from base64 import b64decode +import json + + +def query_to_base64(query): + """ + Converts a query dict object to a base64-encoded bytes object. + """ + return b64encode(json.dumps(query).encode('UTF-8')).decode("ascii") + + +def query_from_base64(b64data): + """ + Converts base64-encoded bytes object back to a query dict object. + """ + query = {'search_string': ''} + query.update(json.loads(b64decode(b64data).decode('utf-8'))) + if query['search_string'] is None: + query['search_string'] = '' + return query + + +def query_to_dict(results, descriptions): + """ + Replacement method for cursor.dictfetchall() as that method no longer + exists in psycopg2, and I'm guessing in other backends too. + + Converts the results of a raw SQL query into a list of dictionaries, suitable + for use in templates etc. + """ + + output = [] + for data in results: + row = {} + i = 0 + for column in descriptions: + row[column[0]] = data[i] + i += 1 + + output.append(row) + return output + + +def apply_query(queryset, params): + """ + Apply a dict-based set of filters & parameters to a queryset. + + queryset is a Django queryset, eg MyModel.objects.all() or + MyModel.objects.filter(user=request.user) + + params is a dictionary that contains the following: + filtering: A dict of Django ORM filters, eg: + {'user__id__in': [1, 3, 103], 'title__contains': 'foo'} + + search_string: A freetext search string + + sorting: The name of the column to sort by + """ + for key in params['filtering'].keys(): + filter = {key: params['filtering'][key]} + queryset = queryset.filter(**filter) + + search = params.get('search_string', '') + if search: + qset = ( + Q(title__icontains=search) | + Q(description__icontains=search) | + Q(resolution__icontains=search) | + Q(submitter_email__icontains=search) | + Q(ticketcustomfieldvalue__value__icontains=search) + ) + + queryset = queryset.filter(qset) + + sorting = params.get('sorting', None) + if sorting: + sortreverse = params.get('sortreverse', None) + if sortreverse: + sorting = "-%s" % sorting + queryset = queryset.order_by(sorting) + + return queryset + + +def get_query(query, huser): + # Prefilter the allowed tickets + objects = cache.get(huser.user.email + query) + if objects is not None: + return objects + tickets = huser.get_tickets_in_queues().select_related() + query_params = query_from_base64(query) + ticket_qs = apply_query(tickets, query_params) + cache.set(huser.user.email + query, ticket_qs, timeout=3600) + return ticket_qs + + +ORDER_COLUMN_CHOICES = Choices( + ('0', 'id'), + ('2', 'priority'), + ('3', 'title'), + ('4', 'queue'), + ('5', 'status'), + ('6', 'created'), + ('7', 'due_date'), + ('8', 'assigned_to') +) + + +def query_tickets_by_args(objects, order_by, **kwargs): + """ + This function takes in a list of ticket objects from the views and throws it + to the datatables on ticket_list.html. If a search string was entered, this + function filters existing dataset on search string and returns a filtered + filtered list. The `draw`, `length` etc parameters are for datatables to + display meta data on the table contents. The returning queryset is passed + to a Serializer called DatatablesTicketSerializer in serializers.py. + """ + draw = int(kwargs.get('draw', None)[0]) + length = int(kwargs.get('length', None)[0]) + start = int(kwargs.get('start', None)[0]) + search_value = kwargs.get('search[value]', None)[0] + order_column = kwargs.get('order[0][column]', None)[0] + order = kwargs.get('order[0][dir]', None)[0] + + order_column = ORDER_COLUMN_CHOICES[order_column] + # django orm '-' -> desc + if order == 'desc': + order_column = '-' + order_column + + queryset = objects.all().order_by(order_by) + total = queryset.count() + + if search_value: + queryset = queryset.filter(Q(id__icontains=search_value) | + Q(priority__icontains=search_value) | + Q(title__icontains=search_value) | + Q(queue__title__icontains=search_value) | + Q(status__icontains=search_value) | + Q(created__icontains=search_value) | + Q(due_date__icontains=search_value) | + Q(assigned_to__email__icontains=search_value)) + + count = queryset.count() + queryset = queryset.order_by(order_column)[start:start + length] + return { + 'items': queryset, + 'count': count, + 'total': total, + 'draw': draw + } diff --git a/helpdesk/serializers.py b/helpdesk/serializers.py new file mode 100644 index 00000000..c3b311b5 --- /dev/null +++ b/helpdesk/serializers.py @@ -0,0 +1,60 @@ +from rest_framework import serializers + +from .models import Ticket +from .lib import format_time_spent + +from django.contrib.humanize.templatetags import humanize + +""" +A serializer for the Ticket model, returns data in the format as required by +datatables for ticket_list.html. Called from staff.datatables_ticket_list. + +""" + + +class DatatablesTicketSerializer(serializers.ModelSerializer): + ticket = serializers.SerializerMethodField() + assigned_to = serializers.SerializerMethodField() + created = serializers.SerializerMethodField() + due_date = serializers.SerializerMethodField() + status = serializers.SerializerMethodField() + row_class = serializers.SerializerMethodField() + time_spent = serializers.SerializerMethodField() + queue = serializers.SerializerMethodField() + + class Meta: + model = Ticket + # fields = '__all__' + fields = ('ticket', 'id', 'priority', 'title', 'queue', 'status', + 'created', 'due_date', 'assigned_to', 'row_class', + 'time_spent') + + def get_queue(self, obj): + return ({"title": obj.queue.title, "id": obj.queue.id}) + + def get_ticket(self, obj): + return (str(obj.id) + " " + obj.ticket) + + def get_status(self, obj): + return (obj.get_status) + + def get_created(self, obj): + return (humanize.naturaltime(obj.created)) + + def get_due_date(self, obj): + return (humanize.naturaltime(obj.due_date)) + + def get_assigned_to(self, obj): + if obj.assigned_to: + if obj.assigned_to.first_name: + return (obj.assigned_to.first_name) + else: + return (obj.assigned_to.email) + else: + return ("None") + + def get_time_spent(self, obj): + return format_time_spent(obj.time_spent) + + def get_row_class(self, obj): + return (obj.get_priority_css_class) diff --git a/helpdesk/settings.py b/helpdesk/settings.py index cc6e6349..32afacc6 100644 --- a/helpdesk/settings.py +++ b/helpdesk/settings.py @@ -2,23 +2,22 @@ Default settings for django-helpdesk. """ - +import warnings from django.conf import settings from django.core.exceptions import ImproperlyConfigured -try: - DEFAULT_USER_SETTINGS = settings.HELPDESK_DEFAULT_SETTINGS -except AttributeError: - DEFAULT_USER_SETTINGS = None +DEFAULT_USER_SETTINGS = { + 'login_view_ticketlist': True, + 'email_on_ticket_change': True, + 'email_on_ticket_assign': True, + 'tickets_per_page': 25, + 'use_email_as_submitter': True, +} -if not isinstance(DEFAULT_USER_SETTINGS, dict): - DEFAULT_USER_SETTINGS = { - 'use_email_as_submitter': True, - 'email_on_ticket_assign': True, - 'email_on_ticket_change': True, - 'login_view_ticketlist': True, - 'tickets_per_page': 25 - } +try: + DEFAULT_USER_SETTINGS.update(settings.HELPDESK_DEFAULT_SETTINGS) +except AttributeError: + pass HAS_TAG_SUPPORT = False @@ -85,12 +84,12 @@ HELPDESK_SUBMIT_A_TICKET_PUBLIC = getattr(settings, 'HELPDESK_SUBMIT_A_TICKET_PU # options for update_ticket views # ################################### +''' options for update_ticket views ''' # allow non-staff users to interact with tickets? -# this will also change how 'staff_member_required' -# in staff.py will be defined. -HELPDESK_ALLOW_NON_STAFF_TICKET_UPDATE = getattr(settings, - 'HELPDESK_ALLOW_NON_STAFF_TICKET_UPDATE', - False) +# can be True/False or a callable accepting the active user and returning True if they must be considered helpdesk staff +HELPDESK_ALLOW_NON_STAFF_TICKET_UPDATE = getattr(settings, 'HELPDESK_ALLOW_NON_STAFF_TICKET_UPDATE', False) +if not (HELPDESK_ALLOW_NON_STAFF_TICKET_UPDATE in (True, False) or callable(HELPDESK_ALLOW_NON_STAFF_TICKET_UPDATE)): + warnings.warn("HELPDESK_ALLOW_NON_STAFF_TICKET_UPDATE should be set to either True/False or a callable.", RuntimeWarning) # show edit buttons in ticket follow ups. HELPDESK_SHOW_EDIT_BUTTON_FOLLOW_UP = getattr(settings, @@ -149,3 +148,6 @@ QUEUE_EMAIL_BOX_UPDATE_ONLY = getattr(settings, 'QUEUE_EMAIL_BOX_UPDATE_ONLY', F # only allow users to access queues that they are members of? HELPDESK_ENABLE_PER_QUEUE_STAFF_PERMISSION = getattr( settings, 'HELPDESK_ENABLE_PER_QUEUE_STAFF_PERMISSION', False) + +# use https in the email links +HELPDESK_USE_HTTPS_IN_EMAIL_LINK = getattr(settings, 'HELPDESK_USE_HTTPS_IN_EMAIL_LINK', False) diff --git a/helpdesk/static/helpdesk/css/sb-admin.css b/helpdesk/static/helpdesk/css/sb-admin.css new file mode 100644 index 00000000..f015c714 --- /dev/null +++ b/helpdesk/static/helpdesk/css/sb-admin.css @@ -0,0 +1,375 @@ +/*! + * Start Bootstrap - SB Admin v5.0.2 (https://startbootstrap.com/template-overviews/sb-admin) + * Copyright 2013-2018 Start Bootstrap + * Licensed under MIT (https://github.com/BlackrockDigital/startbootstrap-sb-admin/blob/master/LICENSE) + */ + +html { + position: relative; + min-height: 100%; +} + +body { + height: 100%; +} + +#wrapper { + display: -webkit-box; + display: -ms-flexbox; + display: flex; +} + +#wrapper #content-wrapper { + overflow-x: hidden; + width: 100%; + padding-top: 1rem; + padding-bottom: 80px; +} + +body.fixed-nav #content-wrapper { + margin-top: 56px; + padding-left: 90px; +} + +body.fixed-nav.sidebar-toggled #content-wrapper { + padding-left: 0; +} + +@media (min-width: 768px) { + body.fixed-nav #content-wrapper { + padding-left: 225px; + } + body.fixed-nav.sidebar-toggled #content-wrapper { + padding-left: 90px; + } +} + +.scroll-to-top { + position: fixed; + right: 15px; + bottom: 15px; + display: none; + width: 50px; + height: 50px; + text-align: center; + color: #fff; + background: rgba(52, 58, 64, 0.5); + line-height: 46px; +} + +.scroll-to-top:focus, .scroll-to-top:hover { + color: white; +} + +.scroll-to-top:hover { + background: #343a40; +} + +.scroll-to-top i { + font-weight: 800; +} + +.smaller { + font-size: 0.7rem; +} + +.o-hidden { + overflow: hidden !important; +} + +.z-0 { + z-index: 0; +} + +.z-1 { + z-index: 1; +} + +.navbar-nav .form-inline .input-group { + width: 100%; +} + +.navbar-nav .nav-item.active .nav-link { + color: #fff; +} + +.navbar-nav .nav-item.dropdown .dropdown-toggle::after { + width: 1rem; + text-align: center; + float: right; + vertical-align: 0; + border: 0; + font-weight: 900; + content: '\f105'; + font-family: 'Font Awesome 5 Free'; +} + +.navbar-nav .nav-item.dropdown.show .dropdown-toggle::after { + content: '\f107'; +} + +.navbar-nav .nav-item.dropdown.no-arrow .dropdown-toggle::after { + display: none; +} + +.navbar-nav .nav-item .nav-link:focus { + outline: none; +} + +.navbar-nav .nav-item .nav-link .badge { + position: absolute; + margin-left: 0.75rem; + top: 0.3rem; + font-weight: 400; + font-size: 0.5rem; +} + +@media (min-width: 768px) { + .navbar-nav .form-inline .input-group { + width: auto; + } +} + +.sidebar { + width: 90px !important; + background-color: #212529; + min-height: calc(100vh - 56px); +} + +.sidebar .nav-item:last-child { + margin-bottom: 1rem; +} + +.sidebar .nav-item .nav-link { + text-align: center; + padding: 0.75rem 1rem; + width: 90px; +} + +.sidebar .nav-item .nav-link span { + font-size: 0.65rem; + display: block; +} + +.sidebar .nav-item .dropdown-menu { + position: absolute !important; + -webkit-transform: none !important; + transform: none !important; + left: calc(90px + 0.5rem) !important; + margin: 0; +} + +.sidebar .nav-item .dropdown-menu.dropup { + bottom: 0; + top: auto !important; +} + +.sidebar .nav-item.dropdown .dropdown-toggle::after { + display: none; +} + +.sidebar .nav-item .nav-link { + color: rgba(255, 255, 255, 0.5); +} + +.sidebar .nav-item .nav-link:active, .sidebar .nav-item .nav-link:focus, .sidebar .nav-item .nav-link:hover { + color: rgba(255, 255, 255, 0.75); +} + +.sidebar.toggled { + width: 0 !important; + overflow: hidden; +} + +@media (min-width: 768px) { + .sidebar { + width: 225px !important; + } + .sidebar .nav-item .nav-link { + display: block; + width: 100%; + text-align: left; + padding: 1rem; + width: 225px; + } + .sidebar .nav-item .nav-link span { + font-size: 1rem; + display: inline; + } + .sidebar .nav-item .dropdown-menu { + position: static !important; + margin: 0 1rem; + top: 0; + } + .sidebar .nav-item.dropdown .dropdown-toggle::after { + display: block; + } + .sidebar.toggled { + overflow: visible; + width: 90px !important; + } + .sidebar.toggled .nav-item:last-child { + margin-bottom: 1rem; + } + .sidebar.toggled .nav-item .nav-link { + text-align: center; + padding: 0.75rem 1rem; + width: 90px; + } + .sidebar.toggled .nav-item .nav-link span { + font-size: 0.65rem; + display: block; + } + .sidebar.toggled .nav-item .dropdown-menu { + position: absolute !important; + -webkit-transform: none !important; + transform: none !important; + left: calc(90px + 0.5rem) !important; + margin: 0; + } + .sidebar.toggled .nav-item .dropdown-menu.dropup { + bottom: 0; + top: auto !important; + } + .sidebar.toggled .nav-item.dropdown .dropdown-toggle::after { + display: none; + } +} + +.sidebar.fixed-top { + top: 56px; + height: calc(100vh - 56px); + overflow-y: auto; +} + +.card-body-icon { + position: absolute; + z-index: 0; + top: -1.25rem; + right: -1rem; + opacity: 0.4; + font-size: 5rem; + -webkit-transform: rotate(15deg); + transform: rotate(15deg); +} + +@media (min-width: 576px) { + .card-columns { + -webkit-column-count: 1; + column-count: 1; + } +} + +@media (min-width: 768px) { + .card-columns { + -webkit-column-count: 2; + column-count: 2; + } +} + +@media (min-width: 1200px) { + .card-columns { + -webkit-column-count: 2; + column-count: 2; + } +} + +:root { + --input-padding-x: 0.75rem; + --input-padding-y: 0.75rem; +} + +.card-login { + max-width: 25rem; +} + +.card-register { + max-width: 40rem; +} + +.form-label-group { + position: relative; +} + +.form-label-group > input, +.form-label-group > label { + padding: var(--input-padding-y) var(--input-padding-x); + height: auto; +} + +.form-label-group > label { + position: absolute; + top: 0; + left: 0; + display: block; + width: 100%; + margin-bottom: 0; + /* Override default `