-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #62 from beshrkayali/feature/forms-plugin
Add forms plugin and use it as base for Image plugin
- Loading branch information
Showing
10 changed files
with
204 additions
and
52 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
|
@@ -17,3 +17,9 @@ htmlcov/ | |
node_modules/ | ||
.next/ | ||
example/db.sqlite3 | ||
|
||
# Emacs Temp/Autosaves | ||
*~ | ||
\#*\# | ||
.#*.* | ||
*_flymake* |
This file contains 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 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,9 @@ | ||
from cio.plugins.base import BasePlugin | ||
|
||
|
||
class DjediPlugin(BasePlugin): | ||
def get_editor_context(self, **kwargs): | ||
""" | ||
Returns custom context | ||
""" | ||
return kwargs |
This file contains 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,67 @@ | ||
import json | ||
from djedi.plugins.base import DjediPlugin | ||
from django import forms | ||
|
||
|
||
def deprefix(s): | ||
# Remove prefix (anything including and before __) | ||
return s.rpartition('__')[-1] | ||
|
||
|
||
def get_custom_render_widget(cls): | ||
class CustomRenderWidget(cls): | ||
def render(self, *args, **kwargs): | ||
name = kwargs.pop("name", None) | ||
|
||
if not name: | ||
name = args[0] | ||
args = args[1:] | ||
|
||
name = deprefix(name) | ||
|
||
return super(CustomRenderWidget, self).render( | ||
"data[%s]" % name, | ||
*args, | ||
**kwargs | ||
) | ||
|
||
return CustomRenderWidget | ||
|
||
|
||
class BaseEditorForm(forms.Form): | ||
def __init__(self, *args, **kwargs): | ||
super(BaseEditorForm, self).__init__(*args, **kwargs) | ||
|
||
for field in list(self.fields.keys()): | ||
self.fields[field].widget.__class__ = get_custom_render_widget( | ||
self.fields[field].widget.__class__ | ||
) | ||
|
||
|
||
class FormsBasePlugin(DjediPlugin): | ||
ext = None | ||
|
||
@property | ||
def forms(self): | ||
return {} | ||
|
||
def get_editor_context(self, **context): | ||
context.update( | ||
{"forms": { | ||
tab: form() | ||
for tab, form in self.forms.items() | ||
}} | ||
) | ||
|
||
return context | ||
|
||
def save(self, data, dumps=True): | ||
data = self.collect_forms_data(data) | ||
return json.dumps(data) if dumps else data | ||
|
||
def collect_forms_data(self, data): | ||
return { | ||
deprefix(field): data.get(deprefix(field)) | ||
for tab, form in self.forms.items() | ||
for field in form.base_fields.keys() | ||
} |
This file contains 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 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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 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 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