root / snf-pithos-backend / pithos / backends / lib / sqlalchemy / public.py @ 8221c89d
History | View | Annotate | Download (4.2 kB)
1 | 2e662088 | Antony Chazapis | # Copyright 2011-2012 GRNET S.A. All rights reserved.
|
---|---|---|---|
2 | 4f917833 | Sofia Papagiannaki | #
|
3 | 4f917833 | Sofia Papagiannaki | # Redistribution and use in source and binary forms, with or
|
4 | 4f917833 | Sofia Papagiannaki | # without modification, are permitted provided that the following
|
5 | 4f917833 | Sofia Papagiannaki | # conditions are met:
|
6 | 4f917833 | Sofia Papagiannaki | #
|
7 | 4f917833 | Sofia Papagiannaki | # 1. Redistributions of source code must retain the above
|
8 | 4f917833 | Sofia Papagiannaki | # copyright notice, this list of conditions and the following
|
9 | 4f917833 | Sofia Papagiannaki | # disclaimer.
|
10 | 4f917833 | Sofia Papagiannaki | #
|
11 | 4f917833 | Sofia Papagiannaki | # 2. Redistributions in binary form must reproduce the above
|
12 | 4f917833 | Sofia Papagiannaki | # copyright notice, this list of conditions and the following
|
13 | 4f917833 | Sofia Papagiannaki | # disclaimer in the documentation and/or other materials
|
14 | 4f917833 | Sofia Papagiannaki | # provided with the distribution.
|
15 | 4f917833 | Sofia Papagiannaki | #
|
16 | 4f917833 | Sofia Papagiannaki | # THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
|
17 | 4f917833 | Sofia Papagiannaki | # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
18 | 4f917833 | Sofia Papagiannaki | # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
19 | 4f917833 | Sofia Papagiannaki | # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
|
20 | 4f917833 | Sofia Papagiannaki | # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
21 | 4f917833 | Sofia Papagiannaki | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
22 | 4f917833 | Sofia Papagiannaki | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
23 | 4f917833 | Sofia Papagiannaki | # USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
24 | 4f917833 | Sofia Papagiannaki | # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
25 | 4f917833 | Sofia Papagiannaki | # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
26 | 4f917833 | Sofia Papagiannaki | # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
27 | 4f917833 | Sofia Papagiannaki | # POSSIBILITY OF SUCH DAMAGE.
|
28 | 4f917833 | Sofia Papagiannaki | #
|
29 | 4f917833 | Sofia Papagiannaki | # The views and conclusions contained in the software and
|
30 | 4f917833 | Sofia Papagiannaki | # documentation are those of the authors and should not be
|
31 | 4f917833 | Sofia Papagiannaki | # interpreted as representing official policies, either expressed
|
32 | 4f917833 | Sofia Papagiannaki | # or implied, of GRNET S.A.
|
33 | 4f917833 | Sofia Papagiannaki | |
34 | 4f917833 | Sofia Papagiannaki | from dbworker import DBWorker |
35 | af75e8a5 | Antony Chazapis | from sqlalchemy import Table, Column, String, Integer, Boolean, MetaData |
36 | af75e8a5 | Antony Chazapis | from sqlalchemy.sql import and_, select |
37 | bb4eafc6 | Antony Chazapis | from sqlalchemy.schema import Index |
38 | bb4eafc6 | Antony Chazapis | |
39 | 4f917833 | Sofia Papagiannaki | |
40 | 4f917833 | Sofia Papagiannaki | class Public(DBWorker): |
41 | 4f917833 | Sofia Papagiannaki | """Paths can be marked as public."""
|
42 | 4f917833 | Sofia Papagiannaki | |
43 | 4f917833 | Sofia Papagiannaki | def __init__(self, **params): |
44 | 4f917833 | Sofia Papagiannaki | DBWorker.__init__(self, **params)
|
45 | 4f917833 | Sofia Papagiannaki | metadata = MetaData() |
46 | 4f917833 | Sofia Papagiannaki | columns=[] |
47 | bb4eafc6 | Antony Chazapis | columns.append(Column('public_id', Integer, primary_key=True)) |
48 | af75e8a5 | Antony Chazapis | columns.append(Column('path', String(2048), nullable=False)) |
49 | af75e8a5 | Antony Chazapis | columns.append(Column('active', Boolean, nullable=False, default=True)) |
50 | bb4eafc6 | Antony Chazapis | self.public = Table('public', metadata, *columns, mysql_engine='InnoDB', sqlite_autoincrement=True) |
51 | bb4eafc6 | Antony Chazapis | # place an index on path
|
52 | 9e9615fe | Sofia Papagiannaki | Index('idx_public_path', self.public.c.path, unique=True) |
53 | 4f917833 | Sofia Papagiannaki | metadata.create_all(self.engine)
|
54 | 4f917833 | Sofia Papagiannaki | |
55 | 4f917833 | Sofia Papagiannaki | def public_set(self, path): |
56 | af75e8a5 | Antony Chazapis | s = select([self.public.c.public_id])
|
57 | a68d51a4 | Sofia Papagiannaki | s = s.where(self.public.c.path == path)
|
58 | 2ce96391 | Sofia Papagiannaki | r = self.conn.execute(s)
|
59 | af75e8a5 | Antony Chazapis | row = r.fetchone() |
60 | 4f917833 | Sofia Papagiannaki | r.close() |
61 | af75e8a5 | Antony Chazapis | if row:
|
62 | af75e8a5 | Antony Chazapis | s = self.public.update().where(self.public.c.public_id == row[0]) |
63 | af75e8a5 | Antony Chazapis | s = s.values(active=True)
|
64 | af75e8a5 | Antony Chazapis | else:
|
65 | 2ce96391 | Sofia Papagiannaki | s = self.public.insert()
|
66 | af75e8a5 | Antony Chazapis | s = s.values(path=path, active=True)
|
67 | af75e8a5 | Antony Chazapis | r = self.conn.execute(s)
|
68 | af75e8a5 | Antony Chazapis | r.close() |
69 | 4f917833 | Sofia Papagiannaki | |
70 | 4f917833 | Sofia Papagiannaki | def public_unset(self, path): |
71 | af75e8a5 | Antony Chazapis | s = self.public.update()
|
72 | af75e8a5 | Antony Chazapis | s = s.where(self.public.c.path == path)
|
73 | af75e8a5 | Antony Chazapis | s = s.values(active=False)
|
74 | 4f917833 | Sofia Papagiannaki | r = self.conn.execute(s)
|
75 | 4f917833 | Sofia Papagiannaki | r.close() |
76 | 8221c89d | Sofia Papagiannaki | |
77 | 8221c89d | Sofia Papagiannaki | def public_unset_bulk(self, paths): |
78 | 8221c89d | Sofia Papagiannaki | s = self.public.update()
|
79 | 8221c89d | Sofia Papagiannaki | s = s.where(self.public.c.path.in_(paths))
|
80 | 8221c89d | Sofia Papagiannaki | s = s.values(active=False)
|
81 | 8221c89d | Sofia Papagiannaki | r = self.conn.execute(s)
|
82 | 8221c89d | Sofia Papagiannaki | r.close() |
83 | 4f917833 | Sofia Papagiannaki | |
84 | bb4eafc6 | Antony Chazapis | def public_get(self, path): |
85 | af75e8a5 | Antony Chazapis | s = select([self.public.c.public_id])
|
86 | af75e8a5 | Antony Chazapis | s = s.where(and_(self.public.c.path == path,
|
87 | 4732ed31 | Antony Chazapis | self.public.c.active == True)) |
88 | bb4eafc6 | Antony Chazapis | r = self.conn.execute(s)
|
89 | bb4eafc6 | Antony Chazapis | row = r.fetchone() |
90 | bb4eafc6 | Antony Chazapis | r.close() |
91 | bb4eafc6 | Antony Chazapis | if row:
|
92 | bb4eafc6 | Antony Chazapis | return row[0] |
93 | bb4eafc6 | Antony Chazapis | return None |
94 | bb4eafc6 | Antony Chazapis | |
95 | 15a96c3e | Antony Chazapis | def public_list(self, prefix): |
96 | 15a96c3e | Antony Chazapis | s = select([self.public.c.path, self.public.c.public_id]) |
97 | 7baa2773 | Antony Chazapis | s = s.where(self.public.c.path.like(self.escape_like(prefix) + '%', escape='\\')) |
98 | 15a96c3e | Antony Chazapis | s = s.where(self.public.c.active == True) |
99 | 15a96c3e | Antony Chazapis | r = self.conn.execute(s)
|
100 | 15a96c3e | Antony Chazapis | rows = r.fetchall() |
101 | 15a96c3e | Antony Chazapis | r.close() |
102 | 15a96c3e | Antony Chazapis | return rows
|
103 | 15a96c3e | Antony Chazapis | |
104 | bb4eafc6 | Antony Chazapis | def public_path(self, public): |
105 | af75e8a5 | Antony Chazapis | s = select([self.public.c.path])
|
106 | af75e8a5 | Antony Chazapis | s = s.where(and_(self.public.c.public_id == public,
|
107 | 4732ed31 | Antony Chazapis | self.public.c.active == True)) |
108 | 4f917833 | Sofia Papagiannaki | r = self.conn.execute(s)
|
109 | bb4eafc6 | Antony Chazapis | row = r.fetchone() |
110 | 4f917833 | Sofia Papagiannaki | r.close() |
111 | bb4eafc6 | Antony Chazapis | if row:
|
112 | bb4eafc6 | Antony Chazapis | return row[0] |
113 | bb4eafc6 | Antony Chazapis | return None |