Filtering ISO date by hour - python

I was trying to get the number of insurance claims by Day and what time it occurred. For Example, it is March 22 today and there were claims on 8 am another in 12noon. I was trying to get all the claim count this day and what hour did the claims occured.
I did try using modified_at__hour but it doesn't show what I needed.
Sample code for claims per week and show what day it was claimed
class GetClaimsCompare_week(APIView):
def get_claim_count(self, claims_data, claim_type):
claims_count = claims_data.filter(claim_type = claim_type).count()
return claims_count
def get_claims_type(self, claims_per_day):
return claims_per_day.claim_type
#return claims_data.claim_type
def get(self, request):
today = datetime.now()
claims_data = Claims.objects.filter(modified_at__day = today.day)
claims_per_day = claims_data.annotate(day = TruncDay('modified_at')).values('day').annotate(claim_type=Count('id'))
labels = []
claims_type = list(set(map(self.get_claims_type, claims_data)))
final = {}
for claims in claims_per_day:
labels.append(claims['day'])
#data.append(claims['claims'])
for claim_type in claims_type:
final[claim_type] = self.get_claim_count(claims_data, claim_type)
context = {
'Date(s)': labels,
'Claims on this date': final
}
return Response(context)

Related

Make Django Form Validation For Reservation

i make a reservation system, which has form validation,
This should check whether the the bus on that date is already booked or not,
but, unfortunately there still error on this code..
so here my code :
forms.py
class BookingForm(forms.ModelForm):
class Meta:
model = Booking
fields = '__all__'
tempReDate = date(1,1,1) # temp local variable
def clean_start(self):
start = self.cleaned_data['start']
# cheacking if date for this bus is free
res_all = Booking.objects.all()
for item in res_all:
if start >= item.start and start <= item.end and item.resourceId == self.instance.resourceId:
raise forms.ValidationError("This date already reserved by same bus, choose other date or other bus")
#set variable to send for next funtion: clean_EndDate
self.tempReDate = start
return start
def clean_end(self):
#check if EndDate is empty
if not self.data['end']:
#set numbers days of reservation 1 if no end date
self.instance.numOfDays = 1
return None
# if start date is empty
if self.tempReDate == date(1,1,1):
raise forms.ValidationError("Must Choose start date")
# if start date is not empty
else:
start = self.tempReDate
end = self.cleaned_data['end']
# cheackig start date is not lower than end date
if end < start:
raise forms.ValidationError("start date cannot be later than end date")
# cheackig if reservation is no longer than 14 days
if end > start+ timedelta(days=14):
raise forms.ValidationError("You can make a reservation for max 14 days")
# cheacking if reservation for this bus is free
res_all = Booking.objects.all().filter(resourceId=self.instance.resourceId)
for item in res_all:
#jesli w przedziale nowego zamowienia jest poczatek albo koniec innego to zajęte
if start <= item.start and item.start <= end or start <= item.end and item.end <= end:
raise forms.ValidationError("This date already reserved by same bus, choose other date or other bus")
# calculateing number days of reservation and save it
var = end - start
self.instance.numOfDays = var.days + 1
return start
And this code on views.py
views.py
def create_book(request):
booking_forms = BookingForm()
customer_forms = CustomerForm()
if request.method == 'POST':
booking_forms = BookingForm(request.POST)
customer_forms = CustomerForm(request.POST)
if customer_forms.is_valid() and booking_forms.is_valid():
nama_pelanggan = customer_forms.cleaned_data['nama_pelanggan']
no_hp = customer_forms.cleaned_data['no_hp']
# nmpl = request.POST['nama_pelanggan']
# nm_pl = {"nama_pelanggan": nmpl}
customer, _ = Customer.objects.get_or_create(no_hp=customer_forms.cleaned_data['no_hp'],
defaults={
'nama_pelanggan': customer_forms.cleaned_data['nama_pelanggan'],
'alamat_pelanggan': '',
'tipe': ''
},)
idbooking = booking_forms.cleaned_data['idbooking']
resourceId = booking_forms.cleaned_data['resourceId']
start = booking_forms.cleaned_data['start']
end = booking_forms.cleaned_data['end']
harga_jual = booking_forms.cleaned_data['harga_jual']
uang_jalan = booking_forms.cleaned_data['uang_jalan']
parkir_bensin = booking_forms.cleaned_data['parkir_bensin']
note = booking_forms.cleaned_data['note']
Booking.objects.create(
idbooking=idbooking,
resourceId=resourceId,
start=start,
end=end,
harga_jual=harga_jual,
uang_jalan=uang_jalan,
parkir_bensin=parkir_bensin,
note=note,
title=resourceId,
nm_pelanggan=nama_pelanggan,
backgroundColor='blue'
),
return redirect('booking-list')
else:
print(booking_forms.errors)
print(customer_forms.errors)
context = {
'cform': customer_forms,
'form': booking_forms,
}
return render(request, 'booking/create_book.html', context)
What error ?
The problem im facing now, when i make reservation use same bus on same date, the validation error message not showing, and program continue make booking which is make double booking and will make conflict
.
what is problem with this form validation ?
.
thanks

