Skip to content

Commit b9f8bb1

Browse files
6 - Intro to Datetime Module
1 parent 8bdec3c commit b9f8bb1

File tree

5 files changed

+64
-6
lines changed

5 files changed

+64
-6
lines changed

src/analytics/views.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@ def dispatch(self, *args, **kwargs):
2020

2121
def get_context_data(self, *args, **kwargs):
2222
context = super(SalesView, self).get_context_data(*args, **kwargs)
23-
qs = Order.objects.all()
23+
qs = Order.objects.all().by_date()
2424
context['orders'] = qs
25-
context['recent_orders'] = qs.recent().not_refunded()[:5]
25+
context['recent_orders'] = qs.recent().not_refunded()
2626
context['recent_orders_data'] = context['recent_orders'].totals_data()
2727
context['recent_orders_cart_data'] = context['recent_orders'].cart_data()
28-
context['shipped_orders'] = qs.recent().not_refunded().by_status(status='shipped')[:1]
28+
context['shipped_orders'] = qs.recent().not_refunded().by_status(status='shipped')
2929
context['shipped_orders_data'] = context['shipped_orders'].totals_data()
30-
context['paid_orders'] = qs.recent().not_refunded().by_status(status='paid')[:4]
30+
context['paid_orders'] = qs.recent().not_refunded().by_status(status='paid')
3131
context['paid_orders_data'] = context['paid_orders'].totals_data()
3232

3333
return context

src/db.sqlite3

0 Bytes
Binary file not shown.

src/ecommerce/settings/local.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@
151151

152152
LANGUAGE_CODE = 'en-us'
153153

154-
TIME_ZONE = 'UTC'
154+
TIME_ZONE = 'America/Los_Angeles' #'UTC'
155155

156156
USE_I18N = True
157157

src/ecommerce/utils.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,63 @@
1+
2+
import datetime
13
import os
24
import random
35
import string
46

7+
from django.utils import timezone
58
from django.utils.text import slugify
69

710

11+
12+
def get_last_month_data(today):
13+
'''
14+
Simple method to get the datetime objects for the
15+
start and end of last month.
16+
'''
17+
this_month_start = datetime.datetime(today.year, today.month, 1)
18+
last_month_end = this_month_start - datetime.timedelta(days=1)
19+
last_month_start = datetime.datetime(last_month_end.year, last_month_end.month, 1)
20+
return (last_month_start, last_month_end)
21+
22+
23+
def get_month_data_range(months_ago=1, include_this_month=False):
24+
'''
25+
A method that generates a list of dictionaires
26+
that describe any given amout of monthly data.
27+
'''
28+
today = datetime.datetime.now().today()
29+
dates_ = []
30+
if include_this_month:
31+
# get next month's data with:
32+
next_month = today.replace(day=28) + datetime.timedelta(days=4)
33+
# use next month's data to get this month's data breakdown
34+
start, end = get_last_month_data(next_month)
35+
dates_.insert(0, {
36+
"start": start.timestamp(),
37+
"end": end.timestamp(),
38+
"start_json": start.isoformat(),
39+
"end": end.timestamp(),
40+
"end_json": end.isoformat(),
41+
"timesince": 0,
42+
"year": start.year,
43+
"month": str(start.strftime("%B")),
44+
})
45+
for x in range(0, months_ago):
46+
start, end = get_last_month_data(today)
47+
today = start
48+
dates_.insert(0, {
49+
"start": start.timestamp(),
50+
"start_json": start.isoformat(),
51+
"end": end.timestamp(),
52+
"end_json": end.isoformat(),
53+
"timesince": int((datetime.datetime.now() - end).total_seconds()),
54+
"year": start.year,
55+
"month": str(start.strftime("%B"))
56+
})
57+
#dates_.reverse()
58+
return dates_
59+
60+
861
def get_filename(path): #/abc/filename.mp4
962
return os.path.basename(path)
1063

src/orders/models.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import math
2+
import datetime
23
from django.conf import settings
34
from django.db import models
45
from django.db.models import Count, Sum, Avg
56
from django.db.models.signals import pre_save, post_save
67
from django.core.urlresolvers import reverse
7-
8+
from django.utils import timezone
89

910
from addresses.models import Address
1011
from billing.models import BillingProfile
@@ -23,6 +24,10 @@ class OrderManagerQuerySet(models.query.QuerySet):
2324
def recent(self):
2425
return self.order_by("-updated", "-timestamp")
2526

27+
def by_date(self):
28+
now = timezone.now() - datetime.timedelta(days=9)
29+
return self.filter(updated__day__gte=now.day)
30+
2631
def totals_data(self):
2732
return self.aggregate(Sum("total"), Avg("total"))
2833

0 commit comments

Comments
 (0)