Skip to content
This repository has been archived by the owner on Feb 24, 2021. It is now read-only.

Commit

Permalink
Changed many things:
Browse files Browse the repository at this point in the history
Added Literature Info models
Separated Literature sources
Given students ability to add literature to models
... more
  • Loading branch information
Xiphoseer committed Aug 4, 2017
1 parent 34f5e0e commit 7c05903
Show file tree
Hide file tree
Showing 20 changed files with 742 additions and 284 deletions.
119 changes: 106 additions & 13 deletions pyBuchaktion/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
and filters for the list view.
"""

import re

from django.contrib.admin import ModelAdmin, register, helpers
from django.db.models import Count
from django.db.models.query import Prefetch
Expand Down Expand Up @@ -76,6 +78,12 @@ class BookAdmin(ImportExportMixin, ModelAdmin):
'accept_selected',
]

fieldsets = [
("", {
'fields': (('title',), ('author',), ('publisher', 'year'), ('isbn_13', 'price', 'state'), ('note',))
}),
]

# Annotate the queryset with the number of orders.
def get_queryset(self, request):
qs = super().get_queryset(request)
Expand Down Expand Up @@ -161,6 +169,15 @@ class OrderAdmin(ImportExportMixin, ModelAdmin):
"reject_selected",
]

fieldsets = (
(_('General'), {
'fields': (('book',), ('student',), ('status', 'order_timeframe'))
}),
(_('Details'), {
'fields': (('hint',),)
}),
)

# The title of the book to be ordered
def book_title(self, order):
return order.book.title
Expand Down Expand Up @@ -301,6 +318,12 @@ class StudentAdmin(ModelAdmin):
'tuid_user',
)

fieldsets = [
("", {
'fields': (('tuid_user', 'email'), ('library_id','language'))
}),
]

# Annotate the queryset with the number of orders.
def get_queryset(self, request):
qs = super().get_queryset(request)
Expand Down Expand Up @@ -358,6 +381,12 @@ class OrderTimeframeAdmin(ModelAdmin):
'semester',
)

fieldsets = [
("", {
'fields': (('semester',), ('start_date', 'end_date'), ('allowed_orders', 'spendings'))
}),
]


@register(Semester)
class SemesterAdmin(ModelAdmin):
Expand All @@ -366,14 +395,32 @@ class SemesterAdmin(ModelAdmin):
The admin for a semester.
"""

fieldsets = [
("", {
'fields': (('season','year'), ('budget',))
}),
]

#radio_fields = {"season": admin.VERTICAL}
pass


SEMESTER_REGEX = re.compile("(W|S)(\d*)")

class SemesterWidget(Widget):

def clean(self, value, row=None, *args, **kwargs):
match = SEMESTER_REGEX.match(value)
get_args = {'season': match.groups()[0], 'year': match.groups()[1]}
return Semester.objects.get(**get_args)

def render(self, value, obj=None):
return "{0}{1}".format(value.season, value.year)

class TUCaNLiteratureWidget(ManyToManyWidget):

def __init__(self):
super().__init__(Book, separator = '|', field = 'isbn_13')
super().__init__(Book, separator = ', ', field = 'isbn_13')

class TUCaNLiteratureField(Field):

Expand All @@ -383,17 +430,25 @@ def __init__(self):
def save(self, obj, data):
ids = []
for book in self.clean(data):
literature, created = Literature.objects.get_or_create(
book=book, module=obj, source=Literature.TUCAN,
)
ids.append(literature.pk)
try:
literature_info = Literature.objects.get(book=book, module=obj)
if not literature_info.in_tucan:
literature_info.update(in_tucan=True)
except Literature.DoesNotExist:
literature_info = Literature.objects.create(
book=book, module=module, source=Literature.TUCAN, in_tucan=True
)
ids.append(literature_info.pk)
obj.refresh_from_db(fields=['literature',])
Literature.objects.filter(source=Literature.TUCAN, module=obj).exclude(pk__in=ids).delete()

literature = Literature.objects.filter(module=obj).exclude(pk__in=ids)
literature.filter(source=Literature.TUCAN).delete()
literature.filter(in_tucan=True).update(in_tucan=False)

