]>
git.ipfire.org Git - people/shoehn/ipfire.org.git/blob - webapp/backend/nopaste.py
3 from misc
import Object
6 def create(self
, subject
, content
, expires
=None, account
=None, address
=None):
7 self
._cleanup
_database
()
13 # http://blog.00null.net/easily-generating-random-strings-in-postgresql/
14 res
= self
.db
.get("INSERT INTO nopaste(uuid, subject, content, time_expires, address, uid) \
15 VALUES(random_slug(), %s, %s, (CASE WHEN %s = 0 THEN NULL ELSE NOW() + INTERVAL '%s seconds' END), \
16 %s, %s) RETURNING uuid", subject
, content
, expires
, expires
, address
, uid
)
22 res
= self
.db
.get("SELECT * FROM nopaste WHERE uuid = %s AND \
23 (CASE WHEN time_expires IS NULL THEN TRUE ELSE NOW() < time_expires END)", uuid
)
26 # Convert the content to a byte string
27 res
.content
= "%s" % res
.content
29 # Get the account that uploaded this if available
32 res
.account
= self
.backend
.accounts
.get_by_uid(res
.uid
)
34 # Touch the entry so it won't be deleted when it is still used
35 self
._update
_lastseen
(uuid
)
39 def _update_lastseen(self
, uuid
):
40 self
.db
.execute("UPDATE nopaste SET time_lastseen = NOW() \
41 WHERE uuid = %s", uuid
)
43 def _cleanup_database(self
):
44 # Delete old pastes when they are expired or when they have not been
45 # accessed in a long time.
46 self
.db
.execute("DELETE FROM nopaste WHERE (CASE \
47 WHEN time_expires IS NULL \
48 THEN time_lastseen + INTERVAL '6 months' <= NOW() \
49 ELSE NOW() >= time_expires END)")