Ejemplo n.º 1
0
 def test_empty_values(self):
     empty_default = 'no_name'
     self.assertEqual(
         generate_code('', []),
         empty_default,
     )
     self.assertEqual(
         generate_code(None, []),
         empty_default,
     )
Ejemplo n.º 2
0
 def test_empty_values(self):
     empty_default = 'no_name'
     self.assertEqual(
         generate_code('', []),
         empty_default,
     )
     self.assertEqual(
         generate_code(None, []),
         empty_default,
     )
Ejemplo n.º 3
0
    def save(self, *args, **kwargs):
        """
        Saving a couch version of Product will trigger
        one way syncing to the SQLProduct version of this
        product.
        """
        # mark modified time stamp for selective syncing
        self.last_modified = datetime.utcnow()

        # generate code if user didn't specify one
        if not self.code:
            from corehq.apps.commtrack.util import generate_code
            self.code = generate_code(
                self.name,
                SQLProduct.objects
                    .filter(domain=self.domain)
                    .values_list('code', flat=True)
                    .distinct()
            )

        result = super(Product, self).save(*args, **kwargs)

        self.sync_to_sql()

        return result
Ejemplo n.º 4
0
    def save(self, *args, **kwargs):
        """
        Saving a couch version of Location will trigger
        one way syncing to the SQLLocation version of this
        location.
        """
        self.last_modified = datetime.utcnow()

        # lazy migration for site_code
        if not self.site_code:
            from corehq.apps.commtrack.util import generate_code
            all_codes = [
                code.lower() for code in
                (SQLLocation.objects.filter(domain=self.domain)
                                    .values_list('site_code', flat=True))
            ]
            self.site_code = generate_code(self.name, all_codes)

        sql_location = None
        result = super(Location, self).save(*args, **kwargs)

        # try sync locations and when SQLLocation doesn't returned, removed Couch object from database.
        # added because when we sync location by tasks we can have behavior that the task can be
        # killed in _sync_location method and this causes the problems
        try:
            sql_location = self._sync_location()
        finally:
            if sql_location:
                sql_location.save()
            else:
                self.delete()
                result = None

        return result
Ejemplo n.º 5
0
    def save(self, *args, **kwargs):
        """
        Saving a couch version of Location will trigger
        one way syncing to the SQLLocation version of this
        location.
        """
        self.last_modified = datetime.utcnow()

        # lazy migration for site_code
        if not self.site_code:
            from corehq.apps.commtrack.util import generate_code
            all_codes = [
                code.lower() for code in
                (SQLLocation.objects.filter(domain=self.domain)
                                    .values_list('site_code', flat=True))
            ]
            self.site_code = generate_code(self.name, all_codes)

        # Set the UUID here so we can save to SQL first (easier to rollback)
        if not self._id:
            self._id = self.get_db().server.next_uuid()

        sync_to_sql = kwargs.pop('sync_to_sql', True)
        kwargs['sync_to_sql'] = False  # only sync here
        with transaction.atomic():
            if sync_to_sql:
                self._migration_do_sync()
            super(Location, self).save(*args, **kwargs)
Ejemplo n.º 6
0
    def save(self, *args, **kwargs):
        """
        Saving a couch version of Location will trigger
        one way syncing to the SQLLocation version of this
        location.
        """
        self.last_modified = datetime.utcnow()

        # lazy migration for site_code
        if not self.site_code:
            from corehq.apps.commtrack.util import generate_code
            all_codes = [
                code.lower() for code in (SQLLocation.objects.filter(
                    domain=self.domain).values_list('site_code', flat=True))
            ]
            self.site_code = generate_code(self.name, all_codes)

        # Set the UUID here so we can save to SQL first (easier to rollback)
        if not self._id:
            self._id = self.get_db().server.next_uuid()

        sync_to_sql = kwargs.pop('sync_to_sql', True)
        kwargs['sync_to_sql'] = False  # only sync here
        with transaction.atomic():
            if sync_to_sql:
                self._migration_do_sync()
            super(Location, self).save(*args, **kwargs)
