Show
Ignore:
Timestamp:
07/24/08 16:16:21 (20 months ago)
Author:
JensDiemer
Message:

blog update 2:

  • nicer "create blog entry" form (existing tags can be selected)
  • some code cleanup
  • try to verify tag slugs
Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/pylucid/PyLucid/plugins_internal/blog/models.py

    r1712 r1713  
    8787 
    8888 
     89BAD_TAG_SLUG_CHARS = (" ", "/", ";") 
    8990class BlogTagManager(models.Manager): 
    9091    """ 
    9192    Manager for BlogTag model. 
    9293    """ 
    93     def get_or_creates(self, tags_string): 
    94         """ 
    95         split the given tags_string and create not existing tags. 
    96         returns a list of all tag model objects and a list of all created tags. 
    97         """ 
    98         tag_objects = [] 
     94    def safe_get(self, slug): 
     95        """ 
     96        Get a tag entry by slug. Try to verify the slug before we access the 
     97        database. Should be used, if the slug comes from the Client 
     98        (e.g. via url) 
     99        TODO: Exist there a better way to verify the tag slug? 
     100        """ 
     101        slug = slug.strip("/") # If it comes from url args 
     102        for char in BAD_TAG_SLUG_CHARS: 
     103            if char in slug: 
     104                raise self.model.DoesNotExist("Not allowed character in tag name!") 
     105 
     106        return self.model.objects.get(slug = slug) 
     107 
     108    def add_new_tags(self, tag_list, blog_obj): 
     109        """ 
     110        Create new tag entries and add it to the given blog entry. 
     111        Skip existing tags and returns only the new created tags. 
     112        """ 
    99113        new_tags = [] 
    100         for tag_name in tags_string.split(" "): 
    101             tag_name = tag_name.strip().lower() 
     114        for tag_name in tag_list: 
    102115            try: 
    103116                tag_obj = self.get(name = tag_name) 
     
    106119                tag_obj = self.create(name = tag_name, slug = tag_name) 
    107120 
    108             tag_objects.append(tag_obj) 
    109  
    110         return tag_objects, new_tags 
     121            # Add many-to-many 
     122            blog_obj.tags.add(tag_obj) 
     123 
     124        return new_tags 
     125 
     126    def get_tag_info(self): 
     127        """ 
     128        Returns all tags with the additional information: 
     129         * tag.count     - How many blog entries used this tag? 
     130 
     131        returns min_frequency and max_frequency, too: The min/max usage of all 
     132        tags. Needed to build a tag cloud. 
     133        """ 
     134        tags = self.model.objects.all() 
     135 
     136        frequency = set() 
     137        # get the counter information 
     138        for tag in tags: 
     139            count = tag.blogentry_set.count() 
     140            tag.count = count 
     141            frequency.add(count) 
     142 
     143        min_frequency = float(min(frequency)) 
     144        max_frequency = float(max(frequency)) 
     145 
     146        return tags, min_frequency, max_frequency 
     147 
     148#    def get_tag_choices(self): 
     149#        """ 
     150#        returns >count< tags witch are the most used tags. 
     151#        """ 
     152#        tags, min_frequency, max_frequency = self.get_tag_info() 
     153# 
     154#        tags = sorted(tags, key=lambda x: x.count, reverse=True) 
     155# 
     156#        choices = tuple([(t.id, t.name) for t in tags]) 
     157#        return choices 
    111158 
    112159 
     
    114161    """ 
    115162    A blog entry tag 
     163    TODO: Add a usage counter! So we can easy sort from more to less usages and 
     164          building a tag cloud is easier. 
    116165    """ 
    117166    objects = BlogTagManager()