diff options
author | Jon Bergli Heier <snakebite@jvnv.net> | 2013-08-30 22:05:54 +0200 |
---|---|---|
committer | Jon Bergli Heier <snakebite@jvnv.net> | 2013-08-30 22:05:54 +0200 |
commit | 0323d71d750ca69653a9934d344547a5a4222c48 (patch) | |
tree | 562fc670d5fada29513cf092b0188ea6d74468aa | |
parent | de90f1583f2ca98c9f83f5a1eaf6072dcf5cd09d (diff) |
Added ip and accessed to the 'files' table.
-rw-r--r-- | db.py | 5 | ||||
-rwxr-xr-x | fbin.py | 19 |
2 files changed, 17 insertions, 7 deletions
@@ -31,13 +31,16 @@ class File(Base): filename = Column(String) date = Column(DateTime) user_id = Column(Integer, ForeignKey('users.id'), nullable = True) + ip = Column(String) + accessed = Column(DateTime) - def __init__(self, hash, file_hash, filename, date, user_id = None): + def __init__(self, hash, file_hash, filename, date, user_id = None, ip = None): self.hash = hash self.file_hash = file_hash self.filename = filename self.date = date self.user_id = user_id + self.ip = ip @staticmethod def pretty_size(size): @@ -81,16 +81,23 @@ class Application(object): finally: session.close() - def get_file(self, hash): + def get_file(self, hash, update_accessed = False): session = db.Session() try: - return session.query(db.File).filter(db.File.hash == hash).one() + f = session.query(db.File).filter(db.File.hash == hash).one() + if update_accessed: + f.accessed = datetime.datetime.utcnow() + session.add(f) + session.commit() + # Refresh after field update. + session.refresh(f) + return f except db.NoResultFound: return None finally: session.close() - def add_file(self, path, filename, file_hash, user = None): + def add_file(self, path, filename, file_hash, user = None, ip = None): hash = ''.join(random.choice(base62_alphabet) for x in xrange(5)) new_path = os.path.join(settings.file_directory, hash + os.path.splitext(filename)[1]) os.rename(path, new_path) @@ -99,7 +106,7 @@ class Application(object): session = db.Session() try: - file = db.File(hash, file_hash, filename, datetime.datetime.utcnow(), user.id if user else None) + file = db.File(hash, file_hash, filename, datetime.datetime.utcnow(), user.id if user else None, ip) session.add(file) session.commit() finally: @@ -173,7 +180,7 @@ class Application(object): hash = path[1] if '.' in hash: hash = hash.split('.')[0] - file = self.get_file(hash) + file = self.get_file(hash, True) filename = file.get_path() if filename == None: start_response('404 Not Found', [('Content-Type', 'text/html')]) @@ -266,7 +273,7 @@ class Application(object): hash = f.hash else: # temp.name will be moved to the destination filename - hash = self.add_file(temp.name, filename, file_hash, user) + hash = self.add_file(temp.name, filename, file_hash, user, environ['REMOTE_ADDR']) # This avoids silly "not bound to a Session" errors when trying to use a newly added file object. f = self.get_file(hash) |