Don't write to the DB during __init__.py import

This commit is contained in:
Roberto Rosario
2014-07-18 13:31:11 -04:00
parent f47a5eb9eb
commit 9e50182978
2 changed files with 3 additions and 24 deletions

View File

@@ -129,21 +129,9 @@ class DocumentSearchTestCase(TestCase):
class DocumentUploadFunctionalTestCase(TestCase):
def setUp(self):
from history.api import register_history_type
from .events import (HISTORY_DOCUMENT_CREATED,
HISTORY_DOCUMENT_EDITED, HISTORY_DOCUMENT_DELETED)
self.admin_user = User.objects.create_superuser(username=TEST_ADMIN_USERNAME, email=TEST_ADMIN_EMAIL, password=TEST_ADMIN_PASSWORD)
self.client = Client()
# There events are registered upon loading documents/__init__.py
# while Django's test DB is still not created, so we created them by
# hand.
register_history_type(HISTORY_DOCUMENT_CREATED)
register_history_type(HISTORY_DOCUMENT_EDITED)
register_history_type(HISTORY_DOCUMENT_DELETED)
def test_upload_a_document(self):
from sources.models import WebForm
from sources.literals import SOURCE_CHOICE_WEB_FORM

View File

@@ -3,10 +3,8 @@ from __future__ import absolute_import
import json
import pickle
from django.db import models, transaction
from django.db.utils import DatabaseError
from django.db import models
from django.core import serializers
from django.shortcuts import get_object_or_404
from .models import HistoryType, History
from .runtime_data import history_types_dict
@@ -16,14 +14,6 @@ def register_history_type(history_type_dict):
namespace = history_type_dict['namespace']
name = history_type_dict['name']
try:
with transaction.atomic():
history_type_obj, created = HistoryType.objects.get_or_create(
namespace=namespace, name=name)
history_type_obj.save()
except DatabaseError:
pass
# Runtime
history_types_dict.setdefault(namespace, {})
history_types_dict[namespace][name] = {
@@ -35,7 +25,8 @@ def register_history_type(history_type_dict):
def create_history(history_type_dict, source_object=None, data=None):
history_type = get_object_or_404(HistoryType, namespace=history_type_dict['namespace'], name=history_type_dict['name'])
history_type, created = HistoryType.objects.get_or_create(namespace=history_type_dict['namespace'], name=history_type_dict['name'])
new_history = History(history_type=history_type)
if source_object:
new_history.content_object = source_object