~joaopinto/apt-portal/pre-modules

« back to all changes in this revision

Viewing changes to common/controllers/packages.py

  • Committer: Joao Pinto
  • Date: 2009-07-06 17:20:36 UTC
  • Revision ID: joao.pinto@getdeb.net-20090706172036-ilg4m4ne7oxn80z3
Include debfactory's apt2sql to simplify installation

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
# -*- coding: utf-8 -*-
2
 
#
3
 
#   (C) Copyright 2009, APT-Portal Developers
4
 
#    https://launchpad.net/~apt-portal-devs
5
 
#
6
 
#    This program is free software: you can redistribute it and/or modify
7
 
#    it under the terms of the GNU General Public License as published by
8
 
#    the Free Software Foundation, either version 3 of the License, or
9
 
#    (at your option) any later version.
10
 
#
11
 
#    This program is distributed in the hope that it will be useful,
12
 
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 
#    GNU General Public License for more details.
15
 
#
16
 
#    You should have received a copy of the GNU General Public License
17
 
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
 
#
19
 
# /packages/ controller
20
 
 
21
2
from cherrypy_mako import *
22
3
from models.package import *
23
4
from models.application import *
24
 
from sqlalchemy import asc
25
 
import time
26
5
import userinfo
27
6
 
28
 
repos_commands_dir = "rep_commands"
29
 
 
30
7
class Packages(object):
31
8
        @cherrypy.expose
32
 
        @cherrypy.tools.expires(secs=0)
33
9
        def index(self, q=None):
34
 
                """
35
 
                The is the main page which presents the list of packages
36
 
                if no key (q) is specified only the packages needing an action
37
 
                are shown.
38
 
                """
39
 
                if not userinfo.is_admin():
40
 
                        raise cherrypy.HTTPError(403)                   
 
10
                show_all = False
41
11
                class Stats():
42
12
                        total = 0
43
13
                        unclassified = 0
44
14
                        unlinked = 0
45
 
                
 
15
                if not userinfo.is_admin():
 
16
                        raise cherrypy.HTTPError(403)   
46
17
                if q:
47
18
                        db_packages = Package.query.filter_by(package=q).order_by(\
48
 
                                asc(Package.package), asc(Package.version)).all()
 
19
                                Package.package, Package.source).all()
 
20
                        show_all = True
49
21
                else:
50
22
                        db_packages = Package.query.order_by( \
51
 
                                asc(Package.package), asc(Package.version)).all()               
 
23
                                        Package.package, Package.source).all()          
52
24
                packages = []
53
25
                last_package_version = None
54
26
                stats = Stats()
55
 
                
56
27
                # Walk the packages list, skip classified and linked
57
28
                for package in db_packages:
58
29
                        package_version = package.package+"-"+package.version
59
30
                        if package_version == last_package_version:
60
31
                                continue
61
 
                        if len(package.lists) == 0: # package is not in a repos
62
 
                                continue
63
32
                        last_package_version = package_version                          
64
33
                        stats.total += 1                        
65
34
                        app = None
72
41
                                ).first()
73
42
                                if not app:
74
43
                                        stats.unlinked += 1
75
 
                        if q \
76
 
                                or not package.install_class \
77
 
                                or (package.install_class=='M' and not app):
78
 
                                        packages.append(package)
 
44
                        if show_all or not (package.install_class and app):
 
45
                                packages.append(package)
79
46
                return serve_template("packages.html", packages = packages\
80
47
                        , stats = stats, q=q
81
48
                )
86
53
                which have a common name and version to the one identified
