summaryrefslogtreecommitdiffstats
path: root/sca-cpp/trunk/hosting/server/apps.py
blob: 9d8a009693cb75488c4dac2fae906ae483573bb7 (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
#  Licensed to the Apache Software Foundation (ASF) under one
#  or more contributor license agreements.  See the NOTICE file
#  distributed with this work for additional information
#  regarding copyright ownership.  The ASF licenses this file
#  to you under the Apache License, Version 2.0 (the
#  "License"); you may not use this file except in compliance
#  with the License.  You may obtain a copy of the License at
#  
#    http://www.apache.org/licenses/LICENSE-2.0
#    
#  Unless required by applicable law or agreed to in writing,
#  software distributed under the License is distributed on an
#  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
#  KIND, either express or implied.  See the License for the
#  specific language governing permissions and limitations
#  under the License.

# App collection implementation
from util import *
from atomutil import *
from sys import debug

# Convert an id to an app id
def appid(id):
    return ("apps", car(id), "app.info")

# Put an app into the apps db
def put(id, app, user, cache, db, dashboard, store, composites, pages, icons):
    debug('apps.py::put::id', id)
    debug('apps.py::put::app', app)

    # Update an app
    eid = entryid(app)
    if car(id) == eid:
        # Check app author
        eapp = cache.get(appid(id))
        if (not isNull(eapp)) and (author(eapp) != user.get(())):
            debug('apps.py::put', 'different author', author(eapp))
            return False

        # Update the app in the apps db
        appentry = mkentry(title(app), car(id), user.get(()), now(), content(app))
        debug('apps.py::put::appentry', appentry)
        cache.put(appid(id), appentry)
        dashboard.put(id, appentry)

        # Create new page and composite if necessary
        if isNull(eapp):
            comp = mkentry(car(id), car(id), user.get(()), now(), ())
            composites.put(id, comp)
            page = mkentry(car(id), car(id), user.get(()), now(), ())
            pages.put(id, page)
            icon = mkentry(car(id), car(id), user.get(()), now(), ())
            icons.put(id, icon)
            return True
        return True

    # Check app author
    eapp = cache.get(appid(id))
    if (not isNull(eapp)) and (author(eapp) != user.get(())):
        debug('apps.py::put', 'different author', author(eapp))
        return False

    # Get app to clone
    capp = cache.get(appid((eid,)))
    if isNull(capp):
        debug('apps.py::put', 'cloned app not found', (eid,))
        return False

    # Clone app
    appentry = mkentry(title(capp), car(id), user.get(()), now(), content(capp))
    debug('apps.py::put::appentry', appentry)
    cache.put(appid(id), appentry)
    composites.put(id, composites.get((eid,)))
    pages.put(id, pages.get((eid,)))
    icons.put(id, icons.get((eid,)))
    dashboard.put(id, appentry)
    return True

# Get an app from the apps db
def get(id, user, cache, db, dashboard, store, composites, pages, icons):
    debug('apps.py::get::id', id)

    # Return the newest apps
    if isNull(id):
        newentries = db.get((("'regex", '("apps" .* "app.info")'), ("'rank", "(regexp_matches(value, '(.*\\(updated )([^\\)]+)(\\).*)'))[2]::timestamp"), ("'limit", 50)))
        entries = tuple(map(lambda v: car(v), () if isNull(newentries) else newentries))
        #def sortkey(e):
        #    return updated((e,))
        #sortedentries = tuple(sorted(entries, key = sortkey, reverse = True))
        newapps = ((("'feed", ("'title", "Apps"), ("'id", 'apps')) + entries),)
        debug('apps.py::get::newapps', newapps)
        return newapps

    # Get the requested app
    app = cache.get(appid(id))
    if isNull(app):
        debug('apps.py::get', 'app not found', id)
        return None

    # Return the app
    debug('apps.py::get::app', app)
    return app

# Delete an app from the apps db
def delete(id, user, cache, db, dashboard, store, composites, pages, icons):
    debug('apps.py::delete::id', id)

    # Get the requested app
    app = cache.get(appid(id))
    if isNull(app):
        debug('apps.py::delete', 'app not found', id)
        return False

    # Check app author
    if author(app) != user.get(()):
        debug('apps.py::delete', 'different author', author(app))
        return False

    # Delete the app, its composite and page
    dashboard.delete(id)
    composites.delete(id)
    pages.delete(id)
    icons.delete(id)
    cache.delete(appid(id))
    return True