-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadmin.py
81 lines (62 loc) · 2.55 KB
/
admin.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# Copyright (c) 2010-2013 Kristian Berg
#
# This file is part of stocks-plugin.
#
# stocks-plugin is free software: you can redistribute it and/or
# modify it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at your
# option) any later version.
#
# stocks-plugin is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
# more details.
#
# You should have received a copy of the GNU General Public License along with
# stocks-plugin. If not, see <http://www.gnu.org/licenses/>.
__date__ = "2013-11-09"
__author__ = "vittoros"
from django.contrib import admin
from django.db.models import Q
from django import forms
from ext_plugins.stocks.models import Item, Fitting, Group
from ecm.apps.eve.models import Type, CelestialObject
from ecm.apps.corp.models import CorpHangar
class GroupForm(forms.ModelForm):
stationID = forms.ModelChoiceField(queryset=CelestialObject.objects \
.filter(Q(group=15) | Q(type=21645)).order_by('itemName'))
hangarID = forms.ModelChoiceField(queryset=CorpHangar.objects.all(),
required=False)
def clean_stationID(self):
station = self.cleaned_data.get('stationID', None)
if station:
return station.itemID
def clean_hangarID(self):
hangar = self.cleaned_data.get('hangarID', None)
if hangar:
return hangar.hangar_id
class Meta:
model = Group
class GroupAdmin(admin.ModelAdmin):
form = GroupForm
class ItemAdmin(admin.ModelAdmin):
def formfield_for_foreignkey(self, db_field, request, **kwargs):
if db_field.name == 'eve_type':
kwargs['queryset'] = Type.objects.order_by('typeName')
return super(ItemAdmin, self).formfield_for_foreignkey(db_field,
request, **kwargs)
class FittingForm(forms.ModelForm):
class Meta:
model = Fitting
class FittingAdmin(admin.ModelAdmin):
readonly_fields = ('ship_type',)
form = FittingForm
def save_model(self, request, obj, form, change):
eft_data = form.cleaned_data.get('eft_export', None)
ship_type, items = obj.parse_fitting(eft_data)
obj.ship_type = ship_type
admin.ModelAdmin.save_model(self, request, obj, form, change)
obj.save()
admin.site.register(Group, GroupAdmin)
admin.site.register(Item, ItemAdmin)
admin.site.register(Fitting, FittingAdmin)