Skip to content
Browse files

Code review changes to file image functionality(changed hardcoded url…

…s, changed file retrieval logic, fixed validation on save)
  • Loading branch information...
1 parent 69b097e commit 5b3b5c4dc8e02d502260e68298bedeaccfeac5c1 Amalia Gherasim committed May 17, 2012
View
42 cmsplugin_image/static/js/image_field.js
@@ -1,3 +1,6 @@
+// Holds the current image field name
+var image_field_name = '';
+
function id_to_windowname(text) {
text = text.replace(/\./g, '__dot__');
text = text.replace(/\-/g, '__dash__');
@@ -10,7 +13,8 @@ function windowname_to_id(text) {
return text;
}
-function showRelatedObjectLookupPopup(triggeringLink) {
+function showRelatedObjectLookupPopup(triggeringLink, field_name) {
+ image_field_name = field_name;
var name = triggeringLink.id.replace(/^lookup_/, '');
name = id_to_windowname(name);
var href;
@@ -26,42 +30,36 @@ function showRelatedObjectLookupPopup(triggeringLink) {
dismissRelatedImageLookupPopup = function(win, chosenId, chosenThumbnailUrl, chosenDescriptionTxt) {
win.close();
-
- var jxhr = $.ajax({
- url: "/imagefield/get_file/",
+ var jxhr = jQuery.ajax({
+ url: filer_image_url,
data: {'id': chosenId},
- beforeSend: function(xhr){
- //add before send logic here if required
- },
success: function(data){
- if (data){
- document.getElementById('var_'+field_name).value = data;
- $(".error_"+field_name).html('');
+ if (data.url){
+ jQuery("td.invalid_image").html('');
+ jQuery('#var_'+image_field_name).val(data.url);
}
else{
- $(".error_"+field_name).html('Please select a valid image type');
+ jQuery("td.error_"+image_field_name).html('Please select a valid image type.');
}
},
error: function(data){
- alert('error');
- },
- complete: function(data){
-
+ alert('Error retrieving file information.');
}
});
return jxhr;
};
-$(document).ready(function(){
- $("form#smartsnippetpointer_form").submit(function(){
+jQuery(document).ready(function(){
+ jQuery("form#smartsnippetpointer_form").submit(function(){
var is_valid = true;
- $("input.filer_image_url").each(function(val) {
- if ($(this).val()==''){
+ jQuery("td.invalid_image").html('');
+ jQuery("input.filer_image_url").each(function(val) {
+ if (jQuery(this).val()==''){
is_valid = false;
- var elem = $(this).attr('id').substr(4, -1);
- $(elem).html('Please select a valid image type');
- }
+ image_field_name = jQuery(this).attr('id').replace('var_', '');
+ jQuery('td.error_'+image_field_name).html('Please select a valid image type.');
+ }
});
return is_valid;
}) ;
View
6 cmsplugin_image/templates/smartsnippets/widgets/imagefield/widget.html
@@ -1,15 +1,15 @@
<link rel="stylesheet" type="text/css" href="{{ STATIC_URL }}css/style.css"/>
<script type="text/javascript" src="{{ STATIC_URL }}js/image_field.js"></script>
<script type="text/javascript">
- var field_name = '{{ field.name }}';
+ var filer_image_url = "{% url cmsplugin_image.views.get_file %}";
</script>
<tr>
<td class="error_{{ field.name }} invalid_image" colspan="2"></td>
</tr>
<tr>
<td><label for="var_{{ field.name }}">{{ field.name }}</label></td>
- <td><input id="var_{{ field.name }}" type="text" name="_{{ field.name }}_" value="{{ field.value }}" size="100" class="filer_image_url"/>
- <a href="/admin/filer/folder/" class="related-lookup" id="lookup_id_image" title="Lookup" onclick="return showRelatedObjectLookupPopup(this);">
+ <td><input id="var_{{ field.name }}" type="text" name="_{{ field.name }}_" value="{{ field.value }}" size="100" class="filer_image_url" />
+ <a href="{% url admin:filer_folder_changelist %}" class="related-lookup" id="lookup_id_image" title="Lookup" onclick="return showRelatedObjectLookupPopup(this, '{{ field.name }}');">
<img src="{{ STATIC_URL }}admin/img/admin/selector-search.gif" width="16" height="16" alt="Lookup">
</a>
</td>
View
8 cmsplugin_image/views.py
@@ -13,11 +13,13 @@
def get_file(request):
if not request.is_ajax():
return HttpResponseForbidden()
+ result = {}
file_id = request.GET.get('id');
try:
file = File.objects.get(pk=file_id)
except File.DoesNotExist:
file = None
- if file and file.__class__ == Image:
- return HttpResponse(json.dumps(file.file.url), mimetype="application/json")
- return HttpResponse(json.dumps(None), mimetype="application/json")
+
+ result['url'] = file.file.url if file and file.__class__ == Image else ''
+
+ return HttpResponse(json.dumps(result), mimetype="application/json")

0 comments on commit 5b3b5c4

Please sign in to comment.
Something went wrong with that request. Please try again.