tags:

views:

187

answers:

5

I block certain referers in my .htacccess file to avoid serving images to warez sites. The images are served directly so Django doesn't touch them and I'd like to keep it that way because of performance.

But I would like to be able to add more blocked sites to the list inside the .htaccess file using the Django admin, without having to use FTP or SVN to access the site. Is this possible? How?

+1  A: 

I don't know whether I'm missing something here, but isn't it something like this?

def add_to_htaccess(line):
    htaccess = open("/path/to/.htaccess", "a")
    htaccess.write(line+"\n")
    htaccess.close()
RichieHindle
Well first of all, the blocked sites are not at the end of the .htaccess so I can't just append this. It has to be inserted. Second, I was thinking of an UI, possibly more or less automatically created by Django for me, rather than the file write operation itself.
TomA
A: 

You might have a pretty hard time doing this from the admin app, but you can probably make an app yourself to do this in about a dozen lines of code, plus a few more for the templates.

TokenMacGuy
A: 

Write a custom app but reuse the admin templates:

{% extends "admin/base_site.html" %}
{% load adminmedia %}

{% block coltype %}flex{% endblock %}
{% block bodyclass %}change-list{% endblock %}

{% block breadcrumbs %}<div class="breadcrumbs"><a href="/admin/">Home</a>&nbsp;&rsaquo;&nbsp;{{page_title}}</div>{% endblock %}
{% block content %}
....Your code here....
{% endblock %}

Add it to your URL's before the admin URLs and to all intents and purposes it's part of the admin. You can customize your admin templates to include links to it in the object tools or the main menu if you like.

andybak
Why would you do that? Why not just write a regular app and register it with the admin?
Dominic Rodger
That will only buy you create/update and delete pages. This is for when you need pages that look like they are part of the admin but are based on your own view functions.
andybak
+2  A: 

You might find it easier to use some mod_rewrite voodoo and employ a RewriteMap to hold the list of sites to block with a rewrite condition to check the map. This has the benefit that the only thing in the map file will be the list of sites and so updating it is easier. The map file will also be automatically reloaded by Apache when it has been updated. You may though need to place the rewrite rules for this in main Apache configuration.

Graham Dumpleton
+2  A: 

You could create a model that defines all the configurable part of .htaccess; then, add a signal (django doc) on each save, to call a function that will write a new .htaccess based on what has been defined on the database.
Something like this (as usual, this is untested code!):

class HtAccessExclusion(models.Model):
    exclusion = models.CharField(max_length=300)

from django.db.models.signals import post_save

def export_on_htaccess(sender, **kwargs):
    # write "standard" part of htaccess
    # use the content of HtAccessExclusion table to write all specific exclusions
    ...

post_save.connect(export_on_htaccess, sender=HtAccessExclusion)
Roberto Liffredo
Aha, signals! I knew they were good for something.
TomA
So I actually implemented this and it works fine, just had to change pre_save.connect() to post_save.connect(), obviously...
TomA
Yep, you're right. Now I have corrected it.
Roberto Liffredo