def get_value(self, obj):
return Book.objects.filter(
literature_info__module=obj,
literature_info__source__in=[Literature.TUCAN, Literature.STAFF],
literature_info__in_tucan=True,
)


Expand All @@ -407,25 +462,30 @@ def get_queryset(self):

books = TUCaNLiteratureField()
category = Field(
column_name = 'category__name_de',
column_name = 'category',
attribute = 'category',
widget = ForeignKeyWidget(
ModuleCategory,
field = 'name_de',
),
)

last_offered = Field(
column_name = 'last_offered',
attribute = 'last_offered',
widget = SemesterWidget()
)

class Meta:
model = Module
import_id_fields = (
'module_id',
'last_offered__year',
'last_offered__season',
)
fields = import_id_fields + (
'name_de',
'name_en',
'category',
'last_offered',
'books',
)

Expand All @@ -438,10 +498,11 @@ class ModuleAdmin(ImportExportMixin, ModelAdmin):
"""

resource_class = ModuleResource
import_template_name = 'pyBuchaktion/admin/import.html'

list_display = (
'name_de',
'name_en',
'name_de_short',
'name_en_short',
'module_id',
'category',
)
Expand All @@ -456,6 +517,22 @@ class ModuleAdmin(ImportExportMixin, ModelAdmin):
'category',
)

fieldsets = [
("", {
'fields': (('name_de','name_en'), ('module_id', 'category'), ('last_offered'))
}),
]

def name_de_short(self, obj):
return Truncator(obj.name_de).chars(30)
name_de_short.short_description = _("german name")
name_de_short.admin_order_field = 'name_de'

def name_en_short(self, obj):
return Truncator(obj.name_en).chars(30)
name_en_short.short_description = _("english name")
name_en_short.admin_order_field = 'name_en'


@register(Literature)
class LiteratureAdmin(ModelAdmin):
Expand All @@ -477,6 +554,12 @@ def get_queryset(self, request):
'source',
)

fieldsets = [
("", {
'fields': (('module',), ('book',), ('source', 'in_tucan', 'active'))
}),
]

def title(self, obj):
return Truncator(obj.book.title).chars(50)
title.short_description = _("title")
Expand Down Expand Up @@ -505,16 +588,20 @@ class ModuleCategoryAdmin(ImportExportMixin, ModelAdmin):
'id',
'name_de',
'name_en',
'visible',
)

list_display_links = (
'name_de',
)

fieldsets = (
('Namen', {
(_('Names'), {
'fields': (('name_de', 'name_en'),)
}),
(_('Settings'), {
'fields': (('visible',),)
}),
)


Expand Down Expand Up @@ -544,6 +631,12 @@ class DisplayMessageAdmin(ImportExportMixin, ModelAdmin):
'trunc_en',
)

fieldsets = (
("", {
'fields': (('key'), ('text_de','text_en'))
}),
)

def trunc_de(self, obj):
return Truncator(obj.text_de).chars(60)
trunc_de.short_description = _("german text")
Expand Down
21 changes: 20 additions & 1 deletion pyBuchaktion/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from django.http.request import QueryDict
from django.db.models import Sum

from .models import Student, Order, Book, OrderTimeframe
from .models import Student, Order, Book, OrderTimeframe, Literature

class BookOrderForm(forms.ModelForm):
class Meta:
Expand Down Expand Up @@ -39,6 +39,25 @@ def clean(self):
raise ValidationError(_("You may not order any more books in this timeframe."), code='no_budget_left')


class LiteratureCreateForm(forms.ModelForm):
class Meta:
model = Literature
fields = ['book']

def clean(self):
cleaned_data = super().clean()
literature = self.instance

book = cleaned_data['book']

# if book.state != Book.ACCEPTED:
# raise ValidationError({'book':_("This book has not been accepted yet")}, code='not_accepted')

if Literature.objects.filter(module=literature.module, book=book).count() > 0:
raise ValidationError({'book':_("This book is already proposed for this module")}, code='exists')

return cleaned_data


class BookSearchForm(forms.Form):
title = forms.CharField(label=_("Title"), max_length=100, required=False)
Expand Down
Loading

0 comments on commit 7c05903

Please sign in to comment.