summaryrefslogtreecommitdiffstats
path: root/store/admin.py
blob: ca915063fd20f777fd37399026360969f31137c8 (plain)
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import os

from django import forms
from django.conf import settings
from django.conf.urls import patterns
from django.contrib import admin
from django.core.exceptions import PermissionDenied
from django.shortcuts import redirect, get_object_or_404
from django.utils.translation import ugettext as _
from django.utils.translation import ugettext_lazy

from store.models import *
from utilities import parseAndValidatePackageMetadata
from utilities import iconPath


class CategoryAdmin(admin.ModelAdmin):
    list_display = ('name', 'move')
    ordering = ('rank',)

    def save_model(self, request, obj, form, change):
        obj.save()

    def name(self, obj):
        # just to forbid sorting by name
        return obj.name
    name.short_description = ugettext_lazy('Item caption')

    def move(sefl, obj):
        """
        Returns html with links to move_up and move_down views.
        """
        button = u'<a href="%s"><img src="%simg/admin/arrow-%s.gif" /> %s</a>'
        prefix = settings.STATIC_URL

        link = '%d/move_up/' % obj.pk
        html = button % (link, prefix, 'up', _('up')) + " | "
        link = '%d/move_down/' % obj.pk
        html += button % (link, prefix, 'down', _('down'))
        return html
    move.allow_tags = True
    move.short_description = ugettext_lazy('Move')

    def get_urls(self):
        admin_view = self.admin_site.admin_view
        urls = patterns('',
            (r'^(?P<item_pk>\d+)/move_up/$', admin_view(self.move_up)),
            (r'^(?P<item_pk>\d+)/move_down/$', admin_view(self.move_down)),
        )
        return urls + super(CategoryAdmin, self).get_urls()

    def move_up(self, request, item_pk):
        """
        Decrease rank (change ordering) of the menu item with
        id=``item_pk``.
        """
        if self.has_change_permission(request):
            item = get_object_or_404(Category, pk=item_pk)
            item.decrease_rank()
        else:
            raise PermissionDenied
        return redirect('admin:appstore_category_changelist')

    def move_down(self, request, item_pk):
        """
        Increase rank (change ordering) of the menu item with
        id=``item_pk``.
        """
        if self.has_change_permission(request):
            item = get_object_or_404(Category, pk=item_pk)
            item.increase_rank()
        else:
            raise PermissionDenied
        return redirect('admin:appstore_category_changelist')


class AppAdminForm(forms.ModelForm):
    class Meta:
        exclude = ["id", "name"]

    appId = ""
    name = ""

    def clean(self):
        cleaned_data = super(AppAdminForm, self).clean()
        file = cleaned_data.get('file');

        # validate package
        pkgdata = None;
        try:
            pkgdata = parseAndValidatePackageMetadata(file)
        except Exception as error:
            raise forms.ValidationError(_('Validation error: %s' % str(error)))

        self.appId = pkgdata['info']['id'];
        self.name = pkgdata['storeName'];

        try:
            a = App.objects.get(name__exact = self.name)
            if a.id != pkgdata['info']['id']:
                raise forms.ValidationError(_('Validation error: the same package name (%s) is already used for application %s' % (self.name, a.id)))
        except App.DoesNotExist:
            pass

        # check if this really is an update
        if hasattr(self, 'instance') and self.instance.id:
            if self.appId != self.instance.id:
                raise forms.ValidationError(_('Validation error: an update cannot change the application id, tried to change from %s to %s' % (self.instance.id, self.appId)))
        else:
            try:
                if App.objects.get(id__exact = self.appId):
                    raise forms.ValidationError(_('Validation error: another application with id %s already exists' % str(self.appId)))
            except App.DoesNotExist:
                pass

        # write icon into file to serve statically
        try:
            if not os.path.exists(iconPath()):
                os.makedirs(iconPath())
            tempicon = open(iconPath(self.appId), 'w')
            tempicon.write(pkgdata['icon'])
            tempicon.flush()
            tempicon.close()

        except IOError as error:
            raise forms.ValidationError(_('Validation error: could not write icon file to media directory: %s' % str(error)))

        return cleaned_data

    def save(self, commit=False):
        m = super(AppAdminForm, self).save(commit);
        m.id = self.appId
        m.name = self.name
        return m


class AppAdmin(admin.ModelAdmin):
    form = AppAdminForm
    list_display = ('name',)

    def save_model(self, request, obj, form, change):
        obj.save()


admin.site.register(Category, CategoryAdmin)
admin.site.register(Vendor)
admin.site.register(App, AppAdmin)