1
+ import flask_login
2
+ import os
3
+ import time
4
+ import timeago
5
+
6
+ from flask import Blueprint , request , make_response , render_template , redirect , url_for , flash , session
7
+ from flask_login import current_user
8
+ from flask_paginate import Pagination , get_page_parameter
9
+
10
+ from changedetectionio import forms
11
+ from changedetectionio .store import ChangeDetectionStore
12
+ from changedetectionio .auth_decorator import login_optionally_required
13
+ from changedetectionio .strtobool import strtobool
14
+
15
+ def construct_blueprint (datastore : ChangeDetectionStore , update_q , queuedWatchMetaData ):
16
+ watchlist_blueprint = Blueprint ('watchlist' , __name__ , template_folder = "templates" )
17
+
18
+ @watchlist_blueprint .route ("/" , methods = ['GET' ])
19
+ @login_optionally_required
20
+ def index ():
21
+ active_tag_req = request .args .get ('tag' , '' ).lower ().strip ()
22
+ active_tag_uuid = active_tag = None
23
+
24
+ # Be sure limit_tag is a uuid
25
+ if active_tag_req :
26
+ for uuid , tag in datastore .data ['settings' ]['application' ].get ('tags' , {}).items ():
27
+ if active_tag_req == tag .get ('title' , '' ).lower ().strip () or active_tag_req == uuid :
28
+ active_tag = tag
29
+ active_tag_uuid = uuid
30
+ break
31
+
32
+ # Redirect for the old rss path which used the /?rss=true
33
+ if request .args .get ('rss' ):
34
+ return redirect (url_for ('rss.feed' , tag = active_tag_uuid ))
35
+
36
+ op = request .args .get ('op' )
37
+ if op :
38
+ uuid = request .args .get ('uuid' )
39
+ if op == 'pause' :
40
+ datastore .data ['watching' ][uuid ].toggle_pause ()
41
+ elif op == 'mute' :
42
+ datastore .data ['watching' ][uuid ].toggle_mute ()
43
+
44
+ datastore .needs_write = True
45
+ return redirect (url_for ('watchlist.index' , tag = active_tag_uuid ))
46
+
47
+ # Sort by last_changed and add the uuid which is usually the key..
48
+ sorted_watches = []
49
+ with_errors = request .args .get ('with_errors' ) == "1"
50
+ errored_count = 0
51
+ search_q = request .args .get ('q' ).strip ().lower () if request .args .get ('q' ) else False
52
+ for uuid , watch in datastore .data ['watching' ].items ():
53
+ if with_errors and not watch .get ('last_error' ):
54
+ continue
55
+
56
+ if active_tag_uuid and not active_tag_uuid in watch ['tags' ]:
57
+ continue
58
+ if watch .get ('last_error' ):
59
+ errored_count += 1
60
+
61
+ if search_q :
62
+ if (watch .get ('title' ) and search_q in watch .get ('title' ).lower ()) or search_q in watch .get ('url' , '' ).lower ():
63
+ sorted_watches .append (watch )
64
+ elif watch .get ('last_error' ) and search_q in watch .get ('last_error' ).lower ():
65
+ sorted_watches .append (watch )
66
+ else :
67
+ sorted_watches .append (watch )
68
+
69
+ form = forms .quickWatchForm (request .form )
70
+ page = request .args .get (get_page_parameter (), type = int , default = 1 )
71
+ total_count = len (sorted_watches )
72
+
73
+ pagination = Pagination (page = page ,
74
+ total = total_count ,
75
+ per_page = datastore .data ['settings' ]['application' ].get ('pager_size' , 50 ), css_framework = "semantic" )
76
+
77
+ sorted_tags = sorted (datastore .data ['settings' ]['application' ].get ('tags' ).items (), key = lambda x : x [1 ]['title' ])
78
+ output = render_template (
79
+ "watch-overview.html" ,
80
+ # Don't link to hosting when we're on the hosting environment
81
+ active_tag = active_tag ,
82
+ active_tag_uuid = active_tag_uuid ,
83
+ app_rss_token = datastore .data ['settings' ]['application' ].get ('rss_access_token' ),
84
+ datastore = datastore ,
85
+ errored_count = errored_count ,
86
+ form = form ,
87
+ guid = datastore .data ['app_guid' ],
88
+ has_proxies = datastore .proxy_list ,
89
+ has_unviewed = datastore .has_unviewed ,
90
+ hosted_sticky = os .getenv ("SALTED_PASS" , False ) == False ,
91
+ pagination = pagination ,
92
+ queued_uuids = [q_uuid .item ['uuid' ] for q_uuid in update_q .queue ],
93
+ search_q = request .args .get ('q' ,'' ).strip (),
94
+ sort_attribute = request .args .get ('sort' ) if request .args .get ('sort' ) else request .cookies .get ('sort' ),
95
+ sort_order = request .args .get ('order' ) if request .args .get ('order' ) else request .cookies .get ('order' ),
96
+ system_default_fetcher = datastore .data ['settings' ]['application' ].get ('fetch_backend' ),
97
+ tags = sorted_tags ,
98
+ watches = sorted_watches
99
+ )
100
+
101
+ if session .get ('share-link' ):
102
+ del (session ['share-link' ])
103
+
104
+ resp = make_response (output )
105
+
106
+ # The template can run on cookie or url query info
107
+ if request .args .get ('sort' ):
108
+ resp .set_cookie ('sort' , request .args .get ('sort' ))
109
+ if request .args .get ('order' ):
110
+ resp .set_cookie ('order' , request .args .get ('order' ))
111
+
112
+ return resp
113
+
114
+ return watchlist_blueprint
0 commit comments