| OLD | NEW |
| 1 # coding: utf-8 | 1 # coding: utf-8 |
| 2 # Copyright 2014 The LUCI Authors. All rights reserved. | 2 # Copyright 2014 The LUCI Authors. All rights reserved. |
| 3 # Use of this source code is governed under the Apache License, Version 2.0 | 3 # Use of this source code is governed under the Apache License, Version 2.0 |
| 4 # that can be found in the LICENSE file. | 4 # that can be found in the LICENSE file. |
| 5 | 5 |
| 6 """Tasks definition. | 6 """Tasks definition. |
| 7 | 7 |
| 8 Each user request creates a new TaskRequest. The TaskRequest instance saves the | 8 Each user request creates a new TaskRequest. The TaskRequest instance saves the |
| 9 metadata of the request, e.g. who requested it, when why, etc. It links to the | 9 metadata of the request, e.g. who requested it, when why, etc. It links to the |
| 10 actual data of the request in a TaskProperties. The TaskProperties represents | 10 actual data of the request in a TaskProperties. The TaskProperties represents |
| (...skipping 695 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 706 def task_id(self): | 706 def task_id(self): |
| 707 """Returns the TaskResultSummary packed id, not the task request key.""" | 707 """Returns the TaskResultSummary packed id, not the task request key.""" |
| 708 return task_pack.pack_result_summary_key( | 708 return task_pack.pack_result_summary_key( |
| 709 task_pack.request_key_to_result_summary_key(self.key)) | 709 task_pack.request_key_to_result_summary_key(self.key)) |
| 710 | 710 |
| 711 @property | 711 @property |
| 712 def expiration_secs(self): | 712 def expiration_secs(self): |
| 713 """Reconstructs this value from expiration_ts and created_ts. Integer.""" | 713 """Reconstructs this value from expiration_ts and created_ts. Integer.""" |
| 714 return int((self.expiration_ts - self.created_ts).total_seconds()) | 714 return int((self.expiration_ts - self.created_ts).total_seconds()) |
| 715 | 715 |
| 716 @property | |
| 717 def has_access(self): | |
| 718 """Returns True if the current user has read-write access to this request. | |
| 719 | |
| 720 This is used for: | |
| 721 * Read access: ability to read the task info and logs. | |
| 722 * Write access: ability to cancel the task. | |
| 723 | |
| 724 Warning: This function looks at the current Authentication context. | |
| 725 """ | |
| 726 return ( | |
| 727 acl.is_privileged_user() or | |
| 728 self.authenticated == auth.get_current_identity()) | |
| 729 | |
| 730 def to_dict(self): | 716 def to_dict(self): |
| 731 """Converts properties_hash to hex so it is json serializable.""" | 717 """Converts properties_hash to hex so it is json serializable.""" |
| 732 # to_dict() doesn't recurse correctly into ndb.LocalStructuredProperty! | 718 # to_dict() doesn't recurse correctly into ndb.LocalStructuredProperty! |
| 733 out = super(TaskRequest, self).to_dict( | 719 out = super(TaskRequest, self).to_dict( |
| 734 exclude=['pubsub_auth_token', 'properties', 'service_account_token']) | 720 exclude=['pubsub_auth_token', 'properties', 'service_account_token']) |
| 735 out['properties'] = self.properties.to_dict() | 721 out['properties'] = self.properties.to_dict() |
| 736 properties_hash = out['properties_hash'] | 722 properties_hash = out['properties_hash'] |
| 737 out['properties_hash'] = ( | 723 out['properties_hash'] = ( |
| 738 properties_hash.encode('hex') if properties_hash else None) | 724 properties_hash.encode('hex') if properties_hash else None) |
| 739 return out | 725 return out |
| (...skipping 274 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1014 init_new_request(request, allow_high_priority, original_secret_bytes) | 1000 init_new_request(request, allow_high_priority, original_secret_bytes) |
| 1015 return request | 1001 return request |
| 1016 | 1002 |
| 1017 | 1003 |
| 1018 def validate_priority(priority): | 1004 def validate_priority(priority): |
| 1019 """Throws ValueError if priority is not a valid value.""" | 1005 """Throws ValueError if priority is not a valid value.""" |
| 1020 if 0 > priority or MAXIMUM_PRIORITY < priority: | 1006 if 0 > priority or MAXIMUM_PRIORITY < priority: |
| 1021 raise datastore_errors.BadValueError( | 1007 raise datastore_errors.BadValueError( |
| 1022 'priority (%d) must be between 0 and %d (inclusive)' % | 1008 'priority (%d) must be between 0 and %d (inclusive)' % |
| 1023 (priority, MAXIMUM_PRIORITY)) | 1009 (priority, MAXIMUM_PRIORITY)) |
| OLD | NEW |