GithubHelp home page GithubHelp logo

idlesign / django-etc Goto Github PK

View Code? Open in Web Editor NEW
38.0 5.0 1.0 139 KB

Tiny stuff for Django that won't fit into separate apps.

Home Page: https://github.com/idlesign/django-etc

License: BSD 3-Clause "New" or "Revised" License

Python 99.30% HTML 0.70%
python django miscellaneous utils gravatar

django-etc's Introduction

django-etc

https://github.com/idlesign/django-etc

Description

Tiny stuff for Django that won't fit into separate apps.

Utils

  • etc.toolbox.get_site_url does its best to provide you with a site URL whether request object is available or not.
  • etc.toolbox.import_app_module imports and returns a module from a specific app by its name.
  • etc.toolbox.import_project_modules imports modules from registered apps using given module name and returns them as a list.

Models

  • etc.toolbox.InheritedModel allows to override fields attributes in inherited models.
  • etc.toolbox.get_model_class_from_string allows getting model class from its string representation.
  • etc.toolbox.get_model_class_from_settings allows getting model class from its string representation in settings module.
  • etc.toolbox.ChoicesEnumMixin helps to define choices for models using Enum from Python 3.
  • etc.toolbox.choices_list helps to define choices for models, that could be addressed later as dictionaries.
  • etc.toolbox.get_choices returns model field choices from a given choices list.

Admin

  • etc.admin.CustomModelPage allows easy construction of custom admin pages processing user input.

Forms

  • etc.toolbox.set_form_widgets_attrs allows bulk apply HTML attributes to every field widget of a given form.

Template tags

  • model_field:
    • model_field_verbose_name returns model field verbose name.
    • model_field_help_text returns model field help text.
  • model_meta:
    • model_meta_verbose_name returns model verbose name singular.
    • model_meta_verbose_name_plural returns model verbose name plural.
  • gravatar
    • gravatar_get_url returns Gravatar image URL for a given string or UserModel.
    • gravatar_get_img returns Gravatar image HTML tag for a given string or UserModel.
  • etc_misc
    • site_url does its best to provide you with a site URL whether request object is available or not.
    • include_ allows a template name to include template variables. Allows fallback template if the target is not found.

Documentation

http://django-etc.readthedocs.org/

django-etc's People

Contributors

idlesign avatar osoloturk avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

Forkers

osoloturk

django-etc's Issues

How to export csv file

Thank for the package useful for me, I created some Postgresql functions and I want to run it on Django admin then export CSV file, so this is my way, but in save(). Could I perform HttpResponse for export csv file ? Thank you

class artist_report_card(CustomModelPage):
    title = 'Aritst report card'  # set page title

    # Define some fields.
    input_date = models.DateField()
    bound_admin = admins.CustomPageModelAdmin  # set admin class for this page

    def save(self):
        # Implement data handling from self attributes here.
        # self.bound_admin has some useful methods.
        # self.bound_request allows you to access current HTTP request.
        self.bound_admin.message_success(self.bound_request, f'Hey, done!')
        curr = connection.cursor()
        curr.execute(
            f"SELECT public.artist_report_card('{self.input_date}')")
        data = curr.fetchall()
        curr.close()
        response = self.bound_request.HttpResponse(content_type='text/csv')
        response['Content-Disposition'] = f"'attachment; filename={self.input_date}.csv'"
        for row in data:
        writer = csv.writer(response, delimiter=',')
        writer.writerow(row)
artist_report_card.register()
        ```

Get translated verbose_name

All the items in my model are being translated by django.utils.translation
For example

code = models.CharField(max_length=200, null=True, blank=True,  unique=True, 
help_text=_('Product catalog code unique identifier'),
verbose_name=_('Product Code'))

Is there any way to get the verbose_name string translated?

Submit input value not in request.POST

I added some more buttons in the submit_line.html as different guides say (https://stackoverflow.com/questions/67463654/add-custom-button-near-save-button-django-admin, you%20need%20to%20override%20%22change_form.).

I just don't get the value of the button I pressed, I noticed that it works with all django models, except with the admins created with etc.admin.CustomModelPage.

Not even if I try to activate the save buttons, etc like this:

def view_custom(self, request):
        context: dict = {
            'show_save_and_continue': True,
            'show_save_and_add_another': True,
            'show_save_as_new': True,
            'show_save': True,
            .........
          return self._changeform_view(request, object_id=None, form_url='', extra_context=context)

The value of the submit on click is not passed.

django.db.utils.ProgrammingError: (1146, "Table 'my_db_name.admin_mypage' doesn't exist")

Hi!
I'm a self-taught programmer and now working on a small app which is using by my classmates. I manage the database through Django Admin.
Because there are several models in this app, when I try to input data, I need to input data in multiple models, it's boring and very easy to make mistakes.
So I want to have a page where I can input all data here, and directly distribute them to the proper model by click the Save button.
I used the django-etc, I found that it is very easy to make a new page in admin site, there are input box or text area and so on...your work is excellent!
While developing, I want to check the data in terminal first, but I got an error:
django.db.utils.ProgrammingError: (1146, "Table 'my_db.admin_mypage' doesn't exist")

here is my code:

class MyPage(CustomModelPage):
    title = 'Add Collection and Info'  # set page title

    # Define some fields.
    collection_title = models.CharField(unique=True)
    collection_description = models.TextField(blank=True)
    collection_remark = models.CharField(blank=True)
    collection_released_at = models.DateTimeField()
    collection_is_active = models.BooleanField(default=False)
    info_bulk_content = MDTextField(blank=True)
    ......

    admin_cls = MyPageModelAdmin  # set admin class for this page

    def save(self):
        print(self.collection_title)
        pass
        # self.bound_admin.message_success(self.bound_request, f'Hey, done!')
        # print(11111111111111111111111111111111111111111111111111111111111111)
        # super().save()

How can I fixed this? I hope that the fields starts with "collection_" can go to Collection Model, the fields starts with "info_" can go to Info Model, and also make some links....

Any help is highly appricated. Thank you!

CustomModelPage: `makemigrations` without app name creates a migration in django.contrib.admin.migrations

Hello!

I want to utilise the CustomModelPage to do some custom action requiring user input without a model.

Issue

Running python manage.py makemigrations a new migration is generated in the admin package.

import json

from django.db import models

from etc.admin import CustomModelPage, admins
import app.django_tasks as tasks


class TaskManagerAdmin(admins.CustomPageModelAdmin):
    fields = ('args', 'kwargs', 'sync')


class TaskManagerPage(CustomModelPage):
    title = 'Task Manager'  # set page title

    # Define some fields.
    args = models.CharField('args', max_length=10000, blank=True, default='')
    kwargs = models.TextField('kwargs', max_length=100000, blank=True, default='')
    sync = models.BooleanField('Run synchronously', default=True)

    admin_cls = TaskManagerAdmin  # set admin class for this page

    def save(self):
            ...

TaskManagerPage.register()

Expectation

I would expect no model to be required for this.

Environment

Django v3.2
Python v3.8.10

Get help_text from model instance

Hi there. I'm a django beginner and I'm trying to figure out how to display a field's help_text in a DetailView. I see you've got a template tag that can display the help text based on a model.field reference, but I need to display the text based on a field from a model instance object, where object is the entire record and object.field is the field.

I want to do something like this:

{% model_field_help_text from object %}

In the wider context of displaying some data in a DetailView template like this:

  <tr>
    <td>
      <strong>{% model_field_verbose_name from object.elements %}</strong>
      {% model_field_help_text from object.elements %}
    </td>
    <td>{{ object.elements }}</td>
  </tr>

Is it possible to somehow use the existing tags by referencing an instance, instead of a model? Thanks

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.