Forked from murilobsd/compartilhamento.models.py
Last active
December 24, 2015 21:39
-
-
Save andrewsmedina/6867119 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | |
return super(Compartilhamento, self).save(*args, **kwargs) | |
@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