Matteo Scandolo | eb0d11c | 2017-08-08 13:05:26 -0700 | [diff] [blame] | 1 | |
| 2 | # Copyright 2017-present Open Networking Foundation |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | |
| 16 | |
Murat Parlakisik | 638c65f | 2017-05-31 11:10:24 +0300 | [diff] [blame] | 17 | default_attributes = {} |
| 18 | def __init__(self, *args, **kwargs): |
| 19 | ceilometer_services = CeilometerService.get_service_objects().all() |
| 20 | if ceilometer_services: |
| 21 | self._meta.get_field("provider_service").default = ceilometer_services[0].id |
| 22 | super(MonitoringPublisher, self).__init__(*args, **kwargs) |
| 23 | |
| 24 | def can_update(self, user): |
| 25 | #Allow creation of this model instances for non-admin users also |
| 26 | return True |
| 27 | |
| 28 | @property |
| 29 | def creator(self): |
| 30 | from core.models import User |
| 31 | if getattr(self, "cached_creator", None): |
| 32 | return self.cached_creator |
| 33 | creator_id=self.get_attribute("creator_id") |
| 34 | if not creator_id: |
| 35 | return None |
| 36 | users=User.objects.filter(id=creator_id) |
| 37 | if not users: |
| 38 | return None |
| 39 | user=users[0] |
| 40 | self.cached_creator = users[0] |
| 41 | return user |
| 42 | |
| 43 | @creator.setter |
| 44 | def creator(self, value): |
| 45 | if value: |
| 46 | value = value.id |
| 47 | if (value != self.get_attribute("creator_id", None)): |
| 48 | self.cached_creator=None |
| 49 | self.set_attribute("creator_id", value) |