svgui/pyjs/jsonrpc/django/jsonrpc.py
author Andrey Skvortsov <andrej.skvortzov@gmail.com>
Wed, 11 Oct 2017 10:34:45 +0300
changeset 1864 9e64afb38963
parent 1850 614396cbffbf
child 1869 49cdd843c006
permissions -rw-r--r--
fix problems with unconnected input of InOut function variables and
enfoce InOut variables to be connected for functions and function blocks
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
     1
# jsonrpc.py
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
     2
#   original code: http://trac.pyworks.org/pyjamas/wiki/DjangoWithPyJamas
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
     3
#   also from: http://www.pimentech.fr/technologies/outils
1783
3311eea28d56 clean-up: fix PEP8 E402 module level import not at top of file
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1769
diff changeset
     4
import datetime
1850
614396cbffbf fix pylint warning '(unused-import), Unused import connectors'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1783
diff changeset
     5
1783
3311eea28d56 clean-up: fix PEP8 E402 module level import not at top of file
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1769
diff changeset
     6
from django.core.serializers import serialize
3311eea28d56 clean-up: fix PEP8 E402 module level import not at top of file
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1769
diff changeset
     7
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
     8
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
     9
from pyjs.jsonrpc import JSONRPCServiceBase
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    10
# JSONRPCService and jsonremote are used in combination to drastically
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    11
# simplify the provision of JSONRPC services.  use as follows:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    12
#
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    13
# jsonservice = JSONRPCService()
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    14
#
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    15
# @jsonremote(jsonservice)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    16
# def test(request, echo_param):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    17
#     return "echoing the param back: %s" % echo_param
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    18
#
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    19
# dump jsonservice into urlpatterns:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    20
#  (r'^service1/$', 'djangoapp.views.jsonservice'),
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    21
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
    22
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    23
class JSONRPCService(JSONRPCServiceBase):
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 728
diff changeset
    24
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    25
    def __call__(self, request, extra=None):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    26
        return self.process(request.raw_post_data)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    27
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
    28
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    29
def jsonremote(service):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    30
    """Make JSONRPCService a decorator so that you can write :
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 728
diff changeset
    31
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    32
    from jsonrpc import JSONRPCService
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    33
    chatservice = JSONRPCService()
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    34
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    35
    @jsonremote(chatservice)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    36
    def login(request, user_name):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    37
        (...)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    38
    """
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    39
    def remotify(func):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    40
        if isinstance(service, JSONRPCService):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    41
            service.add_method(func.__name__, func)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    42
        else:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    43
            emsg = 'Service "%s" not found' % str(service.__name__)
