| 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 | """ |
| 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 |