Ejemplo n.º 7
0
    def save(self, *args, **kwargs):
        """
        Saving a couch version of Location will trigger
        one way syncing to the SQLLocation version of this
        location.
        """
        self.last_modified = datetime.utcnow()

        # lazy migration for site_code
        if not self.site_code:
            from corehq.apps.commtrack.util import generate_code
            self.site_code = generate_code(
                self.name,
                Location.site_codes_for_domain(self.domain)
            )

        sql_location = None
        result = super(Location, self).save(*args, **kwargs)

        # try sync locations and when SQLLocation doesn't returned, removed Couch object from database.
        # added because when we sync location by tasks we can have behavior that the task can be
        # killed in _sync_location method and this causes the problems
        try:
            sql_location = self._sync_location()
        finally:
            if sql_location:
                sql_location.save()
            else:
                self.delete()
                result = None

        return result
Ejemplo n.º 8
0
    def save(self, *args, **kwargs):
        """
        Saving a couch version of Product will trigger
        one way syncing to the SQLProduct version of this
        product.
        """
        # mark modified time stamp for selective syncing
        self.last_modified = datetime.utcnow()

        # generate code if user didn't specify one
        if not self.code:
            from corehq.apps.commtrack.util import generate_code
            self.code = generate_code(
                self.name,
                SQLProduct.objects
                    .filter(domain=self.domain)
                    .values_list('code', flat=True)
                    .distinct()
            )

        result = super(Product, self).save(*args, **kwargs)

        self.clear_caches(self.domain)
        self.sync_to_sql()

        return result
Ejemplo n.º 9
0
    def test_adds_1(self):
        name = 'turtle'
        existing = ['turtle']

        self.assertEqual(
            generate_code(name, existing),
            'turtle1'
        )
Ejemplo n.º 10
0
    def test_doesnt_die_on_only_numbers(self):
        name = '1'
        existing = []

        self.assertEqual(
            generate_code(name, existing),
            '1'
        )
Ejemplo n.º 11
0
    def test_sluggifies(self):
        name = 'türtłę'
        existing = []

        self.assertEqual(
            generate_code(name, existing),
            'turtle'
        )
Ejemplo n.º 12
0
    def test_no_change_needed(self):
        name = 'turtle'
        existing = []

        self.assertEqual(
            generate_code(name, existing),
            'turtle'
        )
Ejemplo n.º 13
0
    def test_doesnt_strip_numbers(self):
        name = 'taco1'
        existing = []

        self.assertEqual(
            generate_code(name, existing),
            'taco1'
        )
Ejemplo n.º 14
0
    def test_doesnt_die_on_only_numbers(self):
        name = '1'
        existing = []

        self.assertEqual(
            generate_code(name, existing),
            '1'
        )
Ejemplo n.º 15
0
    def test_doesnt_strip_numbers(self):
        name = 'taco1'
        existing = []

        self.assertEqual(
            generate_code(name, existing),
            'taco1'
        )
Ejemplo n.º 16
0
    def test_increments_number(self):
        name = 'taco'
        existing = ['taco', 'taco1', 'taco2', 'taco3']

        self.assertEqual(
            generate_code(name, existing),
            'taco4'
        )
Ejemplo n.º 17
0
    def test_strips_spaces(self):
        name = 'pizza cat'
        existing = []

        self.assertEqual(
            generate_code(name, existing),
            'pizza_cat'
        )
Ejemplo n.º 18
0
    def test_increments_number(self):
        name = 'taco'
        existing = ['taco', 'taco1', 'taco2', 'taco3']

        self.assertEqual(
            generate_code(name, existing),
            'taco4'
        )
Ejemplo n.º 19
0
    def test_adds_1(self):
        name = 'turtle'
        existing = ['turtle']

        self.assertEqual(
            generate_code(name, existing),
            'turtle1'
        )
Ejemplo n.º 20
0
    def test_strips_spaces(self):
        name = 'pizza cat'
        existing = []

        self.assertEqual(
            generate_code(name, existing),
            'pizza_cat'
        )
Ejemplo n.º 21
0
    def test_sluggifies(self):
        name = u'türtłę'
        existing = []

        self.assertEqual(
            generate_code(name, existing),
            'turtle'
        )
Ejemplo n.º 22
0
    def test_no_change_needed(self):
        name = 'turtle'
        existing = []

        self.assertEqual(
            generate_code(name, existing),
            'turtle'
        )
Ejemplo n.º 23
0
 def clean(self):
     if 'name' in self.cleaned_data and not self.cleaned_data.get('site_code', None):
         all_codes = [
             code.lower() for code in
             (SQLLocation.objects.exclude(location_id=self.location.location_id)
                                 .filter(domain=self.domain)
                                 .values_list('site_code', flat=True))
         ]
         self.cleaned_data['site_code'] = generate_code(self.cleaned_data['name'], all_codes)
