Source

transifex / transifex / resources / models.py

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
# -*- coding: utf-8 -*-
"""
String Level models.
"""

import datetime, sys, re, operator
from itertools import groupby

from hashlib import md5
from django.conf import settings
from django.core.cache import cache
from django.db import models, transaction
from django.db.models import Q, Sum
from django.utils.translation import ugettext_lazy as _
from django.utils.hashcompat import md5_constructor
from django.utils import simplejson as json
from django.contrib.auth.models import User, AnonymousUser

from transifex.languages.models import Language
from transifex.projects.models import Project
from transifex.storage.models import StorageFile
from transifex.txcommon.db.models import CompressedTextField, ChainerManager
from transifex.txcommon.log import logger
from transifex.resources.utils import invalidate_template_cache


def _aggregate_rlstats(rlstats_query, grouping_key, total=None):
    """
    Yield AggregatedRLStats objects resulting from grouped and summed RLStats
    objects given in the ``rlstats_query``. The grouping happens per language.

    Parameters:
    rlstats_query: This is the queryset of RLStats to be aggregated
    """

    class AggregatedRLStats(object):
        pass

    grouped_rlstats = groupby(rlstats_query.order_by(grouping_key),
        key=operator.attrgetter(grouping_key))

    for key, rlstats in grouped_rlstats:
        stats = AggregatedRLStats()
        # Init attrs
        stats.translated = 0
        stats.untranslated = 0
        stats.translated_perc = 0
        stats.untranslated_perc = 0
        stats.last_update = None
        stats.last_committer = None
        stats.wordcount = 0
        stats.total = 0
        stats.object = key
        count = 0

        for rl in rlstats:
            stats.translated += rl.translated
            stats.untranslated += rl.untranslated
            stats.translated_perc += rl.translated_perc
            stats.untranslated_perc += rl.untranslated_perc
            stats.total += rl.total
            #FIXME: Add wordcount to RLStats and Translation
            #stats.wordcount += rl.wordcount
            count += 1

            if not stats.last_update or rl.last_update > stats.last_update:
                stats.last_update = rl.last_update
                stats.last_committer = rl.last_committer if rl.last_committer_id else None

        # Recalculate percentage completion
        stats.translated_perc = stats.translated_perc / count
        stats.untranslated_perc = 100 - stats.translated_perc

        if total:
            stats.total=total

        stats.number_resources = count
        yield stats


class ResourceQuerySet(models.query.QuerySet):

    def for_user(self, user):
        """
        Filter available resources based on the user doing the query. This
        checks permissions and filters out private resources that the user
        doesn't have access to.
        """
        return Resource.objects.filter(
            #FIXME: Adding "Project.objects.for_user(user).values('pk').query"
            # breaks some queries like
            # RLStats.objects.private(User.objects.get(username="margie")).count()
            project__in=Project.objects.for_user(user))

