<commit_before>'''
ARCHES - a program developed to inventory and manage immovable cultural heritage.
Copyright (C) 2013 <NAME> and World Monuments Fund

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
'''

import json
import couchdb
from django.db import transaction
from urlparse import urlparse, urljoin
from arches.app.models import models
from arches.app.models.mobile_survey import MobileSurvey
from arches.app.models.system_settings import settings
from django.utils.translation import ugettext as _
from arches.app.utils.response import JSONResponse
from arches.app.utils.betterJSONSerializer import JSONSerializer, JSONDeserializer
from django.http import HttpRequest, HttpResponseNotFound
import arches.app.views.search as search

class Couch(object):
    def __init__(self):
        self.couch = couchdb.Server(settings.COUCHDB_URL)
        # self.logger = logging.getLogger(__name__)

    def create_survey(self, mobile_survey, user=None):
        print 'Creating Couch DB: project_', str(mobile_survey.id)
        db = self.couch.create('project_' + str(mobile_survey.id))

        survey = JSONSerializer().serializeToPython(mobile_survey, exclude='cards')
        survey['type'] = 'metadata'
        db.save(survey)

    def delete_survey(self, mobile_survey_id):
        try:
            print 'Deleting Couch DB: project_', str(mobile_survey_id)
            return self.couch.delete('project_' + str(mobile_survey_id))
        except Exception as e:
            print e
            return connection_error


    def clear_associated_surveys(self):
        surveys = [str(msm['id']) for msm in models.MobileSurveyModel.objects.values("id")]
        couchdbs = [dbname for dbname in self.couch]
        for db in couchdbs:
            survey_id = db[-36:]
            if survey_id in surveys:
                self.delete_survey(survey_id)

    def clear_unassociated_surveys(self):
        surveys = [str(msm['id']) for msm in models.MobileSurveyModel.objects.values("id")]
        couchdbs = [dbname for dbname in couchserver]
        for db in couchdbs:
            survey_id = db[-36:]
            if survey_id not in surveys:
                if 'project' in db:
                    self.delete_survey(survey_id)

    def create_associated_surveys(self):
        for mobile_survey in models.MobileSurveyModel.objects.all():
            print "Creating", mobile_survey
            self.create_survey(mobile_survey)
            print "Populating"
            db = couch['project_' + str(mobile_survey.id)]
            self.load_data_into_couch(mobile_survey, db, mobile_survey.lasteditedby)

    def collect_resource_instances_for_couch(self, mobile_survey, user):
        """
        Uses the data definition configs of a mobile survey object to search for
        resource instances relevant to a mobile survey. Takes a user object which
        is required for search.
        """<commit_msg>Rejigger couch.py into a class, similar to ES<commit_after>'''
ARCHES - a program developed to inventory and manage immovable cultural heritage.
Copyright (C) 2013 <NAME> and World Monuments Fund

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
'''

import json
import couchdb
from django.db import transaction
from urlparse import urlparse, urljoin
from arches.app.models import models
from arches.app.models.mobile_survey import MobileSurvey
from arches.app.models.system_settings import settings
from django.utils.translation import ugettext as _
from arches.app.utils.response import JSONResponse
from arches.app.utils.betterJSONSerializer import JSONSerializer, JSONDeserializer
from django.http import HttpRequest, HttpResponseNotFound
import arches.app.views.search as search

class Couch(object):
    def __init__(self):
        self.couch = couchdb.Server(settings.COUCHDB_URL)
        # self.logger = logging.getLogger(__name__)

    def create_survey(self, mobile_survey, user=None):
        print 'Creating Couch DB: project_', str(mobile_survey.id)
        db = self.couch.create('project_' + str(mobile_survey.id))

        survey = JSONSerializer().serializeToPython(mobile_survey, exclude='cards')
        survey['type'] = 'metadata'
        db.save(survey)

    def delete_survey(self, mobile_survey_id):
        try:
            print 'Deleting Couch DB: project_', str(mobile_survey_id)
            return self.couch.delete('project_' + str(mobile_survey_id))
        except Exception as e:
            print e
            return connection_error


    def clear_associated_surveys(self):
        surveys = [str(msm['id']) for msm in models.MobileSurveyModel.objects.values("id")]
        couchdbs = [dbname for dbname in self.couch]
        for db in couchdbs:
            survey_id = db[-36:]
            if survey_id in surveys:
                self.delete_survey(survey_id)

    def clear_unassociated_surveys(self):
        surveys = [str(msm['id']) for msm in models.MobileSurveyModel.objects.values("id")]
        couchdbs = [dbname for dbname in couchserver]
        for db in couchdbs:
            survey_id = db[-36:]
            if survey_id not in surveys:
                if 'project' in db:
                    self.delete_survey(survey_id)

    def create_associated_surveys(self):
        for mobile_survey in models.MobileSurveyModel.objects.all():
            print "Creating", mobile_survey
            self.create_survey(mobile_survey)
            print "Populating"
            db = self.couch['project_' + str(mobile_survey.id)]
            self.load_data_into_couch(mobile_survey, db, mobile_survey.lasteditedby)

    def collect_resource_instances_for_couch(self, mobile_survey, user):
        """
        Uses the data definition configs of a mobile survey object to search for
        resource instances relevant to a mobile survey. Takes a user object which
        is required for search.
        """
{'fraction_non_alphanumeric': 0.0739000427167877, 'fraction_numerical': 0.0025630072618539087, 'mean_word_length': 3.211031175059952, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '207110', 'n_tokens_mistral': 1980, 'n_tokens_neox': 1929, 'n_words': 655}