1765
ccf59c1f0b45 clean-up: fix PEP8 W602 deprecated form of raising exception
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1763
diff changeset
    44
            raise NotImplementedError(emsg)
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    45
        return func
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    46
    return remotify
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    47
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    48
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    49
# FormProcessor provides a mechanism for turning Django Forms into JSONRPC
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    50
# Services.  If you have an existing Django app which makes prevalent
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    51
# use of Django Forms it will save you rewriting the app.
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    52
# use as follows.  in djangoapp/views.py :
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    53
#
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    54
# class SimpleForm(forms.Form):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    55
#     testfield = forms.CharField(max_length=100)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    56
#
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    57
# class SimpleForm2(forms.Form):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    58
#     testfield = forms.CharField(max_length=20)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    59
#
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    60
# processor = FormProcessor({'processsimpleform': SimpleForm,
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    61
#                            'processsimpleform2': SimpleForm2})
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    62
#
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    63
# this will result in a JSONRPC service being created with two
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    64
# RPC functions.  dump "processor" into urlpatterns to make it
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    65
# part of the app:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    66
#  (r'^formsservice/$', 'djangoapp.views.processor'),
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    67
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
    68
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    69
def builderrors(form):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    70
    d = {}
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    71
    for error in form.errors.keys():
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    72
        if error not in d:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    73
            d[error] = []
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    74
        for errorval in form.errors[error]:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    75
            d[error].append(unicode(errorval))
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    76
    return d
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    77
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    78
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    79
# contains the list of arguments in each field
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    80
field_names = {
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    81
 'CharField': ['max_length', 'min_length'],
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    82
 'IntegerField': ['max_value', 'min_value'],
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    83
 'FloatField': ['max_value', 'min_value'],
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    84
 'DecimalField': ['max_value', 'min_value', 'max_digits', 'decimal_places'],
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    85
 'DateField': ['input_formats'],
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    86
 'DateTimeField': ['input_formats'],
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    87
 'TimeField': ['input_formats'],
1737
a39c2918c015 clean-up: fix PEP8 E261 at least two spaces before inline comment
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1736
diff changeset
    88
 'RegexField': ['max_length', 'min_length'],  # sadly we can't get the expr
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    89
 'EmailField': ['max_length', 'min_length'],
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    90
 'URLField': ['max_length', 'min_length', 'verify_exists', 'user_agent'],
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    91
 'ChoiceField': ['choices'],
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    92
 'FilePathField': ['path', 'match', 'recursive', 'choices'],
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    93
 'IPAddressField': ['max_length', 'min_length'],
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    94
 }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    95
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
    96
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    97
def describe_field_errors(field):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    98
    res = {}
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    99
    field_type = field.__class__.__name__
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   100
    msgs = {}
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   101
    for n, m in field.error_messages.items():
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   102
        msgs[n] = unicode(m)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   103
    res['error_messages'] = msgs
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   104
    if field_type in ['ComboField', 'MultiValueField', 'SplitDateTimeField']:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   105
        res['fields'] = map(describe_field, field.fields)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   106
    return res
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   107
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
   108
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   109
def describe_fields_errors(fields, field_names):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   110
    res = {}
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   111
    if not field_names:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   112
        field_names = fields.keys()
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   113
    for name in field_names:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   114
        field = fields[name]
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   115
        res[name] = describe_field_errors(field)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   116
    return res
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   117
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
   118
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   119
def describe_field(field):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   120
    res = {}
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   121
    field_type = field.__class__.__name__
1769
4665ba25a0ba clean-up: fix PEP8 E125 continuation line with same indent as next logical line
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1767
diff changeset
   122
    for fname in (field_names.get(field_type, []) +
4665ba25a0ba clean-up: fix PEP8 E125 continuation line with same indent as next logical line
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1767
diff changeset
   123
                  ['help_text', 'label', 'initial', 'required']):
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   124
        res[fname] = getattr(field, fname)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   125
    if field_type in ['ComboField', 'MultiValueField', 'SplitDateTimeField']:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   126
        res['fields'] = map(describe_field, field.fields)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   127
    return res
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   128
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
   129
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   130
def describe_fields(fields, field_names):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   131
    res = {}
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   132
    if not field_names:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   133
        field_names = fields.keys()
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   134
    for name in field_names:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   135
        field = fields[name]
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   136
        res[name] = describe_field(field)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   137
    return res
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   138
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
   139
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   140
class FormProcessor(JSONRPCService):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   141
    def __init__(self, forms, _formcls=None):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   142
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   143
        if _formcls is None:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   144
            JSONRPCService.__init__(self)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   145
            for k in forms.keys():
1754
63f4af6bf6d9 clean-up: fix most PEP8 E221 multiple spaces before operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1749
diff changeset
   146
                s = FormProcessor({}, forms[k])
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   147
                self.add_method(k, s.__process)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   148
        else:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   149
            JSONRPCService.__init__(self, forms)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   150
            self.formcls = _formcls
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   151
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   152
    def __process(self, request, params, command=None):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   153
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   154
        f = self.formcls(params)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   155
1737
a39c2918c015 clean-up: fix PEP8 E261 at least two spaces before inline comment
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1736
diff changeset
   156
        if command is None:  # just validate
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   157
            if not f.is_valid():
1740
b789b695b5c6 clean-up: fix PEP8 E231 missing whitespace after ':' or ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1737
diff changeset
   158
                return {'success': False, 'errors': builderrors(f)}
