Change the way to generate attachment resource path.

remotes/origin/enhancement/email-actions
Andrey Antukh 2014-09-16 12:21:00 +02:00
parent b20a9d1199
commit 91296886a5
3 changed files with 29 additions and 27 deletions

View File

@ -58,8 +58,6 @@ SEND_BROKEN_LINK_EMAILS = True
IGNORABLE_404_ENDS = (".php", ".cgi") IGNORABLE_404_ENDS = (".php", ".cgi")
IGNORABLE_404_STARTS = ("/phpmyadmin/",) IGNORABLE_404_STARTS = ("/phpmyadmin/",)
# Default django tz/i18n config
ATOMIC_REQUESTS = True ATOMIC_REQUESTS = True
TIME_ZONE = "UTC" TIME_ZONE = "UTC"
LANGUAGE_CODE = "en" LANGUAGE_CODE = "en"
@ -94,13 +92,21 @@ EVENTS_PUSH_BACKEND = "taiga.events.backends.postgresql.EventsPushBackend"
# Message System # Message System
MESSAGE_STORAGE = "django.contrib.messages.storage.session.SessionStorage" MESSAGE_STORAGE = "django.contrib.messages.storage.session.SessionStorage"
# Static configuration. # The absolute url is mandatory because attachments
MEDIA_ROOT = os.path.join(BASE_DIR, "media") # urls depends on it. On production should be set
MEDIA_URL = "/media/" # something like https://media.taiga.io/
STATIC_ROOT = os.path.join(BASE_DIR, "static") MEDIA_URL = "http://localhost:8000/media/"
# Static url is not widelly used by taiga (only
# if admin is activated).
STATIC_URL = "/static/" STATIC_URL = "/static/"
ADMIN_MEDIA_PREFIX = "/static/admin/" ADMIN_MEDIA_PREFIX = "/static/admin/"
# Static configuration.
MEDIA_ROOT = os.path.join(BASE_DIR, "media")
STATIC_ROOT = os.path.join(BASE_DIR, "static")
STATICFILES_FINDERS = [ STATICFILES_FINDERS = [
"django.contrib.staticfiles.finders.FileSystemFinder", "django.contrib.staticfiles.finders.FileSystemFinder",
"django.contrib.staticfiles.finders.AppDirectoriesFinder", "django.contrib.staticfiles.finders.AppDirectoriesFinder",

View File

@ -14,25 +14,32 @@
# You should have received a copy of the GNU Affero General Public License # You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
import time import hashlib
import os
import os.path as path
from django.db import models from django.db import models
from django.conf import settings from django.conf import settings
from django.contrib.contenttypes.models import ContentType from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes import generic from django.contrib.contenttypes import generic
from django.utils.translation import ugettext_lazy as _
from django.utils import timezone from django.utils import timezone
from django.utils.encoding import force_bytes
from django.utils.translation import ugettext_lazy as _
from taiga.base.utils.iterators import split_by_n
def get_attachment_file_path(instance, filename): def get_attachment_file_path(instance, filename):
template = "attachment-files/{project}/{model}/{stamp}/{filename}" basename = path.basename(filename).lower()
current_timestamp = int(time.mktime(timezone.now().timetuple()))
upload_to_path = template.format(stamp=current_timestamp, hs = hashlib.sha256()
project=instance.project.slug, hs.update(force_bytes(timezone.now().isoformat()))
model=instance.content_type.model, hs.update(os.urandom(1024))
filename=filename)
return upload_to_path p1, p2, p3, p4, *p5 = split_by_n(hs.hexdigest(), 1)
hash_part = path.join(p1, p2, p3, p4, "".join(p5))
return path.join("attachments", hash_part, basename)
class Attachment(models.Model): class Attachment(models.Model):

View File

@ -39,15 +39,4 @@ class AttachmentSerializer(serializers.ModelSerializer):
read_only_fields = ("owner", "created_date", "modified_date") read_only_fields = ("owner", "created_date", "modified_date")
def get_url(self, obj): def get_url(self, obj):
token = None return obj.attached_file.url
url = reverse("attachment-url", kwargs={"pk": obj.pk})
if "request" in self.context and self.context["request"].user.is_authenticated():
user_id = self.context["request"].user.id
token_src = "{}-{}-{}".format(settings.ATTACHMENTS_TOKEN_SALT, user_id, obj.id)
token = hashlib.sha1(token_src.encode("utf-8"))
return "{}?user={}&token={}".format(url, user_id, token.hexdigest())
return url