snipt/snipts/models.py

185 lines
7.4 KiB
Python
Raw Normal View History

2011-10-02 15:38:20 -07:00
from django.contrib.sites.models import Site
from django.contrib.auth.models import User
2011-10-02 15:38:20 -07:00
from django.conf import settings
from django.db import models
2011-06-05 09:51:56 -07:00
from taggit.managers import TaggableManager
2012-02-17 16:14:53 -08:00
from taggit.utils import edit_string_for_tags
2011-06-05 09:51:56 -07:00
2012-04-23 11:34:21 -07:00
from markdown_deux import markdown
2011-10-02 15:38:20 -07:00
from pygments import highlight
from pygments.lexers import get_lexer_by_name
from pygments.formatters import HtmlFormatter
2012-02-11 07:23:09 -08:00
from snipts.utils import slugify_uniquely
2012-05-29 19:57:23 -07:00
import datetime, md5, re
2012-01-29 20:49:14 -08:00
2011-10-02 15:38:20 -07:00
2011-10-23 20:30:57 -07:00
site = Site.objects.all()[0]
2011-10-02 15:38:20 -07:00
2012-07-18 21:39:43 -07:00
class Snipt(models.Model):
2011-10-01 10:23:05 -07:00
"""An individual Snipt."""
2012-05-27 11:51:21 -07:00
user = models.ForeignKey(User, blank=True, null=True)
title = models.CharField(max_length=255)
slug = models.SlugField(max_length=255, blank=True)
custom_slug = models.SlugField(max_length=255, blank=True)
tags = TaggableManager()
lexer = models.CharField(max_length=50)
code = models.TextField()
stylized = models.TextField(blank=True, null=True)
embedded = models.TextField(blank=True, null=True)
line_count = models.IntegerField(blank=True, null=True, default=None)
key = models.CharField(max_length=100, blank=True, null=True)
public = models.BooleanField(default=False)
blog_post = models.BooleanField(default=False)
2012-05-27 11:51:21 -07:00
created = models.DateTimeField(auto_now_add=True, editable=False)
modified = models.DateTimeField(auto_now=True, editable=False)
publish_date = models.DateTimeField(blank=True, null=True)
2011-10-01 10:23:05 -07:00
def save(self, *args, **kwargs):
2012-01-29 20:49:14 -08:00
2011-10-01 10:23:05 -07:00
if not self.slug:
2012-02-14 08:39:45 -08:00
self.slug = slugify_uniquely(self.title, Snipt)
2011-10-01 10:23:05 -07:00
2012-04-09 12:19:09 -07:00
if not self.key:
self.key = md5.new(self.slug + str(datetime.datetime.now())).hexdigest()
2012-04-09 12:19:09 -07:00
2012-04-23 11:34:21 -07:00
if self.lexer == 'markdown':
self.stylized = markdown(self.code, 'default')
2012-06-11 12:53:56 -07:00
# Snipt embeds
2012-06-04 15:28:42 -07:00
for match in re.findall('\[\[(\w{32})\]\]', self.stylized):
2012-05-29 19:57:23 -07:00
self.stylized = self.stylized.replace('[[' + str(match) + ']]',
2012-06-11 12:53:56 -07:00
'<script type="text/javascript" src="https://snipt.net/embed/{}/?snipt"></script><div id="snipt-embed-{}"></div>'.format(match, match))
# YouTube embeds
for match in re.findall('\[\[youtube-(\w{11})\-(\d+)x(\d+)\]\]', self.stylized):
self.stylized = self.stylized.replace('[[youtube-{}-{}x{}]]'.format(str(match[0]), str(match[1]), str(match[2])),
2012-06-12 07:34:16 -07:00
'<iframe width="{}" height="{}" src="https://www.youtube.com/embed/{}" frameborder="0" allowfullscreen></iframe>'.format(match[1], match[2], match[0]))
2012-06-11 12:53:56 -07:00
2012-08-13 07:21:50 -07:00
# Vimeo embeds
for match in re.findall('\[\[vimeo-(\d+)\-(\d+)x(\d+)\]\]', self.stylized):
self.stylized = self.stylized.replace('[[vimeo-{}-{}x{}]]'.format(str(match[0]), str(match[1]), str(match[2])),
2012-08-14 17:04:58 -07:00
'<iframe src="https://player.vimeo.com/video/{}" width="{}" height="{}" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>'.format(match[0], match[1], match[2]))
2012-08-13 07:21:50 -07:00
2012-04-23 11:34:21 -07:00
else:
self.stylized = highlight(self.code,
get_lexer_by_name(self.lexer, encoding='UTF-8'),
2012-07-22 19:25:33 -07:00
HtmlFormatter(linenos='table', linenospecial=1, lineanchors='line'))
2012-01-29 20:49:14 -08:00
self.line_count = len(self.code.split('\n'))
2012-04-23 11:34:21 -07:00
if self.lexer == 'markdown':
lexer_for_embedded = 'text'
else:
lexer_for_embedded = self.lexer
2012-02-26 19:20:37 -08:00
embedded = highlight(self.code,
2012-04-23 11:34:21 -07:00
get_lexer_by_name(lexer_for_embedded, encoding='UTF-8'),
2012-02-26 19:20:37 -08:00
HtmlFormatter(
style='native',
noclasses=True,
prestyles="""
background-color: #1C1C1C;
border-radius: 5px;
color: #D0D0D0;
display: block;
2012-07-18 17:17:34 -07:00
font: 11px Monaco, monospace;
2012-02-26 19:20:37 -08:00
margin: 0;
overflow: auto;
padding: 15px;
-webkit-border-radius: 5px;
-moz-border-radius: 5px;
"""))
embedded = (embedded.replace("\\\"","\\\\\"")
.replace('\'','\\\'')
2012-05-07 12:27:22 -07:00
.replace("\\", "\\\\")
2012-02-26 19:20:37 -08:00
.replace('background: #202020', ''))
self.embedded = embedded
2011-10-02 11:02:13 -07:00
return super(Snipt, self).save(*args, **kwargs)
2011-10-01 10:23:05 -07:00
def __unicode__(self):
2011-10-01 10:23:05 -07:00
return self.title
2011-06-05 08:55:27 -07:00
2011-10-01 16:09:59 -07:00
def get_absolute_url(self):
2012-05-03 19:19:05 -07:00
2012-06-01 18:05:25 -07:00
if self.blog_post:
2012-06-04 12:58:03 -07:00
if self.user.id == 3:
2012-07-24 20:31:32 -07:00
if settings.DEBUG:
return 'http://nick.snipt.localhost/{}/'.format(self.slug)
else:
return 'http://nicksergeant.com/{}/'.format(self.slug)
2012-08-09 12:47:27 -07:00
elif self.user.id == 2156:
2012-08-15 08:07:58 -07:00
return 'http://rochacbruno.com.br/{}/'.format(self.slug)
2012-07-23 10:02:39 -07:00
elif self.user.id == 10325:
return 'http://snipt.joshhudnall.com/{}/'.format(self.slug)
2012-06-04 12:58:03 -07:00
else:
2012-06-07 07:22:03 -07:00
return 'https://{}.snipt.net/{}/'.format(self.user.username.replace('_', '-'), self.slug)
2012-06-01 18:05:25 -07:00
2012-06-04 17:57:20 -07:00
if self.custom_slug:
return '/{}/'.format(self.custom_slug)
if self.public:
return '/{}/{}/'.format(self.user.username, self.slug)
else:
return '/{}/{}/?key={}'.format(self.user.username, self.slug, self.key)
2011-10-01 16:09:59 -07:00
2012-01-16 21:07:15 -08:00
def get_full_absolute_url(self):
2012-06-04 17:57:20 -07:00
if self.blog_post:
if self.user.id == 3:
2012-08-20 18:48:37 -07:00
if settings.DEBUG:
return 'http://nick.snipt.localhost/{}/'.format(self.slug)
else:
return 'http://nicksergeant.com/{}/'.format(self.slug)
2012-08-15 08:07:58 -07:00
elif self.user.id == 2156:
return 'http://rochacbruno.com.br/{}/'.format(self.slug)
elif self.user.id == 10325:
return 'http://snipt.joshhudnall.com/{}/'.format(self.slug)
2012-06-04 17:57:20 -07:00
else:
return 'https://{}.snipt.net/{}/'.format(self.user.username, self.slug)
2012-01-16 21:07:15 -08:00
if settings.DEBUG:
root = 'http://snipt.localhost'
else:
2012-04-23 18:41:16 -07:00
root = 'https://snipt.net'
2012-06-04 17:57:20 -07:00
2012-06-23 19:55:52 -07:00
if self.public:
return '{}/{}/{}/'.format(root, self.user.username, self.slug)
else:
2012-06-23 19:56:10 -07:00
return '{}/{}/{}/?key={}'.format(root, self.user.username, self.slug, self.key)
2012-01-16 21:07:15 -08:00
2011-10-23 20:30:57 -07:00
def get_embed_url(self):
2012-04-23 18:41:16 -07:00
return 'https://{}/embed/{}/'.format(site.domain, self.key)
2011-10-02 15:38:20 -07:00
2011-11-10 11:34:51 -08:00
@property
def sorted_tags(self):
return self.tags.all().order_by('name')
2012-02-17 16:14:53 -08:00
@property
def tags_list(self):
return edit_string_for_tags(self.tags.all())
2011-10-13 10:30:44 -07:00
@property
def lexer_name(self):
2012-04-23 11:34:21 -07:00
if self.lexer == 'markdown':
return 'Markdown'
else:
return get_lexer_by_name(self.lexer).name
2012-02-13 19:36:59 -08:00
2012-07-18 21:39:43 -07:00
class Favorite(models.Model):
2012-02-13 19:36:59 -08:00
snipt = models.ForeignKey(Snipt)
user = models.ForeignKey(User)
2012-04-07 21:52:10 -07:00
created = models.DateTimeField(auto_now_add=True, editable=False)
modified = models.DateTimeField(auto_now=True, editable=False)
2012-02-13 19:36:59 -08:00
def __unicode__(self):
return u'{} favorited by {}'.format(self.snipt.title, self.user.username)