# Path: core/models.py
# class Job(models.Model):
#     """
#     In [1]: languages = Language.objects.all()
# 
#     In [2]: packages = Package.objects.all()
# 
#     In [3]: for package in packages:
#         for language in languages:
#             (job, created) = Job.objects.get_or_create(language=language, package=package)
#        ....:         job.save()
#        ....:
# 
#     """
# 
#     last_updated = models.DateTimeField(auto_now_add=True, auto_now=True)
#     update_on = models.DateTimeField(blank=True, null=True)
#     active = models.BooleanField(default=False)
# 
#     language = models.ForeignKey(Language, db_index=True)
#     package = models.ForeignKey(Package, db_index=True)
# 
#     def save(self, *args, **kwargs):
#         super(Job, self).save(*args, **kwargs)
#         # Set update_on to end of queue
#         end_queue = Job.objects.latest("update_on")
#         if not end_queue or not end_queue.update_on:
#             end_of_line = self.last_updated
#         else:
#             end_of_line = end_queue.update_on
#         # Now, add 5 minutes to it!
#         from datetime import timedelta
#         five_minutes = timedelta(minutes=5)
# 
#         self.update_on = end_of_line + five_minutes
#         super(Job, self).save(*args, **kwargs)
# 
# class Language(models.Model):
# 
#     long_name = models.CharField(max_length=255, blank=True, null=True)
#     short_name = models.CharField(max_length=30)
# 
#     def __unicode__(self):
#         return "%s (%s)" % (self.long_name, self.short_name) if self.long_name else self.short_name
# 
# class Package(models.Model):
# 
#     name = models.CharField(max_length=255)
#     src_url = models.URLField(blank=True, null=True)
# 
#     def __unicode__(self):
#         return self.name
# 
#     @property
#     def languages(self):
#         languages = Translation.objects.filter(package=self).values(
#             'language').distinct()
#         return Language.objects.filter(id__in=languages).order_by('long_name')
# 
#     @property
#     def translations(self):
#         return self.translation_set.all()
# 
#     @property
#     def translations_by_language(self):
#         return [(l, Translation.objects.filter(package=self,
#                 language=l).count()) for l in self.languages]
# 
#     def get_absolute_url(self):
#         return reverse('package_detail', args=[self.name])
# 
# Path: core/lib.py
# @transaction.commit_manually
# def populate_db(po, language, package):
# 
#     logging.info("Updating %s translations for %s" % (language.long_name, package.name))
# 
#     # Delete existing translations for this package/language combination
#     translations = Translation.objects.filter(language=language, package=package)
# 
#     if translations:
#         logging.info("Deleting existing translations for %s / %s." % (package.name, language.long_name))
#         translations.delete()
# 
#     valid_entries = [e for e in po if not e.obsolete]
# 
#     if not valid_entries:
#         logging.info("Apparently there are no %s translations for %s" % (language.long_name, package.name))
# 
#     try:
#         for entry in valid_entries:
#             if entry.translated():
#                 if entry.msgid_plural:
#                     entry.msgstr = entry.msgstr_plural.get('0', '')
#                     entry.msgstr_plural = entry.msgstr_plural.get('1', '')
# 
#                 add_translation(entry, language, package)
# 
#     except Exception, e:
#         logging.error("Failed to create sentences: %s" % str(e))
#         transaction.rollback()
#     else:
#         transaction.commit()


import glob
import os.path
import tempfile
import logging
from django.core.management.base import AppCommand
from django.core.management.base import BaseCommand, CommandError
from polib import pofile
from core.models import Job, Language, Package
from core.lib import populate_db
from urllib import urlopen
# Pylyglot 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Pylyglot.  If not, see <http://www.gnu.org/licenses/>.






class Command(BaseCommand):

    def handle(self, *args, **options):
        # Check if exists any Job and create them if necessary
        for language in Language.objects.all():
            for package in Package.objects.all():
                try:
                    Job.objects.get(language=language, package=package)
                except Job.DoesNotExist:
                    Job(language=language, package=package).save()

        active_job = Job.objects.filter(active = True)
        if active_job:
            logging.info("There's a job being run right now for %s - %s later" % (active_job[0].package.name, active_job[0].language.short_name))
            logging.info("Will try again later!")
            return