87
54
                by the package_id parameter """
88
55
                if not userinfo.is_admin():
89
 
                        raise cherrypy.HTTPError(403)                   
 
56
                        raise cherrypy.HTTPError(403)
90
57
                package = Package.query.filter_by(id = package_id).one()
91
58
                package_list = Package.query.filter_by(\
92
59
                        package = package.package, version=package.version).all()
100
67
                """ Returns list of packages for the search box """
101
68
                if not userinfo.is_admin():
102
69
                        raise cherrypy.HTTPError(403)
103
 
                        
104
70
                results = ""
105
71
                last_package = ""
106
72
                package_list = Package.query.filter(Package.package.like('%'+q+'%')).order_by(Package.package)
111
77
                        results += "%s|%d\n" % (package.package, package.id)
112
78
                return results
113
79
                
114
 
        @cherrypy.expose                
115
 
        def remove(self, package_id, list_id, confirm=None):
116
 
                """ Create a command to remove a package from the repository """
117
 
                if not userinfo.is_admin():
118
 
                        raise cherrypy.HTTPError(403)   
119
 
                package_id      = long(package_id)
120
 
                list_id = long(list_id)
121
 
                package = Package.query.filter_by(id = package_id).first()
122
 
                packagelist = PackageList.query.filter_by(id = list_id).first()
123
 
                if not package:
124
 
                        return "Package %d not found" % package_id
125
 
                if not packagelist:
126
 
                        return "List %d not found" % list_id
127
 
                if confirm != "Y":
128
 
                        return serve_template("package_remove.html" \
129
 
                                , package = package, packagelist = packagelist)
130
 
                source_package = package.source or package.package
131
 
                user = userinfo.find_user()
132
 
                action = "%s %s %s %s %s" % (user.email, 'remove' \
133
 
                        , packagelist.suite, source_package, package.version)
134
 
                        
135
 
                time_now = time.strftime("%Y%m%d.%H%M%S", time.localtime())
136
 
                filename = "%s_%s_%s" % (source_package, package.version, time_now)
137
 
                if not os.path.isdir(repos_commands_dir):
138
 
                        return "%s directory is not available, " \
139
 
                                "repository commands are not supported" % repos_commands_dir
140
 
                os.umask(002)
141
 
                f = open(os.path.join(repos_commands_dir, filename), 'w')
142
 
                os.umask(022)
143
 
                f.write(action)
144
 
                f.close()
145
 
                return serve_template("package_remove.html" \
146
 
                        , ticket_name = filename, confirm='Y')
147
 
                        
148
 
        @cherrypy.expose                
149
 
        def copy(self, package_id, source_list_id, target_list):
150
 
                """ Create a command to copy a package to another repository """
151
 
                if not userinfo.is_admin():
152
 
                        raise cherrypy.HTTPError(403)   
153
 
                package_id      = long(package_id)
154
 
                source_list_id = long(source_list_id)
155
 
                package = Package.query.filter_by(id = package_id).first()
156
 
                source_packagelist = PackageList.query.filter_by(id = source_list_id).first()
157
 
                target_packagelist = PackageList.query.filter_by(suite = target_list).first()
158
 
                if not package:
159
 
                        return "Package %d not found" % package_id
160
 
                if not source_packagelist:
161
 
                        return "List %d not found" % list_id
162
 
                if target_list and not target_packagelist:
163
 
                        return "Repository %s not found" % target_list          
164
 
                repository_list = []
165
 
                for plist in PackageList.query.all():
166
 
                        if plist.suite == source_packagelist.suite:
167
 
                                continue
168
 
                        if plist.suite not in repository_list:
169
 
                                repository_list.append(plist.suite)
170
 
                if len(target_list) < 2:
171
 
                        return serve_template("package_copy.html" \
172
 
                                , package = package \
173
 
                                , source_packagelist = source_packagelist \
174
 
                                , target_packagelist = target_packagelist \
175
 
                                , repository_list = repository_list \
176
 
                                , package_id = package_id \
177
 
                                , source_list_id = source_list_id \
178
 
                                , target_list = target_list \
179
 
                                , asking = True \
180
 
                                )
181
 
                source_package = package.source or package.package
182
 
                user = userinfo.find_user()
183
 
                action = "%s %s %s %s %s %s" % (user.email, 'copy' \
184
 
                        , target_packagelist.suite, source_packagelist.suite, source_package, package.version)
185
 
                        
186
 
                time_now = time.strftime("%Y%m%d.%H%M%S", time.localtime())
187
 
                filename = "%s_%s_%s" % (source_package, package.version, time_now)
188
 
                if not os.path.isdir(repos_commands_dir):
189
 
                        return "%s directory is not available, " \
190
 
                                "repository commands are not supported" % repos_commands_dir
191
 
                os.umask(002)                           
192
 
                f = open(os.path.join(repos_commands_dir, filename), 'w')
193
 
                os.umask(022)
194
 
                f.write(action)
195
 
                f.close()
196
 
                return serve_template("package_copy.html" \
197
 
                        , ticket_name = filename \
198
 
                        , asking = False \
199
 
                        )                       
200
80
 
201
81
cherrypy.root.packages = Packages()