Skip to content

Commit

Permalink
Allows Show/hide related fields.
Browse files Browse the repository at this point in the history
Fixed datatables responsive headers.
Include shortcut to build without download files process of sdist
Fixed select multiple convert to json, and convert repeted names as list of elements
Create serializers fields for GTDateField and GTDateTimeField preventing empty  values wrong validation
Update version to 0.3.17.
  • Loading branch information
luisza committed Jul 2, 2023
1 parent a3160d3 commit 00e2acf
Show file tree
Hide file tree
Showing 12 changed files with 229 additions and 55 deletions.
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,7 @@ sdist: clean
python3 -m build
ls -l dist

fuzzysdist:
cd demo && python manage.py makemigrations && python manage.py loaddevstatic && python manage.py createbasejs
cd djgentelella && django-admin compilemessages -l es
python3 -m build
10 changes: 6 additions & 4 deletions demo/demoapp/datatables/serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

from demoapp.models import Person, Country
from djgentelella.fields.drfdatetime import DateRangeTextWidget, DateTimeRangeTextWidget
from djgentelella.serializers import GTDateField, GTDateTimeField


class PersonFilterSet(FilterSet):
Expand Down Expand Up @@ -49,10 +50,11 @@ class PersonDataTableSerializer(serializers.Serializer):


class PersonCreateSerializer(serializers.ModelSerializer):
born_date = serializers.DateField(
input_formats=[formats.get_format('DATE_INPUT_FORMATS')[0]],
format=formats.get_format('DATE_INPUT_FORMATS')[0])
last_time = serializers.DateTimeField(
born_date = GTDateField()
# also can overwrite input_formats and formt
last_time = GTDateTimeField(
allow_empty_str=True,
# True it is default value allow "" as none and prevent validation error
input_formats=[formats.get_format('DATETIME_INPUT_FORMATS')[0]],
format=formats.get_format('DATETIME_INPUT_FORMATS')[0])

Expand Down
6 changes: 4 additions & 2 deletions demo/demoapp/templates/object_management.html
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ <h3 class="heading-1"><span> {% trans 'List of Objects' %} </span></h3>
destroy: "#delete_obj_modal",
}
var obj_actions = {
instance_action: [], //table_actions
obj_action: [], // object_actions
table_actions: [], //table_actions
object_actions: [], // object_actions
title: gettext('Actions'),
className: "no-export-col"
}
Expand All @@ -103,6 +103,8 @@ <h3 class="heading-1"><span> {% trans 'List of Objects' %} </span></h3>
let ocrud=ObjectCRUD("setmeunique", objconfig)
ocrud.init();



</script>


Expand Down
2 changes: 1 addition & 1 deletion djgentelella/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '0.3.16'
__version__ = '0.3.17'
33 changes: 33 additions & 0 deletions djgentelella/serializers/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from django.utils import formats
from rest_framework.fields import DateField, DateTimeField


class GTDateField(DateField):

def __init__(self, allow_empty_str=True, format=None, input_formats=None, **kwargs):
self.allow_empty_str = allow_empty_str
self.format = format or formats.get_format('DATE_INPUT_FORMATS')[0]
self.input_formats = input_formats or [
formats.get_format('DATE_INPUT_FORMATS')[0]]

super().__init__(format=self.format, input_formats=self.input_formats, **kwargs)

def to_internal_value(self, value):
if not value and self.allow_empty_str:
return None
return super().to_internal_value(value)


class GTDateTimeField(DateTimeField):
def __init__(self, allow_empty_str=True, format=None, input_formats=None, **kwargs):
self.allow_empty_str = allow_empty_str
self.format = format or formats.get_format('DATETIME_INPUT_FORMATS')[0]
self.input_formats = input_formats or [
formats.get_format('DATETIME_INPUT_FORMATS')[0]]

super().__init__(format=self.format, input_formats=self.input_formats, **kwargs)

def to_internal_value(self, value):
if not value and self.allow_empty_str:
return None
return super().to_internal_value(value)
15 changes: 15 additions & 0 deletions djgentelella/static/gentelella/js/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -1351,8 +1351,23 @@ function showHideRelatedFormFields(instance){
});
instance.trigger('change');
}
var relh = instance.data('relhidden');
if(relh != undefined ){
var relateditemsh = create_identifiers(relh.split(';'));
instance.on('change', function(e){
for(var x=0; x<relateditemsh.length; x++){
if(this.checked){
$(relateditemsh[x]).closest(parentclass).hide();
}else{
$(relateditemsh[x]).closest(parentclass).show();
}
}
});
instance.trigger('change');
}
}