# Path: core/models.py
# class Job(models.Model):
#     """
#     In [1]: languages = Language.objects.all()
# 
#     In [2]: packages = Package.objects.all()
# 
#     In [3]: for package in packages:
#         for language in languages:
#             (job, created) = Job.objects.get_or_create(language=language, package=package)
#        ....:         job.save()
#        ....:
# 
#     """
# 
#     last_updated = models.DateTimeField(auto_now_add=True, auto_now=True)
#     update_on = models.DateTimeField(blank=True, null=True)
#     active = models.BooleanField(default=False)
# 
#     language = models.ForeignKey(Language, db_index=True)
#     package = models.ForeignKey(Package, db_index=True)
# 
#     def save(self, *args, **kwargs):
#         super(Job, self).save(*args, **kwargs)
#         # Set update_on to end of queue
#         end_queue = Job.objects.latest("update_on")
#         if not end_queue or not end_queue.update_on:
#             end_of_line = self.last_updated
#         else:
#             end_of_line = end_queue.update_on
#         # Now, add 5 minutes to it!
#         from datetime import timedelta
#         five_minutes = timedelta(minutes=5)
# 
#         self.update_on = end_of_line + five_minutes
#         super(Job, self).save(*args, **kwargs)
# 
# class Language(models.Model):
# 
#     long_name = models.CharField(max_length=255, blank=True, null=True)
#     short_name = models.CharField(max_length=30)
# 
#     def __unicode__(self):
#         return "%s (%s)" % (self.long_name, self.short_name) if self.long_name else self.short_name
# 
# class Package(models.Model):
# 
#     name = models.CharField(max_length=255)
#     src_url = models.URLField(blank=True, null=True)
# 
#     def __unicode__(self):
#         return self.name
# 
#     @property
#     def languages(self):
#         languages = Translation.objects.filter(package=self).values(
#             'language').distinct()
#         return Language.objects.filter(id__in=languages).order_by('long_name')
# 
#     @property
#     def translations(self):
#         return self.translation_set.all()
# 
#     @property
#     def translations_by_language(self):
#         return [(l, Translation.objects.filter(package=self,
#                 language=l).count()) for l in self.languages]
# 
#     def get_absolute_url(self):
#         return reverse('package_detail', args=[self.name])
# 
# Path: core/lib.py
# @transaction.commit_manually
# def populate_db(po, language, package):
# 
#     logging.info("Updating %s translations for %s" % (language.long_name, package.name))
# 
#     # Delete existing translations for this package/language combination
#     translations = Translation.objects.filter(language=language, package=package)
# 
#     if translations:
#         logging.info("Deleting existing translations for %s / %s." % (package.name, language.long_name))
#         translations.delete()
# 
#     valid_entries = [e for e in po if not e.obsolete]
# 
#     if not valid_entries:
#         logging.info("Apparently there are no %s translations for %s" % (language.long_name, package.name))
# 
#     try:
#         for entry in valid_entries:
#             if entry.translated():
#                 if entry.msgid_plural:
#                     entry.msgstr = entry.msgstr_plural.get('0', '')
#                     entry.msgstr_plural = entry.msgstr_plural.get('1', '')
# 
#                 add_translation(entry, language, package)
# 
#     except Exception, e:
#         logging.error("Failed to create sentences: %s" % str(e))
#         transaction.rollback()
#     else:
#         transaction.commit()


# Path: core/management/commands/populatedb.py
import glob
import os.path
import tempfile
import logging
from django.core.management.base import AppCommand
from django.core.management.base import BaseCommand, CommandError
from polib import pofile
from core.models import Job, Language, Package
from core.lib import populate_db
from urllib import urlopen

# Pylyglot 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Pylyglot.  If not, see <http://www.gnu.org/licenses/>.






class Command(BaseCommand):

    def handle(self, *args, **options):
        # Check if exists any Job and create them if necessary
        for language in Language.objects.all():
            for package in Package.objects.all():
                try:
                    Job.objects.get(language=language, package=package)
                except Job.DoesNotExist:
                    Job(language=language, package=package).save()

        active_job = Job.objects.filter(active = True)
        if active_job:
            logging.info("There's a job being run right now for %s - %s later" % (active_job[0].package.name, active_job[0].language.short_name))
            logging.info("Will try again later!")
            return


        job = Job.objects.order_by('update_on')[0]