Skip to content

shell_plus and poking around

hanleybrand edited this page · 17 revisions

This page isn't well explained - but it shows what information can be gleaned using the shell_plus command on your mdid3 server.

Starting a session

On the command line in the $ROOIBOS directory:

$ python shell_plus

This will start a python interactive shell with many of the mdid3 specific libraries loaded.

Checking out records

>>> Record.objects.all()
 [<Record: r-1416356>, <Record: r-2384062>, <Record: r-9164565>, <Record: r-2439076>, <Record: r-5817437>, 
<Record: r-7123435>, <Record: r-9935313>, <Record: r-2921325>, <Record: r-1319993>, <Record: r-4109358>, 
<Record: r-9938959>, <Record: r-4575035>, <Record: r-5908705>, <Record: r-5846490>, <Record: r-3797232>, 
<Record: r-6854016>, <Record: r-3484099>, <Record: r-7268477>, <Record: r-4795782>, <Record: r-7127890>, '...
(remaining elements truncated)...']

First, create a variable that holds a single Record object (if done correctly, no output will be produced):

>>> rec = Record.objects.get(name='r-2439076')      

Then, you can begin calling methods on that - here's the base Record object minus all of the methods (see rooibos/data/ for those) :

class Record(models.Model):
    created = models.DateTimeField(auto_now_add=True)
    modified = models.DateTimeField(auto_now=True)
    name = models.SlugField(max_length=50, unique=True)
    parent = models.ForeignKey('self', null=True, blank=True)
    source = models.CharField(max_length=1024, null=True, blank=True)
    manager = models.CharField(max_length=50, null=True, blank=True)
    next_update = models.DateTimeField(null=True, blank=True)
    owner = models.ForeignKey(User, null=True, blank=True)

If you check the source of an object, you can see from the code what can be gotten from it. In the case of the rooibos Record object created by rec = Record.objects.get(name='r-2439076') above, the attributes can be gotten via rec.attribute and methods can be called like rec.method():

>>> rec.get_absolute_url()


>>> rec.get_thumbnail_url()


>>> rec.get_image_url()


>>> rec.get_fieldvalues()

[<FieldValue: IDNoneNone=A00015>, <FieldValue: TitleNoneNone=Fresco restoration, Knossos, Crete, Greece.>, <FieldValue: CreatorNoneNone=Unknown.>, <FieldValue: Creation YearNoneNone=ca. 1700-1300 BCE.>, <FieldValue: MediumNoneNone=Architecture.>, <FieldValue: CultureNoneNone=Indonesian: Java>, <FieldValue: PeriodNoneNone=Ancient: 3000 BCE to 4th c. CE.>, <FieldValue: StyleNoneNone=Minoan>, <FieldValue: CountryNoneNone=Greece>, <FieldValue: SiteNoneNone=Crete.>, <FieldValue: Copyright PermissionNoneNone=YES>, <FieldValue: CopyrightNoneNone=Maurie McInnis, 04/01/99.>]

Getting the id /pk of an object

For some other objects, you need to pass a record in, and for that you might need to pass the primary key or to select the specific record (the is the internal django pk (primary key), and has nothing to do with data you import (e.g. it wouldn't be the record's ID field)





Media files, and their records

You can get media objects based on the record they're associated with:

>>> rec = Record.objects.get(name='r-2439076')  
>>> m = Media.objects.get(record=rec)

>>> m

<Media: full/_img__1_12104.jpg>


<Storage: personal-images>

>>> m.get_absolute_url()


>>> m.get_absolute_file_path()


Presentations and PresentationItems

>>> p = Presentation.objects.get(id=1)
>>> p

<Presentation: Presentation object>

>>> items = p.items.select_related('record').filter(hidden=False)
>>> items

[<PresentationItem: PresentationItem object>, <PresentationItem: PresentationItem object>, <PresentationItem: PresentationItem object>, <PresentationItem: PresentationItem object>, <PresentationItem: PresentationItem object>, <PresentationItem: PresentationItem object>]

Not super useful, so check the dir(items[0]) (items is a list of PresentationItem objects, so dir(items) will just give us the methods for a list, whereas dir(items[0]) is the same as dir()

>>> dir(items[0])
['DoesNotExist', 'MultipleObjectsReturned', '__class__', '__delattr__', '__dict__', '__doc__', 
'__eq__', '__format__', '__getattribute__', '__hash__', '__init__', '__metaclass__', '__module__', 
'__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__',  
'__subclasshook__', '__weakref__', '_annotation_filter', '_base_manager', '_collect_sub_objects', 
'_default_manager', '_deferred', '_get_FIELD_display', '_get_next_or_previous_by_FIELD',  
'_get_next_or_previous_in_order', '_get_pk_val', '_get_unique_checks', '_meta', '_perform_date_checks', 
'_perform_unique_checks', '_presentation_cache', '_record_cache', '_set_pk_val', '_state',  
'annotation', 'annotation_getter', 'annotation_setter', 'clean', 'clean_fields', 'date_error_message',  
'delete', 'duplicate', 'full_clean', 'get_fieldvalues', 'hidden', 'id', 'media', 'objects', 'order',  
'pk', 'prepare_database_save', 'presentation', 'presentation_id', 'record', 'record_id', 'save',  
'save_base', 'serializable_value', 'title', 'title_from_fieldvalues', 'type', 'unique_error_message',  


That's a bit much, so to try to figure out what you actually want out of that, a quick for loop can help:

>>>for eachThing in dir(items[0]):
...     try:
...         print 'item.%s : ' % eachThing, eval( 'items[0].' + eachThing) 
...     except:   # this is a bad habit
...         pass  # but it stops syntax errors from the items[0].eachThing call
item.DoesNotExist :  <class 'rooibos.presentation.models.DoesNotExist'>
item.MultipleObjectsReturned :  <class 'rooibos.presentation.models.MultipleObjectsReturned'>
item.__class__ :  <class 'rooibos.presentation.models.PresentationItem'>
item.__delattr__ :  <method-wrapper '__delattr__' of PresentationItem object at 0x10481d9d0>
item.__dict__ :  {'presentation_id': 1L, '_presentation_cache': <Presentation: Presentation object>, '_state': <django.db.models.base.ModelState object at 0x10481dc90>, 'order': 0, '_record_cache': <Record: 2012-07-15_231945>, 'record_id': 25L, 'hidden': False, 'type': u'', 'id': 6L}
item.__doc__ :  PresentationItem(id, presentation_id, record_id, hidden, type, order)
item.__eq__ :  <bound method PresentationItem.__eq__ of <PresentationItem: PresentationItem object>>
item.__format__ :  <built-in method __format__ of PresentationItem object at 0x10481d9d0>
item.__getattribute__ :  <method-wrapper '__getattribute__' of PresentationItem object at 0x10481d9d0>
item.__hash__ :  <bound method PresentationItem.__hash__ of <PresentationItem: PresentationItem object>>
item.__init__ :  <bound method PresentationItem.__init__ of <PresentationItem: PresentationItem object>>
item.__metaclass__ :  <class 'django.db.models.base.ModelBase'>
item.__module__ :  rooibos.presentation.models
item.__ne__ :  <bound method PresentationItem.__ne__ of <PresentationItem: PresentationItem object>>
item.__new__ :  <built-in method __new__ of type object at 0x1037e1300>
item.__reduce__ :  <bound method PresentationItem.__reduce__ of <PresentationItem: PresentationItem object>>
item.__reduce_ex__ :  <built-in method __reduce_ex__ of PresentationItem object at 0x10481d9d0>
item.__repr__ :  <bound method PresentationItem.__repr__ of <PresentationItem: PresentationItem object>>
item.__setattr__ :  <method-wrapper '__setattr__' of PresentationItem object at 0x10481d9d0>
item.__sizeof__ :  <built-in method __sizeof__ of PresentationItem object at 0x10481d9d0>
item.__str__ :  <bound method PresentationItem.__str__ of <PresentationItem: PresentationItem object>>
item.__subclasshook__ :  <built-in method __subclasshook__ of ModelBase object at 0x7fd2e2bf43b0>
item.__weakref__ :  None
item._annotation_filter :  <bound method PresentationItem._annotation_filter of <PresentationItem: PresentationItem object>>
item._base_manager :  <django.db.models.manager.Manager object at 0x104707e50>
item._collect_sub_objects :  <bound method PresentationItem._collect_sub_objects of <PresentationItem: PresentationItem object>>
item._default_manager :  <django.db.models.manager.Manager object at 0x104707e50>
item._deferred :  False
item._get_FIELD_display :  <bound method PresentationItem._get_FIELD_display of <PresentationItem: PresentationItem object>>
item._get_next_or_previous_by_FIELD :  <bound method PresentationItem._get_next_or_previous_by_FIELD of <PresentationItem: PresentationItem object>>
item._get_next_or_previous_in_order :  <bound method PresentationItem._get_next_or_previous_in_order of <PresentationItem: PresentationItem object>>
item._get_pk_val :  <bound method PresentationItem._get_pk_val of <PresentationItem: PresentationItem object>>
item._get_unique_checks :  <bound method PresentationItem._get_unique_checks of <PresentationItem: PresentationItem object>>
item._meta :  presentation.presentationitem
item._perform_date_checks :  <bound method PresentationItem._perform_date_checks of <PresentationItem: PresentationItem object>>
item._perform_unique_checks :  <bound method PresentationItem._perform_unique_checks of <PresentationItem: PresentationItem object>>
item._presentation_cache :  Presentation object
item._record_cache :  2012-07-15_231945
item._set_pk_val :  <bound method PresentationItem._set_pk_val of <PresentationItem: PresentationItem object>>
item._state :  <django.db.models.base.ModelState object at 0x10481dc90>
item.annotation :  None
item.annotation_getter :  <bound method PresentationItem.annotation_getter of <PresentationItem: PresentationItem object>>
item.annotation_setter :  <bound method PresentationItem.annotation_setter of <PresentationItem: PresentationItem object>>
item.clean :  <bound method PresentationItem.clean of <PresentationItem: PresentationItem object>>
item.clean_fields :  <bound method PresentationItem.clean_fields of <PresentationItem: PresentationItem object>>
item.date_error_message :  <bound method PresentationItem.date_error_message of <PresentationItem: PresentationItem object>>
item.delete :  <bound method PresentationItem.delete of <PresentationItem: PresentationItem object>>
item.duplicate :  <bound method PresentationItem.duplicate of <PresentationItem: PresentationItem object>>
item.full_clean :  <bound method PresentationItem.full_clean of <PresentationItem: PresentationItem object>>
item.get_fieldvalues :  <bound method PresentationItem.get_fieldvalues of <PresentationItem: PresentationItem object>>
item.hidden :  False :  6 :  <django.db.models.fields.related.RelatedManager object at 0x104790e90>
item.objects :  item.order :  0 :  6
item.prepare_database_save :  <bound method PresentationItem.prepare_database_save of <PresentationItem: PresentationItem object>>
item.presentation :  Presentation object
item.presentation_id :  1
item.record :  2012-07-15_231945
item.record_id :  25 :  <bound method of <PresentationItem: PresentationItem object>>
item.save_base :  <bound method PresentationItem.save_base of <PresentationItem: PresentationItem object>>
item.serializable_value :  <bound method PresentationItem.serializable_value of <PresentationItem: PresentationItem object>>
item.title :  2012-07-15_23.19.45
item.title_from_fieldvalues :  <bound method PresentationItem.title_from_fieldvalues of <PresentationItem: PresentationItem object>>
item.type :  
item.unique_error_message :  <bound method PresentationItem.unique_error_message of <PresentationItem: PresentationItem object>>
item.validate_unique :  <bound method PresentationItem.validate_unique of <PresentationItem: PresentationItem object>>```

So going through that mess we might decide we're interested in the id, order, record_id, title of each PresentationItem, so we can get that list with a simple list

>>> for item in items:
...     print, item.order, item.record_id, item.title

which outputs

6 0 25 2012-07-15_23.19.45
1 1 5 2012-07-15_22.02.08
2 2 9 2012-07-15_22.26.58
3 3 12 2012-07-15_22.36.08
4 4 13 2012-07-15_22.42.59
5 5 15 2012-07-15_22.43.59
Something went wrong with that request. Please try again.