<s> import collections <EOL> import datetime <EOL> from django . conf import settings <EOL> from django . core . exceptions import ObjectDoesNotExist <EOL> from django . db import models <EOL> STATUS_CHOICES = ( <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ) <EOL> POST_STATUS_CHOICES = ( <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ) <EOL> POST_TYPE_CHOICES = ( <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ) <EOL> USER_STATUS_CHOICES = ( <EOL> ( <NUM_LIT:0> , "<STR_LIT>" ) , <EOL> ) <EOL> READ_ONLY = getattr ( settings , "<STR_LIT>" , True ) <EOL> TABLE_PREFIX = getattr ( settings , "<STR_LIT>" , "<STR_LIT>" ) <EOL> class WordPressException ( Exception ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> class WordPressManager ( models . Manager ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> class WordPressModel ( models . Model ) : <EOL> """<STR_LIT>""" <EOL> objects = WordPressManager ( ) <EOL> class Meta : <EOL> abstract = True <EOL> managed = False <EOL> def _get_object ( self , model , obj_id ) : <EOL> try : <EOL> return model . objects . get ( pk = obj_id ) <EOL> except model . DoesNotExist : <EOL> pass <EOL> def save ( self , override = False , ** kwargs ) : <EOL> if READ_ONLY and not override : <EOL> raise WordPressException ( "<STR_LIT>" ) <EOL> super ( WordPressModel , self ) . save ( ** kwargs ) <EOL> def delete ( self , override = False ) : <EOL> if READ_ONLY and not override : <EOL> raise WordPressException ( "<STR_LIT>" ) <EOL> super ( WordPressModel , self ) . delete ( ) <EOL> class OptionManager ( WordPressManager ) : <EOL> def get_value ( self , name ) : <EOL> try : <EOL> o = self . get ( name = name ) <EOL> return o . value <EOL> except ObjectDoesNotExist : <EOL> pass <EOL> class Option ( WordPressModel ) : <EOL> objects = OptionManager ( ) <EOL> id = models . IntegerField ( db_column = '<STR_LIT>' , primary_key = True ) <EOL> name = models . CharField ( max_length = <NUM_LIT:64> , db_column = '<STR_LIT>' ) <EOL> value = models . TextField ( db_column = '<STR_LIT>' ) <EOL> autoload = models . CharField ( max_length = <NUM_LIT:20> ) <EOL> class Meta : <EOL> db_table = '<STR_LIT>' % TABLE_PREFIX <EOL> ordering = [ "<STR_LIT:name>" ] <EOL> managed = False <EOL> def __unicode__ ( self ) : <EOL> return u"<STR_LIT>" % ( self . name , self . value ) <EOL> class User ( WordPressModel ) : <EOL> """<STR_LIT>""" <EOL> login = models . CharField ( max_length = <NUM_LIT> , db_column = '<STR_LIT>' ) <EOL> password = models . CharField ( max_length = <NUM_LIT:64> , db_column = '<STR_LIT>' ) <EOL> username = models . CharField ( max_length = <NUM_LIT:255> , db_column = '<STR_LIT>' ) <EOL> email = models . CharField ( max_length = <NUM_LIT:100> , db_column = '<STR_LIT>' ) <EOL> url = models . URLField ( max_length = <NUM_LIT:100> , db_column = '<STR_LIT>' , blank = True ) <EOL> date_registered = models . DateTimeField ( auto_now_add = True , db_column = '<STR_LIT>' ) <EOL> activation_key = models . CharField ( max_length = <NUM_LIT> , db_column = '<STR_LIT>' ) <EOL> status = models . IntegerField ( default = <NUM_LIT:0> , choices = USER_STATUS_CHOICES , db_column = '<STR_LIT>' ) <EOL> display_name = models . CharField ( max_length = <NUM_LIT:255> , db_column = '<STR_LIT>' ) <EOL> class Meta : <EOL> db_table = '<STR_LIT>' % TABLE_PREFIX <EOL> ordering = [ "<STR_LIT>" ] <EOL> managed = False <EOL> def __unicode__ ( self ) : <EOL> return self . display_name <EOL> class UserMeta ( WordPressModel ) : <EOL> """<STR_LIT>""" <EOL> id = models . IntegerField ( db_column = '<STR_LIT>' , primary_key = True ) <EOL> user = models . ForeignKey ( User , related_name = "<STR_LIT>" , db_column = '<STR_LIT>' ) <EOL> key = models . CharField ( max_length = <NUM_LIT:255> , db_column = '<STR_LIT>' ) <EOL> value = models . TextField ( db_column = '<STR_LIT>' ) <EOL> class Meta : <EOL> db_table = '<STR_LIT>' % TABLE_PREFIX <EOL> managed = False <EOL> def __unicode__ ( self ) : <EOL> return u"<STR_LIT>" % ( self . key , self . value ) <EOL> class Link ( WordPressModel ) : <EOL> """<STR_LIT>""" <EOL> id = models . IntegerField ( db_column = '<STR_LIT>' , primary_key = True ) <EOL> url = models . URLField ( max_length = <NUM_LIT:255> , db_column = '<STR_LIT>' ) <EOL> name = models . CharField ( max_length = <NUM_LIT:255> , db_column = '<STR_LIT>' ) <EOL> image = models . CharField ( max_length = <NUM_LIT:255> , db_column = '<STR_LIT>' ) <EOL> target = models . CharField ( max_length = <NUM_LIT> , db_column = '<STR_LIT>' ) <EOL> description = models . CharField ( max_length = <NUM_LIT:255> , db_column = '<STR_LIT>' ) <EOL> visible = models . CharField ( max_length = <NUM_LIT:20> , db_column = '<STR_LIT>' ) <EOL> owner = models . ForeignKey ( User , related_name = '<STR_LIT>' , db_column = '<STR_LIT>' ) <EOL> rating = models . IntegerField ( default = <NUM_LIT:0> , db_column = '<STR_LIT>' ) <EOL> updated = models . DateTimeField ( blank = True , null = True , db_column = '<STR_LIT>' ) <EOL> rel = models . CharField ( max_length = <NUM_LIT:255> , db_column = '<STR_LIT>' ) <EOL> notes = models . TextField ( db_column = '<STR_LIT>' ) <EOL> rss = models . CharField ( max_length = <NUM_LIT:255> , db_column = '<STR_LIT>' ) <EOL> class Meta : <EOL> db_table = '<STR_LIT>' % TABLE_PREFIX <EOL> managed = False <EOL> def __unicode__ ( self ) : <EOL> return u"<STR_LIT>" % ( self . name , self . url ) <EOL> def is_visible ( self ) : <EOL> return self . visible == '<STR_LIT:Y>' <EOL> class PostManager ( WordPressManager ) : <EOL> """<STR_LIT>""" <EOL> def _by_status ( self , status , post_type = '<STR_LIT>' ) : <EOL> return self . filter ( status = status , post_type = post_type ) . select_related ( ) . prefetch_related ( '<STR_LIT>' ) <EOL> def drafts ( self , post_type = '<STR_LIT>' ) : <EOL> return self . _by_status ( '<STR_LIT>' , post_type ) <EOL> def private ( self , post_type = '<STR_LIT>' ) : <EOL> return self . _by_status ( '<STR_LIT>' , post_type ) <EOL> def published ( self , post_type = '<STR_LIT>' ) : <EOL> return self . _by_status ( '<STR_LIT>' , post_type ) <EOL> def term ( self , terms , taxonomy = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> terms = terms if isinstance ( terms , ( list , tuple ) ) else [ terms ] <EOL> try : <EOL> tx = Taxonomy . objects . filter ( name = taxonomy , term__slug__in = terms ) <EOL> post_ids = TermTaxonomyRelationship . objects . filter ( term_taxonomy__in = tx ) . values_list ( '<STR_LIT>' , flat = True ) <EOL> return self . published ( ) . filter ( pk__in = post_ids ) <EOL> except Taxonomy . DoesNotExist : <EOL> return self . none ( ) <EOL> def from_path ( self , path ) : <EOL> ( ymd , slug ) = path . strip ( '<STR_LIT:/>' ) . rsplit ( '<STR_LIT:/>' , <NUM_LIT:1> ) <EOL> start_date = datetime . datetime . strptime ( ymd , "<STR_LIT>" ) <EOL> end_date = start_date + datetime . timedelta ( days = <NUM_LIT:1> ) - datetime . timedelta ( minutes = <NUM_LIT:1> ) <EOL> params = { <EOL> '<STR_LIT>' : ( start_date , end_date ) , <EOL> '<STR_LIT>' : slug , <EOL> } <EOL> try : <EOL> return self . published ( ) . get ( ** params ) <EOL> except ObjectDoesNotExist : <EOL> pass <EOL> class TermTaxonomyRelationship ( WordPressModel ) : <EOL> object = models . ForeignKey ( '<STR_LIT>' ) <EOL> term_taxonomy = models . ForeignKey ( '<STR_LIT>' , related_name = '<STR_LIT>' , db_column = '<STR_LIT>' ) <EOL> order = models . IntegerField ( db_column = '<STR_LIT>' ) <EOL> class Meta : <EOL> db_table = '<STR_LIT>' % TABLE_PREFIX <EOL> ordering = [ '<STR_LIT>' ] <EOL> managed = False <EOL> class Post ( WordPressModel ) : <EOL> """<STR_LIT>""" <EOL> objects = PostManager ( ) <EOL> id = models . AutoField ( primary_key = True , db_column = '<STR_LIT>' ) <EOL> guid = models . CharField ( max_length = <NUM_LIT:255> ) <EOL> post_type = models . CharField ( max_length = <NUM_LIT:20> , choices = POST_TYPE_CHOICES ) <EOL> status = models . CharField ( max_length = <NUM_LIT:20> , db_column = '<STR_LIT>' , choices = POST_STATUS_CHOICES ) <EOL> title = models . TextField ( db_column = '<STR_LIT>' ) <EOL> slug = models . SlugField ( max_length = <NUM_LIT:200> , db_column = "<STR_LIT>" ) <EOL> author = models . ForeignKey ( User , related_name = '<STR_LIT>' , db_column = '<STR_LIT>' ) <EOL> excerpt = models . TextField ( db_column = '<STR_LIT>' ) <EOL> content = models . TextField ( db_column = '<STR_LIT>' ) <EOL> content_filtered = models . TextField ( db_column = '<STR_LIT>' ) <EOL> post_date = models . DateTimeField ( db_column = '<STR_LIT>' ) <EOL> modified = models . DateTimeField ( db_column = '<STR_LIT>' ) <EOL> comment_status = models . CharField ( max_length = <NUM_LIT:20> , choices = STATUS_CHOICES ) <EOL> comment_count = models . IntegerField ( default = <NUM_LIT:0> ) <EOL> ping_status = models . CharField ( max_length = <NUM_LIT:20> , choices = STATUS_CHOICES ) <EOL> to_ping = models . TextField ( ) <EOL> pinged = models . TextField ( ) <EOL> password = models . CharField ( max_length = <NUM_LIT:20> , db_column = "<STR_LIT>" ) <EOL> parent_id = models . IntegerField ( default = <NUM_LIT:0> , db_column = "<STR_LIT>" ) <EOL> menu_order = models . IntegerField ( default = <NUM_LIT:0> ) <EOL> mime_type = models . CharField ( max_length = <NUM_LIT:100> , db_column = '<STR_LIT>' ) <EOL> terms = models . ManyToManyField ( '<STR_LIT>' , through = '<STR_LIT>' , blank = True ) <EOL> term_cache = None <EOL> child_cache = None <EOL> class Meta : <EOL> db_table = '<STR_LIT>' % TABLE_PREFIX <EOL> get_latest_by = '<STR_LIT>' <EOL> ordering = [ "<STR_LIT>" ] <EOL> managed = False <EOL> def __unicode__ ( self ) : <EOL> return self . title <EOL> def save ( self , ** kwargs ) : <EOL> if self . parent_id is None : <EOL> self . parent_id = <NUM_LIT:0> <EOL> super ( Post , self ) . save ( ** kwargs ) <EOL> self . child_cache = None <EOL> self . term_cache = None <EOL> @ models . permalink <EOL> def get_absolute_url ( self ) : <EOL> return ( '<STR_LIT>' , ( <EOL> self . post_date . year , <EOL> "<STR_LIT>" % self . post_date . month , <EOL> "<STR_LIT>" % self . post_date . day , <EOL> self . slug <EOL> ) ) <EOL> def _get_children ( self ) : <EOL> if self . child_cache is None : <EOL> self . child_cache = list ( Post . objects . filter ( parent_id = self . pk ) ) <EOL> return self . child_cache <EOL> def _get_terms ( self , taxonomy ) : <EOL> if not self . term_cache : <EOL> self . term_cache = collections . defaultdict ( list ) <EOL> qs = Taxonomy . objects . filter ( relationships__object_id = self . id ) . select_related ( ) <EOL> qs = qs . order_by ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> term_ids = [ tax . term_id for tax in qs ] <EOL> terms = { } <EOL> for term in Term . objects . filter ( id__in = term_ids ) : <EOL> terms [ term . id ] = term <EOL> for tax in qs : <EOL> if tax . term_id in terms : <EOL> self . term_cache [ tax . name ] . append ( terms [ tax . term_id ] ) <EOL> return self . term_cache . get ( taxonomy ) <EOL> @ property <EOL> def children ( self ) : <EOL> return self . _get_children ( ) <EOL> @ property <EOL> def parent ( self ) : <EOL> if self . parent_id : <EOL> return Post . objects . get ( pk = self . parent_id ) <EOL> @ parent . setter <EOL> def parent ( self , post ) : <EOL> if post . pk is None : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> self . parent_id = post . pk <EOL> def categories ( self ) : <EOL> return self . _get_terms ( "<STR_LIT>" ) <EOL> def attachments ( self ) : <EOL> for post in self . _get_children ( ) : <EOL> if post . post_type == '<STR_LIT>' : <EOL> yield { <EOL> '<STR_LIT:id>' : post . id , <EOL> '<STR_LIT>' : post . slug , <EOL> '<STR_LIT>' : post . post_date , <EOL> '<STR_LIT:description>' : post . content , <EOL> '<STR_LIT:title>' : post . title , <EOL> '<STR_LIT>' : post . guid , <EOL> '<STR_LIT>' : post . mime_type , <EOL> } <EOL> def tags ( self ) : <EOL> return self . _get_terms ( "<STR_LIT>" ) <EOL> class PostMeta ( WordPressModel ) : <EOL> """<STR_LIT>""" <EOL> id = models . IntegerField ( db_column = '<STR_LIT>' , primary_key = True ) <EOL> post = models . ForeignKey ( Post , related_name = '<STR_LIT>' , db_column = '<STR_LIT>' ) <EOL> key = models . CharField ( max_length = <NUM_LIT:255> , db_column = '<STR_LIT>' ) <EOL> value = models . TextField ( db_column = '<STR_LIT>' ) <EOL> class Meta : <EOL> db_table = '<STR_LIT>' % TABLE_PREFIX <EOL> managed = False <EOL> def __unicode__ ( self ) : <EOL> return u"<STR_LIT>" % ( self . key , self . value ) <EOL> class Comment ( WordPressModel ) : <EOL> """<STR_LIT>""" <EOL> id = models . IntegerField ( db_column = '<STR_LIT>' , primary_key = True ) <EOL> post = models . ForeignKey ( Post , related_name = "<STR_LIT>" , db_column = "<STR_LIT>" ) <EOL> user_id = models . IntegerField ( db_column = '<STR_LIT>' , default = <NUM_LIT:0> ) <EOL> parent_id = models . IntegerField ( default = <NUM_LIT:0> , db_column = '<STR_LIT>' ) <EOL> author_name = models . CharField ( max_length = <NUM_LIT:255> , db_column = '<STR_LIT>' ) <EOL> author_email = models . EmailField ( max_length = <NUM_LIT:100> , db_column = '<STR_LIT>' ) <EOL> author_url = models . URLField ( blank = True , db_column = '<STR_LIT>' ) <EOL> author_ip = models . IPAddressField ( db_column = '<STR_LIT>' ) <EOL> post_date = models . DateTimeField ( db_column = '<STR_LIT>' ) <EOL> content = models . TextField ( db_column = '<STR_LIT>' ) <EOL> karma = models . IntegerField ( default = <NUM_LIT:0> , db_column = '<STR_LIT>' ) <EOL> approved = models . CharField ( max_length = <NUM_LIT:20> , db_column = '<STR_LIT>' ) <EOL> agent = models . CharField ( max_length = <NUM_LIT:255> , db_column = '<STR_LIT>' ) <EOL> comment_type = models . CharField ( max_length = <NUM_LIT:20> ) <EOL> class Meta : <EOL> db_table = '<STR_LIT>' % TABLE_PREFIX <EOL> ordering = [ '<STR_LIT>' ] <EOL> managed = False <EOL> def __unicode__ ( self ) : <EOL> return u"<STR_LIT>" % ( self . author_name , self . post . title ) <EOL> def get_absolute_url ( self ) : <EOL> return "<STR_LIT>" % ( self . post . get_absolute_url ( ) , self . pk ) <EOL> def parent ( self ) : <EOL> return self . _get_object ( Comment , self . parent_id ) <EOL> """<STR_LIT>""" <EOL> def is_approved ( self ) : <EOL> return self . approved == '<STR_LIT:1>' <EOL> def is_spam ( self ) : <EOL> return self . approved == '<STR_LIT>' <EOL> class Term ( WordPressModel ) : <EOL> id = models . IntegerField ( db_column = '<STR_LIT>' , primary_key = True ) <EOL> name = models . CharField ( max_length = <NUM_LIT:200> ) <EOL> slug = models . CharField ( max_length = <NUM_LIT:200> ) <EOL> group = models . IntegerField ( default = <NUM_LIT:0> , db_column = '<STR_LIT>' ) <EOL> class Meta : <EOL> db_table = '<STR_LIT>' % TABLE_PREFIX <EOL> ordering = [ '<STR_LIT:name>' ] <EOL> managed = False <EOL> def __unicode__ ( self ) : <EOL> return self . name <EOL> @ models . permalink <EOL> def get_absolute_url ( self ) : <EOL> return ( '<STR_LIT>' , ( self . slug , ) ) <EOL> class Taxonomy ( WordPressModel ) : <EOL> id = models . IntegerField ( db_column = '<STR_LIT>' , primary_key = True ) <EOL> term = models . ForeignKey ( Term , related_name = '<STR_LIT>' , blank = True , null = True ) <EOL> name = models . CharField ( max_length = <NUM_LIT:32> , db_column = '<STR_LIT>' ) <EOL> description = models . TextField ( ) <EOL> parent_id = models . IntegerField ( default = <NUM_LIT:0> , db_column = '<STR_LIT>' ) <EOL> count = models . IntegerField ( default = <NUM_LIT:0> ) <EOL> class Meta : <EOL> db_table = '<STR_LIT>' % TABLE_PREFIX <EOL> ordering = [ '<STR_LIT:name>' ] <EOL> managed = False <EOL> def __unicode__ ( self ) : <EOL> try : <EOL> term = self . term <EOL> except Term . DoesNotExist : <EOL> term = '<STR_LIT>' <EOL> return u"<STR_LIT>" % ( self . name , term ) <EOL> def parent ( self ) : <EOL> return self . _get_object ( Taxonomy , self . parent_id ) </s>
