|
| 1 | +""" |
| 2 | +Dashboard Index Views |
| 3 | +to show all statistics. |
| 4 | +""" |
| 5 | +# -*- coding: utf-8 -*- |
| 6 | +from __future__ import unicode_literals |
| 7 | + |
| 8 | +from django.utils import timezone |
| 9 | +from django.utils.translation import ugettext_lazy as _ |
| 10 | +from django.views.generic import TemplateView |
| 11 | + |
| 12 | +from core.utils.mixins import SuperUserLoginRequiredMixin |
| 13 | + |
| 14 | +from apps.accounts.models.user import User |
| 15 | +from apps.product.models.product import Product |
| 16 | +from apps.blog.models.addons import Visitor |
| 17 | +from apps.blog.models.post import Post |
| 18 | +from apps.blog.models.tag import Tag |
| 19 | + |
| 20 | + |
| 21 | +class DashboardIndexView(SuperUserLoginRequiredMixin, TemplateView): |
| 22 | + template_name = 'apps/dashboard/index.html' |
| 23 | + now = timezone.now() |
| 24 | + |
| 25 | + def get_total_tags(self, period: str, month: int = None) -> int: |
| 26 | + """ |
| 27 | + function to get the total tags per-month / year. |
| 28 | + :param `period` is string period (choices: "year", "month") |
| 29 | + :param `month` is optional integer month if it period as "month". |
| 30 | + :return int total of tags. |
| 31 | + """ |
| 32 | + if period == 'year': |
| 33 | + return Tag.objects.filter( |
| 34 | + created_at__year=self.now.year |
| 35 | + ).count() |
| 36 | + elif period == 'month': |
| 37 | + return Tag.objects.filter( |
| 38 | + created_at__year=self.now.year, |
| 39 | + created_at__month=month |
| 40 | + ).count() |
| 41 | + return 0 |
| 42 | + |
| 43 | + def get_total_users(self, period: str, month: int = None) -> int: |
| 44 | + """ |
| 45 | + function to get the total users per-month / year. |
| 46 | + :param `period` is string period (choices: "year", "month") |
| 47 | + :param `month` is optional integer month if it period as "month". |
| 48 | + :return int total of users |
| 49 | + """ |
| 50 | + if period == 'year': |
| 51 | + return User.objects.filter( |
| 52 | + date_joined__year=self.now.year |
| 53 | + ).count() |
| 54 | + elif period == 'month': |
| 55 | + return User.objects.filter( |
| 56 | + date_joined__year=self.now.year, |
| 57 | + date_joined__month=month |
| 58 | + ).count() |
| 59 | + return 0 |
| 60 | + |
| 61 | + def get_total_posts(self, period: str, month: int = None) -> int: |
| 62 | + """ |
| 63 | + function to get the total posts per-month / year. |
| 64 | + :param `period` is string period (choices: "year", "month") |
| 65 | + :param `month` is optional integer month if it period as "month". |
| 66 | + :return int total of posts. |
| 67 | + """ |
| 68 | + if period == 'year': |
| 69 | + return Post.objects.filter( |
| 70 | + created_at__year=self.now.year |
| 71 | + ).count() |
| 72 | + elif period == 'month': |
| 73 | + return Post.objects.filter( |
| 74 | + created_at__year=self.now.year, |
| 75 | + created_at__month=month |
| 76 | + ).count() |
| 77 | + return 0 |
| 78 | + |
| 79 | + def get_total_visitors(self, period: str, month: int = None) -> int: |
| 80 | + """ |
| 81 | + function to get the total visitors per-month / year. |
| 82 | + :param `period` is string period (choices: "year", "month") |
| 83 | + :param `month` is optional integer month if it period as "month". |
| 84 | + :return int total of visitors. |
| 85 | + """ |
| 86 | + if period == 'year': |
| 87 | + return Visitor.objects.filter( |
| 88 | + created_at__year=self.now.year |
| 89 | + ).count() |
| 90 | + elif period == 'month': |
| 91 | + return Visitor.objects.filter( |
| 92 | + created_at__year=self.now.year, |
| 93 | + created_at__month=month |
| 94 | + ).count() |
| 95 | + return 0 |
| 96 | + |
| 97 | + def get_data_visitors_per_months(self) -> list: |
| 98 | + """ |
| 99 | + function to get the calculate the total data |
| 100 | + of visitors for each month. |
| 101 | + :return list dict of data per month. |
| 102 | + """ |
| 103 | + data_per_months = [] |
| 104 | + for month in range(1, 12): |
| 105 | + total = self.get_total_visitors(period='month', month=month) |
| 106 | + data_per_months.append(total) |
| 107 | + return data_per_months |
| 108 | + |
| 109 | + def get_data_per_months(self) -> list: |
| 110 | + """ |
| 111 | + function to get the calculate the total data |
| 112 | + includes (user, product) for each month. |
| 113 | + :return list dict of data per month. |
| 114 | + """ |
| 115 | + data_per_months = [] |
| 116 | + for month in range(1, 12): |
| 117 | + data_per_months.append({ |
| 118 | + 'tag': self.get_total_tags(period='month', month=month), |
| 119 | + 'post': self.get_total_posts(period='month', month=month), |
| 120 | + 'user': self.get_total_users(period='month', month=month) |
| 121 | + }) |
| 122 | + return data_per_months |
| 123 | + |
| 124 | + def get_context_data(self, **kwargs): |
| 125 | + context_data = super().get_context_data(**kwargs) |
| 126 | + context_data['total_tags'] = Tag.objects.all().count() |
| 127 | + context_data['total_tags_this_year'] = self.get_total_tags(period='year') |
| 128 | + context_data['total_users'] = User.objects.all().count() |
| 129 | + context_data['total_users_this_year'] = self.get_total_users(period='year') |
| 130 | + context_data['total_posts'] = Post.objects.all().count() |
| 131 | + context_data['total_posts_this_year'] = self.get_total_posts(period='year') |
| 132 | + context_data['total_visitors'] = Visitor.objects.all().count() |
| 133 | + context_data['total_visitors_this_year'] = self.get_total_visitors(period='year') |
| 134 | + context_data['data_visitors_per_months'] = self.get_data_visitors_per_months() |
| 135 | + context_data['data_per_months'] = self.get_data_per_months() |
| 136 | + return context_data |
0 commit comments