Skip to content

Commit

Permalink
models: extract blobs functions to a separate file
Browse files Browse the repository at this point in the history
They will be used by migrations, and we don't want to import
api.models there.
  • Loading branch information
bonzini committed May 30, 2018
1 parent 183034b commit 06a9a1e
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 23 deletions.
37 changes: 37 additions & 0 deletions api/blobs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#!/usr/bin/env python3
#
# Copyright 2016, 2018 Red Hat, Inc.
#
# Authors:
# Fam Zheng <[email protected]>
# Paolo Bonzini <[email protected]>
#
# This work is licensed under the MIT License. Please see the LICENSE file or
# http://opensource.org/licenses/MIT.


import os
import json
import uuid
import logging

from django.conf import settings
import lzma

def save_blob(data, name=None):
if not name:
name = str(uuid.uuid4())
fn = os.path.join(settings.DATA_DIR, "blob", name + ".xz")
lzma.open(fn, 'w').write(data.encode("utf-8"))
return name

def load_blob(name):
fn = os.path.join(settings.DATA_DIR, "blob", name + ".xz")
return lzma.open(fn, 'r').read().decode("utf-8")

def load_blob_json(name):
try:
return json.loads(load_blob(name))
except json.decoder.JSONDecodeError as e:
logging.error('Failed to load blob %s: %s' %(name, e))
return None
27 changes: 4 additions & 23 deletions api/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,39 +10,20 @@


from collections import namedtuple
import os
import json
import datetime
import re
import uuid
import logging

from django.conf import settings
from django.db import models
from django.contrib.auth.models import User
from django.urls import reverse
import jsonfield
from mbox import MboxMessage
from event import emit_event, declare_event
import lzma

def save_blob(data, name=None):
if not name:
name = str(uuid.uuid4())
fn = os.path.join(settings.DATA_DIR, "blob", name + ".xz")
lzma.open(fn, 'w').write(data.encode("utf-8"))
return name

def load_blob(name):
fn = os.path.join(settings.DATA_DIR, "blob", name + ".xz")
return lzma.open(fn, 'r').read().decode("utf-8")

def load_blob_json(name):
try:
return json.loads(load_blob(name))
except json.decoder.JSONDecodeError as e:
logging.error('Failed to load blob %s: %s' %(name, e))
return None
from mbox import MboxMessage
from event import emit_event, declare_event
from .blobs import save_blob, load_blob, load_blob_json
import mod

class Project(models.Model):
name = models.CharField(max_length=1024, db_index=True, unique=True,
Expand Down

0 comments on commit 06a9a1e

Please sign in to comment.