Created
November 11, 2011 16:53
-
-
Save gileno/1358513 to your computer and use it in GitHub Desktop.
Upload
This file contains 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
# -*- coding: utf-8 -*- | |
import os | |
from django.db import models | |
from django.utils.translation import ugettext_lazy as _ | |
from django.contrib.auth.models import User | |
from django.core.files.storage import FileSystemStorage | |
from django.conf import settings | |
storage = FileSystemStorage(location=settings.PROJECT_PATH, base_url='/') | |
def _upload_to_file(file, filename): | |
user_id = file.user.id | |
path = os.path.join('files', 'uploads') | |
path = os.path.join(path, unicode(user_id)) | |
return os.path.join(path, filename) | |
class Upload(models.Model): | |
title = models.CharField(max_length=255, verbose_name=_(u'Título')) | |
file = models.FileField(upload_to=_upload_to_file, verbose_name=_(u'Arquivo'), storage=storage) | |
user = models.ForeignKey(User, verbose_name=_(u'Usuário'), related_name='uploads') | |
added_on = models.DateTimeField(verbose_name=_(u'Adicionado em'), auto_now_add=True) | |
def __unicode__(self): | |
return self.title | |
class Meta: | |
ordering = ['title'] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment