| 1 | # -*- coding: utf-8 -*- |
|---|
| 2 | |
|---|
| 3 | """ |
|---|
| 4 | Counter |
|---|
| 5 | ~~~~~~~ |
|---|
| 6 | |
|---|
| 7 | Simple page counter. |
|---|
| 8 | Note: Counts not cached views! |
|---|
| 9 | |
|---|
| 10 | Last commit info: |
|---|
| 11 | ~~~~~~~~~ |
|---|
| 12 | $LastChangedDate$ |
|---|
| 13 | $Rev$ |
|---|
| 14 | $Author$ |
|---|
| 15 | |
|---|
| 16 | :copyleft: 2008 by the PyLucid team, see AUTHORS for more details. |
|---|
| 17 | :license: GNU GPL v3 or above, see LICENSE for more details |
|---|
| 18 | """ |
|---|
| 19 | |
|---|
| 20 | __version__= "$Rev$" |
|---|
| 21 | |
|---|
| 22 | from django.db import models |
|---|
| 23 | |
|---|
| 24 | from PyLucid.models import Page |
|---|
| 25 | from PyLucid.system.BasePlugin import PyLucidBasePlugin |
|---|
| 26 | |
|---|
| 27 | #_____________________________________________________________________________ |
|---|
| 28 | # models |
|---|
| 29 | |
|---|
| 30 | class PageCount(models.Model): |
|---|
| 31 | page = models.ForeignKey(Page) |
|---|
| 32 | counter = models.PositiveIntegerField() |
|---|
| 33 | |
|---|
| 34 | class Meta: |
|---|
| 35 | app_label = 'PyLucidPlugins' # essential |
|---|
| 36 | |
|---|
| 37 | # essential: a list of all plugin models: |
|---|
| 38 | PLUGIN_MODELS = (PageCount,) |
|---|
| 39 | |
|---|
| 40 | |
|---|
| 41 | class page_counter(PyLucidBasePlugin): |
|---|
| 42 | |
|---|
| 43 | def lucidTag(self, **kwargs): |
|---|
| 44 | """ |
|---|
| 45 | increase the page count and display the current count number. |
|---|
| 46 | """ |
|---|
| 47 | try: |
|---|
| 48 | # Get or create the page counter for the current page |
|---|
| 49 | page_count, created = PageCount.objects.get_or_create( |
|---|
| 50 | page = self.current_page, # PyLucid.models.Page instance |
|---|
| 51 | defaults = {"counter": 1} |
|---|
| 52 | ) |
|---|
| 53 | except Exception, err: |
|---|
| 54 | self.page_msg.red("Page counter error: %s" % err) |
|---|
| 55 | return |
|---|
| 56 | |
|---|
| 57 | if not created: |
|---|
| 58 | # increase a existing page count entry |
|---|
| 59 | page_count.counter += 1 |
|---|
| 60 | page_count.save() |
|---|
| 61 | |
|---|
| 62 | # Display the current page count number |
|---|
| 63 | self.response.write("%i" % page_count.counter) |
|---|