Basic support for mentioning Snipt users in Markdown posts.

master
Nick Sergeant 2013-04-04 12:39:20 -04:00
parent eb6069e08b
commit 8e0cfc07e2
2 changed files with 28 additions and 0 deletions

View File

@ -1,5 +1,6 @@
from django.contrib.auth.models import User
from django.db import models
from snipts.models import Snipt
class UserProfile(models.Model):
@ -59,6 +60,9 @@ class UserProfile(models.Model):
google_ad_width = models.CharField(max_length=250, null=True, blank=True)
google_ad_height = models.CharField(max_length=250, null=True, blank=True)
def get_blog_posts(self):
return Snipt.objects.filter(user=self.user, blog_post=True, public=True)
def get_primary_blog_domain(self):
if not self.blog_domain:
return None

View File

@ -1,3 +1,5 @@
from annoying.functions import get_object_or_None
from django.contrib.auth.models import User
from django.conf import settings
from django.db import models
@ -73,6 +75,28 @@ class Snipt(models.Model):
for match in re.findall('\[\[tweet-(\d+)\]\]', self.stylized):
self.stylized = self.stylized.replace('[[tweet-{}]]'.format(str(match)), '<div class="embedded-tweet" data-tweet-id="{}"></div>'.format(str(match)))
# Parse Snipt usernames
for match in re.findall('@(\w+)', self.stylized):
# Try and get the Snipt user by username.
user = get_object_or_None(User, username=match)
if user:
# If the user has a blog domain, use that.
if user.profile.get_primary_blog_domain():
url = 'http://{}'.format(user.profile.get_primary_blog_domain())
# Otherwise, if they have blog posts, use their Snipt blog URL.
elif user.profile.get_blog_posts():
url = 'https://{}.snipt.net/'.format(user.username)
# Otherwise, use their regular Snipt profile page.
else:
url = 'https://snipt.net/{}/'.format(match)
self.stylized = self.stylized.replace('@{}'.format(str(match)), '<a href="{}">@{}</a>'.format(url, match))
else:
self.stylized = highlight(self.code,
get_lexer_by_name(self.lexer, encoding='UTF-8'),