passing the dates between two dates back into the formatday method

I'm learning Python and Django and am building a job management app, I have the following, which is all working ok, and displays the jobs on a HTML calendar. It displays the start and end dates okay, however if the job is more than two days there is a gap in the calendar as there is no date related to the job to show.
I can get the dates in between by doing the following;
start = job.start_time
end = job.end_time
between = end - start
for i in range(between.days + 1):
datesbetween = [start + timedelta(days=i)]
Below is my views.py file:
class AdminCalendarView(LoginRequiredMixin,PermissionRequiredMixin, ListView):
model = Job
template_name = 'jobs/admin_diary.html'
permission_required = 'jobs.add_job', 'raise_exception=True'
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
# use today's date for the calendar
d = get_date(self.request.GET.get('month', None))
# Instantiate our calendar class with today's year and date
cal = AdminCalendar(d.year, d.month)
jobs = Job.objects.filter(start_time__year=d.year, start_time__month=d.month)
# Call the formatmonth method, which returns our calendar as a table
html_cal = cal.formatmonth(jobs, withyear=True)
context['calendar'] = mark_safe(html_cal)
context['prev_month'] = prev_month(d)
context['next_month'] = next_month(d)
return context
The utils.py file:
from datetime import datetime, timedelta, date
from calendar import HTMLCalendar
from .models import Job
from django.db.models import Q
class AdminCalendar(HTMLCalendar):
def __init__(self, year=None, month=None):
self.year = year
self.month = month
super(AdminCalendar, self).__init__()
# formats a day as a td
# filter jobs by day
def formatday(self, day, jobs):
jobs_per_day = jobs.filter(Q(start_time__day=day) | Q(end_time__day=day))
d = ''
for job in jobs_per_day:
d += f"<a href='/job/{job.id}'> <li class='job-diary'> {job.client} Job: {job.id}</li> </a>"
if day != 0:
return f"<td><span class='date'>{day}</span><ul> {d} </ul></td>"
return '<td></td>'
# formats a week as a tr
def formatweek(self, theweek, jobs):
week = ''
i = [a_tuple[0] for a_tuple in theweek]
for d in i:
week += self.formatday(d, jobs)
return f'<tr> {week} </tr>'
# formats a month as a table
# filter jobs by year and month
def formatmonth(self, jobs, withyear=True):
cal = f'<table border="0" cellpadding="0" cellspacing="0" class="calendar table-responsive">\n'
cal += f'{self.formatmonthname(self.year, self.month, withyear=withyear)}\n'
cal += f'{self.formatweekheader()}\n'
for week in self.monthdays2calendar(self.year, self.month):
cal += f'{self.formatweek(week, jobs)}\n'
cal += f'</table>'
return cal
However, I'm not sure how I need to approach passing these days back into the formatday() method, or if I should be looking at a different way of doing it, as it all new to me, any help would be much appreciated.
Thanks
In your formatday function you can filter for jobs that are between start and end date (including those dates) by doing something like:
def formatday(self, day, jobs):
jobs_per_day = jobs.filter(start_time__day__gte=day, end_time__day__lte=day)
This will give you jobs that span any days from start to end including start and end. Then your current code will work to display a job on any day it spans. I believe this is what you were asking for, but feel free to clarify if not.

ValueError: timestamp out of range for platform localtime()/gmtime() function

