Skip to content

Commit

Permalink
Allows uploading images through the Django admin
Browse files Browse the repository at this point in the history
  • Loading branch information
davidfischer committed Oct 30, 2018
1 parent 5fa1ca1 commit 3172b73
Show file tree
Hide file tree
Showing 12 changed files with 83 additions and 5 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ mailhog
# Compiled static files
assets/dist
static
media

# Development database
db.sqlite3
Expand Down
2 changes: 1 addition & 1 deletion adserver/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def display_image(self, obj):
return ""

return mark_safe(
'<img src="{url}" style="width: 120px" />'.format(url=obj.image)
'<img src="{url}" style="width: 120px" />'.format(url=obj.image.url)
)

def num_clicks(self, obj):
Expand Down
21 changes: 21 additions & 0 deletions adserver/migrations/0002_image-upload.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11.15 on 2018-10-29 23:49
from __future__ import unicode_literals

from django.db import migrations
from django.db import models


class Migration(migrations.Migration):

dependencies = [
('adserver', '0001_initial'),
]

operations = [
migrations.AlterField(
model_name='advertisement',
name='image',
field=models.ImageField(blank=True, help_text='240x180', max_length=255, null=True, upload_to='%Y/%m/', verbose_name='Image'),
),
]
11 changes: 8 additions & 3 deletions adserver/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -542,8 +542,13 @@ class Advertisement(IndestructibleModel):
slug = models.CharField(_("Slug"), max_length=200)
text = models.TextField(_("Text"), blank=True)
link = models.URLField(_("Link URL"), max_length=255, blank=True, null=True)
image = models.URLField(
_("Image URL"), max_length=255, blank=True, null=True, help_text=_("240x180")
image = models.ImageField(
_("Image"),
max_length=255,
upload_to="images/%Y/%m/",
blank=True,
null=True,
help_text=_("240x180"),
)
live = models.BooleanField(_("Live"), default=False)
flight = models.ForeignKey(
Expand Down Expand Up @@ -777,7 +782,7 @@ def render_links(self, link=None):

def render_ad(self, image_url=None, link_url=None): # noqa TODO: fix
# TODO: render the ad based on the ad type
return image_url or self.image
return image_url or self.image.url


class BaseImpression(models.Model):
Expand Down
2 changes: 1 addition & 1 deletion adserver/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ def proxy_ad_view(request, ad_id, nonce):
)
cache.incr(ad.cache_key(impression_type=VIEWS, nonce=nonce))
if ad.image:
return redirect(ad.image)
return redirect(ad.image.url)

return HttpResponse("View Proxy")

Expand Down
5 changes: 5 additions & 0 deletions config/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,11 @@
STATICFILES_STORAGE = "whitenoise.storage.CompressedManifestStaticFilesStorage"
STATICFILES_DIRS = [os.path.join(BASE_DIR, "assets", "dist")]

# User-uploaded files (ad images)
# https://docs.djangoproject.com/en/1.11/topics/files/
MEDIA_ROOT = os.path.join(BASE_DIR, "media")
MEDIA_URL = "/media/"

# Logging
# See: https://docs.djangoproject.com/en/1.11/ref/settings/#logging
# A sample logging configuration. The only tangible logging
Expand Down
15 changes: 15 additions & 0 deletions config/settings/production.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
ALLOWED_HOSTS=(list, ["*"]), # eg. "adserver.yourserver.com,adserver.yourserver.io"
INTERNAL_IPS=(list, []),
REDIS_PORT=(int, 6379),
# User-uploaded media
DEFAULT_FILE_STORAGE=(str, "storages.backends.azure_storage.AzureStorage"),
MEDIA_URL=(str, ""),
MEDIA_ROOT=(str, ""),
# Ad server settings
ADSERVER_HTTPS=(bool, False),
ADSERVER_ADMIN_URL=(str, ""),
Expand Down Expand Up @@ -92,6 +96,17 @@
EMAIL_BACKEND = "anymail.backends.mailgun.EmailBackend"
ANYMAIL = {"MAILGUN_API_KEY": env("MAILGUN_API_KEY")}

#
# User upload storage
# https://docs.djangoproject.com/en/1.11/topics/files/
# https://django-storages.readthedocs.io/en/latest/backends/azure.html
DEFAULT_FILE_STORAGE = env("DEFAULT_FILE_STORAGE")
MEDIA_ROOT = env("MEDIA_ROOT")
MEDIA_URL = env("MEDIA_URL")
AZURE_ACCOUNT_NAME = env("AZURE_ACCOUNT_NAME", default="")
AZURE_ACCOUNT_KEY = env("AZURE_ACCOUNT_KEY", default="")
AZURE_CONTAINER = env("AZURE_CONTAINER", default="")

#
# Ad server settings

Expand Down
3 changes: 3 additions & 0 deletions config/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from django.conf import settings
from django.conf.urls import include
from django.conf.urls import url
from django.conf.urls.static import static
from django.contrib import admin
from django.views import defaults as default_views

Expand Down Expand Up @@ -30,6 +31,8 @@
url("500/", default_views.server_error),
]

urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

# Enable Django Debug Toolbar in development
if "debug_toolbar" in settings.INSTALLED_APPS:
import debug_toolbar
Expand Down
20 changes: 20 additions & 0 deletions docs/install/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,14 @@ It should be off in production (which is the default).
Set to ``True`` to enable it.


DEFAULT_FILE_STORAGE
~~~~~~~~~~~~~~~~~~~~

Adjusts Django's ``DEFAULT_FILE_STORAGE`` setting.
Defaults to ``storages.backends.azure_storage.AzureStorage`` which
can be used to storage uploaded ad images in Azure.


INTERNAL_IPS
~~~~~~~~~~~~

Expand All @@ -82,6 +90,18 @@ This setting has a few additional meanings for the ad server including:
* All ad impressions and clicks from ``INTERNAL_IPS`` are ignored for reporting purposes


MEDIA_ROOT
~~~~~~~~~~

Adjusts Django's ``MEDIA_ROOT`` setting (default ``""``).


MEDIA_URL
~~~~~~~~~

Adjusts Django's ``MEDIA_URL`` setting (default ``""``).


SECRET_KEY
~~~~~~~~~~

Expand Down
Empty file added media/.gitkeep
Empty file.
3 changes: 3 additions & 0 deletions requirements/base.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ pytz==2018.5
# Static files
whitenoise==4.0

# Handling and processing uploaded images
Pillow==5.3.0

# Our API
djangorestframework==3.9.0
djangorestframework-jsonp==1.0.2
Expand Down
5 changes: 5 additions & 0 deletions requirements/production.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,8 @@ django-anymail==4.2

# Redis (cache)
django-redis==4.9.0

# Upload files to cloud storage (Azure)
django-storages==1.7.1
azure==4.0.0
azure-storage-blob==1.3.1

0 comments on commit 3172b73

Please sign in to comment.