Skip to content

Commit

Permalink
use lists instead of dicts for choices (see #222)
Browse files Browse the repository at this point in the history
  • Loading branch information
xi committed Jun 20, 2018
1 parent 057d262 commit 4fbf9aa
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 31 deletions.
4 changes: 2 additions & 2 deletions mail/ajax.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@

import six

from mail.models import usages
from mail.models import USAGES
from mail.models import MailTemplate


class LoadChoices(View):
def post(self, request, *args, **kwargs):
department = request.POST["department"]
used = MailTemplate.objects.values_list('usage', flat=True).filter(department=department)
valid = {str(x): six.text_type(y) for x, y in usages.items() if not any(z in x for z in used)}
valid = {str(x): six.text_type(y) for x, y in USAGES if not any(z in x for z in used)}
valid.update({'': '--------'})
return(HttpResponse(json.dumps(valid), content_type="application/json"))
18 changes: 9 additions & 9 deletions mail/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@

from users.models import Lageruser, Department

usages = {
"new": _("New Device is created"),
"room": _("Room is changed"),
"owner": _("person currently lending is changed"),
"reminder": _("Reminder that device is still owned"),
"overdue": _("Reminder that device is overdue"),
"trashed": _("Device is trashed")
}
USAGES = [
("new", _("New Device is created")),
("room", _("Room is changed")),
("owner", _("person currently lending is changed")),
("reminder", _("Reminder that device is still owned")),
("overdue", _("Reminder that device is overdue")),
("trashed", _("Device is trashed")),
]


@six.python_2_unicode_compatible
Expand All @@ -29,7 +29,7 @@ class MailTemplate(models.Model):
subject = models.CharField(_('Subject'), max_length=500)
body = models.CharField(_('Body'), max_length=10000)
department = models.ForeignKey(Department, null=True)
usage = models.CharField(_('Usage'), choices=list(usages.items()), null=True, blank=True, max_length=200)
usage = models.CharField(_('Usage'), choices=USAGES, null=True, blank=True, max_length=200)

def __str__(self):
return self.name
Expand Down
10 changes: 5 additions & 5 deletions main/ajax.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@
from django.views.generic.base import View
from django.http import HttpResponse

from main.models import DashboardWidget, widgets
from main.models import DashboardWidget, WIDGETS
from main.views import get_widget_data


class WidgetAdd(View):
def post(self, request):
widgetname = request.POST["widgetname"]
if widgetname in widgets:
if widgetname in dict(WIDGETS):
userwidgets = DashboardWidget.objects.filter(user=request.user)
if len(userwidgets.filter(widgetname=widgetname)) != 0:
return HttpResponse("")
Expand All @@ -37,7 +37,7 @@ def post(self, request):
class WidgetRemove(View):
def post(self, request):
widgetname = request.POST["widgetname"]
if widgetname in widgets:
if widgetname in dict(WIDGETS):
DashboardWidget.objects.get(user=request.user, widgetname=widgetname).delete()
return HttpResponse("")
else:
Expand All @@ -47,7 +47,7 @@ def post(self, request):
class WidgetToggle(View):
def post(self, request):
widgetname = request.POST["widgetname"]
if widgetname in widgets:
if widgetname in dict(WIDGETS):
w = DashboardWidget.objects.get(user=request.user, widgetname=widgetname)
w.minimized = not w.minimized
w.save()
Expand All @@ -61,7 +61,7 @@ def post(self, request):
userwidgets = json.loads(request.POST["widgets"])

for widgetname, widgetattr in userwidgets.items():
if widgetname in widgets:
if widgetname in dict(WIDGETS):
w = DashboardWidget.objects.get(user=request.user, widgetname=widgetname)
if w.index != widgetattr["index"] or w.column != widgetattr["column"]:
w.index = widgetattr["index"]
Expand Down
26 changes: 13 additions & 13 deletions main/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@
from users.models import Lageruser


widgets = {
"edithistory": _("Edit history"),
"newestdevices": _("Newest devices"),
"overdue": _("Overdue devices"),
"statistics": _("Statistics"),
"groups": _("Groups"),
"sections": _("Sections"),
"recentlendings": _("Recent lendings"),
"shorttermdevices": _("Devices for short term lending"),
"bookmarks": _("Bookmarked Devices"),
"returnsoon": _("Devices, that are due soon")
}
WIDGETS = [
("edithistory", _("Edit history")),
("newestdevices", _("Newest devices")),
("overdue", _("Overdue devices")),
("statistics", _("Statistics")),
("groups", _("Groups")),
("sections", _("Sections")),
("recentlendings", _("Recent lendings")),
("shorttermdevices", _("Devices for short term lending")),
("bookmarks", _("Bookmarked Devices")),
("returnsoon", _("Devices, that are due soon")),
]


def get_progresscolor(percent):
Expand All @@ -33,7 +33,7 @@ def get_progresscolor(percent):
class DashboardWidget(models.Model):
column = models.CharField(max_length=1, choices=[("l", "left"), ("r", "right")])
index = models.IntegerField()
widgetname = models.CharField(max_length=200, choices=list(widgets.items()))
widgetname = models.CharField(max_length=200, choices=WIDGETS)
user = models.ForeignKey(Lageruser)
minimized = models.BooleanField(default=False)

Expand Down
4 changes: 2 additions & 2 deletions main/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from network.models import *
from devicegroups.models import Devicegroup
from locations.models import Section
from main.models import DashboardWidget, widgets, get_progresscolor
from main.models import DashboardWidget, WIDGETS, get_progresscolor
from Lagerregal.utils import PaginationMixin
from devices.forms import LendForm

Expand Down Expand Up @@ -93,7 +93,7 @@ def get_context_data(self, **kwargs):
).order_by("index")
context["widgets_right"] = DashboardWidget.objects.filter(user=self.request.user, column="r"
).order_by("index")
userwidget_list = dict(widgets)
userwidget_list = dict(WIDGETS)
widgetlist = [x[0] for x in DashboardWidget.objects.filter(user=self.request.user
).values_list("widgetname")]
context.update(get_widget_data(self.request.user, widgetlist, self.request.user.departments.all()))
Expand Down

0 comments on commit 4fbf9aa

Please sign in to comment.