-
Notifications
You must be signed in to change notification settings - Fork 7
WIP django admin #109
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
Open
miki725
wants to merge
1
commit into
master
Choose a base branch
from
admin
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
WIP django admin #109
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -47,6 +47,7 @@ docs/_build | |
| # IDEs | ||
| /.idea | ||
| .venv | ||
| .envrc | ||
| .pytest_cache | ||
| db.sqlite3 | ||
| .mypy_cache | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| # -*- coding: utf-8 -*- | ||
| from __future__ import absolute_import, print_function, unicode_literals |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| # -*- coding: utf-8 -*- | ||
| from __future__ import absolute_import, print_function, unicode_literals | ||
| from functools import wraps | ||
|
|
||
| from django.contrib.admin import options | ||
| from django.contrib.admin.options import ModelAdmin as DjangoModelAdmin | ||
|
|
||
| from ..forms import ModelForm, modelform_factory | ||
|
|
||
|
|
||
| def swap(module, attr, replacement): | ||
| def wrapper(f): | ||
| @wraps(f) | ||
| def inner(*args, **kwargs): | ||
| original = getattr(module, attr) | ||
| setattr(module, attr, replacement) | ||
| try: | ||
| return f(*args, **kwargs) | ||
| finally: | ||
| setattr(module, attr, original) | ||
|
|
||
| return inner | ||
|
|
||
| return wrapper | ||
|
|
||
|
|
||
| class ModelAdmin(DjangoModelAdmin): | ||
| form = ModelForm | ||
|
|
||
| @swap(options, "modelform_factory", modelform_factory) | ||
| def get_form(self, *args, **kwargs): | ||
| return super(ModelAdmin, self).get_form(*args, **kwargs) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -48,19 +48,29 @@ | |
| } | ||
|
|
||
|
|
||
| class DjangoCompiledQuery(object): | ||
| def __init__(self, query): | ||
| self.query = query | ||
| self.order_by = [] | ||
| self.select_related = True | ||
|
|
||
|
|
||
| class Query(sa.orm.Query): | ||
| """ | ||
| A customized sqlalchemy query | ||
| """ | ||
|
|
||
| @property | ||
| def model(self): | ||
| return self._only_full_mapper_zero("get").class_ | ||
|
|
||
| def get(self, *args, **kwargs): | ||
| """ | ||
| Return an instance based on the given primary key identifier, either as args or | ||
| kwargs for composite keys. If no instance is found, returns ``None``. | ||
| """ | ||
| if kwargs: | ||
| mapper = self._only_full_mapper_zero("get") | ||
| pk = meta.model_info(mapper).primary_keys_from_dict(kwargs) | ||
| pk = meta.model_info(self.model).primary_keys_from_dict(kwargs) | ||
|
|
||
| if pk is not None: | ||
| return super(Query, self).get(pk) | ||
|
|
@@ -83,9 +93,22 @@ def filter(self, *args, **kwargs): | |
| args = args + tuple(self._lookup_to_expression(k, v) for k, v in kwargs.items()) | ||
| return super(Query, self).filter(*args) | ||
|
|
||
| def get_queryset(self): | ||
| """ | ||
| Necessary to comply with Django API | ||
| """ | ||
| return self | ||
|
|
||
| @property | ||
| def query(self): | ||
| """ | ||
| Necessary to comply with Django API | ||
| """ | ||
| return DjangoCompiledQuery(self) | ||
|
|
||
| def _lookup_to_expression(self, lookup, value): | ||
| parts = lookup.split(LOOKUP_SEP) | ||
| info = meta.model_info(self._only_full_mapper_zero("get")) | ||
| info = meta.model_info(self.model) | ||
|
|
||
| props = dict(info.column_properties) | ||
| lhs = None | ||
|
|
@@ -121,6 +144,13 @@ def _lookup_to_expression(self, lookup, value): | |
|
|
||
| return lhs == value | ||
|
|
||
| # todo need to make len(query) == len(cl.result_list.all()) without recursion | ||
| # currently requires patch in django/contrib/admin/options.py | ||
| # 1792 - 'selection_note': _('0 of %(cnt)s selected') % {'cnt': len(cl.result_list)}, | ||
| # 1792 + 'selection_note': _('0 of %(cnt)s selected') % {'cnt': len(cl.result_list.all())}, | ||
| # def __len__(self): | ||
| # return self.count() | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. hmm,
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. just tried. nope. still recursion |
||
|
|
||
|
|
||
| class QueryProperty(object): | ||
| def __init__(self, db, model=None, *args, **kwargs): | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,19 +1,22 @@ | ||
| -e . | ||
| pytest | ||
| pytest-cov | ||
| attrs | ||
| beautifulsoup4 | ||
| coveralls | ||
| django-extensions | ||
| flake8 | ||
| simplejson | ||
| sphinx-autobuild | ||
| gitchangelog | ||
| yapf | ||
| mock; python_version <= '2.7' | ||
| pdbpp | ||
| pre_commit | ||
| psycopg2 | ||
| ptpython | ||
| pytest | ||
| pytest-cov | ||
| simplejson | ||
| Sphinx | ||
| sphinx-autobuild | ||
| sphinx_rtd_theme | ||
| mock; python_version <= '2.7' | ||
| beautifulsoup4 | ||
| attrs | ||
| tox | ||
| coveralls | ||
| sqlalchemy_utils | ||
| psycopg2 | ||
| tox | ||
| Werkzeug | ||
| yapf |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,18 @@ | ||
| # -*- coding: utf-8 -*- | ||
| from __future__ import absolute_import, print_function, unicode_literals | ||
|
|
||
| from django.contrib import admin # noqa | ||
| from django.contrib import admin | ||
|
|
||
| from django_sorcery.admin.options import ModelAdmin | ||
|
|
||
| # Register your models here. | ||
| from .models import Choice, Question | ||
|
|
||
|
|
||
| @admin.register(Question) | ||
| class QuestionAdmin(ModelAdmin): | ||
| pass | ||
|
|
||
|
|
||
| @admin.register(Choice) | ||
| class ChoiceAdmin(ModelAdmin): | ||
| pass |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.