I have a class assignment to write a python program to download end-of-day data last 25 years the major global stock market indices from Yahoo Finance:
Dow Jones Index (USA)
S&P 500 (USA)
NASDAQ (USA)
DAX (Germany)
FTSE (UK)
HANGSENG (Hong Kong)
KOSPI (Korea)
CNX NIFTY (India)
Unfortunately, when I run the program an error occurs.
File "C:\ProgramData\Anaconda2\lib\site-packages\yahoofinancials__init__.py", line 91, in format_date
form_date = datetime.datetime.fromtimestamp(int(in_date)).strftime('%Y-%m-%d')
ValueError: timestamp out of range for platform localtime()/gmtime() function
If you see below, you can see the code that I have written. I'm trying to debug my mistakes. Can you help me out please? Thanks
from yahoofinancials import YahooFinancials
import pandas as pd
# Select Tickers and stock history dates
index1 = '^DJI'
index2 = '^GSPC'
index3 = '^IXIC'
index4 = '^GDAXI'
index5 = '^FTSE'
index6 = '^HSI'
index7 = '^KS11'
index8 = '^NSEI'
freq = 'daily'
start_date = '1993-06-30'
end_date = '2018-06-30'
# Function to clean data extracts
def clean_stock_data(stock_data_list):
new_list = []
for rec in stock_data_list:
if 'type' not in rec.keys():
new_list.append(rec)
return new_list
# Construct yahoo financials objects for data extraction
dji_financials = YahooFinancials(index1)
gspc_financials = YahooFinancials(index2)
ixic_financials = YahooFinancials(index3)
gdaxi_financials = YahooFinancials(index4)
ftse_financials = YahooFinancials(index5)
hsi_financials = YahooFinancials(index6)
ks11_financials = YahooFinancials(index7)
nsei_financials = YahooFinancials(index8)
# Clean returned stock history data and remove dividend events from price history
daily_dji_data = clean_stock_data(dji_financials
.get_historical_stock_data(start_date, end_date, freq)[index1]['prices'])
daily_gspc_data = clean_stock_data(gspc_financials
.get_historical_stock_data(start_date, end_date, freq)[index2]['prices'])
daily_ixic_data = clean_stock_data(ixic_financials
.get_historical_stock_data(start_date, end_date, freq)[index3]['prices'])
daily_gdaxi_data = clean_stock_data(gdaxi_financials
.get_historical_stock_data(start_date, end_date, freq)[index4]['prices'])
daily_ftse_data = clean_stock_data(ftse_financials
.get_historical_stock_data(start_date, end_date, freq)[index5]['prices'])
daily_hsi_data = clean_stock_data(hsi_financials
.get_historical_stock_data(start_date, end_date, freq)[index6]['prices'])
daily_ks11_data = clean_stock_data(ks11_financials
.get_historical_stock_data(start_date, end_date, freq)[index7]['prices'])
daily_nsei_data = clean_stock_data(nsei_financials
.get_historical_stock_data(start_date, end_date, freq)[index8]['prices'])
stock_hist_data_list = [{'^DJI': daily_dji_data}, {'^GSPC': daily_gspc_data}, {'^IXIC': daily_ixic_data},
{'^GDAXI': daily_gdaxi_data}, {'^FTSE': daily_ftse_data}, {'^HSI': daily_hsi_data},
{'^KS11': daily_ks11_data}, {'^NSEI': daily_nsei_data}]
# Function to construct data frame based on a stock and it's market index
def build_data_frame(data_list1, data_list2, data_list3, data_list4, data_list5, data_list6, data_list7, data_list8):
data_dict = {}
i = 0
for list_item in data_list2:
if 'type' not in list_item.keys():
data_dict.update({list_item['formatted_date']: {'^DJI': data_list1[i]['close'], '^GSPC': list_item['close'],
'^IXIC': data_list3[i]['close'], '^GDAXI': data_list4[i]['close'],
'^FTSE': data_list5[i]['close'], '^HSI': data_list6[i]['close'],
'^KS11': data_list7[i]['close'], '^NSEI': data_list8[i]['close']}})
i += 1
tseries = pd.to_datetime(list(data_dict.keys()))
df = pd.DataFrame(data=list(data_dict.values()), index=tseries,
columns=['^DJI', '^GSPC', '^IXIC', '^GDAXI', '^FTSE', '^HSI', '^KS11', '^NSEI']).sort_index()
return df
Your problem is your datetime stamps are in the wrong format. If you look at the error code it clugely tells you:
datetime.datetime.fromtimestamp(int(in_date)).strftime('%Y-%m-%d')
Notice the int(in_date) part?
It wants the unix timestamp. There are several ways to get this, out of the time module or the calendar module, or using Arrow.
import datetime
import calendar
date = datetime.datetime.strptime("1993-06-30", "%Y-%m-%d")
start_date = calendar.timegm(date.utctimetuple())
* UPDATED *
OK so I fixed up to the dataframes portion. Here is my current code:
# Select Tickers and stock history dates
index = {'DJI' : YahooFinancials('^DJI'),
'GSPC' : YahooFinancials('^GSPC'),
'IXIC':YahooFinancials('^IXIC'),
'GDAXI':YahooFinancials('^GDAXI'),
'FTSE':YahooFinancials('^FTSE'),
'HSI':YahooFinancials('^HSI'),
'KS11':YahooFinancials('^KS11'),
'NSEI':YahooFinancials('^NSEI')}
freq = 'daily'
start_date = '1993-06-30'
end_date = '2018-06-30'
# Clean returned stock history data and remove dividend events from price history
daily = {}
for k in index:
tmp = index[k].get_historical_stock_data(start_date, end_date, freq)
if tmp:
daily[k] = tmp['^{}'.format(k)]['prices'] if 'prices' in tmp['^{}'.format(k)] else []
Unfortunately I had to fix a couple things in the yahoo module. For the class YahooFinanceETL:
#staticmethod
def format_date(in_date, convert_type):
try:
x = int(in_date)
convert_type = 'standard'
except:
convert_type = 'unixstamp'
if convert_type == 'standard':
if in_date < 0:
form_date = datetime.datetime(1970, 1, 1) + datetime.timedelta(seconds=in_date)
else:
form_date = datetime.datetime.fromtimestamp(int(in_date)).strftime('%Y-%m-%d')
else:
split_date = in_date.split('-')
d = date(int(split_date[0]), int(split_date[1]), int(split_date[2]))
form_date = int(time.mktime(d.timetuple()))
return form_date
AND:
# private static method to scrap data from yahoo finance
#staticmethod
def _scrape_data(url, tech_type, statement_type):
response = requests.get(url)
soup = BeautifulSoup(response.content, "html.parser")
script = soup.find("script", text=re.compile("root.App.main")).text
data = loads(re.search("root.App.main\s+=\s+(\{.*\})", script).group(1))
if tech_type == '' and statement_type != 'history':
stores = data["context"]["dispatcher"]["stores"]["QuoteSummaryStore"]
elif tech_type != '' and statement_type != 'history':
stores = data["context"]["dispatcher"]["stores"]["QuoteSummaryStore"][tech_type]
else:
if "HistoricalPriceStore" in data["context"]["dispatcher"]["stores"] :
stores = data["context"]["dispatcher"]["stores"]["HistoricalPriceStore"]
else:
stores = data["context"]["dispatcher"]["stores"]["QuoteSummaryStore"]
return stores
You will want to look at the daily dict, and rewrite your build_data_frame function, which it should be a lot simpler now since you are working with a dictionary already.
I am actually the maintainer and author of YahooFinancials. I just saw this post and wanted to personally apologize for the inconvenience and let you all know I will be working on fixing the module this evening.
Could you please open an issue on the module's Github page detailing this?
It would also be very helpful to know which version of python you were running when you encountered these issues.
https://github.com/JECSand/yahoofinancials/issues
I am at work right now, however as soon as I get home in ~7 hours or so I will attempt to code a fix and release it. I'll also work on the exception handling. I try my best to maintain this module, but my day (and often night time) job is rather demanding. I will report back with the final results of these fixes and publish to pypi when it is done and stable.
Also if anyone else has any feedback or personal fixes made you can offer, it would be a huge huge help in fixing this. Proper credit will be given of course. I am also in desperate need of contributers, so if anyone is interested in that as well let me know. I am really wanting to take YahooFinancials to the next level and have this project become a stable and reliable alternative for free financial data for python projects.
Thank you for your patience and for using YahooFinancials.

