Good evening, could you help me in how I can put a condition so that a message comes out saying that you can not take an hour because it is already busy ?, I currently have this:
class reserva (models.Model):
_name='gimnasio.reserva'
tipo_reserva=fields.Selection([('clase','Clase'),('evaluacion','Evaluacion')])
fecha_reserva=fields.Date()
start_time=fields.Float()
end_time=fields.Float()
def fecha(self):
if self.star_time==self.star_time:
raise validationError('the hour is busy')
I have another question for you. you know how to configure Datetime only for hour and minutes because I only need hour and minutes but not the date.
To configure Datetime only for hour and minutes.
time = fields.Datetime("time")
custom_time = fields.Char('my_custome_time')
#api.onchange('time')
def _get_time(self):
if self.time:
for rec in self:
# datetime value is a string like 'YYYY-mm-dd HH:MM:SS'
# so just extract string from position 11 to 16
_time = self.time[11:16]
self.custom_time = _time
rec.custom_time = self.custom_time
I think you can use strptime method from datetime module.
from datetime import datetime as dt
start_time = fields.Float()
end_time = fields.Float()
#api.onchange('start_time','end_time')
def _check(self):
records = self.env["gimnasio.reserva"].search([("day", '=', the day you want to check eg. "2019-06-13")])
for rec in records:
ref_start = dt.strptime(str(rec.start_time), "%H:%M")
curr_start = dt.strptime(str(self.start_time), "%H:%M")
if ref_start == curr_start:
raise validationError('the hour is busy')
I didn't debug yet, you can try it.
how to eliminate the default date that you added ("2019-06-13") and that any date should not have the same busy schedule?
In this case you don't need datetime module just
#api.constrains("start_time")
def _check(self):
# search db for any record have same start time.
records = self.env["gimnasio.reserva"].search([('start_time ','=', self.start_time)])
if len(records) > 0:
raise validationError('the hour is busy')
Related
I am trying to print Instance name, volume id , instance ID, instance private IP, snapshot start time snapshot ID in one tabular form.
Code :-
#!/usr/bin/python
import os
import boto.ec2
import time
import datetime
conn = boto.ec2.connect_to_region("eu-west-1")
reservations = conn.get_all_instances()
tags = conn.get_all_tags()
snapshots = conn.get_all_snapshots(owner="self")
instances = [i for r in reservations for i in r.instances]
import datetime
today = datetime.datetime.today()
one_day = datetime.timedelta(days=1)
yesterday = today - one_day
from datetime import datetime
for inst in instances:
for snap in snapshots:
start_time = datetime.strptime (snap.start_time.split('.')[0],'%Y-%m-%dT%H:%M:%S')
if start_time > yesterday and 'Name' in inst.tags:
print "%s (%s) (%s) (%s) [%s] " % (inst.tags['Name'], start_time, snap.volume_id
It's looping through all instances again & again. E.g-> I have 17 instances then it loops through all instances 17*17 times and displays the result.
Any suggestions..
It looks like your snapshots don't come from your instances so you could use something like this
for inst, snap in zip(instances, snapshots):
do stuff
That way it will loop through both at the same time and not all of your snapshots per instance
I currently have:
#classmethod
def get_past_week(self):
start_date = datetime.now().date()
end_date = datetime.now().date() - timedelta(weeks=1)
return MyModel.objects.filter(pub_date__range=(end_date, start_date)).aggregate(Sum('off_hours'))
which simply pulls all posts from the current date minus 7 days
I want to pull posts from within the same day factoring in the time at the moment. Thus if the time is 15:00 GMT now, I want all posts from 14:59:49 GMT back to 00:00:01 GMT of the same day. How can I do something like that?
I've tried something like (not work
def get_today_hours(self):
start_time = datetime.now().time()
end_time = datetime.now().time() - timedelta(hours=datetime.today().hour, minutes=0, seconds=0)
return MyModel.objects.filter(pub_date__range=(end_time, start_time))
According to this post on SO, there's a way to get items from same day by using CURDATE()
You can do the following
end_time = datetime.now()
start_time = datetime(end_time.year,end_time.month,end_time.day)
That will set end_time to actual time, and start_time to the same day as end_time, but with the time at 0 hours and 0 seconds.
How about this:
from datetime import datetime, timedelta
end_time = datetime.now()
start_time = datetime.combine(end_time, datetime.min.time()) #Reset the hours/time offset
qs = MyModel.objects.filter(pub_date__range=(start_time, end_time))
Or something even simpler:
qs = MyModel.objects.filter(pub_date__startswith=end_time.date())
Say I have a model:
Mymodel(models.Model):
endtimestamp = models.DateTimeField(null=True)
I need a query to get all Mymodel objects with endstimestamp between today's midnight and yesterday midnight.
what have I tried:
today = datetime.datetime.today()
todays_midnigh = datetime.datetime.fromtimestamp(
today.strftime('%Y-%m-%d 00:00:00.0')
)
yesterday_midnight = todays_midnight - datetime.timedelta(days=1)
objs = Mymodel.objects.filter(endtimestamp__range(
yesterday_midnight, todays_midnight)
)
But this line todays_midnigh = datetime.datetime.fromtimestamp(today.strftime('%Y-%m-%d 00:00:00.0')) does not work, and I know there must be a much pythonic and clear way of achieving this.
assumming this from datetime import datetime as dt, do that:
today = dt.today()
todays_midnigh = dt.combine(today,dt.max.time())
or
todays_midnigh = dt.combine(today,dt.min.time())
as appropriate
what i wanted to do with this code is get all the datas thats dated older than yesterday. It throws an error "an integer is required" at the line:
date = datetime.date(year, month, yesterday)
So far to my knowledge, its taking year as an integer but not month field. It takes the month field as the default datetime field.
Heres my view:
current = datetime.datetime.now()
yesterday = datetime.datetime.today() + datetime.timedelta(days = -1)
year = datetime.date.today().year
month = datetime.date.today() + relativedelta(months = -1)
date = datetime.date(year, month, yesterday)
hist_obj = Events.objects.filter(uploader = request.user,
start_date__lte = date)
return render_to_response('history.html', {'history_obj':hist_obj})
This code is confusing. yesterday and month are both datetimes, because that's how you defined them in lines 2 and 4. So what are you trying to achieve in the code that throws the error? As the message says, you can't pass a datetime as the day or month parameter to construct another datetime. Especially as, surely, yesterday is already the date that you want? Why can't you simply pass that to the query?
Try this,
day_ago = datetime.date.today() - datetime.timedelta(days=1)
yesterday = datetime.datetime(day_ago.year, day_ago.month, day_ago.day)
hist_obj = Events.objects.filter(uploader = request.user,
start_date__lt = yesterday)
return render_to_response('history.html', {'history_obj':hist_obj})
How does one set the date on the CalendarDatePicker. i.e. it defaults to current date and I want to display it with another date which I will set from my controller.
I am displaying the CalendarDatePicker widget in a TableForm from tw.form. I have looked at this for a few hours and can't work out how to do this so any pointers greatly appreciated.
import tw.forms as twf
form = twf.TableForm('dateSel', action='changeDate', children=[
twf.CalendarDatePicker('StartDate', date_format = "%d/%m/%Y"),
twf.CalendarDatePicker('EndDate', date_format = "%d/%m/%Y" )
])
I don't have a copy of twforms laying around, but based on their sample code, it looks like you might want to do something like:
from datetime import datetime
start = twf.CalendarDatePicker('StartDate', date_format = "%d/%m/%Y")
start.default = datetime.now() # or any valid datetime object
end = twf.CalendarDatePicker('EndDate', date_format = "%d/%m/%Y" )
start.default = datetime.now() # or any valid datetime object
form = twf.TableForm('dateSel', action='changeDate', children=[start, end])