Django save a dynamically created file to s3
I am wanting to dynamically create a css file for my site that updates when I upload new navigation items. I know that I need to override the save of my navigation model and after the file has been uploaded to my s3 I can then call a function to do some action, but I don;t know how I can create a file dynamically and then upload it to the same s3 server, but a different location.
from django.db import models
from django.forms import CheckboxSelectMultiple
import tempfile
from django.conf import settings
from django.core.files.base import ContentFile
from django.core.files.storage import default_storage as s3_storage
from django.core.cache import cache
from datetime import datetime
import Image, os
import PIL.Image as PIL
import re, os, sys, urlparse
def createDynamicCSS():
#create then save a css file here
#probably best to 'navigation/nav.css'
#loop through all entries and create class for all of them
navs = PrimaryNav.all()
cssfile = ""
for n in navs:
string = "." + n.slug + " { ... }"
cssfile += string
#save cssfile to s3
class PrimaryNav(models.Model):
title = models.CharField(max_length=200)
slug开发者_开发百科 = models.SlugField(max_length=200)
active = models.BooleanField()
#saves to s3 (s3 address + /icon/ )
icon = models.ImageField(upload_to='icons')
def save(self):
super(PrimaryNav, self).save() # Call the "real" save() method
createDynamicCSS()
...
I'm not sure where to start. I was going to try and dissect the csv functions I have seen around the web, but that proved to be too difficult. Help please
精彩评论