-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.py
68 lines (53 loc) · 2.29 KB
/
models.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
from django.db import models
from tagging.fields import TagField
from django.contrib.auth.models import User
from pygments import formatters, highlight, lexers
import textile
class WithDate(models.Model):
date_created = models.DateTimeField(auto_now_add=True)
date_updated = models.DateTimeField(auto_now=True)
class Meta:
abstract = True
# ============================================================================
class Language(WithDate):
name = models.CharField(max_length=100)
slug = models.SlugField(unique=True)
language_code = models.CharField(max_length=100)
mime_type = models.CharField(max_length=100)
class Meta:
ordering = ['name']
def __unicode__(self):
return self.name
@models.permalink
def get_absolute_url(self):
return ('kb_language_detail', (), {'slug': self.slug})
def get_lexer(self):
return lexers.get_lexer_by_name(self.language_code)
# ============================================================================
class Snippet(WithDate):
author = models.ForeignKey(User)
title = models.CharField(max_length=200)
slug = models.SlugField(unique=True)
language = models.ForeignKey(Language)
description = models.TextField()
description_html = models.TextField(editable=False)
code = models.TextField()
highlighted_code = models.TextField(editable=False)
tags = TagField()
class Meta:
ordering = ['-date_created']
def __unicode__(self):
return self.title
def highlight(self):
return highlight(self.code,
self.language.get_lexer(),
formatters.HtmlFormatter(linenos=True))
def save(self, force_insert=False, force_update=False):
self.description_html = textile.textile(self.description.encode('utf-8'),
encoding='utf-8',
output='utf-8')
self.highlighted_code = self.highlight()
super(Snippet, self).save(force_insert, force_update)
@models.permalink
def get_absolute_url(self):
return ('kb_snippet_detail', (), {'object_id': self.pk})