class Resource(models.Model):
    """
    A translatable resource, such as a document, a set of strings, etc.
    
    This is roughly equivalent to a POT file, string stream, or some other
    strings grouped together as a single translatable element.
    
    The resource is the rough equivalent to the deprecated 'Component' object,
    but with an important difference: a resource can only have one "source file"
    whereas the Component was able to encapsulate multiple ones.
    
    A resource is always related to a project.
    """

    # Short identifier to be used in the API URLs
    slug = models.SlugField(_('Slug'), max_length=50,
        help_text=_("A short label to be used in the URL, containing only "
                    "letters, numbers, underscores or hyphens."))
    name = models.CharField(_('Name'), max_length=255, null=False, blank=False,
        help_text=_("A descriptive name unique inside the project."))

    # i18n related fields
    source_file = models.ForeignKey(StorageFile, verbose_name=_("Source file"),
        blank=True, null=True,
        help_text=_("A local file used to extract the strings to be "
                    "translated."))
    i18n_type = models.CharField(_('I18n type'), max_length=20, editable=False,
        choices=((k,settings.I18N_METHODS[k]['description']) for k,v in settings.I18N_METHODS.items()),
        help_text=_("The type of i18n method used in this resource (%s)") %
                    ', '.join(settings.TRANS_CHOICES.keys()))
    accept_translations = models.BooleanField(_('Accepting translations?'),
        blank=False, null=False, default=True,
        help_text=_('Is this resource accepting submissions of translations?'))
    total_entities = models.IntegerField(_('Total source entities'),
        blank=False, null=False, editable=False, default=0,
        help_text=_('The number of source strings in this translation'
            ' resource.'))
    wordcount = models.IntegerField(_('Number of words in source entities.'),
        blank=False, null=False, editable=False, default=0,
        help_text=_('The number of words contained in the source entities in'
            ' this translation resource.'))

    # Timestamps
    created = models.DateTimeField(auto_now_add=True, editable=False)
    last_update = models.DateTimeField(auto_now=True, editable=False)

    # Foreign Keys
    source_language = models.ForeignKey(Language,
        verbose_name=_('Source Language'), blank=False, null=False,
        help_text=_("The source language of this Resource."))
    project = models.ForeignKey(Project, verbose_name=_('Project'),
        blank=False, null=True, related_name='resources',
        help_text=_("The project containing the translation resource."))

    # Managers
    objects = ChainerManager(ResourceQuerySet)

    def __unicode__(self):
        return u'%s (%s)' % (self.slug, self.project)

    def __repr__(self):
        return repr(u"<Resource: %s>" % self.slug)

    class Meta:
        unique_together = ('slug', 'project',)
        verbose_name = _('resource')
        verbose_name_plural = _('resources')
        ordering  = ['name',]
        order_with_respect_to = 'project'
        models.get_latest_by = 'created'


    def save(self, *args, **kwargs):
        """
        Do some etxra processing along with the actual save to db.
        """
        # If object is new (aka created=True)
        created=False
        if not self.pk:
            created=True

        # Update total_entries
        self.update_total_entities(save=False)

        # Save the object
        super(Resource, self).save(*args, **kwargs)

        # Create the team language stat objects
        if created:
            Team = models.get_model('teams', 'Team')
            for team in Team.objects.select_related('language'
                ).filter(project=self.project):
                RLStats.objects.get_or_create(resource=self,
                    language=team.language)

        invalidate_template_cache("project_resource_details",
            self.project.slug, self.slug)
        invalidate_template_cache("resource_details",
            self.project.slug, self.slug)

    def delete(self, *args, **kwargs):
        """
        Do some extra processing along with the actual delete to db.
        """
        # Import is here to avoid circular imports
        from transifex.resources.handlers import invalidate_stats_cache

        invalidate_stats_cache(self, self.source_language)
        RLStats.objects.filter(resource=self).delete()
        super(Resource, self).delete(*args, **kwargs)

    def update_total_entities(self, total_entities=None, save=True):
        """
        Return the total number of SourceEntity objects to be translated.
        """
        if total_entities:
            self.total_entities = total_entities
        else:
            self.total_entities = SourceEntity.objects.filter(
                resource=self).values('id').count()
        if save:
            self.save()

    def update_wordcount(self, save=True):
        """
        Return the number of words which need translation in this resource.

        The counting of the words uses the Translation objects of the SOURCE
        LANGUAGE as set of objects. This function does not count the plural
        strings!
        """
        wc = 0
        source_trans = Translation.objects.filter(source_entity__id__in=
            SourceEntity.objects.filter(resource=self).values('id'),
            language=self.source_language)
        for t in source_trans:
            if t:
                wc += t.wordcount
        self.wordcount = wc
        if save:
            self.save()

    @models.permalink
    def get_absolute_url(self):
        return ('resource_detail', None,
            { 'project_slug': self.project.slug, 'resource_slug' : self.slug })

    @property
    def full_name(self):
        """
        Return a simple string without spaces identifying the resource.

        Can be used instead of __unicode__ to create files on disk, URLs, etc.
        """        
        return "%s.%s" % (self.project.slug, self.slug)

    @property
    def entities(self):
        """Return the resource's translation entities."""
        return SourceEntity.objects.filter(resource=self)

    @property
    def available_languages(self):
        """
        All available languages for the resource. This list includes team 
        languages that may have 0 translated entries.
        """
        return Language.objects.filter(
            id__in=RLStats.objects.by_resource(self).values('language').query)

    @property
    def available_languages_without_teams(self):
        """
        All languages for the resource that have at least one translation.
        """
        return Language.objects.filter(
            id__in=RLStats.objects.by_resource(self).filter(
                translated__gt=0).values('language').query)

