Skip to content

Commit

Permalink
The beginnings of list_display support, and some slightly more robust…
Browse files Browse the repository at this point in the history
… ajax handling
  • Loading branch information
issackelly committed Mar 31, 2011
1 parent 170ef22 commit c40067d
Show file tree
Hide file tree
Showing 13 changed files with 203 additions and 84 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
{% load insert_tags %}
{% load uni_form_tags %}
<form id="srv_upload_image" method="POST" enctype="multipart/form-data" class="servee uniform" action="{% url servee:insert_sample_image_insert_image_add %}">
{% csrf_token %}
{{ form|as_uni_form }}
{{ form.as_p }}
<div class="buttonHolder">
<input type="submit" class="srv_button insert button" id="srv_upload_image_action" value="Upload">
</div>
Expand Down
2 changes: 1 addition & 1 deletion servee/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
VERSION = (0, 6, 0, "a", 1) # following PEP 386
DEV_N = "14"
DEV_N = "15"


# cribbed from pinax
Expand Down
18 changes: 18 additions & 0 deletions servee/frontendadmin/options.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,21 @@
from django.contrib.admin.options import ModelAdmin, StackedInline, TabularInline
from django.contrib.admin.views.main import ChangeList
from django.contrib.admin.util import quote
from django.http import HttpResponse, HttpResponseRedirect

class ServeeChangeList(ChangeList):
"""
I need full path links everywhere in the admin, so I subclassed the changelist
"""

def url_for_result(self, result):
return "/servee/%s/%s/%s/" % (
self.model._meta.app_label,
self.model._meta.object_name.lower(),
quote(getattr(result, self.pk_attname)),
)


