<commit_before>
import base64
import ConfigParser
import commonware
import datetime
import fnmatch
import json
import os
import polib
import pysvn
import silme.core, silme.format.properties
import urllib2

from django.conf import settings
from django.core.exceptions import ValidationError
from django.core.urlresolvers import reverse
from django.core.validators import URLValidator
from django.forms.models import inlineformset_factory
from django.http import HttpResponse, HttpResponseRedirect, Http404
from django.shortcuts import render
from django.utils.datastructures import MultiValueDictKeyError
from pontoon.base.models import Locale, Project, Subpage, Entity, Translation, ProjectForm, UserProfile
from pontoon.base.views import _request

from mercurial import commands, hg, ui, error
from mobility.decorators import mobile_template


log = commonware.log.getLogger('playdoh')


@mobile_template('{mobile/}admin.html')
def admin(request, template=None):
    """Admin interface."""
    log.debug("Admin interface.")

    if not (request.user.is_authenticated() and request.user.has_perm('base.can_manage')):
        raise Http404

    data = {
        'projects': Project.objects.all(),
    }

    return render(request, template, data)

@mobile_template('{mobile/}project.html')
def manage_project(request, name=None, template=None):
    """Admin interface: manage project."""
    log.debug("Admin interface: manage project.")

    if not (request.user.is_authenticated() and request.user.has_perm('base.can_manage')):
        raise Http404

    SubpageInlineFormSet = inlineformset_factory(Project, Subpage, extra=1)
    form = ProjectForm()
    formset = SubpageInlineFormSet()
    locales_selected = []
    subtitle = 'Add project'
    pk = None
    warning = None

    if request.method == 'POST':
        locales_selected = Locale.objects.filter(pk__in=request.POST.getlist('locales'))
        # Update existing project
        try:
            pk = request.POST['pk']
            project = Project.objects.get(pk=pk)
            form = ProjectForm(request.POST, instance=project)
            formset = SubpageInlineFormSet(request.POST, instance=project) # Needed if form invalid
            subtitle = 'Edit project'

        # Add a new project
        except MultiValueDictKeyError:
            form = ProjectForm(request.POST)
            formset = SubpageInlineFormSet(request.POST) # Needed if form invalid

        if form.is_valid():
            project = form.save(commit=False)
            formset = SubpageInlineFormSet(request.POST, instance=project)
            if formset.is_valid():
                project.save()
                form.save_m2m() # https://docs.djangoproject.com/en/dev/topics/forms/modelforms/#the-save-method
                formset.save()
                formset = SubpageInlineFormSet(instance=project) # Properly displays formset, but removes errors (only usable if valid)
                subtitle += '. Saved.'
                pk = project.pk
                if len(Entity.objects.filter(project=project)) is 0:
                    warning = 'Before localizing the project, you need to import strings from the repository.'
            else:
                subtitle += '. Error.'
        else:
            subtitle += '. Error.'

    # If URL specified and found, show edit, otherwise show add form
    elif name is not None:
        try:
            project = Project.objects.get(name=name)
            pk = project.pk
            form = ProjectForm(instance=project)
            formset = SubpageInlineFormSet(instance=project)
            locales_selected = project.locales.all()
            subtitle = 'Edit project'
            if len(Entity.objects.filter(project=project)) is 0:
                warning = 'Before localizing the project, you need to import strings from the repository.'
        except Project.DoesNotExist:
            form = ProjectForm(initial={'name': name})

    data = {
        'form': form,
        'formset': formset,
        'locales_selected': locales_selected,
        'locales_available': Locale.objects.exclude(pk__in=locales_selected),
        'subtitle': subtitle,
        'pk': pk,
        'warning': warning
    }

    return render(request, template, data)

def delete_project(request, pk, template=None):
    """Admin interface: delete project."""
    log.debug("Admin interface: delete project.")

    if not (request.user.is_authenticated() and request.user.has_perm('base.can_manage')):
        raise Http404

    Project.objects.get(pk=pk).delete()
    return HttpResponseRedirect(reverse('pontoon.admin'))

def _save_entity(project, original, comment):
    """Admin interface: save new or update existing entity in DB."""

    try: # Update entity (comments-only)
        e = Entity.objects.get(project=project, string=original)
    except Entity.DoesNotExist: # New entity
        e = Entity(project=project, string=original)

    if len(comment) > 0:
        e.comment = comment
    e.save()

def _save_translation(project, original, locale, translation, author):
    """Admin interface: save new or update existing translation in DB."""

    e = Entity.objects.get(project=project, string=original)
    translations = Translation.objects.filter(entity=e, locale=locale).order_by('date')

    if len(translations) == 0: # New translation
        t = Translation(entity=e, locale=locale, string=translation,
            author=author, date=datetime.datetime.now())
    else: # Update translation
        t = translations.reverse()[0]
        t.string = translation
        t.author = author
        t.date = datetime.datetime.now()
    t.save()

def update_from_repository(request, template=None):
    """Update all project locales from repository."""
    log.debug("Update all project locales from repository.")

    if not (request.user.is_authenticated() and request.user.has_perm('base.can_manage')) or request.method != 'POST':
        raise Http404

    try:
        pk = request.POST['pk']
        url_repository = request.POST['repository']
    except MultiValueDictKeyError:
        return HttpResponse("error")

    try:
        p = Project.objects.get(pk=pk)
    except Project.DoesNotExist:
        return HttpResponse("error")

    fileName, fileExtension = os.path.splitext(url_repository)

    if fileExtension == '.ini':
        config = ConfigParser.ConfigParser()
        try:
            config.readfp(urllib2.urlopen(url_repository))
        except Exception, e:
            log.debug("INI: " + str(e))
            return HttpResponse("error")
        sections = config.sections()
        sections.insert(0, sections.pop(sections.index('en')))
        for section in sections:
            for item in config.items(section):
                if section == 'en':
                    try: # Update entity
                        e = Entity.objects.get(project=p, key=item[0], source=url_repository)
                        e.string = item[1]
                    except Entity.DoesNotExist: # New entity<commit_msg>Previous commit: Split update_from_repository to two functions (_save_entity and _save_translation)<commit_after>
import base64
import ConfigParser
import commonware
import datetime
import fnmatch
import json
import os
import polib
import pysvn
import silme.core, silme.format.properties
import urllib2

from django.conf import settings
from django.core.exceptions import ValidationError
from django.core.urlresolvers import reverse
from django.core.validators import URLValidator
from django.forms.models import inlineformset_factory
from django.http import HttpResponse, HttpResponseRedirect, Http404
from django.shortcuts import render
from django.utils.datastructures import MultiValueDictKeyError
from pontoon.base.models import Locale, Project, Subpage, Entity, Translation, ProjectForm, UserProfile
from pontoon.base.views import _request

from mercurial import commands, hg, ui, error
from mobility.decorators import mobile_template


log = commonware.log.getLogger('playdoh')


@mobile_template('{mobile/}admin.html')
def admin(request, template=None):
    """Admin interface."""
    log.debug("Admin interface.")

    if not (request.user.is_authenticated() and request.user.has_perm('base.can_manage')):
        raise Http404

    data = {
        'projects': Project.objects.all(),
    }

    return render(request, template, data)

