Django

Code

Ticket #6110: 6110-3.diff

File 6110-3.diff, 6.7 kB (added by empty, 1 year ago)

Used flag in place of hardcoding

  • django/core/serializers/json.py

    old new  
    2020    """ 
    2121    Convert a queryset to JSON. 
    2222    """ 
     23    internal_use_only = False 
     24     
    2325    def end_serialization(self): 
    2426        self.options.pop('stream', None) 
    2527        self.options.pop('fields', None) 
  • django/core/serializers/base.py

    old new  
    2222    Abstract serializer base class. 
    2323    """ 
    2424 
     25    # Indicates if the implemented serializer is only available for  
     26    # internal Django use. 
     27    internal_use_only = False 
     28     
    2529    def serialize(self, queryset, **options): 
    2630        """ 
    2731        Serialize a queryset. 
  • django/core/serializers/pyyaml.py

    old new  
    1919    Convert a queryset to YAML. 
    2020    """ 
    2121     
     22    internal_use_only = False 
     23     
    2224    def handle_field(self, obj, field): 
    2325        # A nasty special case: base YAML doesn't support serialization of time 
    2426        # types (as opposed to dates or datetimes, which it does support). Since 
  • django/core/serializers/__init__.py

    old new  
    5353        _load_serializers() 
    5454    return _serializers.keys() 
    5555 
     56def get_public_serializer_formats(): 
     57    if not _serializers: 
     58        _load_serializers() 
     59    return [k for k, v in _serializers.iteritems() if not v.Serializer.internal_use_only] 
     60 
    5661def get_deserializer(format): 
    5762    if not _serializers: 
    5863        _load_serializers() 
  • django/core/serializers/python.py

    old new  
    1313    """ 
    1414    Serializes a QuerySet to basic Python objects. 
    1515    """ 
    16  
     16     
     17    internal_use_only = True 
     18     
    1719    def start_serialization(self): 
    1820        self._current = None 
    1921        self.objects = [] 
  • django/core/management/commands/dumpdata.py

    old new  
    11from django.core.management.base import BaseCommand, CommandError 
     2from django.core import serializers 
    23 
    34from optparse import make_option 
    45 
    56class Command(BaseCommand): 
     7    serializer_formats = serializers.get_public_serializer_formats() 
    68    option_list = BaseCommand.option_list + ( 
    79        make_option('--format', default='json', dest='format', 
    8             help='Specifies the output serialization format for fixtures'), 
     10            help='Specifies the output serialization format for fixtures.  Formats available: %s' % serializer_formats), 
    911        make_option('--indent', default=None, dest='indent', type='int', 
    1012            help='Specifies the indent level to use when pretty-printing output'), 
    1113    ) 
     
    1416 
    1517    def handle(self, *app_labels, **options): 
    1618        from django.db.models import get_app, get_apps, get_models 
    17         from django.core import serializers 
    1819 
    1920        format = options.get('format', 'json') 
    2021        indent = options.get('indent', None) 
     
    2627 
    2728        # Check that the serialization format exists; this is a shortcut to 
    2829        # avoid collating all the objects and _then_ failing. 
     30        if format not in self.serializer_formats: 
     31            raise CommandError("Unknown serialization format: %s" % format) 
     32         
    2933        try: 
    3034            serializers.get_serializer(format) 
    3135        except KeyError: 
  • django/core/management/commands/loaddata.py

    old new  
    5050            parts = fixture_label.split('.') 
    5151            if len(parts) == 1: 
    5252                fixture_name = fixture_label 
    53                 formats = serializers.get_serializer_formats() 
     53                formats = serializers.get_public_serializer_formats() 
    5454            else: 
    5555                fixture_name, format = '.'.join(parts[:-1]), parts[-1] 
    56                 if format in serializers.get_serializer_formats(): 
     56                if format in serializers.get_public_serializer_formats(): 
    5757                    formats = [format] 
    5858                else: 
    5959                    formats = [] 
  • tests/modeltests/fixtures/fixtures/fixture4.python

    old new  
  • tests/modeltests/fixtures/models.py

    old new  
    5454# object list is unaffected 
    5555>>> Article.objects.all() 
    5656[<Article: XML identified as leading cause of cancer>, <Article: Django conquers world!>, <Article: Copyright is fine the way it is>, <Article: Poker on TV is great!>, <Article: Python program becomes self aware>] 
     57 
     58# Try to dump the current contents of the database in a format that is flagged for internal_use_only 
     59>>> management.call_command('dumpdata', 'fixtures', format='python', verbosity=0) 
     60Traceback (most recent call last): 
     61  ... 
     62SystemExit: 1 
     63 
     64# Load a fixture from a format that is flagged for internal_use_only 
     65>>> management.call_command('loaddata', 'fixture4.python', verbosity=0) 
     66 
     67# object list is unaffected 
     68>>> Article.objects.all() 
     69[<Article: XML identified as leading cause of cancer>, <Article: Django conquers world!>, <Article: Copyright is fine the way it is>, <Article: Poker on TV is great!>, <Article: Python program becomes self aware>] 
    5770"""} 
    5871 
    5972# Database flushing does not work on MySQL with the default storage engine