|
Class DayHandler (tornado. web. RequestHandler ):
Def get (self ):
# Get the parameters of year, month, and day
Y = self. get_argument ("y", None)
M = self. get_argument ("m", None)
D = self. get_argument ("d", None)
# Determine whether the parameters are complete
If y and m and d:
# Convert a parameter to an integer for ease of use
Y = int (y)
M = int (m)
D = int (d)
# Retrieve all records of the current day
Coll_record = self. application. db. record
Record = coll_record.find ({"y": y, "m": m, "d": d })
# Obtain the day of the week
Weekday = datetime. datetime (y, m, d). strftime ("% w ")
# Obtain the daily duty schedule
Coll_duty = self. application. db. duty
Duty = coll_duty.find_one ({"id": int (weekday )})
# Initialize empty Target records (on-duty personnel records on the current day)
Target = list ()
# Traverse all records of the current day
For item in record:
# When the user of the record has a duty task on the same day, calculate and store the task to the target array
If int (item ['id']) in duty ['LIST']:
# Obtain the user's duty start and end times by using the user ID
Start = duty [item ['id'] [0]
End = duty [item ['id'] [1]
# Calculate the duty time/second
Date1 = datetime. datetime (y, m, d, int (start [: 2]), int (start [-2:])
Date2 = datetime. datetime (y, m, d, int (end [: 2]), int (end [-2:])
Item ['length'] = (date2-date1). seconds
# Initialize the actual duty percentage
Item ['per '] = 0
# Initialize the commuting time
Item ['start'] = 0
Item ['end'] = 0
# Traverse the user's logging
For t in item ['check']:
# When it is earlier than the duty time
If t <start:
# Calculating Time Difference
Date1 = datetime. datetime (y, m, d, int (start [: 2]), int (start [-2:])
Date2 = datetime. datetime (y, m, d, int (t [: 2]), int (t [-2:])
Dif = (date1-date2). seconds
# When the punching time is within half an hour before the duty time
If dif <= 1800:
# Punch in at work
Item ['start'] = start
Elif t <end:
# If you are not at work
If not item ['start']:
# The current record time is the start and end times.
Item ['start'] = t
Else:
# Otherwise, the current record time is the off-duty punch-in time
Item ['end'] = t
Else:
# If you have timed out at work
If item ['start']:
# Calculating Time Difference
Date1 = datetime. datetime (y, m, d, int (end [: 2]), int (end [-2:])
Date2 = datetime. datetime (y, m, d, int (t [: 2]), int (t [-2:])
Dif = (date1-date2). seconds
# When the punching time is within half an hour of the duty time
If dif <= 1800:
# Punch in after work
Item ['end'] = end
# Punching at work and off duty
If item ['start'] and item ['end']:
# Calculate the actual duty time
Date1 = datetime. datetime (y, m, d, int (item ['start'] [: 2]), int (item ['start'] [-2:])
Date2 = datetime. datetime (y, m, d, int (item ['end'] [: 2]), int (item ['end'] [-2:])
Dif = (date2-date1). seconds
# Calculated percentage (actual duty time/duty time)
Item ['per '] = int (dif/float (item ['length']) * 100)
Else:
# Abnormal commuting is deemed as not on duty
Item ['start'] = 0
Item ['end'] = 0
# Add records to the target array
Target. append (item)
# Output data
Self. render ("index.html ",
Target = target
)
Def main ():
Tornado. options. parse_command_line ()
Http_server = tornado. httpserver. HTTPServer (Application ())
Http_server.listen (options. port)
Tornado. ioloop. IOLoop. instance (). start ()
If _ name _ = "_ main __":
Main ()
Index.html
{
{% For item in target %}
{
'Id': {item ['id']},
'Start': {item ['start']},
'End': {item ['end']},
'Length': {item ['length']},
'Per ': {item ['per']}
}
{% End %}
}
|