Skip to content

Commit

Permalink
20 - A Custom Filename
Browse files Browse the repository at this point in the history
  • Loading branch information
codingforentrepreneurs committed Nov 16, 2017
1 parent a879fcc commit ed23c9b
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 6 deletions.
Binary file modified src/db.sqlite3
Binary file not shown.
2 changes: 1 addition & 1 deletion src/orders/templates/orders/library.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ <h1>Library</h1>
<tr>
<td><a href='{{ object.get_absolute_url }}'>{{ object.title }}</a></td>
<td>{% for download in object.get_downloads %}
<a href='{{ download.get_download_url }}'>{{download.name }}</a><br/>
<a href='{{ download.get_download_url }}'>{{ download.display_name }}</a><br/>
{% endfor %}
</td>
</tr>
Expand Down
22 changes: 22 additions & 0 deletions src/products/migrations/0014_auto_20171116_0011.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11.6 on 2017-11-16 00:11
from __future__ import unicode_literals

from django.db import migrations, models
import products.models
import storages.backends.s3boto3


class Migration(migrations.Migration):

dependencies = [
('products', '0013_auto_20171109_0023'),
]

operations = [
migrations.AlterField(
model_name='productfile',
name='file',
field=models.FileField(storage=storages.backends.s3boto3.S3Boto3Storage(location='protected'), upload_to=products.models.upload_product_file_loc),
),
]
20 changes: 20 additions & 0 deletions src/products/migrations/0015_productfile_name.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11.6 on 2017-11-16 00:12
from __future__ import unicode_literals

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('products', '0014_auto_20171116_0011'),
]

operations = [
migrations.AddField(
model_name='productfile',
name='name',
field=models.CharField(blank=True, max_length=120, null=True),
),
]
14 changes: 9 additions & 5 deletions src/products/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ def upload_product_file_loc(instance, filename):

class ProductFile(models.Model):
product = models.ForeignKey(Product)
name = models.CharField(max_length=120, null=True, blank=True)
file = models.FileField(
upload_to=upload_product_file_loc,
storage=ProtectedS3Storage(), #FileSystemStorage(location=settings.PROTECTED_ROOT)
Expand All @@ -136,6 +137,13 @@ class ProductFile(models.Model):
def __str__(self):
return str(self.file.name)

@property
def display_name(self):
og_name = get_filename(self.file.name)
if self.name:
return self.name
return og_name

def get_default_url(self):
return self.product.get_absolute_url()

Expand All @@ -149,18 +157,14 @@ def generate_download_url(self):
PROTECTED_DIR_NAME = getattr(settings, 'PROTECTED_DIR_NAME', 'protected')
path = "{base}/{file_path}".format(base=PROTECTED_DIR_NAME, file_path=str(self.file))
aws_dl_object = AWSDownload(access_key, secret_key, bucket, region)
file_url = aws_dl_object.generate_url(path)#, new_filename='New awesome file')
file_url = aws_dl_object.generate_url(path, new_filename=self.display_name)
return file_url

def get_download_url(self): # detail view
return reverse("products:download",
kwargs={"slug": self.product.slug, "pk": self.pk}
)

@property
def name(self):
return get_filename(self.file.name)




Expand Down

0 comments on commit ed23c9b

Please sign in to comment.