diff options
author | luxagraf <sng@luxagraf.net> | 2014-05-23 11:28:10 -0400 |
---|---|---|
committer | luxagraf <sng@luxagraf.net> | 2014-05-23 11:28:10 -0400 |
commit | 518b2d618bc10f93cfa44a83715593b8358eb9ce (patch) | |
tree | a07993c3ae31bed42f32c0e00788989568790716 /app/links | |
parent | 4bae11bb25a8e3c43118891d17fd8e981ecf8dc6 (diff) |
minor refactor to adoipt pep8 and pyflakes coding styles and clean up
some cruft that's been hangin round for years
Diffstat (limited to 'app/links')
-rw-r--r-- | app/links/admin.py | 28 | ||||
-rw-r--r-- | app/links/detail_urls.py | 8 | ||||
-rw-r--r-- | app/links/models.py | 28 | ||||
-rw-r--r-- | app/links/retriever.py | 104 | ||||
-rw-r--r-- | app/links/sync_links.py | 11 |
5 files changed, 76 insertions, 103 deletions
diff --git a/app/links/admin.py b/app/links/admin.py index 2c73c76..68efe9e 100644 --- a/app/links/admin.py +++ b/app/links/admin.py @@ -1,14 +1,30 @@ from django.contrib import admin - from links.models import Link + class LinkAdmin(admin.ModelAdmin): list_display = ('title', 'rating', 'pub_date', 'status') - search_fields = ['title','description','url'] + search_fields = ['title', 'description', 'url'] list_filter = ['rating', 'status'] fieldsets = ( - (None, {'fields': ('title','url','description','rating')}), - ('Details', {'fields': ('screen_url', 'pub_date', 'link_id', 'tags', 'status'), 'classes': 'collapse'}), + (None, { + 'fields': ( + 'title', + 'url', + 'description', + 'rating' + ) + }), + ('Details', { + 'fields': ( + 'screen_url', + 'pub_date', + 'link_id', + 'tags', + 'status' + ), + 'classes': 'collapse' + }), ) - -admin.site.register(Link, LinkAdmin)
\ No newline at end of file + +admin.site.register(Link, LinkAdmin) diff --git a/app/links/detail_urls.py b/app/links/detail_urls.py index b629f3e..9c76a7f 100644 --- a/app/links/detail_urls.py +++ b/app/links/detail_urls.py @@ -4,11 +4,7 @@ from django.views.generic.detail import DetailView from links.models import Link -detail_dict = { - 'queryset': Link.objects.filter(status__exact=1), -} urlpatterns = patterns('', - (r'^$', RedirectView.as_view(url="/links/1/")), - (r'^(?P<object_id>\d+)/$', DetailView.as_view(model=Link,template_name='details/link.html')), + (r'^$', RedirectView.as_view(url="/links/1/")), + (r'^(?P<object_id>\d+)/$', DetailView.as_view(model=Link, template_name='details/link.html')), ) - diff --git a/app/links/models.py b/app/links/models.py index 4c3e59d..c6b502c 100644 --- a/app/links/models.py +++ b/app/links/models.py @@ -2,7 +2,7 @@ import datetime from django.db import models from django.contrib.sitemaps import Sitemap from django.contrib.syndication.views import Feed -from taggit.managers import TaggableManager +from taggit.managers import TaggableManager RATINGS = ( ('1', "1 Star"), @@ -12,8 +12,7 @@ RATINGS = ( ('5', "5 Stars"), ) -DEBUG = 1 - + class Link(models.Model): link_id = models.CharField(max_length=60, blank=True, null=True) title = models.CharField(max_length=400) @@ -31,29 +30,29 @@ class Link(models.Model): class Meta: ordering = ['-pub_date'] - + def __unicode__(self): return self.title - + def get_absolute_url(self): return self.url - + def get_model_name(self): return 'link' - + def get_previous_published(self): return self.get_previous_by_pub_date(status__exact=1) - + def get_next_published(self): return self.get_next_by_pub_date(status__exact=1) - - + def get_thumbnail_url(self): - return "http://images.luxagraf.net/magnolia_thumbs/%s" %(self.screen_url) + return "http://images.luxagraf.net/magnolia_thumbs/%s" % (self.screen_url) + def comment_period_open(self): return self.enable_comments and datetime.datetime.today() - datetime.timedelta(30) <= self.pub_date - - + + class LinkSitemap(Sitemap): changefreq = "never" priority = 0.4 @@ -63,7 +62,8 @@ class LinkSitemap(Sitemap): def lastmod(self, obj): return obj.pub_date - + + class LatestLinks(Feed): title = "Luxagraf: Links" link = "http://ma.gnolia.com/people/luxagraf/bookmarks" diff --git a/app/links/retriever.py b/app/links/retriever.py index bcbc8fa..9e7aae5 100644 --- a/app/links/retriever.py +++ b/app/links/retriever.py @@ -5,15 +5,33 @@ from django.template.defaultfilters import striptags from django.core.mail import EmailMessage from django.conf import settings - -from utils.strutils import safestr,unquotehtml from links.models import Link -from utils import pinboard +# https://github.com/mgan59/python-pinboard/ +import pinboard + + +def safestr(s): + """ + Safely corerce *anything* to a string. If the object can't be str'd, an + empty string will be returned. + + You can (and I do) use this for really crappy unicode handling, but it's + a bit like killing a mosquito with a bazooka. + """ + if s is None: + return "" + if isinstance(s, unicode): + return s.encode('ascii', 'xmlcharrefreplace') + else: + try: + return str(s) + except: + return "" + def sync_pinboard_links(): """ sync bookmarks from my pinboard account - dependancies: python-pinboard https://github.com/mgan59/python-pinboard/ """ p = pinboard.open(settings.PIN_USER, settings.PIN_PASS) @@ -22,17 +40,17 @@ def sync_pinboard_links(): for link in links: try: #check to see if link exists - row = Link.objects.get(link_id=safestr(link['hash'])) + row = Link.objects.get(link_id=safestr(link['hash'])) except ObjectDoesNotExist: l, created = Link.objects.get_or_create( - title = link['description'], - link_id = safestr(link['hash']), - url = safestr(link['href']), - description = safestr(link['extended']), - rating = "3", - pub_date = datetime.datetime.strptime(link['time'], "%Y-%m-%dT%H:%M:%SZ"), - status = 0 - ) + title=link['description'], + link_id=safestr(link['hash']), + url=safestr(link['href']), + description=safestr(link['extended']), + rating="3", + pub_date=datetime.datetime.strptime(link['time'], "%Y-%m-%dT%H:%M:%SZ"), + status=0 + ) print l.title if created: print l.title @@ -40,72 +58,14 @@ def sync_pinboard_links(): l.tags.add(t) email_link(l) -''' -def sync_delicious_links(*args, **kwargs): - b = delicious.get_all(settings.DELICIOUS_USER, settings.DELICIOUS_PASS) - dupe = False - for post in b: - taglist = [] - try: - row = Link.objects.get(magnolia_id=safestr(post['hash'])) - # If the row exists already, set the dupe flag - dupe = True - except ObjectDoesNotExist: - #f = copy_file(safestr(post.findtext('screenshot')), safestr(info['id'])) - #fake the image since delicious doesn't offer them - local_image_url = "%s/%s.jpg" %(safestr(datetime.datetime.today().strftime("%b").lower()), safestr(post['hash'])) - tags = str(post['tag']).split(" ") - for tag in tags: - taglist.append(tag) - for tag in taglist: - if tag == '2lux': - status = 1 - break - else: - status = 0 - descr = markdown.markdown(unquotehtml(safestr(post['extended'])), safe_mode = False) - l, created = Link.objects.get_or_create( - title = post['description'], - magnolia_id = safestr(post['hash']), - url = safestr(post['href']), - description = descr, - screen_url = local_image_url, - #fake the rating since delicious doesn't offer such things - rating = "3", - pub_date = datetime.datetime.strptime(post['time'], "%Y-%m-%dT%H:%M:%SZ"), - status = status, - enable_comments = True, - tags = ", ".join(t for t in taglist if t != "2lux") - ) - - email_link(l) - if l.status == 1: - pass - #post_to_tumblr(l) - #send_to_deliciousfb(l) - if(dupe): - break - - -''' def email_link(link): """ Sends an imported link to Gmail (never hurts to have backups) """ subject = link.title - body = "%s\n\n%s\n\n\nvisit site:%s\n" %(link.title, link.description, link.url) + body = "%s\n\n%s\n\n\nvisit site:%s\n" % (link.title, link.description, link.url) msg = EmailMessage(subject, striptags(body), 'sng@luxagraf.net', ['luxagraf+links@gmail.com']) msg.send() msg = EmailMessage(subject, striptags(body), 'sng@luxagraf.net', ['sng+links@luxagraf.net']) msg.send() - -''' -def send_to_delicious(link): - del_tags = '' - tags = link.tags.split(',') - for tag in tags: - del_tags += tag.strip().replace(' ','_')+' ' - delicious.add(settings.DELICIOUS_USER, settings.DELICIOUS_PASS, link.url, link.title, tags = del_tags, extended = striptags(link.description), dt =safestr(link.pub_date), replace="no") - -''' diff --git a/app/links/sync_links.py b/app/links/sync_links.py index cd1373d..b61395c 100644 --- a/app/links/sync_links.py +++ b/app/links/sync_links.py @@ -1,12 +1,13 @@ -import sys, os +import sys +import os from os.path import dirname, abspath -PROJECT_ROOT = abspath(dirname(dirname(dirname(__file__))))+'/' +PROJECT_ROOT = abspath(dirname(dirname(dirname(__file__)))) + '/' #PROJECT_ROOT = abspath(dirname(dirname(__file__))) print PROJECT_ROOT sys.path.append(PROJECT_ROOT) -sys.path.append(PROJECT_ROOT+'/app') -sys.path.append(PROJECT_ROOT+'/app/lib') -sys.path.append(PROJECT_ROOT+'/config') +sys.path.append(PROJECT_ROOT + '/app') +sys.path.append(PROJECT_ROOT + '/app/lib') +sys.path.append(PROJECT_ROOT + '/config') sys.path.append('/home/luxagraf/apps/venv/bin/python2.7/') os.environ['DJANGO_SETTINGS_MODULE'] = 'settings.settings' from links import retriever |