@@ -0,0 +1,3452 @@
+# -*- coding: utf-8 -*-%0A# vim: set ts=4%0A%0A# Copyright 2015 R%C3%A9mi Duraffort%0A# This file is part of DataTag.%0A#%0A# DataTag is free software: you can redistribute it and/or modify%0A# it under the terms of the GNU Affero General Public License as published by%0A# the Free Software Foundation, either version 3 of the License, or%0A# (at your option) any later version.%0A#%0A# DataTag is distributed in the hope that it will be useful,%0A# but WITHOUT ANY WARRANTY; without even the implied warranty of%0A# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the%0A# GNU Affero General Public License for more details.%0A#%0A# You should have received a copy of the GNU Affero General Public License%0A# along with DataTag.  If not, see %3Chttp://www.gnu.org/licenses/%3E%0A%0Afrom __future__ import unicode_literals%0A%0Afrom django.db import transaction%0Afrom django.conf import settings%0Afrom django.contrib.auth.models import Group%0Afrom django.core.management.base import BaseCommand%0Afrom django.utils import timezone%0A%0Afrom DataTag.models import Media, Tag%0Afrom DataTag.utils import Configuration, load_exif%0A%0Aimport fnmatch%0Aimport os%0A%0A%0Aclass Command(BaseCommand):%0A    args = None%0A    help = 'Update the tags'%0A    option_list = BaseCommand.option_list%0A%0A    @transaction.atomic%0A    def handle(self, *args, **kwargs):%0A        self.stdout.write(%22Removing old tags%22)%0A        Tag.objects.all().delete()%0A%0A        self.stdout.write(%22Importing Tags...%22)%0A        root_conf = Configuration()%0A        root_conf.load(os.path.join(settings.MEDIA_ROOT, '.DataTag.yaml'))%0A%0A        tags = %7B%7D%0A        for tag_conf in root_conf.tags:%0A            self.stdout.write(%22 - %25s%22 %25 (tag_conf.name))%0A            tag = Tag(name=tag_conf.name, is_public=tag_conf.public,%0A                      is_root=tag_conf.root)%0A            tag.save()%0A            # Add groups%0A            if tag_conf.groups:%0A                group_list = tag_conf.groups%0A            else:%0A                group_list = root_conf.default_groups%0A%0A            for group in group_list:%0A                self.stdout.write(%22   - %25s%22 %25 (group))%0A                tag.groups.add(Group.objects.get(name=group))%0A%0A            tags%5Btag_conf.name%5D = tag%0A%0A        self.stdout.write(%22Updating the Media%22)%0A        for root, _, files in os.walk(settings.MEDIA_ROOT,%0A                                      followlinks=True):%0A            # Parse the local configuration file (if it exists)%0A            local_conf = Configuration()%0A            if '.DataTag.yaml' in files:%0A                local_conf.load(os.path.join(root, '.DataTag.yaml'))%0A%0A            # Add all files, skipping hidden files and excluded ones%0A            for filename in files:%0A                if filename%5B0%5D == '.':%0A                    continue%0A                # Do we have to skip this file?%0A                skip = False%0A                for exclude in root_conf.exclude:%0A                    if fnmatch.fnmatchcase(filename, exclude):%0A                        self.stdout.write(%22%25s %5Bskip%5D%5Cn%22 %25 (filename))%0A                        skip = True%0A                if skip:%0A                    continue%0A%0A                path = os.path.join(root, filename)%0A                self.stdout.write(%22%25s%22 %25 path)%0A%0A                # Read EXIF data%0A                media = Media.objects.get(path=path)%0A                for media_conf in local_conf.medias:%0A                    if fnmatch.fnmatchcase(filename, media_conf.pattern):%0A                        media.tags.add(*%5Btags%5Btag_name%5D for tag_name in media_conf.tags%5D)%0A

Add a function to update the tags