class SourceEntityManager(models.Manager):

    def for_user(self, user):
        """
        Filter available source entities based on the user doing the query. This
        checks permissions and filters out private source entites that the user
        doesn't have access to.
        """
        return SourceEntity.objects.filter(
            resource__in=Resource.objects.for_user(user))

class SourceEntity(models.Model):
    """
    A representation of a source string which is translated in many languages.
    
    The SourceEntity is pointing to a specific Resource and it is uniquely 
    defined by the string, context and resource fields (so they are unique
    together).
    """
    string = models.TextField(_('String'), blank=False, null=False,
        help_text=_("The actual string content of source string."))
    string_hash = models.CharField(_('String Hash'), blank=False, null=False,
        max_length=32, editable=False,
        help_text=_("The hash of the translation string used for indexing"))
    context = models.CharField(_('Context'), max_length=255,
        null=False, default="",
        help_text=_("A description of the source string. This field specifies"
                    "the context of the source string inside the resource."))
    position = models.IntegerField(_('Position'), blank=True, null=True,
        help_text=_("The position of the source string in the Resource."
                    "For example, the specific position of msgid field in a "
                    "po template (.pot) file in gettext."))
    #TODO: Decision for the following
    occurrences = models.TextField(_('Occurrences'), max_length=1000,
        blank=True, editable=False, null=True,
        help_text=_("The occurrences of the source string in the project code."))
    flags = models.TextField(_('Flags'), max_length=100,
        blank=True, editable=False,
        help_text=_("The flags which mark the source string. For example, if"
                    "there is a python formatted string this is marked as "
                    "\"#, python-format\" in gettext."))
    developer_comment = models.TextField(_('Flags'), max_length=1000,
        blank=True, editable=False,
        help_text=_("The comment of the developer."))

    pluralized = models.BooleanField(_('Pluralized'), blank=False,
        null=False, default=False,
        help_text=_("Identify if the entity is pluralized ot not."))

    # Timestamps
    created = models.DateTimeField(auto_now_add=True, editable=False)
    last_update = models.DateTimeField(auto_now=True, editable=False)

    # Foreign Keys
    # A source string must always belong to a resource
    resource = models.ForeignKey(Resource, verbose_name=_('Resource'),
        blank=False, null=False, related_name='source_entities',
        help_text=_("The translation resource which owns the source string."))

    objects = SourceEntityManager()

    def __unicode__(self):
        return self.string

    class Meta:
        unique_together = (('string_hash', 'context', 'resource'),)
        verbose_name = _('source string')
        verbose_name_plural = _('source strings')
        ordering = ['string', 'context']
        order_with_respect_to = 'resource'
        get_latest_by = 'created'

    def save(self, *args, **kwargs):
        """
        Do some exra processing before the actual save to db.
        """
        context = self.context
        # This is for sqlite support since None objects are treated as strings
        # containing 'None'
        if not context or context == 'None':
            context = ""

        # Calculate new hash
        self.string_hash = md5_constructor(':'.join([self.string,
            context]).encode('utf-8')).hexdigest()

        super(SourceEntity, self).save(*args, **kwargs)

    def get_translation(self, lang_code, rule=5):
        """Return the current active translation for this entity."""
        try:
            return self.translations.get(language__code=lang_code, rule=rule)
        except Translation.DoesNotExist:
            return None


