Skip to content

[BUGFIX] fix JS error and backend bug #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Feb 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion django_dyn_dt/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

<!-- data-tables -->
<link href="https://cdn.jsdelivr.net/npm/simple-datatables@latest/dist/style.css" rel="stylesheet" type="text/css">
<script src="https://cdn.jsdelivr.net/npm/simple-datatables@latest" type="text/javascript"></script>
<script src="https://cdn.jsdelivr.net/npm/simple-datatables@3.2.0" type="text/javascript"></script>

<!-- bootstrap5 -->
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
Expand Down
2 changes: 1 addition & 1 deletion django_dyn_dt/templates/static/src/controller/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ export const exportData = (dataTable, type) => {

const searchParam = new URLSearchParams(window.location.search).get('search') || ''

const hiddenColumns = myData.headings.filter((d,i) => !dataTable.columns().visible(i))
const hiddenColumns = myData.headings.filter((d,i) => !dataTable.columns.visible(i))

fetch (`/datatb/${modelName}/export/`,
{method: 'POST',body: JSON.stringify({
Expand Down
29 changes: 22 additions & 7 deletions django_dyn_dt/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from django.core.exceptions import ValidationError
from django.db.models import Q
from django.db.models.fields.related import RelatedField
from django.http import HttpResponse
from django.shortcuts import render

Expand All @@ -22,18 +23,17 @@
DYNAMIC_DATATB = {}

try:
DYNAMIC_DATATB = getattr(settings, 'DYNAMIC_DATATB')
except:
pass
DYNAMIC_DATATB = getattr(settings, 'DYNAMIC_DATATB')
except:
pass

# TODO: 404 for wrong page number
def data_table_view(request, **kwargs):
try:
model_class = Utils.get_class(DYNAMIC_DATATB, kwargs.get('model_name'))
except KeyError:
return render(request, '404.html', status=404)
headings = [field.name for field in model_class._meta.get_fields()]

headings = _get_headings(model_class)
page_number = int(request.GET.get('page', 1))
search_key = request.GET.get('search', '')
entries = int(request.GET.get('entries', 10))
Expand Down Expand Up @@ -149,8 +149,7 @@ def export(request, **kwargs):
export_type = request_body.get('type', 'csv')
filter_options = Q()

headings = filter(lambda field: field.name not in hidden,
[field for field in model_class._meta.get_fields()])
headings = filter(lambda field: field.name not in hidden, _get_headings(model_class))
headings = list(headings)
for field in headings:
field_name = field.name
Expand Down Expand Up @@ -230,3 +229,19 @@ def get_random_string(length):
# choose from all lowercase letter
letters = string.ascii_lowercase
return ''.join(random.choice(letters) for i in range(length))


def _get_headings(model_class, filter_relations=True):
headings = []
for field in model_class._meta.get_fields():
if filter_relations and _is_relation_field(field):
continue
headings.append(field.name)
return headings

def _is_relation_field(field):
is_many_to_many_field = field.many_to_many is not None
is_many_to_one_field = field.many_to_one is not None
is_one_to_many_field = field.one_to_many is not None
is_one_to_one_field = field.one_to_one is not None
return is_many_to_many_field or is_many_to_one_field or is_one_to_many_field or is_one_to_one_field