Python django.db.models.functions.TruncYear() Examples
The following are 3
code examples of django.db.models.functions.TruncYear().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
django.db.models.functions
, or try the search function
.
Example #1
Source File: tasks.py From polemarch with GNU Affero General Public License v3.0 | 5 votes |
def stats(self, last: int) -> OrderedDict: qs = self.filter(start_time__gte=now()-timedelta(days=last)) qs = qs.annotate( day=dbfunc.TruncDay('start_time'), month=dbfunc.TruncMonth('start_time'), year=dbfunc.TruncYear('start_time'), ) result = OrderedDict() result['day'] = self._get_history_stats_by(qs, 'day') result['month'] = self._get_history_stats_by(qs, 'month') result['year'] = self._get_history_stats_by(qs, 'year') return result
Example #2
Source File: views.py From lexpredict-contraxsuite with GNU Affero General Public License v3.0 | 4 votes |
def get_context_data(self, **kwargs): ctx = super().get_context_data(**kwargs) documents = Document.objects if self.request.user.is_reviewer: documents = documents.filter( taskqueue__user_id=self.request.user.pk, textunit__dateusage__isnull=False).distinct() ctx['documents'] = documents.values('pk', 'name').iterator() periods_qs = DateUsage.objects # got rid of probably false-pos periods_qs = periods_qs.filter(date__gte=datetime.date.today() - datetime.timedelta(365 * 300), date__lte=datetime.date.today() + datetime.timedelta(365 * 100)) periods_qs = periods_qs \ .annotate(year=TruncYear('date')).values('year') \ .annotate(count=Count('date', distinct=True)).order_by('year') periods = [] start = end = count = years_count = 0 limit = 1000 years_count_limit = 10 flag = False current_year = datetime.date.today().year # get periods to truncate data by periods otherwise d3 hangs on large datasets for q in periods_qs: if not start: start = q['year'] if count + q['count'] > limit or years_count > years_count_limit: periods.append({'start': start.year, 'end': end.year, 'count': count, 'checked': start.year <= current_year <= end.year}) count = q['count'] start = q['year'] years_count = 0 flag = True else: count += q['count'] years_count += 1 flag = False end = q['year'] if not flag and count: periods.append({'start': start.year, 'end': end.year, 'count': count, 'checked': start.year <= current_year <= end.year}) ctx['periods'] = periods return ctx
Example #3
Source File: museum.py From eoj3 with MIT License | 4 votes |
def museum_view(request): def convert_timedelta(td): return { 'year': td.days // 365, 'day': td.days % 365, 'hour': td.seconds // 3600, 'minute': (td.seconds % 3600) // 60, 'second': td.seconds % 60 } ctx = {} ctx['total_problem_count'] = Problem.objects.count() ctx['total_submission_count'] = Submission.objects.count() ctx['total_user_count'] = User.objects.filter(is_active=True).count() # NOTE: this will break if there is no submission at all first_submission = Submission.objects.last() ctx['first_submission_time'] = first_submission.create_time ctx['first_submission_duration'] = convert_timedelta(datetime.now() - ctx['first_submission_time']) ctx['first_submission_author'] = first_submission.author from uptime import uptime ctx['uptime'] = convert_timedelta(timedelta(seconds=uptime())) ctx['server_time'] = datetime.now() ctx['eoj3_create_duration'] = convert_timedelta(datetime.now() - datetime(2017, 3, 11, 18, 32)) ctx['submission_count_1'] = Submission.objects.filter(create_time__gt=datetime.now() - timedelta(days=1)).count() ctx['submission_count_7'] = Submission.objects.filter(create_time__gt=datetime.now() - timedelta(days=7)).count() ctx['submission_count_30'] = Submission.objects.filter(create_time__gt=datetime.now() - timedelta(days=30)).count() ctx['submission_stat'] = Submission.objects.filter(create_time__gt=datetime.today() - timedelta(days=30)). \ annotate(date=TruncDate('create_time')).values('date'). \ annotate(count=Count('id')).values('date', 'count').order_by() ctx['user_stat'] = User.objects.filter(is_active=True).annotate(date=TruncYear('date_joined')).values('date'). \ annotate(count=Count('id')).values('date', 'count').order_by("date") for idx, user in enumerate(ctx['user_stat']): if idx == 0: continue user['count'] += ctx['user_stat'][idx - 1]['count'] ctx['problem_stat'] = Problem.objects.annotate(date=TruncYear('create_time')).values('date'). \ annotate(count=Count('id')).values('date', 'count').order_by("date") for idx, user in enumerate(ctx['problem_stat']): if idx == 0: continue user['count'] += ctx['problem_stat'][idx - 1]['count'] ctx['servers'] = servers = Server.objects.filter(enabled=True) for server in servers: server.status = ping(server) return render(request, 'museum.jinja2', context=ctx)