class TranslationManager(models.Manager):
    def by_source_entity_and_language(self, string,
            source_code='en', target_code=None):
        """
        Return the results of searching, based on a specific source string and
        maybe on specific source and/or target language.
        """
        source_entities = []

        source_entities = SourceEntity.objects.filter(string=string,)

        # If no target language given search on any target language.
        if target_code:
            language = Language.objects.by_code_or_alias(target_code)
            results = self.filter(
                        source_entity__in=source_entities, language=language)
        else:
            results = self.filter(source_entity__in=source_entities)
        return results

    def by_string_and_language(self, string, source_code='en', target_code=None):
        """
        Search translation for source strings queries and only in Public projects!
        """
        query = models.Q()
        for term in string.split(' '):
            query &= models.Q(string__icontains=term)

        source_language = Language.objects.by_code_or_alias(source_code)

        # If no target language given search on any target language.
        if target_code:
            language = Language.objects.by_code_or_alias(target_code)
            results =  self.filter(language=language,
                source_entity__resource__project__in=Project.public.all(),
                source_entity__id__in=self.filter(query, language=source_language).values_list(
                    'source_entity', flat=True))
        else:
            results =  self.filter(
                source_entity__resource__project__in=Project.public.all(),
                source_entity__id__in=self.filter(query, language=source_language).values_list(
                    'source_entity', flat=True))
        return results

class Translation(models.Model):
    """
    The representation of a live translation for a given source string.
    
    This model encapsulates all the necessary fields for the translation of a 
    source string in a specific target language. It also contains a set of meta
    fields for the context of this translation.
    """

    string = models.TextField(_('String'), blank=False, null=False,
        help_text=_("The actual string content of translation."))
    string_hash = models.CharField(_('String Hash'), blank=False, null=False,
        max_length=32, editable=False,
        help_text=_("The hash of the translation string used for indexing"))
    rule = models.IntegerField(_('Plural rule'), blank=False,
        null=False, default=5,
        help_text=_("Number related to the plural rule of the translation. "
                    "It's 0=zero, 1=one, 2=two, 3=few, 4=many and 5=other. "
                    "For translations that have its entity not pluralized, "
                    "the rule must be 5 (other)."))
    wordcount = models.PositiveIntegerField(_("Wordcount for this string."),
        blank=False, null=False, default=0, help_text="The number of words "
        " contained in this translation string.")

    # Timestamps
    created = models.DateTimeField(auto_now_add=True, editable=False)
    last_update = models.DateTimeField(auto_now=True, editable=False)

    # Foreign Keys
    # A source string must always belong to a resource
    source_entity = models.ForeignKey(SourceEntity,
        verbose_name=_('Source String'),
        related_name='translations',
        blank=False, null=False,
        help_text=_("The source string this translation string translates."))

    language = models.ForeignKey(Language,
        verbose_name=_('Target Language'),blank=False, null=True,
        help_text=_("The language in which this translation string is written."))

    user = models.ForeignKey(User,
        verbose_name=_('Committer'), blank=False, null=True,
        help_text=_("The user who committed the specific translation."))

    #TODO: Managers
    objects = TranslationManager()

    def __unicode__(self):
        return self.string

    class Meta:
        unique_together = (('source_entity', 'language', 'rule'),)
        verbose_name = _('translation string')
        verbose_name_plural = _('translation strings')
        ordering  = ['string',]
        order_with_respect_to = 'source_entity'
        get_latest_by = 'last_update'

    def save(self, *args, **kwargs):
        """
        Do some exra processing before the actual save to db.
        """
        # encoding happens to support unicode characters
        self.string_hash = md5(self.string.encode('utf-8')).hexdigest()
        self._update_wordcount()
        super(Translation, self).save(*args, **kwargs)

    def _update_wordcount(self):
        """
        Return the number of words for this translation string.
        """
        # use None to split at any whitespace regardless of length
        # so for instance double space counts as one space
        self.wordcount = len(self.string.split(None))



