Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/sugar_network/model/__init__.py
blob: f7be2616796bb773d4e698b0bccd5685aa8e757a (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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
# Copyright (C) 2012-2014 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/>.

import os
import gettext
import logging
import mimetypes
from os.path import join

import xapian

from sugar_network import toolkit, db
from sugar_network.db import blobs
from sugar_network.model.routes import FrontRoutes
from sugar_network.toolkit.spec import parse_version, parse_requires
from sugar_network.toolkit.spec import EMPTY_LICENSE
from sugar_network.toolkit.coroutine import this
from sugar_network.toolkit.bundle import Bundle
from sugar_network.toolkit.router import ACL
from sugar_network.toolkit import i18n, http, svg_to_png, exception, enforce


CONTEXT_TYPES = [
        'activity', 'group', 'package', 'book',
        ]

POST_TYPES = [
        'review',        # Review the Context
        'object',        # Object generated by Context application
        'question',      # Q&A request
        'answer',        # Q&A response
        'issue',         # Propblem with the Context
        'announce',      # General announcement
        'notification',  # Auto-generated Post for updates within the Context
        'feedback',      # Review parent Post
        'post',          # General purpose dependent Post
        ]

STABILITIES = [
        'insecure', 'buggy', 'developer', 'testing', 'stable',
        ]

RESOURCES = (
        'sugar_network.model.context',
        'sugar_network.model.post',
        'sugar_network.model.report',
        'sugar_network.model.user',
        )

_logger = logging.getLogger('model')


class Rating(db.List):

    def __init__(self, **kwargs):
        db.List.__init__(self, db.Numeric(), default=[0, 0], **kwargs)

    def slotting(self, value):
        rating = float(value[1]) / value[0] if value[0] else 0
        return xapian.sortable_serialise(rating)


class Release(object):

    def typecast(self, release):
        if this.resource.exists and \
                'activity' not in this.resource['type'] and \
                'book' not in this.resource['type']:
            return release
        if not isinstance(release, dict):
            __, release = load_bundle(
                    blobs.post(release, this.request.content_type),
                    context=this.request.guid)
        return release['spec']['*-*']['bundle'], release

    def teardown(self, release):
        if this.resource.exists and \
                'activity' not in this.resource['type'] and \
                'book' not in this.resource['type']:
            return
        for spec in release['spec'].values():
            blobs.delete(spec['bundle'])

    def encode(self, value):
        return []


def generate_node_stats(volume):

    def calc_rating(**kwargs):
        rating = [0, 0]
        alldocs, __ = volume['post'].find(**kwargs)
        for post in alldocs:
            if post['vote']:
                rating[0] += 1
                rating[1] += post['vote']
        return rating

    alldocs, __ = volume['context'].find()
    for context in alldocs:
        rating = calc_rating(type='review', context=context.guid)
        volume['context'].update(context.guid, {'rating': rating})

    alldocs, __ = volume['post'].find(topic='')
    for topic in alldocs:
        rating = calc_rating(type='feedback', topic=topic.guid)
        volume['post'].update(topic.guid, {'rating': rating})


def populate_context_images(props, svg):
    if 'guid' in props:
        from sugar_network.toolkit.sugar import color_svg
        svg = color_svg(svg, props['guid'])
    props['artifact_icon'] = blobs.post(svg, 'image/svg+xml').digest
    props['icon'] = blobs.post(svg_to_png(svg, 55, 55), 'image/png').digest
    props['logo'] = blobs.post(svg_to_png(svg, 140, 140), 'image/png').digest


def load_bundle(blob, context=None, initial=False, extra_deps=None):
    contexts = this.volume['context']
    context_type = None
    context_meta = None
    release_notes = None
    release = {}
    version = None

    try:
        bundle = Bundle(blob.path, mime_type='application/zip')
    except Exception:
        context_type = 'book'
        if not context:
            context = this.request['context']
        version = this.request['version']
        if 'license' in this.request:
            release['license'] = this.request['license']
            if isinstance(release['license'], basestring):
                release['license'] = [release['license']]
        release['spec'] = {'*-*': {
            'bundle': blob.digest,
            }}
    else:
        context_type = 'activity'
        unpack_size = 0

        with bundle:
            changelog = join(bundle.rootdir, 'CHANGELOG')
            for arcname in bundle.get_names():
                if changelog and arcname == changelog:
                    with bundle.extractfile(changelog) as f:
                        release_notes = f.read()
                    changelog = None
                unpack_size += bundle.getmember(arcname).size
            spec = bundle.get_spec()
            context_meta = _load_context_metadata(bundle, spec)

        if not context:
            context = spec['context']
        else:
            enforce(context == spec['context'],
                    http.BadRequest, 'Wrong context')
        if extra_deps:
            spec.requires.update(parse_requires(extra_deps))

        version = spec['version']
        release['stability'] = spec['stability']
        if spec['license'] is not EMPTY_LICENSE:
            release['license'] = spec['license']
        release['commands'] = spec.commands
        release['requires'] = spec.requires
        release['spec'] = {'*-*': {
            'bundle': blob.digest,
            }}
        release['unpack_size'] = unpack_size
        blob['content-type'] = 'application/vnd.olpc-sugar'

    enforce(context, http.BadRequest, 'Context is not specified')
    enforce(version, http.BadRequest, 'Version is not specified')
    release['version'] = parse_version(version)
    if initial and not contexts.exists(context):
        enforce(context_meta, http.BadRequest, 'No way to initate context')
        context_meta['guid'] = context
        context_meta['type'] = [context_type]
        this.call(method='POST', path=['context'], content=context_meta)
    else:
        enforce(context_type in contexts[context]['type'],
                http.BadRequest, 'Inappropriate bundle type')
    context_doc = contexts[context]

    if 'license' not in release:
        releases = context_doc['releases'].values()
        enforce(releases, http.BadRequest, 'License is not specified')
        recent = max(releases, key=lambda x: x.get('value', {}).get('release'))
        enforce(recent, http.BadRequest, 'License is not specified')
        release['license'] = recent['value']['license']

    _logger.debug('Load %r release: %r', context, release)

    if this.request.principal in context_doc['author']:
        diff = context_doc.patch(context_meta)
        if diff:
            this.call(method='PUT', path=['context', context], content=diff)
            context_doc.props.update(diff)
        # TRANS: Release notes title
        title = i18n._('%(name)s %(version)s release')
    else:
        # TRANS: 3rd party release notes title
        title = i18n._('%(name)s %(version)s third-party release')
    release['announce'] = this.call(method='POST', path=['post'],
            content={
                'context': context,
                'type': 'notification',
                'title': i18n.encode(title,
                    name=context_doc['title'],
                    version=version,
                    ),
                'message': release_notes or '',
                },
            content_type='application/json')

    blob['content-disposition'] = 'attachment; filename="%s-%s%s"' % (
            ''.join(i18n.decode(context_doc['title']).split()),
            version, mimetypes.guess_extension(blob.get('content-type')) or '',
            )
    blobs.update(blob.digest, blob)

    return context, release


def _load_context_metadata(bundle, spec):
    result = {}
    for prop in ('homepage', 'mime_types'):
        if spec[prop]:
            result[prop] = spec[prop]
    result['guid'] = spec['context']

    try:
        icon_file = bundle.extractfile(join(bundle.rootdir, spec['icon']))
        populate_context_images(result, icon_file.read())
        icon_file.close()
    except Exception:
        exception(_logger, 'Failed to load icon')

    msgids = {}
    for prop, confname in [
            ('title', 'name'),
            ('summary', 'summary'),
            ('description', 'description'),
            ]:
        if spec[confname]:
            msgids[prop] = spec[confname]
            result[prop] = {'en': spec[confname]}
    with toolkit.mkdtemp() as tmpdir:
        for path in bundle.get_names():
            if not path.endswith('.mo'):
                continue
            mo_path = path.strip(os.sep).split(os.sep)
            if len(mo_path) != 5 or mo_path[1] != 'locale':
                continue
            lang = mo_path[2]
            bundle.extract(path, tmpdir)
            try:
                translation = gettext.translation(spec['context'],
                        join(tmpdir, *mo_path[:2]), [lang])
                for prop, value in msgids.items():
                    msgstr = translation.gettext(value).decode('utf8')
                    if lang == 'en' or msgstr != value:
                        result[prop][lang] = msgstr
            except Exception:
                exception(_logger, 'Gettext failed to read %r', mo_path[-1])

    return result