<commit_before>from django.conf import settings
from django.http import HttpResponseRedirect
from pymongo.connection import Connection
from pymongo.errors import ConnectionFailure

class MongodbConnectionMiddleware(object):

    def process_request(self, request):
        try:
            connection = Connection(settings.CDR_MONGO_HOST, settings.CDR_MONGO_PORT)
            if connection.is_locked:
                if connection.unlock(): # if db gets unlocked
                    return None
                return HttpResponseRedirect('/?db_error=locked')
            else:
                return None
        except ConnectionFailure:
            return HttpResponseRedirect('/?db_error=closed')


<commit_msg>Add middleware check for existing data<commit_after>from django.conf import settings
from django import http
from django.http import HttpResponseRedirect
from pymongo.connection import Connection
from pymongo.errors import ConnectionFailure

class MongodbConnectionMiddleware(object):

    def process_request(self, request):
        try:
            connection = Connection(settings.CDR_MONGO_HOST, settings.CDR_MONGO_PORT)
            if connection.is_locked:
                if connection.unlock(): # if db gets unlocked
                    return None
                return HttpResponseRedirect('/?db_error=locked')
            else:
                #check if collection have any data
                db = connection[settings.CDR_MONGO_DB_NAME]
                collection = db[settings.CDR_MONGO_CDR_COMMON]
                doc = collection.find_one()
                if not doc:
                    return http.HttpResponseForbidden('<h1>Error Import data</h1> Make sure you have existing data in your collections')
                else:
                    return None
        except ConnectionFailure:
            return HttpResponseRedirect('/?db_error=closed')



{'fraction_non_alphanumeric': 0.05985169491525424, 'fraction_numerical': 0.001059322033898305, 'mean_word_length': 2.3792486583184256, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '812450', 'n_tokens_mistral': 462, 'n_tokens_neox': 434, 'n_words': 126}