b789b695b5c6 clean-up: fix PEP8 E231 missing whitespace after ':' or ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1737
diff changeset
   159
            return {'success': True}
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   160
1763
bcc07ff2362c clean-up: fix PEP8 W601 .has_key() is deprecated, use 'in'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1754
diff changeset
   161
        elif 'describe_errors' in command:
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   162
            field_names = command['describe_errors']
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   163
            return describe_fields_errors(f.fields, field_names)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   164
1763
bcc07ff2362c clean-up: fix PEP8 W601 .has_key() is deprecated, use 'in'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1754
diff changeset
   165
        elif 'describe' in command:
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   166
            field_names = command['describe']
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   167
            return describe_fields(f.fields, field_names)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   168
1763
bcc07ff2362c clean-up: fix PEP8 W601 .has_key() is deprecated, use 'in'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1754
diff changeset
   169
        elif 'save' in command:
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   170
            if not f.is_valid():
1740
b789b695b5c6 clean-up: fix PEP8 E231 missing whitespace after ':' or ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1737
diff changeset
   171
                return {'success': False, 'errors': builderrors(f)}
1737
a39c2918c015 clean-up: fix PEP8 E261 at least two spaces before inline comment
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1736
diff changeset
   172
            instance = f.save()  # XXX: if you want more, over-ride save.
1746
45d6f5fba016 clean-up: fix PEP8 E202 whitespace before ')'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1741
diff changeset
   173
            return {'success': True, 'instance': json_convert(instance)}
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   174
1763
bcc07ff2362c clean-up: fix PEP8 W601 .has_key() is deprecated, use 'in'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1754
diff changeset
   175
        elif 'html' in command:
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   176
            return {'success': True, 'html': f.as_table()}
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   177
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   178
        return "unrecognised command"
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   179
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   180
# The following is incredibly convenient for saving vast amounts of
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   181
# coding, avoiding doing silly things like this:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   182
#     jsonresult = {'field1': djangoobject.field1,
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   183
#                   'field2': djangoobject.date.strftime('%Y.%M'),
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   184
#                    ..... }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   185
#
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   186
# The date/time flatten function is there because JSONRPC doesn't
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   187
# support date/time objects or formats, so conversion to a string
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   188
# is the most logical choice.  pyjamas, being python, can easily
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   189
# be used to parse the string result at the other end.
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   190
#
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   191
# use as follows:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   192
#
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   193
# jsonservice = JSONRPCService()
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   194
#
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   195
# @jsonremote(jsonservice)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   196
# def list_some_model(request, start=0, count=10):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   197
#     l = SomeDjangoModelClass.objects.filter()
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   198
#     res = json_convert(l[start:end])
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   199
#
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   200
# @jsonremote(jsonservice)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   201
# def list_another_model(request, start=0, count=10):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   202
#     l = AnotherDjangoModelClass.objects.filter()
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   203
#     res = json_convert(l[start:end])
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   204
#
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   205
# dump jsonservice into urlpatterns to make the two RPC functions,
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   206
# list_some_model and list_another_model part of the django app:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   207
#  (r'^service1/$', 'djangoapp.views.jsonservice'),
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   208
1749
d73b64672238 clean-up: fix PEP8 E305 expected 2 blank lines after class or function definition
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1746
diff changeset
   209
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   210
def dict_datetimeflatten(item):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   211
    d = {}
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   212
    for k, v in item.items():
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   213
        k = str(k)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   214
        if isinstance(v, datetime.date):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   215
            d[k] = str(v)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   216
        elif isinstance(v, dict):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   217
            d[k] = dict_datetimeflatten(v)
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   218
        else:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   219
            d[k] = v
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   220
    return d
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   221
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
   222
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   223
def json_convert(l, fields=None):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   224
    res = []
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   225
    for item in serialize('python', l, fields=fields):
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   226
        res.append(dict_datetimeflatten(item))
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   227
    return res