2

I'm trying to find a way to build a robust report library that I can use with a Django project.

Essentially, what I'm looking for is a way to access a list of functions(reports) that I can allow an end-user to attach to a Django model.

For example, let's say I have an Employee object. Well, I have a module called reports.py that has a growing list of possible reports that take an employee object and output a report, usually in JSON form. There might be number of timecards submitted, number of supervisions created, etc.

I want to be able to link those changing report lists to the Employee object via a FK called (job description), so admins can create custom reports per job description.

What I've tried:

  • Direct model methods: good for some things, but it requires a programmer to call them in a template or via API to generate some output. Since the available reports are changing, I don't want to hard-code anything and would rather allow the end-user to choose from a list of available reports and attach them to a related model (say a JobDescription).

  • dir(reports): I could offer up a form where the select values are the results from dir(reports), but then I'd get the names of variables/libraries called in the file, not just a list of available reports

Am I missing something? Is there a way to create a custom class from which I can call all methods available? Where would I even start with that type of architecture?

I really appreciate any sort of input re: the path to take. Really just a 'look in this direction' response would be really appreciated.

Craig
  • 402
  • 1
  • 4
  • 15

1 Answers1

1

What I would do is expand on your dir(reports) idea and create a dynamically loaded module system. Have a folder with .py files containing module classes. Here's an example of how you can dynamically load classes in Python.

Each class would have a static function called getReportName() so you could show something readable to the user, and a member function createReport(self, myModel) which gets the model and does it's magic on it.

And then just show all the possible reports to the user, user selects one and you run the createReport on the selected class.

In the future you might think about having different report folders for different models, and this too should be possible by reflection using model's __name__ attribute.

Community
  • 1
  • 1
Ritave
  • 1,333
  • 9
  • 25
  • So in that example, would each report be a standalone class? – Craig Mar 30 '16 at 20:41
  • @Craig Yes, a small one, but it would allow you a lot more flexibillity in the future in case you would want to add a report description or anything additional – Ritave Mar 30 '16 at 20:43