<commit_before># -*- coding: utf-8 -*-

from django.core.management.base import NoArgsCommand

from lemon import metatags
from lemon.metatags.models import Page


class Command(NoArgsCommand):

    help = 'Sync metatags with all registered models'

    def handle_noargs(self, **options):
        from django.contrib import admin
        admin.autodiscover()

        print 'Starting metatags synchronisation with all registered models.'
        for model, model_metatags in metatags.site._registry.items():
            print 'Syncing %s.%s model.' % (model._meta.app_label, model.__name__)
            self.sync_metatags(model, model_metatags)
        print 'All objects with `get_absolute_url` method was synced.',

        print 'Removing orphaned metatags.'
        self.remove_orphaned()
        print 'Done.'

    def sync_metatags(self, model, model_metatags):
        for obj in model.objects.all():
            try:
                page = Page.objects.get_for_content_object(obj)
            except Page.DoesNotExist:
                pass
            else:
                page.update_url_path()
                page.update_language()
                page.update_sites()
                sites = ', '.join([s.domain for s in page.sites.all()])
                print '  Metatags for %s (%s) was updated.' % (page.url_path, sites)

    def remove_orphaned(self):
        for page in Page.objects.all():
            if page.content_type and page.object_id:
                if not page.content_object:
                    sites = ', '.join([s.domain for s in page.sites.all()])
                    print '  Metatags for %s (%s) was deleted.' % (page.url_path, sites)
                    page.delete()<commit_msg>Remove unused variable from sync_metatags command<commit_after># -*- coding: utf-8 -*-

from django.core.management.base import NoArgsCommand

from lemon import metatags
from lemon.metatags.models import Page


class Command(NoArgsCommand):

    help = 'Sync metatags with all registered models'

    def handle_noargs(self, **options):
        from django.contrib import admin
        admin.autodiscover()

        print 'Starting metatags synchronisation with all registered models.'
        for model in metatags.site._registry:
            print 'Syncing %s.%s model.' % (model._meta.app_label, model.__name__)
            self.sync_metatags(model)
        print 'All objects with `get_absolute_url` method was synced.',

        print 'Removing orphaned metatags.'
        self.remove_orphaned()
        print 'Done.'

    def sync_metatags(self, model):
        for obj in model.objects.all():
            try:
                page = Page.objects.get_for_content_object(obj)
            except Page.DoesNotExist:
                pass
            else:
                page.update_url_path()
                page.update_language()
                page.update_sites()
                sites = ', '.join([s.domain for s in page.sites.all()])
                print '  Metatags for %s (%s) was updated.' % (page.url_path, sites)

    def remove_orphaned(self):
        for page in Page.objects.all():
            if page.content_type and page.object_id:
                if not page.content_object:
                    sites = ', '.join([s.domain for s in page.sites.all()])
                    print '  Metatags for %s (%s) was deleted.' % (page.url_path, sites)
                    page.delete()
{'fraction_non_alphanumeric': 0.0984759671746776, 'fraction_numerical': 0.0005861664712778429, 'mean_word_length': 2.0310834813499112, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '3102292', 'n_tokens_mistral': 980, 'n_tokens_neox': 931, 'n_words': 261}