<s> import warnings <EOL> from django . contrib . contenttypes . models import ContentType <EOL> from django . core . urlresolvers import reverse <EOL> from django . db import models <EOL> from django . db . models import Max , Min , F <EOL> from django . utils . translation import ugettext as _ <EOL> class OrderedModelBase ( models . Model ) : <EOL> """<STR_LIT>""" <EOL> order_field_name = None <EOL> order_with_respect_to = None <EOL> class Meta : <EOL> abstract = True <EOL> def _get_order_with_respect_to ( self ) : <EOL> if type ( self . order_with_respect_to ) is str : <EOL> self . order_with_respect_to = ( self . order_with_respect_to , ) <EOL> return [ ( field , getattr ( self , field ) ) for field in self . order_with_respect_to ] <EOL> def _valid_ordering_reference ( self , reference ) : <EOL> return self . order_with_respect_to is None or ( <EOL> self . _get_order_with_respect_to ( ) == reference . _get_order_with_respect_to ( ) <EOL> ) <EOL> def get_ordering_queryset ( self , qs = None ) : <EOL> qs = qs or self . __class__ . objects . all ( ) <EOL> order_with_respect_to = self . order_with_respect_to <EOL> if order_with_respect_to : <EOL> order_values = self . _get_order_with_respect_to ( ) <EOL> qs = qs . filter ( * order_values ) <EOL> return qs <EOL> def save ( self , * args , ** kwargs ) : <EOL> if getattr ( self , self . order_field_name ) is None : <EOL> c = self . get_ordering_queryset ( ) . aggregate ( Max ( self . order_field_name ) ) . get ( self . order_field_name + '<STR_LIT>' ) <EOL> setattr ( self , self . order_field_name , <NUM_LIT:0> if c is None else c + <NUM_LIT:1> ) <EOL> super ( OrderedModelBase , self ) . save ( * args , ** kwargs ) <EOL> def delete ( self , * args , ** kwargs ) : <EOL> qs = self . get_ordering_queryset ( ) <EOL> qs . filter ( ** { self . order_field_name + '<STR_LIT>' : getattr ( self , self . order_field_name ) } ) . update ( ** { self . order_field_name : F ( self . order_field_name ) - <NUM_LIT:1> } ) <EOL> super ( OrderedModelBase , self ) . delete ( * args , ** kwargs ) <EOL> def _move ( self , up , qs = None ) : <EOL> qs = self . get_ordering_queryset ( qs ) <EOL> if up : <EOL> qs = qs . order_by ( '<STR_LIT:->' + self . order_field_name ) . filter ( ** { self . order_field_name + '<STR_LIT>' : getattr ( self , self . order_field_name ) } ) <EOL> else : <EOL> qs = qs . filter ( ** { self . order_field_name + '<STR_LIT>' : getattr ( self , self . order_field_name ) } ) <EOL> try : <EOL> replacement = qs [ <NUM_LIT:0> ] <EOL> except IndexError : <EOL> return <EOL> order , replacement_order = getattr ( self , self . order_field_name ) , getattr ( replacement , self . order_field_name ) <EOL> setattr ( self , self . order_field_name , replacement_order ) <EOL> setattr ( replacement , self . order_field_name , order ) <EOL> self . save ( ) <EOL> replacement . save ( ) <EOL> def move ( self , direction , qs = None ) : <EOL> warnings . warn ( <EOL> _ ( "<STR_LIT>" ) , <EOL> DeprecationWarning <EOL> ) <EOL> if direction == '<STR_LIT>' : <EOL> self . up ( ) <EOL> else : <EOL> self . down ( ) <EOL> def move_down ( self ) : <EOL> """<STR_LIT>""" <EOL> warnings . warn ( <EOL> _ ( "<STR_LIT>" ) , <EOL> DeprecationWarning <EOL> ) <EOL> return self . down ( ) <EOL> def move_up ( self ) : <EOL> """<STR_LIT>""" <EOL> warnings . warn ( <EOL> _ ( "<STR_LIT>" ) , <EOL> DeprecationWarning <EOL> ) <EOL> return self . up ( ) <EOL> def swap ( self , qs ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> replacement = qs [ <NUM_LIT:0> ] <EOL> except IndexError : <EOL> return <EOL> if not self . _valid_ordering_reference ( replacement ) : <EOL> raise ValueError ( <EOL> "<STR_LIT>" % ( <EOL> self , self . __class__ , '<STR_LIT>' . join ( [ "<STR_LIT>" . format ( o [ <NUM_LIT:0> ] ) for o in self . _get_order_with_respect_to ( ) ] ) <EOL> ) <EOL> ) <EOL> order , replacement_order = getattr ( self , self . order_field_name ) , getattr ( replacement , self . order_field_name ) <EOL> setattr ( self , self . order_field_name , replacement_order ) <EOL> setattr ( replacement , self . order_field_name , order ) <EOL> self . save ( ) <EOL> replacement . save ( ) <EOL> def up ( self ) : <EOL> """<STR_LIT>""" <EOL> self . swap ( self . get_ordering_queryset ( ) <EOL> . filter ( ** { self . order_field_name + '<STR_LIT>' : getattr ( self , self . order_field_name ) } ) <EOL> . order_by ( '<STR_LIT:->' + self . order_field_name ) ) <EOL> def down ( self ) : <EOL> """<STR_LIT>""" <EOL> self . swap ( self . get_ordering_queryset ( ) . filter ( ** { self . order_field_name + '<STR_LIT>' : getattr ( self , self . order_field_name ) } ) ) <EOL> def to ( self , order ) : <EOL> """<STR_LIT>""" <EOL> if order is None or getattr ( self , self . order_field_name ) == order : <EOL> return <EOL> qs = self . get_ordering_queryset ( ) <EOL> if getattr ( self , self . order_field_name ) > order : <EOL> qs . filter ( ** { self . order_field_name + '<STR_LIT>' : getattr ( self , self . order_field_name ) , <EOL> self . order_field_name + '<STR_LIT>' : order } ) . update ( ** { self . order_field_name : F ( self . order_field_name ) + <NUM_LIT:1> } ) <EOL> else : <EOL> qs . filter ( ** { self . order_field_name + '<STR_LIT>' : getattr ( self , self . order_field_name ) , <EOL> self . order_field_name + '<STR_LIT>' : order } ) . update ( ** { self . order_field_name : F ( self . order_field_name ) - <NUM_LIT:1> } ) <EOL> setattr ( self , self . order_field_name , order ) <EOL> self . save ( ) <EOL> def above ( self , ref ) : <EOL> """<STR_LIT>""" <EOL> if not self . _valid_ordering_reference ( ref ) : <EOL> raise ValueError ( <EOL> "<STR_LIT>" % ( <EOL> self , self . __class__ , '<STR_LIT>' . join ( [ "<STR_LIT>" . format ( o [ <NUM_LIT:0> ] ) for o in self . _get_order_with_respect_to ( ) ] ) <EOL> ) <EOL> ) <EOL> if getattr ( self , self . order_field_name ) == getattr ( ref , self . order_field_name ) : <EOL> return <EOL> if getattr ( self , self . order_field_name ) > getattr ( ref , self . order_field_name ) : <EOL> o = getattr ( ref , self . order_field_name ) <EOL> else : <EOL> o = self . get_ordering_queryset ( ) . filter ( ** { self . order_field_name + '<STR_LIT>' : getattr ( ref , self . order_field_name ) } ) . aggregate ( Max ( self . order_field_name ) ) . get ( self . order_field_name + '<STR_LIT>' ) or <NUM_LIT:0> <EOL> self . to ( o ) <EOL> def below ( self , ref ) : <EOL> """<STR_LIT>""" <EOL> if not self . _valid_ordering_reference ( ref ) : <EOL> raise ValueError ( <EOL> "<STR_LIT>" % ( <EOL> self , self . __class__ , '<STR_LIT>' . join ( [ "<STR_LIT>" . format ( o [ <NUM_LIT:0> ] ) for o in self . _get_order_with_respect_to ( ) ] ) <EOL> ) <EOL> ) <EOL> if getattr ( self , self . order_field_name ) == getattr ( ref , self . order_field_name ) : <EOL> return <EOL> if getattr ( self , self . order_field_name ) > getattr ( ref , self . order_field_name ) : <EOL> o = self . get_ordering_queryset ( ) . filter ( ** { self . order_field_name + '<STR_LIT>' : getattr ( ref , self . order_field_name ) } ) . aggregate ( Min ( self . order_field_name ) ) . get ( self . order_field_name + '<STR_LIT>' ) or <NUM_LIT:0> <EOL> else : <EOL> o = getattr ( ref , self . order_field_name ) <EOL> self . to ( o ) <EOL> def top ( self ) : <EOL> """<STR_LIT>""" <EOL> o = self . get_ordering_queryset ( ) . aggregate ( Min ( self . order_field_name ) ) . get ( self . order_field_name + '<STR_LIT>' ) <EOL> self . to ( o ) <EOL> def bottom ( self ) : <EOL> """<STR_LIT>""" <EOL> o = self . get_ordering_queryset ( ) . aggregate ( Max ( self . order_field_name ) ) . get ( self . order_field_name + '<STR_LIT>' ) <EOL> self . to ( o ) <EOL> class OrderedModel ( OrderedModelBase ) : <EOL> """<STR_LIT>""" <EOL> order = models . PositiveIntegerField ( editable = False , db_index = True ) <EOL> order_field_name = '<STR_LIT>' <EOL> class Meta : <EOL> abstract = True <EOL> ordering = ( '<STR_LIT>' , ) </s>