class RLStatsQuerySet(models.query.QuerySet):

    def for_user(self, user):
        """
        Return a queryset matching projects plus private projects that the 
        given user has access to.
        """
        return self.filter(
            resource__in=Resource.objects.for_user(user).values('pk').query).distinct()

    def private(self):
        """
        Return a queryset matching only RLStats associated with private 
        projects.
        """
        resources = Resource.objects.filter(project__private=True)
        return self.filter(resource__in=resources.values('pk').query).distinct()

    def public(self):
        """
        Return a queryset matching only RLStats associated with non-private 
        projects.
        """
        resources = Resource.objects.filter(project__private=False)
        return self.filter(resource__in=resources.values('pk').query).distinct()


    def by_project(self, project):
        """
        Return a queryset matching all RLStats associated with a given
        ``project``.
        """
        return self.filter(resource__project=project).distinct()

    def by_resource(self, resource):
        """
        Return a queryset matching all RLStats associated with a given
        ``resource``.
        """
        return self.filter(resource=resource).order_by('-translated_perc')

    def by_resources(self, resources):
        """
        Return a queryset matching all RLStats associated with the given
        ``resources``.
        """
        return self.filter(resource__in=resources).distinct()

    def by_language(self, language):
        """
        Return a queryset matching RLStats associated with a given ``language``.
        """
        return self.filter(language=language).distinct()

    def by_release(self, release):
        """
        Return a queryset matching RLStats associated with a given ``release``.
        """
        return self.filter(resource__in=release.resources.values('pk').query).distinct()

    def by_release_and_language(self, release, language):
        """
        Return a queryset matching RLStats associated with the given 
        ``release`` and ``language``.

        """
        return self.by_language(language).by_resources(
            release.resources.values('pk').query)

    def by_project_and_language(self, project, language):
        """
        Return a queryset matching RLStats associated with the given 
        ``project`` and ``language``.
        """
        return self.by_language(language).by_resources(
            project.resources.values('pk').query)

    def by_release_aggregated(self, release):
        """
        Aggregate stats for a ``release``.

        RLStats from several resources are grouped by language.
        """
        total = Resource.objects.filter(releases=release).aggregate(
            total=Sum('total_entities'))['total']

        return _aggregate_rlstats(self.by_release(release), 'language', total)

    def by_project_aggregated(self, project):
        """
        Aggregate stats for a ``project``.

        RLStats from a project are grouped by resources.
        """
        total = Resource.objects.filter(project=project).aggregate(
            total=Sum('total_entities'))['total']

        return _aggregate_rlstats(self.by_project(project), 'resource', total)


