Changeset 95d55f4 in rattail


Ignore:
Timestamp:
03/20/19 16:48:38 (6 months ago)
Author:
Lance Edgar <ledgar@…>
Branches:
master
Children:
e9c2ed8
Parents:
20beaf0
Message:

Add simple JSONTextDict data type for SQLAlchemy columns

for basic JSON awareness/conversion

File:
1 edited

Legend:

Unmodified
Added
Removed
  • rattail/db/types.py

    r20beaf0 r95d55f4  
    1 # -*- coding: utf-8 -*-
     1# -*- coding: utf-8; -*-
    22################################################################################
    33#
    44#  Rattail -- Retail Software Framework
    5 #  Copyright © 2010-2017 Lance Edgar
     5#  Copyright © 2010-2019 Lance Edgar
    66#
    77#  This file is part of Rattail.
     
    2525"""
    2626
    27 from __future__ import unicode_literals
     27from __future__ import unicode_literals, absolute_import
     28
     29import json
    2830
    2931from sqlalchemy import types
     
    4850            return None
    4951        return GPC(value)
     52
     53
     54class JSONTextDict(types.TypeDecorator):
     55    """
     56    SQLAlchemy type engine for JSON data.  Interprets "raw" text as JSON when
     57    reading from DB, and writes JSON text back to DB.
     58
     59    Note that the Python value for a field with this type will appear as a dict.
     60    """
     61    impl = types.Text
     62
     63    def process_bind_param(self, value, dialect):
     64        if value is not None:
     65            value = json.dumps(value)
     66        return value
     67
     68    def process_result_value(self, value, dialect):
     69        if value is not None:
     70            value = json.loads(value)
     71        return value
Note: See TracChangeset for help on using the changeset viewer.