]>
git.ipfire.org Git - ipfire.org.git/blob - webapp/backend/nopaste.py
4f877330cf25fd9d3c248b94e0a1139054fe2b02
3 from misc
import Object
6 def create(self
, subject
, content
, type="text", expires
=None, account
=None, address
=None):
7 self
._cleanup
_database
()
13 # Escape any backslashes. PostgreSQL tends to think that they lead octal
14 # values or something that confuses the convertion from text to bytea.
16 content
= content
.replace("\\", "\\\\")
18 # http://blog.00null.net/easily-generating-random-strings-in-postgresql/
19 res
= self
.db
.get("INSERT INTO nopaste(uuid, subject, content, time_expires, address, uid) \
20 VALUES(random_slug(), %s, %s, (CASE WHEN %s = 0 THEN NULL ELSE NOW() + INTERVAL '%s seconds' END), \
21 %s, %s) RETURNING uuid", subject
, content
, expires
, expires
, address
, uid
)
27 res
= self
.db
.get("SELECT * FROM nopaste WHERE uuid = %s AND \
28 (CASE WHEN time_expires IS NULL THEN TRUE ELSE NOW() < time_expires END)", uuid
)
31 # Convert the content to a byte string
32 res
.content
= "%s" % res
.content
34 # Get the account that uploaded this if available
37 res
.account
= self
.backend
.accounts
.get_by_uid(res
.uid
)
39 # Touch the entry so it won't be deleted when it is still used
40 self
._update
_lastseen
(uuid
)
44 def _update_lastseen(self
, uuid
):
45 self
.db
.execute("UPDATE nopaste SET time_lastseen = NOW() \
46 WHERE uuid = %s", uuid
)
48 def _cleanup_database(self
):
49 # Delete old pastes when they are expired or when they have not been
50 # accessed in a long time.
51 self
.db
.execute("DELETE FROM nopaste WHERE (CASE \
52 WHEN time_expires IS NULL \
53 THEN time_lastseen + INTERVAL '6 months' <= NOW() \
54 ELSE NOW() >= time_expires END)")