~lutostag/ubuntu/utopic/maas/1.5.2

« back to all changes in this revision

Viewing changes to src/metadataserver/models.py

  • Committer: Package Import Robot
  • Author(s): Andres Rodriguez
  • Date: 2012-04-12 16:46:22 UTC
  • mto: (20.1.1 quantal) (1.2.1)
  • mto: This revision was merged to the branch mainline in revision 14.
  • Revision ID: package-import@ubuntu.com-20120412164622-laz1qoxycfrddka0
Tags: upstream-0.1+bzr462+dfsg
ImportĀ upstreamĀ versionĀ 0.1+bzr462+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
20
20
    Manager,
21
21
    Model,
22
22
    )
 
23
from django.shortcuts import get_object_or_404
23
24
from maasserver.models import (
24
25
    create_auth_token,
25
26
    Node,
177
178
 
178
179
    node = ForeignKey(Node, null=False, editable=False, unique=True)
179
180
    data = BinaryField(null=False)
 
181
 
 
182
 
 
183
class NodeCommissionResultManager(Manager):
 
184
    """Utility to manage a collection of :class:`NodeCommissionResult`s."""
 
185
 
 
186
    def clear_results(self, node):
 
187
        """Remove all existing results for a node."""
 
188
        self.filter(node=node).delete()
 
189
 
 
190
    def store_data(self, node, name, data):
 
191
        """Store data about a node."""
 
192
        existing, created = self.get_or_create(
 
193
            node=node, name=name, defaults=dict(data=data))
 
194
        if not created:
 
195
            existing.data = data
 
196
            existing.save()
 
197
 
 
198
    def get_data(self, node, name):
 
199
        """Get data about a node."""
 
200
        ncr = get_object_or_404(NodeCommissionResult, node=node, name=name)
 
201
        return ncr.data
 
202
 
 
203
 
 
204
class NodeCommissionResult(Model):
 
205
    """Storage for data returned from node commissioning.
 
206
 
 
207
    Commissioning a node results in various bits of data that need to be
 
208
    stored, such as lshw output.  This model allows storing of this data
 
209
    as unicode text, with an arbitrary name, for later retrieval.
 
210
 
 
211
    :ivar node: The context :class:`Node`.
 
212
    :ivar name: A unique name to use for the data being stored.
 
213
    :ivar data: The file's actual data, unicode only.
 
214
    """
 
215
 
 
216
    objects = NodeCommissionResultManager()
 
217
 
 
218
    node = ForeignKey(Node, null=False, editable=False, unique=False)
 
219
    name = CharField(max_length=100, unique=False, editable=False)
 
220
    data = CharField(max_length=1024 * 1024, editable=True)
 
221
 
 
222
    class Meta:
 
223
        unique_together = ('node', 'name')