Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/sugar_network_server/resources/context.py
blob: 0cfa53461c5e261286a94b483f6cc3779fc69cb5 (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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# Copyright (C) 2012, Aleksey Lim
#
# This program 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.
#
# This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.

# pylint: disable-msg=E1101,E0102

import os
import json
from cStringIO import StringIO
from os.path import exists, join
from gettext import gettext as _

import active_document as ad
import restful_document as rd
from sweets_recipe import Bundle
enforce = ad.util.enforce

from sugar_network_server import env
from sugar_network_server.resources.resource import Resource, Vote


_ASLO_DOWNLOAD_URL = 'http://download.sugarlabs.org/activities'
_ASLO_ACTIVITIES_PATH = '/upload/activities'


class Context(Resource):

    @ad.active_property(prefix='T', typecast=[env.CONTEXT_TYPES])
    def type(self, value):
        return value

    @ad.active_property(slot=1, prefix='N', full_text=True,
            permissions=ad.ACCESS_READ, default='')
    def name(self, value):
        return value

    @ad.active_property(prefix='M',
            full_text=True, default=[], typecast=[])
    def implement(self, value):
        return value

    @implement.setter
    def implement(self, value):
        return value

    @ad.active_property(slot=2, prefix='S', full_text=True)
    def title(self, value):
        return value

    @title.setter
    def title(self, value):
        return value

    @ad.active_property(slot=3, prefix='R', full_text=True)
    def summary(self, value):
        return value

    @summary.setter
    def summary(self, value):
        return value

    @ad.active_property(prefix='D', full_text=True)
    def description(self, value):
        return value

    @description.setter
    def description(self, value):
        return value

    @ad.active_property(slot=4, prefix='H', default='', full_text=True)
    def homepage(self, value):
        return value

    @homepage.setter
    def homepage(self, value):
        return value

    @ad.active_property(prefix='Y', default=[], typecast=[])
    def mime_types(self, value):
        return value

    @ad.active_property(ad.BlobProperty)
    def icon(self, value):
        return value

    @ad.active_property(ad.BlobProperty)
    def artifact_icon(self, value):
        return value

    @ad.active_property(ad.BlobProperty,
            permissions=ad.ACCESS_READ, mime_type='application/json')
    def feed(self, value):
        return value

    @ad.active_property(ad.CounterProperty, slot=5)
    def rating(self, value):
        return value

    @ad.active_property(Vote, counter='rating')
    def vote(self, value):
        return value

    @vote.setter
    def vote(self, value):
        return value

    @rd.restful_method(method='POST', cmd='release-from-aslo')
    def _release_from_aslo(self, url, date, notes, licenses):
        from sugar_network_server import resources

        enforce(rd.principal.user in self.author, rd.Forbidden,
                _('Operation is permitted only for authors'))
        enforce(url.startswith(_ASLO_DOWNLOAD_URL))

        path = url[len(_ASLO_DOWNLOAD_URL):].strip('/').split('/')
        enforce(len(path) == 2 and path[-1].endswith('.xo'),
                _('Incorrect activities.sugarlabs.org path'))
        path = join(_ASLO_ACTIVITIES_PATH, *path)
        enforce(exists(path), _('Cannot find ASLO bundle at %s'), path)

        bundle = Bundle(path)
        spec = bundle.get_spec()
        # TODO
        #spec.lint()

        feed_data = self.get_blob('feed')
        if feed_data is None:
            feed = {}
        else:
            feed = json.load(feed_data)

        impl = resources.Implementation(
                author=[rd.principal.user],
                context=self.guid,
                license=licenses,
                version=spec.version,
                date=date,
                stability=spec.stability,
                notes=notes)
        impl.post()

        if not feed or spec.version >= max(feed.keys()):
            self['type'] = spec.types
            self['title'] = spec.name
            self['summary'] = spec.summary
            self['description'] = spec.description
            self['homepage'] = spec.homepage
            self['tags'] = spec.tags
            self['mime_types'] = spec.mime_types
            self.post()

            icon_path = join(bundle.extract, spec.icon)
            self.set_blob('artifact_icon', bundle.extractfile(icon_path))

        feed.setdefault(spec.version, {})
        feed[spec.version]['*-*'] = {
                'guid': impl.guid,
                'stability': spec.stability,
                'commands': spec.commands,
                'requires': spec.requires,
                'url': url,
                'extract': bundle.extract,
                'size': os.stat(path).st_size,
                }
        self.set_blob('feed', StringIO(json.dumps(feed)), raw=True)