Fix saving and loading custom queries, update for Py3/Django2.x

This commit is contained in:
Garret Wassermann 2018-12-21 19:14:54 -05:00
parent 2a3abc44fc
commit ea8c75b884
3 changed files with 46 additions and 24 deletions

View File

@ -19,11 +19,25 @@ from helpdesk.models import Attachment, EmailTemplate
from model_utils import Choices
from base64 import encodebytes as b64encode
from base64 import decodebytes as b64decode
from base64 import b64encode
from base64 import b64decode
import json
logger = logging.getLogger('helpdesk')
def query_to_base64(query):
"""
Converts a query dict object to a base64-encoded bytes object.
"""
return b64encode(json.dumps(query).encode('UTF-8'))
def query_from_base64(b64data):
"""
Converts base64-encoded bytes object back to a query dict object.
"""
return json.loads(b64decode(b64data).decode('utf-8'))
def query_to_dict(results, descriptions):
"""

View File

@ -5,7 +5,7 @@
{% block helpdesk_title %}{% trans "Tickets" %}{% endblock %}
{% block helpdesk_head %}
<script src='{% static "helpdesk/filter.js" %}'></script>
{% endblock %}
@ -235,7 +235,22 @@
<button id='select_inverse_btn' type="button" class="btn btn-primary btn-sm"><i class="fas fa-expand-arrows-alt"></i>&nbsp;{% trans "Invert" %}</button>
</p>
<p><label for='id_mass_action'>{% trans "With Selected Tickets:" %}</label> <select name='action' id='id_mass_action'><option value='take'>{% trans "Take (Assign to me)" %}</option><option value='delete'>{% trans "Delete" %}</option><optgroup label='{% trans "Close" %}'><option value='close'>{% trans "Close (Don't Send E-Mail)" %}</option><option value='close_public'>{% trans "Close (Send E-Mail)" %}</option></optgroup><optgroup label='{% trans "Assign To" %}'><option value='unassign'>{% trans "Nobody (Unassign)" %}</option>{% for u in user_choices %}<option value='assign_{{ u.id }}'>{{ u.get_username }}</option>{% endfor %}</optgroup></select> <button type="submit" class="btn btn-primary btn-sm"><i class="fas fa-arrow-circle-right"></i>&nbsp;{% trans "Go" %}</button></p>
<p>
<label for='id_mass_action'>{% trans "With Selected Tickets:" %}</label>
<select name='action' id='id_mass_action'>
<option value='take'>{% trans "Take (Assign to me)" %}</option>
<option value='delete'>{% trans "Delete" %}</option>
<optgroup label='{% trans "Close" %}'>
<option value='close'>{% trans "Close (Don't Send E-Mail)" %}</option>
<option value='close_public'>{% trans "Close (Send E-Mail)" %}</option>
</optgroup>
<optgroup label='{% trans "Assign To" %}'>
<option value='unassign'>{% trans "Nobody (Unassign)" %}</option>
{% for u in user_choices %}<option value='assign_{{ u.id }}'>{{ u.get_username }}</option>{% endfor %}
</optgroup>
</select>
<button type="submit" class="btn btn-primary btn-sm"><i class="fas fa-arrow-circle-right"></i>&nbsp;{% trans "Go" %}</button>
</p>
{% csrf_token %}</form>
</div>
<!-- /.panel-body -->
@ -246,6 +261,7 @@
{% block helpdesk_js %}
<script src='{% static "helpdesk/filter.js" %}'></script>
<script>
{% if not server_side %}
$('#ticketTable').DataTable({

View File

@ -819,16 +819,12 @@ def ticket_list(request):
return HttpResponseRedirect(reverse('helpdesk:list'))
import json
from helpdesk.lib import b64decode
from helpdesk.lib import query_from_base64
try:
if six.PY3:
if DJANGO_VERSION[0] > 1:
# if Django >= 2.0
query_params = json.loads(b64decode(str(saved_query.query).lstrip("b\\'")).decode())
else:
query_params = json.loads(b64decode(str(saved_query.query)).decode())
else:
query_params = json.loads(b64decode(str(saved_query.query)))
# we get a string like: b'stuff'
# so leave of the first two chars (b') and last (')
b64query = saved_query.query[2:-1]
query_params = query_from_base64(b64query)
except ValueError:
# Query deserialization failed. (E.g. was a pickled query)
return HttpResponseRedirect(reverse('helpdesk:list'))
@ -920,8 +916,8 @@ def ticket_list(request):
'Django Documentation on string matching in SQLite</a>.')
import json
from helpdesk.lib import b64encode
urlsafe_query = b64encode(json.dumps(query_params).encode('UTF-8'))
from helpdesk.lib import query_to_base64
urlsafe_query = query_to_base64(query_params)
user_saved_queries = SavedSearch.objects.filter(Q(user=request.user) | Q(shared__exact=True))
@ -1153,16 +1149,12 @@ def run_report(request, report):
return HttpResponseRedirect(reverse('helpdesk:report_index'))
import json
from helpdesk.lib import b64decode
from helpdesk.lib import query_from_base64
try:
if six.PY3:
if DJANGO_VERSION[0] > 1:
# if Django >= 2.0
query_params = json.loads(b64decode(str(saved_query.query).lstrip("b\\'")).decode())
else:
query_params = json.loads(b64decode(str(saved_query.query)).decode())
else:
query_params = json.loads(b64decode(str(saved_query.query)))
# we get a string like: b'stuff'
# so leave of the first two chars (b') and last (')
b64query = saved_query.query[2:-1]
query_params = query_from_base64(b64query)
except json.JSONDecodeError:
return HttpResponseRedirect(reverse('helpdesk:report_index'))