@@ -79,20 +79,36 @@ def _jinja2_filter_datetimestamp(timestamp, format="%Y-%m-%d %H:%M:%S"):
7979def main_page ():
8080 global messages
8181
82- # Show messages but once.
83- # maybe if the change happened more than a few days ago.. add a class
82+ # Sort by last_changed and add the uuid which is usually the key..
83+ sorted_watches = []
84+ for uuid , watch in datastore .data ['watching' ].items ():
85+ watch ['uuid' ]= uuid
86+ sorted_watches .append (watch )
87+
88+ sorted_watches .sort (key = lambda x : x ['last_changed' ], reverse = True )
8489
85- # Sort by last_changed
86- datastore . data [ 'watching' ]. sort ( key = lambda x : x [ 'last_changed' ], reverse = True )
87- output = render_template ( "watch-overview.html" , watches = datastore . data [ 'watching' ], messages = messages )
90+ output = render_template ( "watch-overview.html" , watches = sorted_watches , messages = messages )
91+
92+ # Show messages but once.
8893 messages = []
8994 return output
9095
9196
97+ @app .route ("/edit" , methods = ['GET' ])
98+ def edit_page ():
99+ global messages
100+
101+ uuid = request .args .get ('uuid' )
102+
103+ output = render_template ("edit.html" , uuid = uuid , watch = datastore .data ['watching' ][uuid ], messages = messages )
104+ return output
105+
106+
92107@app .route ("/favicon.ico" , methods = ['GET' ])
93108def favicon ():
94109 return send_from_directory ("/app/static/images" , filename = "favicon.ico" )
95110
111+
96112@app .route ("/static/<string:group>/<string:filename>" , methods = ['GET' ])
97113def static_content (group , filename ):
98114 try :
@@ -112,38 +128,63 @@ def api_watch_add():
112128 return redirect (url_for ('main_page' ))
113129
114130
131+ @app .route ("/api/delete" , methods = ['GET' ])
132+ def api_delete ():
133+ global messages
134+ uuid = request .args .get ('uuid' )
135+ datastore .delete (uuid )
136+ messages .append ({'class' : 'ok' , 'message' : 'Deleted.' })
137+
138+ return redirect (url_for ('main_page' ))
139+
140+
141+ @app .route ("/api/update" , methods = ['POST' ])
142+ def api_update ():
143+ global messages
144+ import validators
145+
146+ uuid = request .args .get ('uuid' )
147+
148+ url = request .form .get ('url' ).strip ()
149+ tag = request .form .get ('tag' ).strip ()
150+
151+ validators .url (url ) #@todo switch to prop/attr/observer
152+ datastore .data ['watching' ][uuid ].update ({'url' : url ,
153+ 'tag' : tag })
154+
155+ #@todo switch to prop/attr/observer
156+ datastore .sync_to_json ()
157+
158+ messages .append ({'class' : 'ok' , 'message' : 'Updated.' })
159+
160+ return redirect (url_for ('main_page' ))
161+
115162@app .route ("/api/checknow" , methods = ['GET' ])
116163def api_watch_checknow ():
117164 global messages
118165
119166 uuid = request .args .get ('uuid' )
120167
121- # dict would be better, this is a simple safety catch.
122- for watch in datastore .data ['watching' ]:
123- if watch ['uuid' ] == uuid :
124- # @todo cancel if already running?
125- running_update_threads [uuid ] = fetch_site_status .perform_site_check (uuid = uuid ,
126- datastore = datastore )
127- running_update_threads [uuid ].start ()
168+ running_update_threads [uuid ] = fetch_site_status .perform_site_check (uuid = uuid ,
169+ datastore = datastore )
170+ running_update_threads [uuid ].start ()
128171
129172 return redirect (url_for ('main_page' ))
130173
131174
132175@app .route ("/api/recheckall" , methods = ['GET' ])
133176def api_watch_recheckall ():
134-
135177 import fetch_site_status
136178
137179 global running_update_threads
138- i = 0
139- for watch in datastore .data ['watching' ]:
140- i = i + 1
180+ i = 0
181+ for uuid , watch in datastore .data ['watching' ]:
182+ i = i + 1
141183
142- running_update_threads [watch ['uuid' ]] = fetch_site_status .perform_site_check (uuid = watch [ ' uuid' ] ,
184+ running_update_threads [watch ['uuid' ]] = fetch_site_status .perform_site_check (uuid = uuid ,
143185 datastore = datastore )
144186 running_update_threads [watch ['uuid' ]].start ()
145187
146-
147188 return "{} rechecked of {} watches." .format (i , len (datastore .data ['watching' ]))
148189
149190
@@ -152,9 +193,9 @@ def launch_checks():
152193 import fetch_site_status
153194 global running_update_threads
154195
155- for watch in datastore .data ['watching' ]:
196+ for uuid , watch in datastore .data ['watching' ]. items () :
156197 if watch ['last_checked' ] <= time .time () - 3 * 60 * 60 :
157- running_update_threads [watch ['uuid' ]] = fetch_site_status .perform_site_check (uuid = watch [ ' uuid' ] ,
198+ running_update_threads [watch ['uuid' ]] = fetch_site_status .perform_site_check (uuid = uuid ,
158199 datastore = datastore )
159200 running_update_threads [watch ['uuid' ]].start ()
160201
0 commit comments