annotate apps/snippet/models.py @ 207:d534881629ff

Updated snippet's template and views files to fix rating code collision which prevented posting new snippets. Updated snippet's models file so that now secret id of each snippet will be unique. It also means that the no two or more than two snippets will have same secret id.
author Ahsan Ali Shahid <ahsan.ali.shahid@gmail.com>
date Mon, 19 Aug 2013 20:30:19 +0500
parents 1f1e867cb37f
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
101
a8da60d611f7 Reorder imports in some python files
dellsystem <ilostwaldo@gmail.com>
parents: 100
diff changeset
4
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.utils.translation import ugettext_lazy as _
54
898881bbfdea Tie snippets to their authors, enable a couple more lexer languages
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 46
diff changeset
7 from django.contrib.auth.models import User
151
c7be7def8b57 Bundles! (basic functionality)
dellsystem <ilostwaldo@gmail.com>
parents: 139
diff changeset
8 from mptt.models import MPTTModel, TreeForeignKey
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
9
151
c7be7def8b57 Bundles! (basic functionality)
dellsystem <ilostwaldo@gmail.com>
parents: 139
diff changeset
10 from apps.snippet.highlight import LEXER_DEFAULT, LEXER_LIST, pygmentize
205
c13a3ab314ab Added RatingField import and fixed an html tag in snippet template
Ahsan Ali Shahid <ahsan.ali.shahid@gmail.com>
parents: 203
diff changeset
11 from djangoratings.fields import RatingField
91
8ef0a2a03034 Add some blank lines where relevant
dellsystem <ilostwaldo@gmail.com>
parents: 54
diff changeset
12
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
13 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
14 def generate_secret_id(length=4):
207
d534881629ff Updated snippet's template and views files to fix rating code collision which prevented posting new snippets.
Ahsan Ali Shahid <ahsan.ali.shahid@gmail.com>
parents: 206
diff changeset
15 unique_id = ''.join([random.choice(t) for i in range(length)])
d534881629ff Updated snippet's template and views files to fix rating code collision which prevented posting new snippets.
Ahsan Ali Shahid <ahsan.ali.shahid@gmail.com>
parents: 206
diff changeset
16 while Snippet.objects.filter(secret_id = unique_id ).count()!= 0:
d534881629ff Updated snippet's template and views files to fix rating code collision which prevented posting new snippets.
Ahsan Ali Shahid <ahsan.ali.shahid@gmail.com>
parents: 206
diff changeset
17 unique_id = ''.join([random.choice(t) for i in range(length)])
d534881629ff Updated snippet's template and views files to fix rating code collision which prevented posting new snippets.
Ahsan Ali Shahid <ahsan.ali.shahid@gmail.com>
parents: 206
diff changeset
18 return unique_id
2
3cef0d445036 Start actual app structure
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
diff changeset
19
139
b8e0bdc37e32 Hide snippets created by anonymous users
dellsystem <ilostwaldo@gmail.com>
parents: 108
diff changeset
20 class SnippetManager(models.Manager):
b8e0bdc37e32 Hide snippets created by anonymous users
dellsystem <ilostwaldo@gmail.com>
parents: 108
diff changeset
21 def public(self):
b8e0bdc37e32 Hide snippets created by anonymous users
dellsystem <ilostwaldo@gmail.com>
parents: 108
diff changeset
22 """
b8e0bdc37e32 Hide snippets created by anonymous users
dellsystem <ilostwaldo@gmail.com>
parents: 108
diff changeset
23 Returns all the snippets that were created by registered users
b8e0bdc37e32 Hide snippets created by anonymous users
dellsystem <ilostwaldo@gmail.com>
parents: 108
diff changeset
24 and thus can be publicly listed.
b8e0bdc37e32 Hide snippets created by anonymous users
dellsystem <ilostwaldo@gmail.com>
parents: 108
diff changeset
25 """
b8e0bdc37e32 Hide snippets created by anonymous users
dellsystem <ilostwaldo@gmail.com>
parents: 108
diff changeset
26 return self.filter(author__isnull=False)
b8e0bdc37e32 Hide snippets created by anonymous users
dellsystem <ilostwaldo@gmail.com>
parents: 108
diff changeset
27
b8e0bdc37e32 Hide snippets created by anonymous users
dellsystem <ilostwaldo@gmail.com>
parents: 108
diff changeset
28
151
c7be7def8b57 Bundles! (basic functionality)
dellsystem <ilostwaldo@gmail.com>
parents: 139
diff changeset
29 class Snippet(MPTTModel):
139
b8e0bdc37e32 Hide snippets created by anonymous users
dellsystem <ilostwaldo@gmail.com>
parents: 108
diff changeset
30 objects = SnippetManager()
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
31 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
32 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
33 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
34 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
35 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
36 blank=True)
99
d858aae811d0 Move choices for lexer field from SnippetForm to Snippet
dellsystem <ilostwaldo@gmail.com>
parents: 91
diff changeset
37 lexer = models.CharField(_(u'Lexer'),
d858aae811d0 Move choices for lexer field from SnippetForm to Snippet
dellsystem <ilostwaldo@gmail.com>
parents: 91
diff changeset
38 max_length=30,
d858aae811d0 Move choices for lexer field from SnippetForm to Snippet
dellsystem <ilostwaldo@gmail.com>
parents: 91
diff changeset
39 choices=LEXER_LIST,
d858aae811d0 Move choices for lexer field from SnippetForm to Snippet
dellsystem <ilostwaldo@gmail.com>
parents: 91
diff changeset
40 default=LEXER_DEFAULT)
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
41 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
42 expires = models.DateTimeField(_(u'Expires'), blank=True, help_text='asdf')
151
c7be7def8b57 Bundles! (basic functionality)
dellsystem <ilostwaldo@gmail.com>
parents: 139
diff changeset
43 parent = TreeForeignKey('self', null=True, blank=True,
46
b7c1c22fdfe8 Format code for 80 columns, add some js to static/ dir
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 42
diff changeset
44 related_name='children')
102
f872c643b056 Updates to snippet functionality (see details)
dellsystem <ilostwaldo@gmail.com>
parents: 101
diff changeset
45 num_views = models.IntegerField(default=0)
203
c5087ce140c9 modified snippets model file to add rating field. This series of updates should be applied carefully and south should be used so that it can add additional fields into the database without resetting the database
Ahsan Ali Shahid <ahsan.ali.shahid@gmail.com>
parents: 151
diff changeset
46 rating = RatingField(range=5,can_change_vote=True,allow_anonymous=True,allow_delete=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
47 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
48 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
49
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 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
51 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
52
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
53 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
54 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
55
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
56 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
57 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
58 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
59 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
60 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
61 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
62
100
365144dad9d1 Add a get_title() method to the Snippet model
dellsystem <ilostwaldo@gmail.com>
parents: 99
diff changeset
63 def get_title(self):
365144dad9d1 Add a get_title() method to the Snippet model
dellsystem <ilostwaldo@gmail.com>
parents: 99
diff changeset
64 return self.title or _('Snippet #%d' % self.id)
365144dad9d1 Add a get_title() method to the Snippet model
dellsystem <ilostwaldo@gmail.com>
parents: 99
diff changeset
65
108
180404efc8cf Remove unnecessary import from snippet models.py
dellsystem <ilostwaldo@gmail.com>
parents: 102
diff changeset
66 @models.permalink
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
67 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
68 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
69
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
70 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
71 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
72
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
73
29
6ba969517b9c Implement initial profiles, cleanup models, change Free_license to FreeLicense
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents: 4
diff changeset
74 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
75 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
76
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
77 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
78 return name