Mercurial > hg > agora-ahsan
annotate apps/snippet/models.py @ 54:898881bbfdea
Tie snippets to their authors, enable a couple more lexer languages
author | Jordi Gutiérrez Hermoso <jordigh@gmail.com> |
---|---|
date | Mon, 07 Feb 2011 04:43:09 -0600 |
parents | b7c1c22fdfe8 |
children | 8ef0a2a03034 |
rev | line source |
---|---|
42
ab608f27ecd5
Copy preliminary django-paste code for snippets along with mptt. Works clunkily. Still need to adapt it for Agora.
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
30
diff
changeset
|
1 import datetime |
ab608f27ecd5
Copy preliminary django-paste code for snippets along with mptt. Works clunkily. Still need to adapt it for Agora.
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
30
diff
changeset
|
2 import difflib |
ab608f27ecd5
Copy preliminary django-paste code for snippets along with mptt. Works clunkily. Still need to adapt it for Agora.
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
30
diff
changeset
|
3 import random |
ab608f27ecd5
Copy preliminary django-paste code for snippets along with mptt. Works clunkily. Still need to adapt it for Agora.
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
30
diff
changeset
|
4 import agora.apps.mptt as mptt |
2
3cef0d445036
Start actual app structure
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
diff
changeset
|
5 from django.db import models |
42
ab608f27ecd5
Copy preliminary django-paste code for snippets along with mptt. Works clunkily. Still need to adapt it for Agora.
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
30
diff
changeset
|
6 from django.db.models import permalink |
ab608f27ecd5
Copy preliminary django-paste code for snippets along with mptt. Works clunkily. Still need to adapt it for Agora.
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
30
diff
changeset
|
7 from django.utils.translation import ugettext_lazy as _ |
ab608f27ecd5
Copy preliminary django-paste code for snippets along with mptt. Works clunkily. Still need to adapt it for Agora.
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
30
diff
changeset
|
8 from agora.apps.snippet.highlight import LEXER_DEFAULT, pygmentize |
54
898881bbfdea
Tie snippets to their authors, enable a couple more lexer languages
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
46
diff
changeset
|
9 from django.contrib.auth.models import User |
42
ab608f27ecd5
Copy preliminary django-paste code for snippets along with mptt. Works clunkily. Still need to adapt it for Agora.
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
30
diff
changeset
|
10 |
ab608f27ecd5
Copy preliminary django-paste code for snippets along with mptt. Works clunkily. Still need to adapt it for Agora.
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
30
diff
changeset
|
11 t = 'abcdefghijkmnopqrstuvwwxyzABCDEFGHIJKLOMNOPQRSTUVWXYZ1234567890' |
ab608f27ecd5
Copy preliminary django-paste code for snippets along with mptt. Works clunkily. Still need to adapt it for Agora.
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
30
diff
changeset
|
12 def generate_secret_id(length=4): |
ab608f27ecd5
Copy preliminary django-paste code for snippets along with mptt. Works clunkily. Still need to adapt it for Agora.
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
30
diff
changeset
|
13 return ''.join([random.choice(t) for i in range(length)]) |
2
3cef0d445036
Start actual app structure
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
diff
changeset
|
14 |
3 | 15 class Snippet(models.Model): |
42
ab608f27ecd5
Copy preliminary django-paste code for snippets along with mptt. Works clunkily. Still need to adapt it for Agora.
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
30
diff
changeset
|
16 secret_id = models.CharField(_(u'Secret ID'), max_length=4, blank=True) |
ab608f27ecd5
Copy preliminary django-paste code for snippets along with mptt. Works clunkily. Still need to adapt it for Agora.
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
30
diff
changeset
|
17 title = models.CharField(_(u'Title'), max_length=120, blank=True) |
54
898881bbfdea
Tie snippets to their authors, enable a couple more lexer languages
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
46
diff
changeset
|
18 author = models.ForeignKey(User, max_length=30, blank=True, null=True) |
42
ab608f27ecd5
Copy preliminary django-paste code for snippets along with mptt. Works clunkily. Still need to adapt it for Agora.
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
30
diff
changeset
|
19 content = models.TextField(_(u'Content'), ) |
46
b7c1c22fdfe8
Format code for 80 columns, add some js to static/ dir
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
42
diff
changeset
|
20 content_highlighted = models.TextField(_(u'Highlighted Content'), |
b7c1c22fdfe8
Format code for 80 columns, add some js to static/ dir
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
42
diff
changeset
|
21 blank=True) |
42
ab608f27ecd5
Copy preliminary django-paste code for snippets along with mptt. Works clunkily. Still need to adapt it for Agora.
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
30
diff
changeset
|
22 lexer = models.CharField(_(u'Lexer'), max_length=30, default=LEXER_DEFAULT) |
ab608f27ecd5
Copy preliminary django-paste code for snippets along with mptt. Works clunkily. Still need to adapt it for Agora.
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
30
diff
changeset
|
23 published = models.DateTimeField(_(u'Published'), blank=True) |
ab608f27ecd5
Copy preliminary django-paste code for snippets along with mptt. Works clunkily. Still need to adapt it for Agora.
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
30
diff
changeset
|
24 expires = models.DateTimeField(_(u'Expires'), blank=True, help_text='asdf') |
46
b7c1c22fdfe8
Format code for 80 columns, add some js to static/ dir
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
42
diff
changeset
|
25 parent = models.ForeignKey('self', null=True, blank=True, |
b7c1c22fdfe8
Format code for 80 columns, add some js to static/ dir
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
42
diff
changeset
|
26 related_name='children') |
42
ab608f27ecd5
Copy preliminary django-paste code for snippets along with mptt. Works clunkily. Still need to adapt it for Agora.
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
30
diff
changeset
|
27 |
ab608f27ecd5
Copy preliminary django-paste code for snippets along with mptt. Works clunkily. Still need to adapt it for Agora.
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
30
diff
changeset
|
28 class Meta: |
ab608f27ecd5
Copy preliminary django-paste code for snippets along with mptt. Works clunkily. Still need to adapt it for Agora.
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
30
diff
changeset
|
29 ordering = ('-published',) |
ab608f27ecd5
Copy preliminary django-paste code for snippets along with mptt. Works clunkily. Still need to adapt it for Agora.
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
30
diff
changeset
|
30 |
ab608f27ecd5
Copy preliminary django-paste code for snippets along with mptt. Works clunkily. Still need to adapt it for Agora.
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
30
diff
changeset
|
31 def get_linecount(self): |
ab608f27ecd5
Copy preliminary django-paste code for snippets along with mptt. Works clunkily. Still need to adapt it for Agora.
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
30
diff
changeset
|
32 return len(self.content.splitlines()) |
ab608f27ecd5
Copy preliminary django-paste code for snippets along with mptt. Works clunkily. Still need to adapt it for Agora.
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
30
diff
changeset
|
33 |
ab608f27ecd5
Copy preliminary django-paste code for snippets along with mptt. Works clunkily. Still need to adapt it for Agora.
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
30
diff
changeset
|
34 def content_splitted(self): |
ab608f27ecd5
Copy preliminary django-paste code for snippets along with mptt. Works clunkily. Still need to adapt it for Agora.
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
30
diff
changeset
|
35 return self.content_highlighted.splitlines() |
ab608f27ecd5
Copy preliminary django-paste code for snippets along with mptt. Works clunkily. Still need to adapt it for Agora.
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
30
diff
changeset
|
36 |
ab608f27ecd5
Copy preliminary django-paste code for snippets along with mptt. Works clunkily. Still need to adapt it for Agora.
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
30
diff
changeset
|
37 def save(self, *args, **kwargs): |
ab608f27ecd5
Copy preliminary django-paste code for snippets along with mptt. Works clunkily. Still need to adapt it for Agora.
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
30
diff
changeset
|
38 if not self.pk: |
ab608f27ecd5
Copy preliminary django-paste code for snippets along with mptt. Works clunkily. Still need to adapt it for Agora.
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
30
diff
changeset
|
39 self.published = datetime.datetime.now() |
ab608f27ecd5
Copy preliminary django-paste code for snippets along with mptt. Works clunkily. Still need to adapt it for Agora.
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
30
diff
changeset
|
40 self.secret_id = generate_secret_id() |
ab608f27ecd5
Copy preliminary django-paste code for snippets along with mptt. Works clunkily. Still need to adapt it for Agora.
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
30
diff
changeset
|
41 self.content_highlighted = pygmentize(self.content, self.lexer) |
ab608f27ecd5
Copy preliminary django-paste code for snippets along with mptt. Works clunkily. Still need to adapt it for Agora.
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
30
diff
changeset
|
42 super(Snippet, self).save(*args, **kwargs) |
ab608f27ecd5
Copy preliminary django-paste code for snippets along with mptt. Works clunkily. Still need to adapt it for Agora.
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
30
diff
changeset
|
43 |
ab608f27ecd5
Copy preliminary django-paste code for snippets along with mptt. Works clunkily. Still need to adapt it for Agora.
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
30
diff
changeset
|
44 @permalink |
ab608f27ecd5
Copy preliminary django-paste code for snippets along with mptt. Works clunkily. Still need to adapt it for Agora.
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
30
diff
changeset
|
45 def get_absolute_url(self): |
ab608f27ecd5
Copy preliminary django-paste code for snippets along with mptt. Works clunkily. Still need to adapt it for Agora.
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
30
diff
changeset
|
46 return ('snippet_details', (self.secret_id,)) |
29
6ba969517b9c
Implement initial profiles, cleanup models, change Free_license to FreeLicense
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
4
diff
changeset
|
47 |
30
f14aaa98306a
Register more apps in the admin site, write an actual template for the user profile page
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
29
diff
changeset
|
48 def __unicode__(self): |
42
ab608f27ecd5
Copy preliminary django-paste code for snippets along with mptt. Works clunkily. Still need to adapt it for Agora.
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
30
diff
changeset
|
49 return '%s' % self.secret_id |
ab608f27ecd5
Copy preliminary django-paste code for snippets along with mptt. Works clunkily. Still need to adapt it for Agora.
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
30
diff
changeset
|
50 |
ab608f27ecd5
Copy preliminary django-paste code for snippets along with mptt. Works clunkily. Still need to adapt it for Agora.
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
30
diff
changeset
|
51 mptt.register(Snippet, order_insertion_by=['content']) |
ab608f27ecd5
Copy preliminary django-paste code for snippets along with mptt. Works clunkily. Still need to adapt it for Agora.
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
30
diff
changeset
|
52 |
30
f14aaa98306a
Register more apps in the admin site, write an actual template for the user profile page
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
29
diff
changeset
|
53 |
29
6ba969517b9c
Implement initial profiles, cleanup models, change Free_license to FreeLicense
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
4
diff
changeset
|
54 class CodeLanguage(models.Model): |
6ba969517b9c
Implement initial profiles, cleanup models, change Free_license to FreeLicense
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
4
diff
changeset
|
55 name = models.CharField(max_length=64) |
30
f14aaa98306a
Register more apps in the admin site, write an actual template for the user profile page
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
29
diff
changeset
|
56 |
f14aaa98306a
Register more apps in the admin site, write an actual template for the user profile page
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
29
diff
changeset
|
57 def __unicode__(self): |
f14aaa98306a
Register more apps in the admin site, write an actual template for the user profile page
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
29
diff
changeset
|
58 return name |