forked from evanelias/typepadapp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
admin.py
268 lines (216 loc) · 10.6 KB
/
admin.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# Copyright (c) 2009-2010 Six Apart Ltd.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
#
# * Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# * Neither the name of Six Apart Ltd. nor the names of its contributors may
# be used to endorse or promote products derived from this software without
# specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
from django.contrib import admin
from django.contrib import messages
from django.db import transaction
from typepadapp.models.auth import Token
from typepadapp.models.feedsub import Subscription
import random
from string import ascii_letters, digits
from django.conf import settings
from urlparse import urlparse, urlunsplit
from django.core.urlresolvers import reverse
import urllib
from oauth import oauth
import typepad
from pprint import pprint as pp
import logging
log = logging.getLogger(__name__)
from django.contrib.sites.models import Site
def init_typepad():
###
# setup for pushing to typepad
for setting in ('OAUTH_CONSUMER_KEY', 'OAUTH_CONSUMER_SECRET', 'OAUTH_GENERAL_PURPOSE_KEY',
'OAUTH_GENERAL_PURPOSE_SECRET'):
if not hasattr(settings, setting):
raise Exception("Cannot initialize connection to Typepad: %s setting is required" % setting)
###
# setup endpoint
try:
typepad.client.endpoint = settings.BACKEND_URL
except AttributeError:
typepad.client.endpoint = 'https://api.typepad.com'
###
# apply any TYPEPAD_COOKIES declared
try:
typepad.client.cookies.update(settings.TYPEPAD_COOKIES)
except AttributeError:
pass
class SubscriptionAdmin(admin.ModelAdmin):
model = Subscription
list_display = ('name', 'feeds_list', 'filters_list','url_id', 'verified')
search_fields = ['name','feeds','filters','verify_token']
fieldsets = (
(None, {
'fields': ('name', 'feeds', 'filters')
}),
('Advanced options', {
'classes': ('collapse',),
'fields': ('url_id', 'secret', 'verified', 'verify_token')
}),
)
def feeds_list(self, obj):
return ', '.join(obj.feeds.split('\n'))
feeds_list.short_description = 'Feeds'
def filters_list(self, obj):
return ', '.join(obj.filters.split('\n'))
filters_list.short_description = 'Filters'
def save_model(self, request, obj, form, change):
log.info('handle create/change subscription')
if change:
orig_obj = Subscription.objects.get(id = obj.id)
orig_feeds = orig_obj.feeds
log.debug('orig_feeds: %s' % orig_feeds)
orig_filters = orig_obj.filters
log.debug('orig_filters: %s' % orig_filters)
init_typepad()
###
# Setup for OAuth authentication
consumer = oauth.OAuthConsumer(settings.OAUTH_CONSUMER_KEY, settings.OAUTH_CONSUMER_SECRET)
token = oauth.OAuthToken(settings.OAUTH_GENERAL_PURPOSE_KEY, settings.OAUTH_GENERAL_PURPOSE_SECRET)
backend = urlparse(typepad.client.endpoint)
typepad.client.add_credentials(consumer, token, domain=backend[1])
feed_idents = set(obj.feeds.split("\n"))
if len(feed_idents) == 0:
raise Exception("At least one feed URL parameter is required")
filter_list = str(obj.filters).split("\n") or []
current_site = Site.objects.get_current()
domain = current_site.domain
if domain == 'example.com':
raise Exception("Your Django 'sites' have not been configured")
secret=None
#secret = ''.join(random.choice(ascii_letters+digits) for x in xrange(0,20))
#obj.secret = secret
# generate a verification token
verify_token = ''.join(random.choice(ascii_letters+digits) for x in xrange(0,20))
obj.verify_token = verify_token
obj.save()
transaction.commit()
if (not change):
log.info('WILL: create subscription in typepad')
# new object
# save to typepad
try:
callback_path = reverse('typepadapp.views.feedsub.callback', kwargs={'sub_id': str(obj.id)})
callback_url = urlunsplit(('http', domain, callback_path, '', ''))
application = typepad.Application.get_by_id(settings.APPLICATION_ID)
resp = application.create_external_feed_subscription(
callback_url=callback_url,
feed_idents=feed_idents,
filter_rules=filter_list,
secret=secret,
verify_token=verify_token)
except Exception, exc:
resp = None
messages.add_message(request, messages.ERROR, exc)
log.exception(exc)
if resp:
# Meanwhile TypePad hit our callback, so reload the object to
# preserve the new "verified" value.
s = Subscription.objects.get(verify_token=verify_token)
s.url_id = resp.subscription.url_id
s.save()
log.info("Created subscription %s (%s)." % (s.name, s.url_id))
messages.add_message(request, messages.INFO, "Created remote subscription %s (%s)" % (s.name, s.url_id))
else:
# obj.delete()
messages.add_message(request, messages.ERROR, "Subscription failed!")
logging.getLogger(__name__).warning("Subscription failed.")
else:
log.info('WILL: update subscription in typepad')
# collect data for sync to typepad
orig_feeds = set(str(orig_feeds).rstrip().split("\n"))
log.debug("orig feeds: %s" % orig_feeds)
new_feeds = set(str(obj.feeds).rstrip().split("\n"))
log.debug("new feeds: %s" % new_feeds)
try:
typepad.client.batch_request()
sub = typepad.ExternalFeedSubscription.get_by_url_id(obj.url_id)
log.debug(sub)
typepad.client.complete_batch()
# in new_feeds but not in old_feeds -- add it
add_feeds = new_feeds.difference(orig_feeds)
log.info('add_feeds: %s' % ",".join(list(add_feeds)))
if len(add_feeds):
log.info('Adding feeds: %s' % ",".join(list(add_feeds)))
sub.add_feeds(feed_idents=list(add_feeds))
# in orig_feeds but not new_feeds -- remove it
remove_feeds = orig_feeds.difference(new_feeds)
log.info('remove_feeds: %s' % ",".join(list(remove_feeds)))
if len(remove_feeds):
log.info('Removing feeds: %s' % ",".join(list(remove_feeds)))
sub.remove_feeds(feed_idents=list(remove_feeds))
# in new_filters but not in old_filters -- add it
if orig_filters != obj.filters:
log.info('Updating filters: %s' % obj.filters)
sub.update_filters(filter_rules=obj.filters)
callback_path = reverse('typepadapp.views.feedsub.callback', kwargs={'sub_id': str(obj.id)})
callback_url = urlunsplit(('http', domain, callback_path, '', ''))
verify_token = ''.join(random.choice(ascii_letters+digits) for x in xrange(0,20))
obj.verify_token = verify_token
obj.verified = False
obj.save()
transaction.commit()
sub.update_notification_settings(callback_url=callback_url, verify_token=verify_token)
print "Assigned new callback URL: %s" % callback_url
except Exception, exc:
messages.add_message(request, messages.ERROR, exc)
messages.add_message(request, messages.ERROR, "Subscription update failed!")
log.exception(exc)
logging.warning("Subscription failed.")
# django.db.models.signals.pre_delete
def delete_subscription(**kwargs):
log.info('WILL: delete subscription')
obj=kwargs['instance']
url_id = obj.url_id
init_typepad()
if (url_id):
###
# Setup for OAuth authentication
consumer = oauth.OAuthConsumer(settings.OAUTH_CONSUMER_KEY, settings.OAUTH_CONSUMER_SECRET)
token = oauth.OAuthToken(settings.OAUTH_GENERAL_PURPOSE_KEY, settings.OAUTH_GENERAL_PURPOSE_SECRET)
backend = urlparse(typepad.client.endpoint)
typepad.client.add_credentials(consumer, token, domain=backend[1])
typepad.client.batch_request()
try:
subscription = typepad.ExternalFeedSubscription.get_by_url_id(url_id).delete()
log.info("Subscription deleted from Typepad")
except typepad.ExternalFeedSubscription.NotFound:
log.error("Could not find subscription with URL_ID: %s to delete" % url_id)
typepad.client.complete_batch()
admin.site.register(Subscription, SubscriptionAdmin)
admin.site.register(Token)
from django.db.models.signals import pre_delete
#pre_delete.connect(delete_subscription, sender=Subscription)
try:
from typepadapp.models.auth import UserForTypePadUser
except ImportError:
pass
else:
admin.site.register(UserForTypePadUser)