Skip to content

Commit 4e72e57

Browse files
committed
Merge pull request #27 from pglauner/master-faster-weblinkback
Master faster weblinkback
2 parents e175c40 + abbd451 commit 4e72e57

File tree

3 files changed

+52
-17
lines changed

3 files changed

+52
-17
lines changed

modules/weblinkback/lib/weblinkback.py

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -157,20 +157,25 @@ def send_pending_linkbacks_notification(linkback_type):
157157
Send notification emails to all linkback moderators for all pending linkbacks
158158
@param linkback_type: of CFG_WEBLINKBACK_LIST_TYPE
159159
"""
160-
pending_linkbacks = get_all_linkbacks(linkback_type=CFG_WEBLINKBACK_TYPE['TRACKBACK'], status=CFG_WEBLINKBACK_STATUS['PENDING'])
160+
pending_linkbacks = get_all_linkbacks(linkback_type=CFG_WEBLINKBACK_TYPE['TRACKBACK'],
161+
status=CFG_WEBLINKBACK_STATUS['PENDING'],
162+
limit=CFG_WEBLINKBACK_MAX_LINKBACKS_IN_EMAIL)
161163

162164
if pending_linkbacks:
163-
pending_count = len(pending_linkbacks)
165+
pending_count = get_all_linkbacks(linkback_type=CFG_WEBLINKBACK_TYPE['TRACKBACK'],
166+
status=CFG_WEBLINKBACK_STATUS['PENDING'],
167+
full_count_only=True)
168+
164169
cutoff_text = ''
165170
if pending_count > CFG_WEBLINKBACK_MAX_LINKBACKS_IN_EMAIL:
166-
cutoff_text = ' (Printing only the first %s requests)' % CFG_WEBLINKBACK_MAX_LINKBACKS_IN_EMAIL
171+
cutoff_text = '(Printing only the first %s requests)' % CFG_WEBLINKBACK_MAX_LINKBACKS_IN_EMAIL
167172

168-
content = """There are %(count)s new %(linkback_type)s requests which you should approve or reject%(cutoff)s:
173+
content = """There are %(count)s new %(linkback_type)s requests which you should approve or reject %(cutoff)s:
169174
""" % {'count': pending_count,
170175
'linkback_type': linkback_type,
171176
'cutoff': cutoff_text}
172177