Check if any date in a range is between another range

I have the following situation:
This is my views.py:
def home(request):
date = datetime.date.today()
start_week = date - datetime.timedelta(date.weekday() + 1)
end_week = start_week + datetime.timedelta(6)
week_tasks = Task.object.filter(owner=request.user, start_date__range=[start_week, end_week])
context = {}
context['week_tasks'] = week_tasks
return render(request, 'home.html', context)
This view check if the start_date (DateField) is inside the range of the current week. But I have another field on the database, end_date, and I want to check if any value of this range is on the current week.
Check the exemple:
Let's supose that the current week is the week of the day 17. With my current view, only the All Day Event and Conference belong to the week. I need to show that all these events belong to the week.
Obs.: I can't just check if start_date and end_date are in the week, because I have the situation of the Long Event, that starts before the week and ends after.
WORKING:
views.py:
def home(request):
date = datetime.date.today()
if date.isoweekday() == 7:
date = date + datetime.timedelta(1)
start_week = date - datetime.timedelta(date.isoweekday())
end_week = start_week + datetime.timedelta(6)
week_tasks = Task.object.filter(owner=request.user).exclude(end_date__lt=start_week).exclude(start_date__gt=end_week)
context = {}
context['week_tasks'] = week_tasks
return render(request, 'home.html', context)
week window is defined by: week_start, week_end
tasks are defined by: task_start, task_end
task has overlap with week if:
task_start < week_end and task_end >= week_start

Issues Grabbing Entry Via Date

So I have an entry in the DB.
article = BlogPost.objects.get(id=1)
I can successfully verify the date of it via day, month and year
article.game_date.day = 26
article.game_date.month = 11
article.game_date.year = 2015
When I run a query to get this entry:
from datetime import date
todays_date = date.today()
articles2 = BlogPost.objects.get(game_date__year = todays_date.year, game_date__month = todays_date.month, game_date__day = todays_date.day)
It finds nothing. However when I run :
articles2 = BlogPost.objects.get(game_date__year = todays_date.year)
It does find it. But If I run:
articles2 = BlogPost.objects.get(game_date__day = todays_date.day)
or
articles2 = BlogPost.objects.get(game_date__day = 26)
It wont find it. It was working for a bit, but I can't figure out why it did this all of a sudden.

Categories