diff options
Diffstat (limited to 'apps/links')
-rw-r--r-- | apps/links/__init__.py | 0 | ||||
-rw-r--r-- | apps/links/admin.py | 14 | ||||
-rw-r--r-- | apps/links/detail_urls.py | 13 | ||||
-rw-r--r-- | apps/links/models.py | 81 | ||||
-rw-r--r-- | apps/links/urls.py | 15 | ||||
-rw-r--r-- | apps/links/utils.py | 133 |
6 files changed, 256 insertions, 0 deletions
diff --git a/apps/links/__init__.py b/apps/links/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/apps/links/__init__.py diff --git a/apps/links/admin.py b/apps/links/admin.py new file mode 100644 index 0000000..acd22df --- /dev/null +++ b/apps/links/admin.py @@ -0,0 +1,14 @@ +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'] + list_filter = ['rating', 'status'] + fieldsets = ( + (None, {'fields': ('title','url','description','rating')}), + ('Details', {'fields': ('screen_url', 'pub_date', 'magnolia_id', 'tags', 'status'), 'classes': 'collapse'}), + ) + +admin.site.register(Link, LinkAdmin)
\ No newline at end of file diff --git a/apps/links/detail_urls.py b/apps/links/detail_urls.py new file mode 100644 index 0000000..2b6ec5d --- /dev/null +++ b/apps/links/detail_urls.py @@ -0,0 +1,13 @@ +from django.conf.urls.defaults import * +from django.views.generic.simple import redirect_to +from links.models import Link + + +detail_dict = { + 'queryset': Link.objects.filter(status__exact=1), +} +urlpatterns = patterns('django.views.generic.list_detail', + (r'^$', redirect_to, {'url': '/links/1/'}), + (r'^(?P<object_id>\d+)/$', 'object_detail', dict(detail_dict, template_name='details/link.html')), +) + diff --git a/apps/links/models.py b/apps/links/models.py new file mode 100644 index 0000000..cac4aa2 --- /dev/null +++ b/apps/links/models.py @@ -0,0 +1,81 @@ +import datetime + +from django.db import models +from django.contrib.syndication.feeds import Feed +from django.contrib.sitemaps import Sitemap + +from tagging.fields import TagField +from tagging.models import Tag + + +RATINGS = ( + ('1', "1 Star"), + ('2', "2 Stars"), + ('3', "3 Stars"), + ('4', "4 Stars"), + ('5', "5 Stars"), +) + +DEBUG = 1 + +class Link(models.Model): + title = models.CharField(max_length=400) + magnolia_id = models.CharField(max_length=20, blank=True, null=True) + url = models.CharField(max_length=400) + description = models.TextField(blank=True, null=True) + screen_url = models.CharField(max_length=400, blank=True) + rating = models.CharField(max_length=1, choices=RATINGS) + pub_date = models.DateTimeField() + enable_comments = models.BooleanField(blank=True, null=True) + PUB_STATUS = ( + (0, 'Private'), + (1, 'Public'), + ) + status = models.IntegerField(choices=PUB_STATUS, default=0) + tags = TagField() + + class Meta: + ordering = ['-pub_date'] + + def __unicode__(self): + return self.title + + def get_absolute_url(self): + return "/link/%s/" % (self.id) + + 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_tags(self): + return Tag.objects.get_for_object(self) + + def get_thumbnail_url(self): + 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 + + def items(self): + return Link.objects.filter(status=1) + + def lastmod(self, obj): + return obj.pub_date + +class LatestLinks(Feed): + title = "Luxagraf: Links" + link = "/collections/" + description = "Latest Links posted to luxagraf.net" + description_template = 'feeds/collections_description.html' + + def items(self): + return Link.objects.filter(status__exact=1).order_by('-pub_date')[:10] diff --git a/apps/links/urls.py b/apps/links/urls.py new file mode 100644 index 0000000..5a16d6e --- /dev/null +++ b/apps/links/urls.py @@ -0,0 +1,15 @@ +from django.conf.urls.defaults import * +from django.views.generic import list_detail +from django.views.generic.simple import redirect_to +from links.models import Link + +photos_paged = { + 'queryset': Link.objects.filter(status__exact=1).order_by('-pub_date'), + 'paginate': True, + 'page_url': '/links/%d/', +} + +urlpatterns = patterns('', + (r'^(?P<page>\d+)/$', list_detail.object_list, dict(photos_paged, template_name='archives/links.html')), + (r'^$', redirect_to, {'url': '/links/1/'}), +) diff --git a/apps/links/utils.py b/apps/links/utils.py new file mode 100644 index 0000000..a575f4f --- /dev/null +++ b/apps/links/utils.py @@ -0,0 +1,133 @@ +import time, datetime + +from django.core.exceptions import ObjectDoesNotExist +from django.template.defaultfilters import slugify,striptags +from django.core.mail import EmailMessage +from django.utils.encoding import smart_unicode + +from strutils import safestr,unquotehtml +from APIClients import MagnoliaClient +from urlgrabber.grabber import URLGrabber +import pydelicious as delicious + +import markdown2 as markdown +from links.models import Link +from django.conf import settings + +def flickr_datetime_to_datetime(fdt): + from datetime import datetime + from time import strptime + date_parts = strptime(fdt, '%Y-%m-%dT%H:%M:%S%Z') + return datetime(*date_parts[0:6]) + +def sync_magnolia_links(*args, **kwargs): + #Number of links to retrieve at one time + get_num = 15 + BASE_PATH = 'http://ma.gnolia.com/api/rest/1/' + client = MagnoliaClient(BASE_PATH, settings.MAGNOLIA_API_KEY) + data = client.bookmarks_find(person="luxagraf",limit=get_num) + dupe = False + for post in data.findall('bookmarks/bookmark'): + taglist = [] + info = dict((k, smart_unicode(post.get(k))) for k in post.keys()) + try: + row = Link.objects.get(magnolia_id=safestr(info['id'])) + # If the row exists already, set the dupe flag + dupe = True + except ObjectDoesNotExist: + f = copy_file(safestr(post.findtext('screenshot')), safestr(info['id'])) + local_image_url = "%s/%s.jpg" %(safestr(datetime.datetime.today().strftime("%b").lower()), safestr(info['id'])) + + for t in post.findall('tags/tag'): + tag = dict((k, smart_unicode(t.get(k))) for k in t.keys()) + taglist.append(tag['name']) + print tag['name'] + for tag in taglist: + if tag == '2lux': + status = 1 + break + else: + status = 0 + descr = markdown.markdown(unquotehtml(safestr(post.findtext('description'))), safe_mode = False) + l, created = Link.objects.get_or_create( + title = post.findtext('title'), + magnolia_id = safestr(info['id']), + url = safestr(post.findtext('url')), + description = descr, + screen_url = local_image_url, + rating = safestr(info['rating']), + pub_date = datetime.datetime(*(time.strptime(str(info['created'][:-6]), '%Y-%m-%dT%H:%M:%S')[0:6])), + status = status, + enable_comments = True, + tags = ", ".join(t for t in taglist if t != "2lux") + ) + + email_link(l) + send_to_delicious(l) + if(dupe): + break + """ + b, created = Link.objects.get_or_create( + url = info['href'], + description = info['extended'], + tags = info.get('tag', ''), + date = parsedate(info['time']), + title = info['description'] + ) + for b in bookmarks.bookmarks.bookmark: + + try: + row = Link.objects.get(magnolia_id=safestr(b.id)) + # If the row exists already, set the dupe flag + print b.title.PCDATA + dupe = True + except ObjectDoesNotExist: + tags=", ".join(t.name for t in b.tags.tag if t.name != "2lux") + # grab the photo (local copies are good, should I ever change bookmark services) + local_image_url = "%s/%s.jpg" %(safestr(datetime.datetime.today().strftime("%b").lower()), safestr(b.id)) + f = copy_file(safestr(b.screenshot.PCDATA), safestr(b.id)) + # set the privacy flag (default to false to be on the safe side) + public = 0 + if safestr(b.private) == 'false': public=1 + # Create and save a new link obj + l = Link.objects.create( + title = safestr(b.title.PCDATA), + magnolia_id = safestr(b.id), + url = safestr(b.url.PCDATA), + description = unquotehtml(safestr(b.description.PCDATA)), + screen_url = local_image_url, + rating = safestr(b.rating), + pub_date = datetime.datetime(*(time.strptime(str(b.created[:-6]), '%Y-%m-%dT%H:%M:%S')[0:6])), + status = public, + enable_comments = True, + tags=", ".join(t.name for t in b.tags.tag if t.name != "2lux") + ) + email_link(l) + send_to_delicious(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\non ma.gnolia: http://ma.gnolia.com/people/luxagraf/bookmarks/%s\n\non luxagraf: http://luxagraf.net/link/%s/" %(link.title, link.description, link.url, link.magnolia_id, link.id) + msg = EmailMessage(subject, striptags(body), 'sng@luxagraf.net', ['luxagraf@gmail.com']) + msg.send() + +def send_to_delicious(link): + del_tags = '' + tags = link.tags.split(',') + for tag in tags: + del_tags += tag.strip().replace(' ','_')+' ' + print del_tags + delicious.add('luxagraf', 'translinguis#', link.url, link.title, tags = del_tags, extended = link.description, dt =safestr(link.pub_date), replace="no") + +def copy_file(url, id): + g = URLGrabber() + filename="/home2/luxagraf/webapps/images/magnolia_thumbs/%s/%s.jpg" %(datetime.datetime.today().strftime("%b").lower(), id) + local_filename = g.urlgrab(url, filename) + return id
\ No newline at end of file |