Skip to content

Instantly share code, notes, and snippets.

@murilobsd
Created October 4, 2013 22:23
Show Gist options
  • Save murilobsd/6833830 to your computer and use it in GitHub Desktop.
Save murilobsd/6833830 to your computer and use it in GitHub Desktop.
Models do Compartilhamento
from south.modelsinspector import add_introspection_rules
from django.db import models
from django.db.models import permalink
from django.contrib.auth.models import User
from core.models import Cliente
from biblioteca.models import Imagem
from base64 import b32encode
from hashlib import sha1
from random import random
def pkgen():
rude = ('lol',)
bad_pk = True
while bad_pk:
pk = b32encode(sha1(str(random())).digest()).lower()[:32]
bad_pk = False
for rw in rude:
if pk.find(rw) >= 0: bad_pk = True
return pk
class Compartilhamento(models.Model):
funcionario = models.ForeignKey(User, verbose_name=u'Funcionário', blank=True)
cliente = models.ForeignKey(Cliente, verbose_name=u'Cliente', blank=True)
urlhash = models.CharField(verbose_name=u'Url Hash', max_length=32, blank=True, unique=True)
expirar = models.DateTimeField(verbose_name=u'Expirar', blank=True)
data = models.DateTimeField(auto_now_add=True)
documento = models.ForeignKey(Imagem, verbose_name=u'Documento', blank=True)
visita = models.IntegerField(verbose_name=u'Visitas', default=0, blank=True)
class Meta:
ordering = ('-data',)
def save(self, *args, **kwargs):
if not self.pk:
self.urlhash = pkgen()
@permalink
def get_absolute_url(self):
return 'compartilhamento_detalhes', (self.urlhash,)
add_introspection_rules([], ['^biblioteca\.util\.fields\.PDFFileField'])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment