Source

transifex-fixes / transifex / addons / lotte / views.py

Full commit
  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
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
# -*- coding: utf-8 -*-
import re
from polib import escape, unescape
from django.conf import settings
from django.core.urlresolvers import reverse
from django.contrib.auth.decorators import login_required
from django.contrib.auth.models import User
from django.db.models import Count, Q
from django.db.models.loading import get_model
from django.http import (HttpResponseRedirect, HttpResponse, Http404, 
                         HttpResponseForbidden, HttpResponseBadRequest)
from django.shortcuts import render_to_response, get_object_or_404
from django.template import RequestContext
from django.utils import simplejson
from django.utils.translation import ugettext as _
from django.utils.html import escape
from django.views.generic import list_detail
from authority.views import permission_denied

from actionlog.models import action_logging
from transifex.languages.models import Language
from transifex.projects.models import Project
from transifex.projects.permissions import *
from transifex.projects.permissions.project import ProjectPermission
from transifex.resources.models import (Translation, Resource, SourceEntity)
from transifex.resources.handlers import invalidate_stats_cache
from transifex.teams.models import Team
from transifex.txcommon.decorators import one_perm_required_or_403

# Temporary
from transifex.txcommon import notifications as txnotification

Suggestion = get_model('suggestions', 'Suggestion')

from signals import lotte_init, lotte_done

# Restrict access only to : (The checks are done in the view's body)
# 1)those belonging to the specific language team (coordinators or members)
# 2)project maintainers
# 3)global submitters (perms given through access control tab)
# 4)superusers
@login_required
def translate(request, project_slug, lang_code, resource_slug=None,
                     *args, **kwargs):
    """
    Main lotte view.
    """

    # Permissions handling
    # Project should always be available
    project = get_object_or_404(Project, slug=project_slug)
    team = Team.objects.get_or_none(project, lang_code)
    check = ProjectPermission(request.user)
    if not check.submit_translations(team or project):
        return permission_denied(request)

    resources = []
    if resource_slug:
        resource_list = [get_object_or_404(Resource, slug=resource_slug,
            project=project)]
    else:
        resource_list = Resource.objects.filter(project=project)

        # Return a page explaining that the project has multiple source langs and
        # cannot be translated as a whole.
        if resource_list.values('source_language').distinct().count() > 1:
            request.user.message_set.create(
                message=_("This project has more than one source languages and as a "
                          "result you can not translate all resources at the "
                          "same time."))

            return HttpResponseRedirect(reverse('project_detail',
                                        args=[project_slug]),)

    # Filter resources that are not accepting translations
    for resource in resource_list:
        if resource.accept_translations:
            resources.append(resource)

    # If no resource accepting translations, raise a 403
    if not resources:
        return permission_denied(request)

    target_language = Language.objects.by_code_or_alias_or_404(lang_code)

    # If it is an attempt to edit the source language, redirect the user to 
    # resource_detail and show him a message explaining the reason.
    if target_language == resources[0].source_language:
        request.user.message_set.create(
                message=_("The source language cannot be edited, because this would"
                          " result in translation mismatching! If you want to "
                          "update the source strings consider using the command"
                          " line client of transifex."))
        if resource_slug:
            return HttpResponseRedirect(reverse('resource_detail',
                                                args=[project_slug,
                                                      resource_slug]),)
        else:
            return HttpResponseRedirect(reverse('project_detail',
                                                args=[project_slug]),)

    total_strings = SourceEntity.objects.filter(
        resource__in = resources).count()

    translated_strings = Translation.objects.filter(
        source_entity__resource__in = resources,
        language = target_language,
        source_entity__pluralized=False,
        rule = 5).exclude(string="").count()

    # Include counting of pluralized entities
    for pluralized_entity in SourceEntity.objects.filter(resource__in = resources,
                                                         pluralized=True):
        plurals_translated = Translation.objects.filter(
            language=target_language,
            source_entity=pluralized_entity).count()
        if plurals_translated == len(target_language.get_pluralrules()):
            translated_strings += 1

    if len(resources) > 1:
        translation_resource = None
    else:
        translation_resource = resources[0]

    contributors = User.objects.filter(pk__in=Translation.objects.filter(
        source_entity__resource__in = resources,
        language = target_language,
        rule = 5).values_list("user", flat=True))

    lotte_init.send(None, request=request, resources=resources,
        language=target_language)

    return render_to_response("translate.html",
        { 'project' : project,
          'resource' : translation_resource,
          'target_language' : target_language,
          'translated_strings': translated_strings,
          'untranslated_strings': total_strings - translated_strings,
          'contributors': contributors,
          'resources': resources,
          'resource_slug': resource_slug,
          'languages': Language.objects.all()
        },
        context_instance = RequestContext(request))

@login_required
def exit(request, project_slug, lang_code, resource_slug=None, *args, **kwargs):
    """
    Exiting Lotte
    """

    # Permissions handling
    # Project should always be available
    project = get_object_or_404(Project, slug=project_slug)
    team = Team.objects.get_or_none(project, lang_code)
    check = ProjectPermission(request.user)
    if not check.submit_translations(team or project):
        return permission_denied(request)

    language = Language.objects.by_code_or_alias(lang_code)

    resources = []
    if resource_slug:
        resources = Resource.objects.filter(slug=resource_slug, project=project)
        if not resources:
            raise Http404
        url = reverse('resource_detail', args=[project_slug, resource_slug])
    else:
        resources = Resource.objects.filter(project=project)
        url = reverse('project_detail', args=[project_slug])

    if request.POST.get('updated', None) == 'true':
        modified = True
        # ActionLog & Notification
        for resource in resources:
            nt = 'project_resource_translated'
            context = {'project': project,
                       'resource': resource,
                       'language': language}
            object_list = [project, resource, language]
            action_logging(request.user, object_list, nt, context=context)
            if settings.ENABLE_NOTICES:
                txnotification.send_observation_notices_for(project,
                        signal=nt, extra_context=context)
    else:
        modified = False

    lotte_done.send(None, request=request, resources=resources,
        language=language, modified=modified)

    if request.is_ajax():
        json = simplejson.dumps(dict(redirect=url))
        return HttpResponse(json, mimetype='application/json')

    return HttpResponseRedirect(url)


# Restrict access only for private projects 
# Allow even anonymous access on public projects
@one_perm_required_or_403(pr_project_private_perm,
    (Project, 'slug__exact', 'project_slug'), anonymous_access=True)
def view_strings(request, project_slug, lang_code, resource_slug=None,
                 *args, **kwargs):
    """
    View for observing the translations strings on a specific language.
    """

    translation_resource = get_object_or_404(Resource,
        slug = resource_slug,
        project__slug = project_slug
    )
    try:
        target_language = Language.objects.by_code_or_alias(lang_code)
    except Language.DoesNotExist:
        raise Http404

    total_strings = Translation.objects.filter(
                        source_entity__resource = translation_resource,
                        language = translation_resource.source_language,
                        rule = 5).count()

    translated_strings = Translation.objects.filter(
                            source_entity__resource = translation_resource,
                            language = target_language,
                            rule = 5).exclude(string="").count()

    return render_to_response("view_strings.html",
        { 'project' : translation_resource.project,
          'resource' : translation_resource,
          'target_language' : target_language,
          'translated_strings': translated_strings,
          'untranslated_strings': total_strings - translated_strings,
        },
        context_instance = RequestContext(request))


#FIXME: Find a more clever way to do it, to avoid putting placeholders.
SORTING_DICT=( 'id', 'id', 'string')

# Restrict access only for private projects since this is used to fetch stuff!
# Allow even anonymous access on public projects
@one_perm_required_or_403(pr_project_private_perm,
    (Project, 'slug__exact', 'project_slug'), anonymous_access=True)
def stringset_handling(request, project_slug, lang_code, resource_slug=None,
                     *args, **kwargs):
    """
    Function to serve AJAX data to the datatable holding the translating
    stringset.
    """

    resources = []
    if resource_slug:
        try:
            resources = [ Resource.objects.get(slug=resource_slug,
                                    project__slug = project_slug) ]
        except Resource.DoesNotExist:
            raise Http404
    else:
        resources = Resource.objects.filter(project__slug = project_slug)

    # Find a way to determine the source language of multiple resources #FIXME
    source_language = resources[0].source_language
    source_strings = Translation.objects.filter(
        source_entity__resource__in=resources,
        language=source_language,
        rule=5)

    translated_strings = Translation.objects.filter(
        source_entity__resource__in=resources,
        language__code=lang_code)

    # These are only the rule=5 (other) translations
    default_translated_strings = translated_strings.filter(rule=5)

    # status filtering (translated/untranslated)
    if request.POST and request.POST.has_key('filters'):
        for f in request.POST['filters'].split(','):
            if f == "translated":
                source_strings = source_strings.filter(
                    Q(source_entity__id__in=default_translated_strings.filter(
                        string="").values('source_entity'))|
                    ~Q(source_entity__id__in=default_translated_strings.values(
                        'source_entity')))
            elif f == "untranslated":
                source_strings = source_strings.exclude(
                    Q(source_entity__id__in=default_translated_strings.filter(
                        string="").values('source_entity'))|
                    ~Q(source_entity__id__in=default_translated_strings.values(
                        'source_entity')))

    # Object filtering (e.g. users, resources, etc.)
    if request.POST and request.POST.has_key('user_filters'):
        # rsplit is used to remove the trailing ','
        users = request.POST.get('user_filters').rstrip(',').split(',')
        source_strings = source_strings.filter(
            source_entity__id__in=default_translated_strings.filter(
                user__id__in=users).values('source_entity'))
    if request.POST and request.POST.has_key('resource_filters'):
        # rsplit is used to remove the trailing ','
        resources = request.POST.get('resource_filters').rstrip(',').split(',')
        source_strings = source_strings.filter(resource__id__in=resources)

    more_languages = []
    if request.POST and request.POST.has_key('more_languages'):
        # rsplit is used to remove the trailing ','
        more_languages = request.POST.get('more_languages').rstrip(',').split(',')


    # keyword filtering
    sSearch = request.POST.get('sSearch','')
    if not sSearch == '':
        query = Q()
        for term in sSearch.split(' '):
            query &= Q(string__icontains=term)
        source_strings = source_strings.filter(query)

    # grouping
    # TODO
    source_strings.group_by = ['string']

    # sorting
    scols = request.POST.get('iSortingCols', '0')
    for i in range(0,int(scols)):
        if request.POST.has_key('iSortCol_'+str(i)):
            col = int(request.POST.get('iSortCol_'+str(i)))
            if request.POST.has_key('sSortDir_'+str(i)) and \
                request.POST['sSortDir_'+str(i)] == 'asc':
                source_strings=source_strings.order_by(SORTING_DICT[col])
            else:
                source_strings=source_strings.order_by(SORTING_DICT[col]).reverse()

    # for items displayed
    dlength = int(request.POST.get('iDisplayLength','25'))
    dstart = int(request.POST.get('iDisplayStart','0'))
    # for statistics
    total = source_strings.count()

    # NOTE: It's important to keep the translation string matching inside this
    # iteration to prevent extra un-needed queries. In this iteration only the
    # strings displayed are calculated, saving a lot of resources.
    json = simplejson.dumps({
        'sEcho': request.POST.get('sEcho','1'),
        'iTotalRecords': total,
        'iTotalDisplayRecords': total,
        'aaData': [
            [
                # 1. Translation object's "id"
                s.id,
                # 2. SourceEntity object's "string" content
                s.source_entity.string,
                # 3. Get all the necessary source strings, including plurals and 
                # similar langs, all in a dictionary (see also below)
                _get_source_strings(s, source_language, lang_code, more_languages),
                # 4. Get all the Translation strings mapped with plural rules 
                # in a single dictionary (see docstring of function)
                _get_strings(translated_strings, lang_code, s.source_entity),
                # 5. A number which indicates the number of Suggestion objects
                # attached to this row of the table.
                Suggestion.objects.filter(source_entity=s.source_entity, language__code=lang_code).count(),
                # 6. save buttons and hidden context (ready to inject snippet)
                # It includes the following content, wrapped in span tags:
                # * SourceEntity object's "context" value
                # * SourceEntity object's "id" value
                ('<span class="i16 save buttonized_simple" id="save_' + str(counter) + '" style="display:none;border:0" title="' + _("Save the specific change") + '"></span>'
                 '<span class="i16 undo buttonized_simple" id="undo_' + str(counter) + '" style="display:none;border:0" title="' + _("Undo to initial text") + '"></span>'
                 '<span class="context" id="context_' + str(counter) + '" style="display:none;">' + escape(str(s.source_entity.context)) + '</span>'
                 '<span class="source_id" id="sourceid_' + str(counter) + '"style="display:none;">' + str(s.source_entity.id) + '</span>'),
            ] for counter,s in enumerate(source_strings[dstart:dstart+dlength])
        ],
        })
    return HttpResponse(json, mimetype='application/json')


def _get_source_strings(source_string, source_language, lang_code, more_languages):
    """
    Get all the necessary source strings, including plurals and similar langs.
    
    Returns a dictionary with the keys:
    'source_strings' : {"one":<string>, "two":<string>, ... , "other":<string>}
    'similar_lang_strings' : 
        {"lang1": {"one":<string>, ... , "other":<string>},
         "lang2": {"one":<string>, "two":<string>, ... , "other":<string>}}
    """
    source_entity = source_string.source_entity
    # This is the rule 5 ('other')
    source_strings = { "other":source_string.string }
    # List that will contain all the similar translations
    similar_lang_strings = {}
    
    if source_entity.pluralized:
        # These are the remaining plural forms of the source string.
        plural_strings = Translation.objects.filter(
            source_entity = source_entity,
            language = source_language).exclude(rule=5).order_by('rule')
        for pl_string in plural_strings:
            plural_name = source_language.get_rule_name_from_num(pl_string.rule)
            source_strings[plural_name] = pl_string.string

    # for each similar language fetch all the translation strings
    for lang_id in more_languages:
        l = Language.objects.get(pk=lang_id)
        similar_lang_strings[l.name] = {}
        for t in Translation.objects.filter(source_entity=source_entity, language=l).order_by('rule'):
            plural_name = source_language.get_rule_name_from_num(t.rule)
            similar_lang_strings[l.name][plural_name] = t.string
    return { 'source_strings' : source_strings,
             'similar_lang_strings' : similar_lang_strings }


def _get_strings(query, target_lang_code, source_entity):
    """
    Helper function for returning all the Translation strings or an empty dict.
    
    Used in the list concatenation above to preserve code sanity.
    Returns a dictionary in the following form:
    {"zero":<string>, "one":<string>, ... , "other":<string>},
    where the 'zero', 'one', ... are the plural names of the corresponding 
    plural forms.
    """
    # It includes the plural translations, too!
    translation_strings = {}
    target_language = Language.objects.by_code_or_alias(target_lang_code)
    if source_entity.pluralized:
        translations = query.filter(source_entity=source_entity).order_by('rule')
        # Fill with empty strings to have the Untranslated entries!
        for rule in target_language.get_pluralrules():
            translation_strings[rule] = ""
        for translation in translations:
            plural_name = target_language.get_rule_name_from_num(translation.rule)
            translation_strings[plural_name] = translation.string
    else:
        try:
            translation_strings["other"] = query.get(source_entity=source_entity,
                                                     rule=5).string
        except Translation.DoesNotExist:
            translation_strings["other"] = ""
    return translation_strings


# Restrict access only to : (The checks are done in the view's body)
# 1)those belonging to the specific language team (coordinators or members)
# 2)project maintainers
# 3)global submitters (perms given through access control tab)
# 4)superusers
# CAUTION!!! WE RETURN 404 instead of 403 for security reasons
@login_required
def push_translation(request, project_slug, lang_code, *args, **kwargs):
    """
    Client pushes an id and a translation string.

    Id is considered to be of the source translation string and the string is
    in the target_lang.
    
    FIXME: Document in detail the form of the 'strings' POST variable.
    """

    # Permissions handling
    # Project should always be available
    project = get_object_or_404(Project, slug=project_slug)
    team = Team.objects.get_or_none(project, lang_code)
    check = ProjectPermission(request.user)
    if not check.submit_translations(team or project):
        return permission_denied(request)

    if not request.POST:
        return HttpResponseBadRequest()

    data = simplejson.loads(request.raw_post_data)
    strings = data["strings"]

    try:
        target_language = Language.objects.by_code_or_alias(lang_code)
    except Language.DoesNotExist:
        raise Http404

    # This dictionary will hold the results of the save operation and will map 
    # status code for each translation pushed, to indicate the result on each 
    # translation push separately.
    push_response_dict = {}

    # Form the strings dictionary, get as Json object
    # The fields are the following:
    # id-> source_entity id
    # translations-> translation strings (includes all plurals)
    # context-> source_entity context
    # occurrence-> occurrence (not yet well supported)
    # Iterate through all the row data that have been sent.
    for row in strings:
        source_id = int(row['id'])
        try:
            source_string = Translation.objects.get(id=source_id,
                source_entity__resource__project=project)
        except Translation.DoesNotExist:
            # TODO: Log or inform here
            push_response_dict[source_id] = { 'status':500,
                 'message':_("Source string cannot be identified in the DB")}
            # If the source_string cannot be identified in the DB then go to next
            # translation pair.
            continue

        if not source_string.source_entity.resource.accept_translations:
            push_response_dict[source_id] = { 'status':500,
                 'message':_("The resource of this source string is not "
                    "accepting translations.") }

        # If the translated source string is pluralized check that all the 
        # source language supported rules have been filled in, else return error
        # and donot save the translations.
        if source_string.source_entity.pluralized:
            error_flag = False
            for rule in target_language.get_pluralrules():
                if rule in row['translations'] and row['translations'][rule] != "":
                    continue
                else:
                    error_flag = True
            if error_flag:
                error_flag = False
                # Check also if all of them are "". If yes, delete all the plurals!
                for rule in target_language.get_pluralrules():
                    if rule in row['translations'] and row['translations'][rule] == "":
                        continue
                    else:
                        error_flag = True
            if error_flag:
                push_response_dict[source_id] = { 'status':500,
                    'message':(_("Cannot save unless plural translations are either "
                               "completely filled or entirely empty!"))}
                # Skip the save as we hit on an error.
                continue

        for rule, string in row['translations'].items():
            # Check for plural entries if we already have an error for one of
            # the plural forms
            if push_response_dict.has_key(source_id) and\
              push_response_dict[source_id]['status'] == 500:
                continue


            ss = unescape(source_string.string)
            tr = unescape(string)

                    # Check whether the translation sting only contains spaces
            if tr and len(tr.strip()) == 0:
                push_response_dict[source_id] = { 'status':200,
                    'message':_("Translation string only contains whitespaces.")}

            # Test that the number of {[()]} appearing in each string are the
            # same
            try:
                for char in '[{()}]':
                    if tr and ss.count(char) != tr.count(char):
                        push_response_dict[source_id] = { 'status':200,
                        'message':_("Translation string doesn't contain the same"
                        " number of '%s' as the source string." % char )}
                        raise StopIteration
            except StopIteration:
                pass
            # Scan for urls and see if they're in both strings
            urls = re.compile('http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+')
            try:
                for url in urls.findall(ss):
                    if tr and url not in tr:
                        push_response_dict[source_id] = { 'status':200,
                        'message':_("The following url is either missing form the"
                        " translation or has been translated: '%s'." % url)}
                        raise StopIteration
            except StopIteration:
                pass 

            # Scan for urls and see if they're in both strings
            emails = re.compile("([\w\-\.+]+@[\w\w\-]+\.+[\w\-]+)")
            try:
                for email in emails.findall(ss):
                    if tr and email not in tr:
                        push_response_dict[source_id] = { 'status':200,
                        'message':_("The following email is either missing form the"
                        " translation or has been translated: '%s'." % email)}
                        raise StopIteration
            except StopIteration:
                pass 

            # Check whether source string and translation start and end
            # with newlines
            if string and ss.startswith('\n') != tr.startswith('\n'):
                if ss.endswith('\n'):
                    push_response_dict[source_id] = { 'status':200,
                    'message':_("Translation must start with a newline (\\n)")}
                else:
                    push_response_dict[source_id] = { 'status':200,
                    'message':_("Translation should not start with a newline (\\n)")}
                pass
            elif string and ss.endswith('\n') != tr.endswith('\n'):
                if ss.endswith('\n'):
                    push_response_dict[source_id] = { 'status':200,
                    'message':_("Translation must end with a newline (\\n)")}
                else:
                    push_response_dict[source_id] = { 'status':200,
                    'message':_("Translation should not end with a newline (\\n)")}
                pass

            # Check the numbers inside the string
            numbers = re.compile("[-+]?[0-9]*\.?[0-9]+")
            try:
                for num in numbers.findall(source_string.string):
                    if string and num not in string:
                        push_response_dict[source_id] = { 'status':200,
                            'message':_("Number %s is in the source string but not "\
                            "in the translation." % num )}
                        raise StopIteration
            except StopIteration:
                pass

            # Check printf variables
            printf_pattern = re.compile('%((?:(?P<ord>\d+)\$|\((?P<key>\w+)\))'\
                '?(?P<fullvar>[+#-]*(?:\d+)?(?:\.\d+)?(hh\|h\|l\|ll)?(?P<type>[\w%])))')
            ss_matches = list(printf_pattern.finditer(source_string.string))
            tr_matches = list(printf_pattern.finditer(string))

            # Check number of printf variables
            if string and len(printf_pattern.findall(source_string.string)) != \
                len(printf_pattern.findall(string)):
                push_response_dict[source_id] = { 'status':500,
                    'message':_('The number of arguments seems to differ '
                        'between the source string and the translation.')}
                continue

            try:
                for pattern in ss_matches:
                    if string and pattern.group(0) not in string:
                        push_response_dict[source_id] = { 'status':200,
                            'message':_('The expression \'%s\' is not present '\
                            'in the translation.' % pattern.group(0) )}
                        raise StopIteration
            except StopIteration:
                continue

            try:
                for pattern in tr_matches:
                    if string and pattern.group(0) not in source_string.string:
                        push_response_dict[source_id] = { 'status':200,
                            'message':_('The expression \'%s\' is not present '\
                            'in the source string.' % pattern.group(0) )}
            except StopIteration:
                pass


            try:
                # TODO: Implement get based on context and/or on context too!
                translation_string = Translation.objects.get(
                    source_entity = source_string.source_entity,
                    language = target_language,
                    source_entity__resource = source_string.source_entity.resource,
                    rule = target_language.get_rule_num_from_name(rule))

                # FIXME: Maybe we don't want to permit anyone to delete!!!
                # If an empty string has been issued then we delete the translation.
                if string == "":
                    translation_string.delete()
                else:
                    translation_string.string = unescape(string)
                    translation_string.user = request.user
                    translation_string.save()

                invalidate_stats_cache(source_string.source_entity.resource,
                    target_language, user=request.user)
                if not push_response_dict.has_key(source_id):
                    push_response_dict[source_id] = { 'status':200}
            except Translation.DoesNotExist:
                # Only create new if the translation string sent, is not empty!
                if string != "":
                    Translation.objects.create(
                        source_entity = source_string.source_entity,
                        language = target_language,
                        rule = target_language.get_rule_num_from_name(rule),
                        string = unescape(string),
                        user = request.user) # Save the sender as last committer
                    invalidate_stats_cache(source_string.source_entity.resource,
                        target_language, user=request.user)
                    if not push_response_dict.has_key(source_id):
                        push_response_dict[source_id] = { 'status':200}
                else:
                    push_response_dict[source_id] = { 'status':500,
                         'message':_("The translation string is empty")}
            # catch-all. if we don't save we _MUST_ inform the user
            except:
                # TODO: Log or inform here
                push_response_dict[source_id] = { 'status':500,
                    'message':_("Error occurred while trying to save translation.")}

    json_dict = simplejson.dumps(push_response_dict)
    return HttpResponse(json_dict, mimetype='application/json')


# Restrict access only for private projects since this is used to fetch stuff
# Allow even anonymous access on public projects
def translation_details_snippet(request, entity_id, lang_code):
    """Return a template snippet with entity & translation details."""

    source_entity = get_object_or_404(SourceEntity, pk=entity_id)

    check = ProjectPermission(request.user)
    if not check.private(source_entity.resource.project):
        return permission_denied(request)

    language = get_object_or_404(Language, code=lang_code)
    translation = source_entity.get_translation(language.code)
    
    return list_detail.object_detail(request,
        queryset=SourceEntity.objects.all(),
        object_id=entity_id,
        template_name="lotte_details_snippet.html",
        template_object_name='source_entity',
        extra_context={"translation" : translation})

# Restrict access only to :
# 1)project maintainers
# 2)superusers
@one_perm_required_or_403(pr_resource_translations_delete,
                          (Project, "slug__exact", "project_slug"))
def delete_translation(request, project_slug=None, resource_slug=None,
                        lang_code=None):
    """
    Delete a list of translations according to the post request.
    """

    if not request.POST:
        return HttpResponseBadRequest()

    project = get_object_or_404(Project, slug=project_slug)

    resource = get_object_or_404(Resource, slug=resource_slug, project=project)
    language = get_object_or_404(Language, code=lang_code)
    data = simplejson.loads(request.raw_post_data)
    to_delete = data["to_delete"]
    ids = []
    # Ensure that there are no empty '' ids
    for se_id in to_delete:
        if se_id:
            ids.append(se_id)


    try:
        translations = Translation.objects.filter(source_entity__pk__in=ids,
                                   language=language)

        translations.delete()
#        request.user.message_set.create(
#            message=_("Translations deleted successfully!"))
    except:
#        request.user.message_set.create(
#            message=_("Translations did not delete due to some error!"))
        raise Http404

    invalidate_stats_cache(resource, language, user=user)

    return HttpResponse(status=200)