@mobile_template('{mobile/}project.html')
def manage_project(request, name=None, template=None):
    """Admin interface: manage project."""
    log.debug("Admin interface: manage project.")

    if not (request.user.is_authenticated() and request.user.has_perm('base.can_manage')):
        raise Http404

    SubpageInlineFormSet = inlineformset_factory(Project, Subpage, extra=1)
    form = ProjectForm()
    formset = SubpageInlineFormSet()
    locales_selected = []
    subtitle = 'Add project'
    pk = None
    warning = None

    if request.method == 'POST':
        locales_selected = Locale.objects.filter(pk__in=request.POST.getlist('locales'))
        # Update existing project
        try:
            pk = request.POST['pk']
            project = Project.objects.get(pk=pk)
            form = ProjectForm(request.POST, instance=project)
            formset = SubpageInlineFormSet(request.POST, instance=project) # Needed if form invalid
            subtitle = 'Edit project'

        # Add a new project
        except MultiValueDictKeyError:
            form = ProjectForm(request.POST)
            formset = SubpageInlineFormSet(request.POST) # Needed if form invalid

        if form.is_valid():
            project = form.save(commit=False)
            formset = SubpageInlineFormSet(request.POST, instance=project)
            if formset.is_valid():
                project.save()
                form.save_m2m() # https://docs.djangoproject.com/en/dev/topics/forms/modelforms/#the-save-method
                formset.save()
                formset = SubpageInlineFormSet(instance=project) # Properly displays formset, but removes errors (only usable if valid)
                subtitle += '. Saved.'
                pk = project.pk
                if len(Entity.objects.filter(project=project)) is 0:
                    warning = 'Before localizing the project, you need to import strings from the repository.'
            else:
                subtitle += '. Error.'
        else:
            subtitle += '. Error.'

    # If URL specified and found, show edit, otherwise show add form
    elif name is not None:
        try:
            project = Project.objects.get(name=name)
            pk = project.pk
            form = ProjectForm(instance=project)
            formset = SubpageInlineFormSet(instance=project)
            locales_selected = project.locales.all()
            subtitle = 'Edit project'
            if len(Entity.objects.filter(project=project)) is 0:
                warning = 'Before localizing the project, you need to import strings from the repository.'
        except Project.DoesNotExist:
            form = ProjectForm(initial={'name': name})

    data = {
        'form': form,
        'formset': formset,
        'locales_selected': locales_selected,
        'locales_available': Locale.objects.exclude(pk__in=locales_selected),
        'subtitle': subtitle,
        'pk': pk,
        'warning': warning
    }

    return render(request, template, data)

def delete_project(request, pk, template=None):
    """Admin interface: delete project."""
    log.debug("Admin interface: delete project.")

    if not (request.user.is_authenticated() and request.user.has_perm('base.can_manage')):
        raise Http404

    Project.objects.get(pk=pk).delete()
    return HttpResponseRedirect(reverse('pontoon.admin'))

def _save_entity(project, original, comment):
    """Admin interface: save new or update existing entity in DB."""

    try: # Update entity (comments-only)
        e = Entity.objects.get(project=project, string=original)
    except Entity.DoesNotExist: # New entity
        e = Entity(project=project, string=original)

    if len(comment) > 0:
        e.comment = comment
    e.save()

def _save_translation(project, original, locale, translation, author):
    """Admin interface: save new or update existing translation in DB."""

    e = Entity.objects.get(project=project, string=original)
    translations = Translation.objects.filter(entity=e, locale=locale).order_by('date')

    if len(translations) == 0: # New translation
        t = Translation(entity=e, locale=locale, string=translation,
            author=author, date=datetime.datetime.now())
    else: # Update translation
        t = translations.reverse()[0]
        t.string = translation
        t.author = author
        t.date = datetime.datetime.now()
    t.save()

def update_from_repository(request, template=None):
    """Update all project locales from repository."""
    log.debug("Update all project locales from repository.")

    if not (request.user.is_authenticated() and request.user.has_perm('base.can_manage')) or request.method != 'POST':
        raise Http404

    try:
        pk = request.POST['pk']
        url_repository = request.POST['repository']
    except MultiValueDictKeyError:
        return HttpResponse("error")

    try:
        p = Project.objects.get(pk=pk)
    except Project.DoesNotExist:
        return HttpResponse("error")

    fileName, fileExtension = os.path.splitext(url_repository)

    if fileExtension == '.ini': 
        config = ConfigParser.ConfigParser()
        try:
            config.readfp(urllib2.urlopen(url_repository))
        except Exception, e:
            log.debug("INI: " + str(e))
            return HttpResponse("error")
        sections = config.sections()
        sections.insert(0, sections.pop(sections.index('en')))
        for section in sections:
            for item in config.items(section):
                if section == 'en':
                    try: # Update entity
                        e = Entity.objects.get(project=p, key=item[0], source=url_repository)
                        e.string = item[1]
                    except Entity.DoesNotExist: # New entity
{'fraction_non_alphanumeric': 0.10297547529860768, 'fraction_numerical': 0.004099229627535515, 'mean_word_length': 2.8274276440357045, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 2, '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': '1186999', 'n_tokens_mistral': 3930, 'n_tokens_neox': 3776, 'n_words': 994}