class RLStats(models.Model):
    """
    Resource-Language statistics object.
    """

    # Fields
    translated = models.PositiveIntegerField(_("Translated Entities"),
        blank=False, null=False, default=0, help_text="The number of "
        "translated entities in a language for a specific resource.")
    translated_wordcount = models.PositiveIntegerField(_("Wordcount for Translated Entities"),
        blank=False, null=False, default=0, help_text="The number of words "
        " contained translated entities in a language for a specific resource.")
    untranslated = models.PositiveIntegerField(_("Untranslated Entities"),
        blank=False, null=False, default=0, help_text="The number of "
        "untranslated entities in a language for a specific resource.")
    last_update = models.DateTimeField(_("Last Update"), auto_now=True,
        default=None, help_text="The datetime that this language was last "
        "updated.")
    last_committer = models.ForeignKey(User, blank=False, null=True,
        default=None, verbose_name=_('Last Committer'), help_text="The user "
        "associated with the last change for this language.")

    # Foreign Keys
    resource = models.ForeignKey(Resource, blank=False, null=False,
        verbose_name="Resource", help_text="The resource the statistics are "
        "associated with.")
    language = models.ForeignKey(Language, blank=False, null=False,
        verbose_name="Language", help_text="The language these statistics "
        "refer to.")

    # Normalized fields
    translated_perc = models.PositiveIntegerField(default=0, editable=False)
    untranslated_perc = models.PositiveIntegerField(default=0, editable=False)

    #objects = generate_chainer_manager(RLStatsManager)
    objects = ChainerManager(RLStatsQuerySet)

    def __unicode__(self):
        return "%s stats for %s" % ( self.resource.slug, self.language.code)

    class Meta:
        unique_together = ('resource', 'language',)
        ordering  = ['translation_perc',]
        order_with_respect_to = 'resource'

    @property
    def total(self):
        return self.resource.total_entities

    @property
    def untranslated_wordcount(self):
        return self.resource.wordcount - self.translated_wordcount

    def save(self, update=True, *args, **kwargs):
        if update:
            self.update(user=None, save=False)
        super(RLStats, self).save(*args, **kwargs)

    def _calculate_perc(self):
        """Update normalized percentage statistics fields."""
        try:
            total = self.resource.total_entities
            self.translated_perc = self.translated * 100 / total
            self.untranslated_perc = 100 - self.translated_perc
        except ZeroDivisionError:
            self.translated_perc = 0
            self.untranslated_perc = 0

    def _calculate_translated_wordcount(self):
        """Calculate wordcount of translated/untranslated entries"""
        wc = 0
        translated = SourceEntity.objects.filter(
            id__in=Translation.objects.filter(language=self.language,
            source_entity__resource=self.resource).distinct().values_list(
            'source_entity', flat=True))
        wordcount = Translation.objects.filter(source_entity__in=translated,
            language=self.resource.source_language).aggregate(Sum('wordcount'))['wordcount__sum']
        self.translated_wordcount = wordcount or 0

    def _calculate_translated(self):
        """
        Calculate translated/untranslated entities.
        """
        total = SourceEntity.objects.values('id').filter(
            resource=self.resource).count()
        translated = Translation.objects.values('id').filter(rule=5,
            language=self.language, source_entity__resource=self.resource
            ).distinct().count()
        untranslated = total - translated
        self.translated = translated
        self.untranslated = untranslated

    def update(self, user=None, save=True):
        """
        Update the RLStat object
        """
        self._calculate_translated()
        self._calculate_translated_wordcount()
        self._calculate_perc()
        if user:
            self._update_now(user)
        if save:
            self.save(update=False)

    def _update_now(self, user=None):
        """
        Update the last update and last committer.
        """
        self.last_update = datetime.datetime.now()
        if user:
            self.last_committer = user

class Template(models.Model):
    """
    Source file template for a specific resource.

    This model holds the source file template in a compressed textfield to save
    space in the database. All translation strings are changed with the md5
    hashes of the SourceEntity string which enables us to do a quick search and
    replace each time we want to recreate the file.
    """

    content = CompressedTextField(null=False, blank=False,
        help_text=_("This is the actual content of the template"))
    resource = models.ForeignKey(Resource,
        verbose_name="Resource",unique=True,
        blank=False, null=False,related_name="source_file_template",
        help_text=_("This is the template of the imported source file which is"
            " used to export translation files from the db to the user."))

    class Meta:
        verbose_name = _('Template')
        verbose_name_plural = _('Templates')
        ordering = ['resource']

from transifex.resources.formats.qt import LinguistHandler # Qt4 TS files
#from resources.formats.java import JavaPropertiesParser # Java .properties
#from resources.formats.apple import AppleStringsParser # Apple .strings
#from resources.formats.ruby import YamlParser # Ruby On Rails (broken)
#from resources.formats.resx import ResXmlParser # Microsoft .NET (not finished)
from transifex.resources.formats.pofile import POHandler # GNU Gettext .PO/.POT parser
from transifex.resources.formats.joomla import JoomlaINIHandler # GNU Gettext .PO/.POT parser

PARSERS = [POHandler , LinguistHandler, JoomlaINIHandler ] #, JavaPropertiesParser, AppleStringsParser]