Django-magic to prevent cross-site request forgery for POST requests
[mygpo.git] / mygpo / web / models.py
blobca7b6f552e9037b8b32d2bff20731f6db7001662
2 # This file is part of my.gpodder.org.
4 # my.gpodder.org is free software: you can redistribute it and/or modify it
5 # under the terms of the GNU Affero General Public License as published by
6 # the Free Software Foundation, either version 3 of the License, or (at your
7 # option) any later version.
9 # my.gpodder.org is distributed in the hope that it will be useful, but
10 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
12 # License for more details.
14 # You should have received a copy of the GNU Affero General Public License
15 # along with my.gpodder.org. If not, see <http://www.gnu.org/licenses/>.
18 from django.db import models
19 from django.contrib.auth.models import User
20 from datetime import datetime
21 import random
22 import string
25 class Rating(models.Model):
26 target = models.CharField(max_length=15)
27 user = models.ForeignKey(User)
28 rating = models.IntegerField()
29 timestamp = models.DateTimeField(default=datetime.now)
31 class Meta:
32 db_table = 'ratings'
34 def __unicode__(self):
35 return '%s rates %s as %s on %s' % (self.user, self.target, self.rating, self.timestamp)
38 class SecurityToken(models.Model):
39 user = models.ForeignKey(User)
40 token = models.CharField(max_length=32, blank=True, default=lambda: "".join(random.sample(string.letters+string.digits, 32)))
41 object = models.CharField(max_length=64)
42 action = models.CharField(max_length=10)
44 class Meta:
45 db_table = 'security_tokens'
47 def __unicode__(self):
48 return '%s %s %s: %s' % (self.user, self.object, self.action, self.token[:5])
50 def random_token(self):
51 self.token = "".join(random.sample(string.letters+string.digits, 32))