Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 74 lines (56 sloc) 2.373 kb
b3d0569 @lavelle restructured project
authored
1 from settings import HISTORY_PATH
4b60277 @lavelle initial commit
authored
2 from time import time
34953ad @lavelle abstracted json operations to separate module
authored
3 from datetime import date
ea40ebd @lavelle Adamework to share them to Github Gists, which doesn't work yet
authored
4 from pyjson import PyJson
bc7fe0c @lavelle moved grid formatter to external module
authored
5 from gridformat import format_grid
4b60277 @lavelle initial commit
authored
6
7
ee28119 @lavelle further restructuring of dependencies
authored
8 class History:
4b60277 @lavelle initial commit
authored
9 """
10 Defines methods for interacting with the file that stores
11 a history of the files that the user has uploaded to the service
12 """
6e985c3 @lavelle Added and formatted docstrings
authored
13
34953ad @lavelle abstracted json operations to separate module
authored
14 def __init__(self):
5a500a5 @lavelle Actually use history module for reading history
authored
15 self.history_file = PyJson(HISTORY_PATH, base={'history': []})
16 self.history = self.history_file.doc['history']
4b60277 @lavelle initial commit
authored
17
6af56c4 @lavelle Fixed new records being added to history for each character in the name ...
authored
18 def display(self, limit, sort_by, direction, start):
4b60277 @lavelle initial commit
authored
19 """
20 Load the history and print out a number of records to the screen.
d7db5ef @lavelle Added support for uploading folders and multiple files
authored
21
4b60277 @lavelle initial commit
authored
22 Arguments:
d7db5ef @lavelle Added support for uploading folders and multiple files
authored
23 - limit: the number of items to show
24 - direction: the order to sort the files in to, (ascending or descending)
25 - sort_by: the value to sort by (id, path, url or date)
918b7ab @lavelle truncate the history grid when it's smaller than the user's terminal win...
authored
26 - start: the record number to start at
4b60277 @lavelle initial commit
authored
27 """
28
5a500a5 @lavelle Actually use history module for reading history
authored
29 if len(self.history) < 1:
3816072 @lavelle Save filenames to history
authored
30 print 'No records to display'
31 return
32
5a500a5 @lavelle Actually use history module for reading history
authored
33 history = sorted(self.history, key=lambda k: k[sort_by], reverse=direction == 'd')
bc7fe0c @lavelle moved grid formatter to external module
authored
34 grid = [['Id', 'URL', 'Local File', 'Date Created']]
7fdb133 @lavelle added sorting when viewing history
authored
35
918b7ab @lavelle truncate the history grid when it's smaller than the user's terminal win...
authored
36 # iterate through the array of records, parse them and create a new 2d array of the formatted values
6af56c4 @lavelle Fixed new records being added to history for each character in the name ...
authored
37 for record in history[start - 1:]:
4b60277 @lavelle initial commit
authored
38 if limit == 0:
b555c15 @lavelle Added formatting to history view
authored
39 break
4b60277 @lavelle initial commit
authored
40
b443403 @lavelle Renamed variables to adhere to Python conventions
authored
41 id_ = str(record['id'])
4b60277 @lavelle initial commit
authored
42 url = record['url']
43 path = record['path']
b443403 @lavelle Renamed variables to adhere to Python conventions
authored
44 date_ = date.fromtimestamp(record['timestamp']).strftime('%d/%m/%Y')
4b60277 @lavelle initial commit
authored
45
b443403 @lavelle Renamed variables to adhere to Python conventions
authored
46 row = [id_, url, path, date_]
b555c15 @lavelle Added formatting to history view
authored
47
48 grid.append(row)
4b60277 @lavelle initial commit
authored
49
50 limit -= 1
51
204ab2e @lavelle More reusable and less lines
authored
52 print format_grid(grid, divider_positions=[1], truncatable_column=2)
b555c15 @lavelle Added formatting to history view
authored
53
3816072 @lavelle Save filenames to history
authored
54 def add(self, path, filename, url):
4b60277 @lavelle initial commit
authored
55 """
56 Write a new record to the history
6e985c3 @lavelle Added and formatted docstrings
authored
57
4b60277 @lavelle initial commit
authored
58 Arguments:
d7db5ef @lavelle Added support for uploading folders and multiple files
authored
59 - path: the path to the local copy of the file that was uploaded
60 - url: the shortened URl that points to the copy of the file hosted on the users dropbox acount
4b60277 @lavelle initial commit
authored
61 """
6e985c3 @lavelle Added and formatted docstrings
authored
62
5a500a5 @lavelle Actually use history module for reading history
authored
63 id_ = 1 if len(self.history) == 0 else self.history[-1]['id'] + 1
3816072 @lavelle Save filenames to history
authored
64 record = {
b443403 @lavelle Renamed variables to adhere to Python conventions
authored
65 'id': id_,
4b60277 @lavelle initial commit
authored
66 'path': path,
3816072 @lavelle Save filenames to history
authored
67 'filename': filename,
4b60277 @lavelle initial commit
authored
68 'url': url,
69 'timestamp': time()
3816072 @lavelle Save filenames to history
authored
70 }
4b60277 @lavelle initial commit
authored
71
5a500a5 @lavelle Actually use history module for reading history
authored
72 self.history.append(record)
73 self.history_file.save()
Something went wrong with that request. Please try again.