-
Notifications
You must be signed in to change notification settings - Fork 9
/
ProudList.py
153 lines (119 loc) · 4.42 KB
/
ProudList.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# -*- Mode: python; coding: utf-8 -*-
#
# Cherokee Web Site
#
# Authors:
# Alvaro Lopez Ortega <[email protected]>
#
# Copyright (C) 2001-2011 Alvaro Lopez Ortega
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of version 2 of the GNU General Public
# License as published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA.
#
import os
import re
import CTK
import time
import Page
import config
import cPickle
import xmlrpclib
URL_NEW_SUBMIT = '/proud/apply'
CACHE_EXPIRATION = 15 * 60 # 10mins
URL_OPEN = 'http://www.octality.com/api/v2/open/proud'
DOMAIN_CLICK_JS = """
$(".domain").click (function (event) {
var domain = $(this).text();
event.preventDefault();
event.stopPropagation();
window.open ("http://" + domain + "/", '_blank');
});
"""
def domain_cmp (x, y):
if y['page_rank'] == x['page_rank']:
return cmp (x['domain'], y['domain'])
return cmp (y['page_rank'], x['page_rank'])
class DomainList_Widget (CTK.Box):
def __init__ (self):
CTK.Box.__init__ (self, {'class': 'domain_list'})
# Load, filter and sort the domain list
try:
domains = cPickle.load (open (config.PROUD_PICKLE, 'r'))
except (IOError, EOFError) as e: # This exception means a corrupt pickle failed to load
domains = []
domains_clean = filter (lambda d: d['publish'], domains)
domains_clean.sort (domain_cmp)
if not domains_clean: # puke in the napkin, politely
domains_clean = [{'domain':'Sorry: list broken!', 'page_rank':0}]
# Render the domain list
l = CTK.List()
for domain in domains_clean:
l += CTK.Box ({'class': 'domain domain-PR%s'%(domain['page_rank'])}, CTK.RawHTML(domain['domain']))
self += l
self += CTK.RawHTML (js = DOMAIN_CLICK_JS)
#
# Factory and cache
#
latest_widget = None
latest_widget_expiration = None
def DomainList():
global latest_widget
global latest_widget_expiration
if not latest_widget or time.time() > latest_widget_expiration:
latest_widget = DomainList_Widget()
latest_widget_expiration = time.time() + CACHE_EXPIRATION
return latest_widget
class Add_New_Domain (CTK.Box):
P1 = """Submit the form and if your site is running Cherokee it
will be listed. Sites are sorted by popularity and, among sites in
the same league, are listed in alphabetical order."""
REPORT_OK_JS = """
$('#%s').html ($('#%s').val() + " added succesfully.");
"""
REPORT_FAIL_JS = """
$('#%s').html ("Couldn't add " + $('#%s').val());
"""
class Apply:
def __call__ (self):
domain = CTK.post.get_val('new_domain')
xmlrpc = xmlrpclib.ServerProxy (URL_OPEN)
try:
reply = xmlrpc.add_domain (domain)
except xmlrpclib.Fault, err:
return {'ret':'error', 'errors': {'new_domain': err.faultString}}
except Exception, e:
return {'ret':'error', 'errors': {'new_domain': str(e)}}
return CTK.cfg_reply_ajax_ok()
def __init__ (self):
CTK.Box.__init__ (self, {'class': 'add_new_domain'})
# Dialog
field = CTK.TextField ({'name': 'new_domain', 'class': 'noauto'})
add = CTK.SubmitterButton ("Add")
report = CTK.Box()
box = CTK.Box()
box += field
box += add
box += report
submit = CTK.Submitter (URL_NEW_SUBMIT)
submit += box
submit.bind ('submit_success',
self.REPORT_OK_JS%(report.id, field.id) +
field.JS_to_clean())
submit.bind ('submit_fail',
self.REPORT_FAIL_JS%(report.id, field.id))
coll = CTK.CollapsibleEasy (('Add your domain…','Add your domain…'))
coll += CTK.RawHTML ('<p>%s</p>' %(self.P1))
coll += submit
self += coll
CTK.publish ('^%s$'%(URL_NEW_SUBMIT), Add_New_Domain.Apply, method="POST")