class ServeeModelAdmin(ModelAdmin):
"""
ServeeModelAdmin is just like the normal ModelAdmin, but with a larger pool of default
Expand Down Expand Up @@ -125,6 +140,9 @@ def add_view(self, *args, **kwargs):
})
return super(ServeeModelAdmin, self).add_view(*args, **kwargs)

def get_changelist(self, request, **kwargs):
return ServeeChangeList


class ServeeStackedInline(StackedInline):
template = 'servee/edit_inline/stacked.html'
Expand Down
96 changes: 74 additions & 22 deletions servee/frontendadmin/static/servee/js/frontendadmin.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,82 @@ $(document).ready(function(){

/* Frontendadmin buttons add, edit, delete */
$("a.frontendadmin_edit:not(.modal)").hover(
function(){$(this).parent().addClass("pre-edit");},
function(){$(this).parent().removeClass("pre-edit");}
function(){
if ((typeof(servee) != "undefined") && (typeof(servee.selector) != "undefined")){
$par = $(servee.selector);
}
else {
$par = $link.parent();
}
$par.addClass("pre-edit");
},
function(){
if ((typeof(servee) != "undefined") && (typeof(servee.selector) != "undefined")){
$par = $(servee.selector);
}
else {
$par = $link.parent();
}
$par.removeClass("pre-edit");
}
);
$("a.frontendadmin_delete:not(.modal)").hover(
function(){$(this).parent().addClass("pre-delete");},
function(){$(this).parent().removeClass("pre-delete");}
function(){
if ((typeof(servee) != "undefined") && (typeof(servee.selector) != "undefined")){
$par = $(servee.selector);
}
else {
$par = $link.parent();
}
$par.addClass("pre-delete");
},
function(){
if ((typeof(servee) != "undefined") && (typeof(servee.selector) != "undefined")){
$par = $(servee.selector);
}
else {
$par = $link.parent();
}
$par.removeClass("pre-delete");
}
);


$("a.frontendadmin_add").live("click", function(e){
var $base, $link = $(this), $par = $link.parent();
var $base, $link = $(this);

if ((typeof(servee) != "undefined") && (typeof(servee.selector) != "undefined")){
$par = $(servee.selector);
}
else {
$par = $link.parent();
}

$.ajax({
url: this.href,
success: function(data, text){
$par.html(data);
}
});
e.preventDefault();
return false;
});


$("a.frontendadmin_list").live("click", function(e){
var $base, $link = $(this);

if ((typeof(servee) != "undefined") && (typeof(servee.selector) != "undefined")){
$par = $(servee.selector);
}
else {
$par = $link.parent();
}

$.ajax({
url: this.href,
success: function(data, text){
if ($link.hasClass("modal")){
$.fancybox({
content: data,
});
}
else {
$par.html(data);
}
$par.html(data);
}
});
e.preventDefault();
Expand All @@ -35,18 +88,18 @@ $(document).ready(function(){
$("a.frontendadmin_edit").live("click", function(e){
var $base, $link = $(this), $par = $link.parent();

if ((typeof(servee) != "undefined") && (typeof(servee.selector) != "undefined")){
$par = $(servee.selector);
}
else {
$par = $link.parent();
}

$par.css("outline", "");
$.ajax({
url: this.href,
success: function(data, text){
if ($link.hasClass("modal")){
$.fancybox({
content: data,
});
}
else {
$par.html(data);
}
$par.html(data);
}
});
e.preventDefault();
Expand Down Expand Up @@ -90,7 +143,6 @@ $(document).ready(function(){
$.ajax({
url: $link.attr("href"),
success: function(data, text){
console.log(data);
$(".srv_insertOptions").remove();
$(".srv_filePane").after(
"<div id='srv_"+ currentInsert +"_insertOptions' class='srv_insertOptions'>"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{% load adminmedia %}
{% load i18n %}
{% if cl.search_fields %}
<div id="toolbar"><form id="changelist-search" action="" method="get">
<div id="toolbar"><form id="changelist-search" action="{{ request.path }}" method="get">
<div><!-- DIV needed for valid HTML -->
<label for="searchbar"><img src="{% admin_media_prefix %}img/admin/icon_searchbox.png" alt="Search" /></label>
<input type="text" size="40" name="{{ search_var }}" value="{{ cl.query }}" id="searchbar" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{% extends "servee/base_site.html" %}
{% load i18n admin_modify adminmedia %}
{% load url from future %}
"
{% block extrahead %}{{ block.super }}
{% url 'admin:jsi18n' as jsi18nurl %}
<script type="text/javascript" src="{{ jsi18nurl|default:"../../../../jsi18n/" }}"></script>
Expand Down
2 changes: 1 addition & 1 deletion servee/frontendadmin/templates/servee/base.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{% load url from future %}<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
"<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="{{ LANGUAGE_CODE|default:"en-us" }}" xml:lang="{{ LANGUAGE_CODE|default:"en-us" }}" {% if LANGUAGE_BIDI %}dir="rtl"{% endif %}>
<head>
<title>{% block title %}{% endblock %}</title>
Expand Down
2 changes: 1 addition & 1 deletion servee/frontendadmin/templates/servee/change_form.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{% extends "servee/base_site.html" %}
{% load i18n admin_modify servee_admin_modify adminmedia %}
{% load url from future %}
"

{% block extrahead %}{{ block.super }}
{% url 'admin:jsi18n' as jsi18nurl %}
Expand Down
50 changes: 29 additions & 21 deletions servee/frontendadmin/templates/servee/change_list.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{% extends "servee/base_site.html" %}
{% load adminmedia admin_list i18n %}
{% load url from future %}
{% load adminmedia admin_list servee_admin_list i18n %}
"
{% block extrastyle %}
{{ block.super }}
<link rel="stylesheet" type="text/css" href="{% admin_media_prefix %}css/changelists.css" />
Expand Down Expand Up @@ -35,22 +35,6 @@

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

{% if not is_popup %}
{% block breadcrumbs %}
<div class="breadcrumbs">
<a href="../../">
{% trans "Home" %}
</a>
&rsaquo;
<a href="../">
{{ app_label|capfirst }}
</a>
&rsaquo;
{{ cl.opts.verbose_name_plural|capfirst }}
</div>
{% endblock %}
{% endif %}

{% block coltype %}flex{% endblock %}

{% block content %}
Expand All @@ -75,7 +59,7 @@
{{ cl.formset.non_form_errors }}
{% endif %}
<div class="module{% if cl.has_filters %} filtered{% endif %}" id="changelist">
{% block search %}{% search_form cl %}{% endblock %}
{% block search %}{% search_form_servee cl %}{% endblock %}
{% block date_hierarchy %}{% date_hierarchy cl %}{% endblock %}

{% block filters %}
Expand All @@ -87,7 +71,7 @@ <h2>{% trans 'Filter' %}</h2>
{% endif %}
{% endblock %}

<form id="changelist-form" action="" method="post"{% if cl.formset.is_multipart %} enctype="multipart/form-data"{% endif %}>{% csrf_token %}
<form id="changelist-form" action="{{ request.path }}" method="post"{% if cl.formset.is_multipart %} enctype="multipart/form-data"{% endif %}>{% csrf_token %}
{% if cl.formset %}
{{ cl.formset.management_form }}
{% endif %}
Expand All @@ -101,4 +85,28 @@ <h2>{% trans 'Filter' %}</h2>
</form>
</div>
</div>
{% endblock %}
<script type="text/javascript">
$(document).ready(function(){
var $base = $("#changelist");
var $par = $base.parent();
$par.find("a").click(function(e){
$par.load(this.href);
e.preventDefault();
return false;
});
$par.find("form").submit(function(e){
$form = $(this);
$.ajax({
url: $form.attr("action"),
type: $form.attr("method"),
data: $form.serialize(),
success: function(data, text){
$par.html(data);
}
});
e.preventDefault();
return false;
});
});
</script>
{% endblock %}
63 changes: 57 additions & 6 deletions servee/frontendadmin/templatetags/frontendadmin_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from classytags.arguments import Argument
from django import template
from django.core.urlresolvers import reverse
from django.contrib.contenttypes.models import ContentType
from django.db.models import Model
from django.db.models.query import QuerySet
from django.utils.translation import ugettext_lazy as _
Expand All @@ -20,14 +21,21 @@ class AddObject(Tag):
name = "frontendadmin_add"

options = Options(
Argument('queryset_instance', required=True),
Argument('querysetish', required=True),
Argument('label', required=False, resolve=False),
Argument('add_class', required=False, resolve=False)
)

def render_tag(self, context, queryset_instance, label=None, add_class=None):
if not isinstance(queryset_instance, QuerySet):
raise template.TemplateSyntaxError, "'%s' argument must be a queryset" % queryset_instance
def render_tag(self, context, querysetish, label=None, add_class=None):
if isinstance(querysetish, basestring):
app_label, model_name = querysetish.lower().split(".")
content_type = ContentType.objects.get(app_label=app_label, model=model_name)
model = content_type.model_class()
queryset_instance = model._default_manager.get_query_set()
elif isinstance(querysetish, QuerySet):
queryset_instance = querysetish
else:
raise template.TemplateSyntaxError, "'%s' argument must be a queryset or string representation" % queryset_instance

user = context["request"].user
app_label = queryset_instance.model._meta.app_label
Expand Down Expand Up @@ -66,7 +74,6 @@ def render_tag(self, context, model_instance, label=None, add_class=None):
return ""

if not label:
print "derp"
label = _("Change")

return '<a class="frontendadmin frontendadmin_edit %s" href="%s">%s</a>' % (
Expand All @@ -79,6 +86,49 @@ def render_tag(self, context, model_instance, label=None, add_class=None):
unicode(label)
)


class ListObjects(Tag):
name = "frontendadmin_list"

options = Options(
Argument('modelish', required=True),
Argument('label', required=False, resolve=False),
Argument('add_class', required=False, resolve=False)
)

def render_tag(self, context, modelish, label=None, add_class=None):
if isinstance(modelish, basestring):
app_label, model_name = modelish.lower().split(".")
content_type = ContentType.objects.get(app_label=app_label, model=model_name)
model = content_type.model_class()
elif isinstance(modelish, QuerySet):
model = modelish.model
elif isinstance(modelish, Model):
model = modelish
else:
raise template.TemplateSyntaxError, "'%s' argument must be a model-instance, queryset, or string representation" % modelish

user = context["request"].user
app_label = model._meta.app_label
model_name = model._meta.module_name

if not check_permission(user, "change", app_label, model_name):
return ""

if not label:
label = _("List")

return '<a class="frontendadmin frontendadmin_list %s" href="%s">%s</a>' % (
add_class,
reverse("servee:%s_%s_changelist" % (
app_label,
model_name,
)
),
unicode(label)
)


class DeleteObject(Tag):
name = "frontendadmin_delete"

Expand Down Expand Up @@ -114,4 +164,5 @@ def render_tag(self, context, model_instance, label=None, add_class=None):

register.tag(AddObject)
register.tag(ChangeObject)
register.tag(DeleteObject)
register.tag(DeleteObject)
register.tag(ListObjects)
Loading

0 comments on commit c40067d

Please sign in to comment.