function upload_files(callback, meta, file, image, video) {
var csrftoken = getCookie('csrftoken');
$.ajaxSetup({
Expand Down
16 changes: 15 additions & 1 deletion djgentelella/static/gentelella/js/base/booleanFields.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,18 @@ function showHideRelatedFormFields(instance){
});
instance.trigger('change');
}
}
var relh = instance.data('relhidden');
if(relh != undefined ){
var relateditemsh = create_identifiers(relh.split(';'));
instance.on('change', function(e){
for(var x=0; x<relateditemsh.length; x++){
if(this.checked){
$(relateditemsh[x]).closest(parentclass).hide();
}else{
$(relateditemsh[x]).closest(parentclass).show();
}
}
});
instance.trigger('change');
}
}
40 changes: 34 additions & 6 deletions djgentelella/static/gentelella/js/datatables.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,31 @@ function formatDataTableParams(dataTableParams, settings){
return data;
}

function addSearchInputsAndFooterDataTable(dataTable, tableId) {
var default_table_columns_display = {}
function addSearchInputsAndFooterDataTable(dataTable, tableId, columns) {
// takes care of adding the search inputs to each of the columns of the datatable, it will
// hide/display them according to how the table changes in the responsive mode

if (!(tableId in default_table_columns_display)){
default_table_columns_display[tableId]=[];
}
if (columns == undefined){
columns=default_table_columns_display[tableId];
}else{
default_table_columns_display[tableId]=columns;
}
if($(tableId + ' thead tr').length < 2){ // clone the tr only if it wasn't cloned before
$(tableId + ' thead tr').clone(false).appendTo(tableId + ' thead');
}

$(tableId + ' thead tr:eq(1) th').each(function (i) { // add search fields if they are not there already and the column is visible
var currentColumn = dataTable.column(i);

var is_display = true
if(i<columns.length){
is_display=columns[i]
}
var columnType = dataTable.settings()[0].aoColumns[i].type; // get the field type
//currentColumn.responsiveHidden()
if (currentColumn.visible() && columnType !== 'actions') { // column is visible
if (is_display && currentColumn.visible() && columnType !== 'actions') { // column is visible
$(this).css('display', ''); // when it was cloned it might have had display:none specified
if($(this).find('input').length === 0 && $(this).find('select').length === 0) { // add the input/select just if it doesn't exist already
var title = currentColumn.header().textContent; // get the field name
Expand Down Expand Up @@ -130,6 +141,23 @@ function listobjprint(data, type, row, meta){
}
return txt != "" ? txt : "---";
};

function gt_print_list_object(display_name){
return function (data, type, row, meta){
var txt = "";
if(data != null ){
if(Array.isArray(data) ){
for(var x=0; x<data.length; x++){
txt += data[x][display_name] + "<br>"
}
}else{
txt += data[display_name] + "<br>"
}
}
return txt != "" ? txt : "---";
}
}

function showlink(data, type, row, meta){ return data ? '<a href="'+data+'" target="_blank" class="btn btn-xs btn-success"> '+gettext('More')+' </a>': ''; };
function downloadlink(data, type, row, meta){ return data ? '<a href="'+data+'" target="_blank" class="btn btn-xs btn-success"> '+gettext('Show')+' </a>': ''; };
function objshowlink(data, type, row, meta){ return data ? '<a href="'+data.url+'" target="_blank" class="'+(data.class!=undefined ? data.class : 'link')+'"> '+data.display_name+ '</a>': ''; };
Expand Down Expand Up @@ -188,11 +216,11 @@ function gtCreateDataTable(id, url, table_options={}){
var instance = $(id).DataTable(default_options);
if(options.addfilter){
instance.on('init.dt', function(e, settings, json){
addSearchInputsAndFooterDataTable(instance, id);
addSearchInputsAndFooterDataTable(instance, id, undefined);
});

instance.on('responsive-resize', function (e, datatable, columns) {
addSearchInputsAndFooterDataTable(instance, id);
addSearchInputsAndFooterDataTable(instance, id, columns);
});
}
return instance;
Expand Down
Loading

0 comments on commit 00e2acf

Please sign in to comment.