173-
for pending_linkback in pending_linkbacks[0:CFG_WEBLINKBACK_MAX_LINKBACKS_IN_EMAIL]:
178+
for pending_linkback in pending_linkbacks:
174179
content += """
175180
For %(recordURL)s from %(origin_url)s.
176181
""" % {'recordURL': generate_redirect_url(pending_linkback[2]),
@@ -180,15 +185,14 @@ def send_pending_linkbacks_notification(linkback_type):
180185
send_email(CFG_SITE_ADMIN_EMAIL, email, 'Pending ' + linkback_type + ' requests', content)
181186

182187

183-
def infix_exists_for_url_in_list(url, list_type):
188+
def infix_exists_for_url_in_list(url, url_list):
184189
"""
185190
Check if an infix of a url exists in a list
186191
@param url
187-
@param list_type, of CFG_WEBLINKBACK_LIST_TYPE
192+
@param url_list
188193
@return True, False
189194
"""
190-
urls = get_url_list(list_type)
191-
for current_url in urls:
195+
for current_url in url_list:
192196
if current_url in url:
193197
return True
194198
return False
@@ -257,8 +261,8 @@ def perform_sendtrackback(req, recid, url, title, excerpt, blog_name, blog_id, s
257261
<message>%s</message>
258262
"""
259263

260-
blacklist_match = infix_exists_for_url_in_list(url, CFG_WEBLINKBACK_LIST_TYPE['BLACKLIST'])
261-
whitelist_match = infix_exists_for_url_in_list(url, CFG_WEBLINKBACK_LIST_TYPE['WHITELIST'])
264+
blacklist_match = infix_exists_for_url_in_list(url, get_url_list(CFG_WEBLINKBACK_LIST_TYPE['BLACKLIST']))
265+
whitelist_match = infix_exists_for_url_in_list(url, get_url_list(CFG_WEBLINKBACK_LIST_TYPE['WHITELIST']))
262266

263267
# faulty request, url argument not set
264268
if url in (CFG_WEBLINKBACK_SUBSCRIPTION_DEFAULT_ARGUMENT_NAME, None, ''):
@@ -336,6 +340,7 @@ def delete_linkbacks_on_blacklist():
336340
linkbacks.extend(list(get_all_linkbacks(status=CFG_WEBLINKBACK_STATUS['REJECTED'])))
337341
linkbacks.extend(list(get_all_linkbacks(status=CFG_WEBLINKBACK_STATUS['BROKEN'])))
338342

343+
blacklist = get_url_list(CFG_WEBLINKBACK_LIST_TYPE['BLACKLIST'])
339344
for linkback in linkbacks:
340-
if infix_exists_for_url_in_list(linkback[1], CFG_WEBLINKBACK_LIST_TYPE['BLACKLIST']):
345+
if infix_exists_for_url_in_list(linkback[1], blacklist):
341346
remove_linkback(linkback[0])

modules/weblinkback/lib/weblinkback_dblayer.py

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,21 +27,30 @@
2727
from invenio.textutils import xml_entities_to_utf8
2828

2929

30-
def get_all_linkbacks(recid=None, status=None, order=CFG_WEBLINKBACK_ORDER_BY_INSERTION_TIME["ASC"], linkback_type=None):
30+
def get_all_linkbacks(recid=None,
31+
status=None,
32+
order=CFG_WEBLINKBACK_ORDER_BY_INSERTION_TIME["ASC"],
33+
linkback_type=None,
34+
limit=None,
35+
full_count_only=False):
3136
"""
3237
Get all linkbacks
3338
@param recid: of one record, of all if None
3439
@param status: with a certain status, of all if None
3540
@param order: order by insertion time either "ASC" or "DESC"
3641
@param linkback_type: of a certain type, of all if None
42+
@param limit: maximum result count, all if None
43+
@param full_count_only: return only full result count (does not consider "limit"), result set if False
3744
@return [(linkback_id,
3845
origin_url,
3946
recid,
4047
additional_properties,
4148
linkback_type,
4249
linkback_status,
4350
insert_time)]
44-
in order by id
51+
in order by id, up to "limited" results
52+
53+
OR integer if count_only
4554
"""
4655

4756
header_sql = """SELECT id,
@@ -52,9 +61,10 @@ def get_all_linkbacks(recid=None, status=None, order=CFG_WEBLINKBACK_ORDER_BY_IN
5261
status,
5362
insert_time
5463
FROM lnkENTRY"""
55-
conditions = []
56-
order_sql = "ORDER by id %s" % order
64+
if full_count_only:
65+
header_sql = 'SELECT count(id) FROM lnkENTRY'
5766

67+
conditions = []
5868
params = []
5969

6070
def add_condition(column, value):
@@ -69,7 +79,17 @@ def add_condition(column, value):
6979
add_condition('status', status)
7080
add_condition('type', linkback_type)
7181

72-
return run_sql(header_sql + ' ' + ' '.join(conditions) + ' ' + order_sql, tuple(params))
82+
order_sql = 'ORDER by id %s' % order
83+
84+
limit_sql = ''
85+
if limit:
86+
limit_sql = 'LIMIT %s' % limit
87+
88+
res = run_sql('%s %s %s %s' % (header_sql, ' '.join(conditions), order_sql, limit_sql), tuple(params))
89+
if full_count_only:
90+
return int(res[0][0])
91+
else:
92+
return res
7393

7494

7595
def approve_linkback(linkbackid, user_info):

modules/weblinkback/lib/weblinkback_regression_tests.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,16 @@ def test_get_all_linkbacks2(self):
224224
"""weblinkback - get all linkbacks with a certain status"""
225225
self.assertEqual(9, len(get_all_linkbacks(status=CFG_WEBLINKBACK_STATUS['PENDING'])))
226226
self.assertEqual(0, len(get_all_linkbacks(status=CFG_WEBLINKBACK_STATUS['APPROVED'])))
227+
self.assertEqual(9, get_all_linkbacks(status=CFG_WEBLINKBACK_STATUS['PENDING'], full_count_only=True))
228+
self.assertEqual(0, get_all_linkbacks(status=CFG_WEBLINKBACK_STATUS['APPROVED'], full_count_only=True))
229+
230+
def test_get_limited_count_of_linkbacks(self):
231+
"""weblinkback - get limited count of linkbacks"""
232+
self.assertEqual(5, len(get_all_linkbacks(status=CFG_WEBLINKBACK_STATUS['PENDING'], limit=5)))
233+
self.assertEqual(2, len(get_all_linkbacks(recid=42, status=CFG_WEBLINKBACK_STATUS['PENDING'], limit=2)))
234+
# Ignores limit
235+
self.assertEqual(9, get_all_linkbacks(status=CFG_WEBLINKBACK_STATUS['PENDING'], limit=5, full_count_only=True))
236+
self.assertEqual(5, get_all_linkbacks(recid=42, status=CFG_WEBLINKBACK_STATUS['PENDING'], limit=2, full_count_only=True))
227237

228238
def test_approve_linkback(self):
229239
"""weblinkback - approve linkback"""

0 commit comments

Comments
 (0)