Ejemplo n.º 24
0
 def clean(self):
     if 'name' in self.cleaned_data and not self.cleaned_data.get('site_code', None):
         all_codes = [
             code.lower() for code in
             (SQLLocation.objects.exclude(location_id=self.location.location_id)
                                 .filter(domain=self.domain)
                                 .values_list('site_code', flat=True))
         ]
         self.cleaned_data['site_code'] = generate_code(self.cleaned_data['name'], all_codes)
Ejemplo n.º 25
0
def set_site_code_if_needed(location):
    from corehq.apps.commtrack.util import generate_code
    if not location.site_code:
        all_codes = [
            code.lower() for code in
            (SQLLocation.objects.exclude(location_id=location.location_id)
                                .filter(domain=location.domain)
                                .values_list('site_code', flat=True))
        ]
        location.site_code = generate_code(location.name, all_codes)
Ejemplo n.º 26
0
def set_site_code_if_needed(location):
    from corehq.apps.commtrack.util import generate_code
    if not location.site_code:
        all_codes = [
            code.lower() for code in (SQLLocation.objects.exclude(
                location_id=location.location_id).filter(
                    domain=location.domain).values_list('site_code', flat=True)
                                      )
        ]
        location.site_code = generate_code(location.name, all_codes)
Ejemplo n.º 27
0
    def save_docs(cls, docs, use_uuids=True, codes_by_domain=None):
        from corehq.apps.commtrack.util import generate_code

        codes_by_domain = codes_by_domain or {}

        def get_codes(domain):
            if domain not in codes_by_domain:
                codes_by_domain[domain] = SQLProduct.objects.filter(domain=domain)\
                    .values_list('code', flat=True).distinct()
            return codes_by_domain[domain]

        for doc in docs:
            doc.last_modified = datetime.utcnow()
            if not doc['code_']:
                doc['code_'] = generate_code(doc['name'],
                                             get_codes(doc['domain']))

        super(Product, cls).save_docs(docs, use_uuids)
Ejemplo n.º 28
0
    def save_docs(cls, docs, use_uuids=True, all_or_nothing=False, codes_by_domain=None):
        from corehq.apps.commtrack.util import generate_code

        codes_by_domain = codes_by_domain or {}

        def get_codes(domain):
            if domain not in codes_by_domain:
                codes_by_domain[domain] = SQLProduct.objects.filter(domain=domain)\
                    .values_list('code', flat=True).distinct()
            return codes_by_domain[domain]

        for doc in docs:
            if not doc['code_']:
                doc['code_'] = generate_code(
                    doc['name'],
                    get_codes(doc['domain'])
                )

        super(Product, cls).save_docs(docs, use_uuids, all_or_nothing)
Ejemplo n.º 29
0
    def save(self, *args, **kwargs):
        """
        Saving a couch version of Location will trigger
        one way syncing to the SQLLocation version of this
        location.
        """
        self.last_modified = datetime.utcnow()

        # lazy migration for site_code
        if not self.site_code:
            from corehq.apps.commtrack.util import generate_code
            self.site_code = generate_code(
                self.name,
                Location.site_codes_for_domain(self.domain)
            )

        result = super(Location, self).save(*args, **kwargs)

        self._sync_location()

        return result
Ejemplo n.º 30
0
    def handle(self, *args, **options):
        self.stdout.write("Populating site codes...\n")

        site_codes_by_domain = {}

        relevant_ids = set([r['id'] for r in Location.get_db().view(
            'locations/by_type',
            reduce=False,
        ).all()])

        to_save = []

        for loc in iter_docs(Location.get_db(), relevant_ids):
            if not loc['site_code']:
                # triggering the safe will cause this to get populated
                self.stdout.write("Updating location %s\n" % loc['name'])

                if loc['domain'] not in site_codes_by_domain:
                    site_codes_by_domain[loc['domain']] = list(
                        Location.site_codes_for_domain(loc['domain'])
                    )

                loc['site_code'] = generate_code(
                    loc['name'],
                    site_codes_by_domain[loc['domain']]
                )
                site_codes_by_domain[loc['domain']].append(loc['site_code'])

                to_save.append(loc)

                if len(to_save) > 500:
                    Location.get_db().bulk_save(to_save)
                    to_save = []

        if to_save:
            Location.get_db().bulk_save(to_save)