<commit_before>from datetime import datetime
from django.template.loader import render_to_string
from django.http import HttpResponse
from django.template.context import RequestContext
from django.shortcuts import render, render_to_response
from django.contrib.auth.models import User
from django.utils import simplejson
from django.utils.datastructures import MultiValueDictKeyError
from main.decorators import login_required
from main.backend.forms import ProfileForm, SiteConfigForm, ModuleForm, StatusSiteDomainForm
from main.models import SiteConfig, Module, UserProfile, StatusSiteDomain, STATUS

__author__ = 'apredoi'

@login_required
def backend(request):
    context = RequestContext(request)
    u = request.user
    u_profile = UserProfile.objects.get_or_create(user = request.user)[0]

    initial_data = u.__dict__
    initial_data.update(u_profile.__dict__)

    profile_form = ProfileForm(initial=initial_data)


    site_config_form_template = SiteConfigForm()
    site_configs = SiteConfig.objects.filter(user=u)
    site_config_forms = []

    for site_config in site_configs:
        site_config_forms.append(SiteConfigForm(instance=site_config))

    module_form_template = ModuleForm(u)
    modules = Module.objects.filter(site_config__user=u)
    module_forms = []

    for module in modules:
        module_forms.append(ModuleForm(u,instance=module))


    site_domain_form_template = StatusSiteDomainForm(u)
    site_domains = StatusSiteDomain.objects.filter(site_config__user=u)
    site_domain_forms = []

    for site_domain in site_domains:
        site_domain_forms.append(StatusSiteDomainForm(u,instance=site_domain))

    return render(request, 'backend/home.html', {'profile_form':profile_form, 'site_config_forms': site_config_forms,
                                                 'site_config_form_template': site_config_form_template,
                                                 'module_forms' : module_forms,
                                                 'module_form_template' : module_form_template,
                                                 'site_domain_forms' : site_domain_forms,
                                                 'site_domain_form_template' : site_domain_form_template})


""" API """
@login_required
def update_profile(request):
    form = ProfileForm(request.POST)
    response_obj = {'status': 'ok', 'target': 'profile'}

    if form.is_valid():
        u = request.user
        u_profile = UserProfile.objects.get_or_create(user = request.user)[0]
        if form.data.get('first_name'):
            u.first_name = form.data.get('first_name')
        if form.data.get('last_name'):
            u.last_name = form.data.get('last_name')
        if form.data.get('birth_date'):
            u_profile.birth_date = form.data.get('birth_date')
        if form.data.get('address'):
            u_profile.address = form.data.get('address')
        if form.data.get('city'):
            u_profile.city = form.data.get('city')
        if form.data.get('country'):
            u_profile.country = form.data.get('country')
        if form.data.get('state'):
            u_profile.state = form.data.get('state')
        if form.data.get('phone_number'):
            u_profile.phone_number = form.data.get('phone_number')
        if form.data.get('password1'):
            u.set_password(form.data.get('password1'))
        u_profile.save()
        u.save()
    else:
        response_obj['error'] = form.errors
        response_obj['status'] = 'error'
        return HttpResponse(simplejson.dumps(response_obj), mimetype='application/json' )
    return HttpResponse(simplejson.dumps(response_obj), mimetype='application/json' )

@login_required
def add_site_config(request):
    response_obj = {'status': 'ok', 'target': 'site_config'}
    if request.method == 'POST':
        object_key=None
        try:
            object_key = request.POST['site_config_id']
        except MultiValueDictKeyError:
            response_obj['action'] = 'add'
        instance = None
        action = request.POST['site_config_action']
        if action in ('update', 'delete') and object_key is not None:
            instance = SiteConfig.objects.get(user=request.user, pk=object_key)
            response_obj['id'] = object_key
            response_obj['action'] = 'update'

        if instance and action == 'delete':
            instance.delete()
            response_obj['action'] = 'delete'
            return HttpResponse(simplejson.dumps(response_obj), mimetype='application/json')

        form = SiteConfigForm(request.POST,instance=instance) if instance else SiteConfigForm(request.POST)
        if form.is_valid():
            site_config = form.save(commit=False)
            site_config.user = request.user
            site_config.save()
            response_obj['id'] = site_config.pk
        else:
            response_obj['error'] = form.errors
            response_obj['status'] = 'error'
            response_obj['item'] = render_to_string("backend/site_config_form.html", dict(site_config_form=form))
            return HttpResponse(simplejson.dumps(response_obj), mimetype='application/json'
            )
        response_obj['item'] = render_to_string("backend/site_config_form.html",
                                                            dict(site_config_form=SiteConfigForm(instance=site_config)))
        response_obj['name'] = site_config.site_name
        return HttpResponse(simplejson.dumps(response_obj), mimetype='application/json')

    return HttpResponse(response_obj, mimetype='application/json')


@login_required
def add_module(request):
    response_obj = {'status': 'ok', 'target': 'module'}
    if request.method == 'POST':
        object_key=None

        try:
            object_key = request.POST['module_id']
        except MultiValueDictKeyError:
            response_obj['action'] = 'add'
        instance = None
        action = request.POST['module_action']

        if action in ('update', 'delete') and object_key is not None:
            instance = Module.objects.get(pk=object_key)
            response_obj['id'] = object_key
            response_obj['action'] = 'update'

        if instance and action == 'delete':
            instance.delete()
            response_obj['action'] = 'delete'
            return HttpResponse(simplejson.dumps(response_obj), mimetype='application/json')

        form = ModuleForm(request.user,request.POST,instance=instance) if instance else ModuleForm(request.user,request.POST)
        if form.is_valid():
            module = form.save(commit=False)
            #add
            if not instance:
                module.monitoring_since = datetime.now()
                module.status = "unknown"
                module.total_downtime = "0.0"

            module.save()
            instance = module
            response_obj['id'] = module.pk<commit_msg>* added missing updated_at when modifying a module<commit_after>from datetime import datetime
from django.template.loader import render_to_string
from django.http import HttpResponse
from django.template.context import RequestContext
from django.shortcuts import render, render_to_response
from django.contrib.auth.models import User
from django.utils import simplejson
from django.utils.datastructures import MultiValueDictKeyError
from main.decorators import login_required
from main.backend.forms import ProfileForm, SiteConfigForm, ModuleForm, StatusSiteDomainForm
from main.models import SiteConfig, Module, UserProfile, StatusSiteDomain, STATUS

__author__ = 'apredoi'

@login_required
def backend(request):
    context = RequestContext(request)
    u = request.user
    u_profile = UserProfile.objects.get_or_create(user = request.user)[0]

    initial_data = u.__dict__
    initial_data.update(u_profile.__dict__)

    profile_form = ProfileForm(initial=initial_data)


    site_config_form_template = SiteConfigForm()
    site_configs = SiteConfig.objects.filter(user=u)
    site_config_forms = []

    for site_config in site_configs:
        site_config_forms.append(SiteConfigForm(instance=site_config))

    module_form_template = ModuleForm(u)
    modules = Module.objects.filter(site_config__user=u)
    module_forms = []

    for module in modules:
        module_forms.append(ModuleForm(u,instance=module))


    site_domain_form_template = StatusSiteDomainForm(u)
    site_domains = StatusSiteDomain.objects.filter(site_config__user=u)
    site_domain_forms = []

    for site_domain in site_domains:
        site_domain_forms.append(StatusSiteDomainForm(u,instance=site_domain))

    return render(request, 'backend/home.html', {'profile_form':profile_form, 'site_config_forms': site_config_forms,
                                                 'site_config_form_template': site_config_form_template,
                                                 'module_forms' : module_forms,
                                                 'module_form_template' : module_form_template,
                                                 'site_domain_forms' : site_domain_forms,
                                                 'site_domain_form_template' : site_domain_form_template})


""" API """
@login_required
def update_profile(request):
    form = ProfileForm(request.POST)
    response_obj = {'status': 'ok', 'target': 'profile'}

    if form.is_valid():
        u = request.user
        u_profile = UserProfile.objects.get_or_create(user = request.user)[0]
        if form.data.get('first_name'):
            u.first_name = form.data.get('first_name')
        if form.data.get('last_name'):
            u.last_name = form.data.get('last_name')
        if form.data.get('birth_date'):
            u_profile.birth_date = form.data.get('birth_date')
        if form.data.get('address'):
            u_profile.address = form.data.get('address')
        if form.data.get('city'):
            u_profile.city = form.data.get('city')
        if form.data.get('country'):
            u_profile.country = form.data.get('country')
        if form.data.get('state'):
            u_profile.state = form.data.get('state')
        if form.data.get('phone_number'):
            u_profile.phone_number = form.data.get('phone_number')
        if form.data.get('password1'):
            u.set_password(form.data.get('password1'))
        u_profile.save()
        u.save()
    else:
        response_obj['error'] = form.errors
        response_obj['status'] = 'error'
        return HttpResponse(simplejson.dumps(response_obj), mimetype='application/json' )
    return HttpResponse(simplejson.dumps(response_obj), mimetype='application/json' )

@login_required
def add_site_config(request):
    response_obj = {'status': 'ok', 'target': 'site_config'}
    if request.method == 'POST':
        object_key=None
        try:
            object_key = request.POST['site_config_id']
        except MultiValueDictKeyError:
            response_obj['action'] = 'add'
        instance = None
        action = request.POST['site_config_action']
        if action in ('update', 'delete') and object_key is not None:
            instance = SiteConfig.objects.get(user=request.user, pk=object_key)
            response_obj['id'] = object_key
            response_obj['action'] = 'update'

        if instance and action == 'delete':
            instance.delete()
            response_obj['action'] = 'delete'
            return HttpResponse(simplejson.dumps(response_obj), mimetype='application/json')

        form = SiteConfigForm(request.POST,instance=instance) if instance else SiteConfigForm(request.POST)
        if form.is_valid():
            site_config = form.save(commit=False)
            site_config.user = request.user
            site_config.save()
            response_obj['id'] = site_config.pk
        else:
            response_obj['error'] = form.errors
            response_obj['status'] = 'error'
            response_obj['item'] = render_to_string("backend/site_config_form.html", dict(site_config_form=form))
            return HttpResponse(simplejson.dumps(response_obj), mimetype='application/json'
            )
        response_obj['item'] = render_to_string("backend/site_config_form.html",
                                                            dict(site_config_form=SiteConfigForm(instance=site_config)))
        response_obj['name'] = site_config.site_name
        return HttpResponse(simplejson.dumps(response_obj), mimetype='application/json')

    return HttpResponse(response_obj, mimetype='application/json')


@login_required
def add_module(request):
    response_obj = {'status': 'ok', 'target': 'module'}
    if request.method == 'POST':
        object_key=None

        try:
            object_key = request.POST['module_id']
        except MultiValueDictKeyError:
            response_obj['action'] = 'add'
        instance = None
        action = request.POST['module_action']

        if action in ('update', 'delete') and object_key is not None:
            instance = Module.objects.get(pk=object_key)
            response_obj['id'] = object_key
            response_obj['action'] = 'update'

        if instance and action == 'delete':
            instance.delete()
            response_obj['action'] = 'delete'
            return HttpResponse(simplejson.dumps(response_obj), mimetype='application/json')

        form = ModuleForm(request.user,request.POST,instance=instance) if instance else ModuleForm(request.user,request.POST)
        if form.is_valid():
            module = form.save(commit=False)
            module.updated_at = datetime.now()
            #add
            if not instance:
                module.monitoring_since = datetime.now()
                module.status = "unknown"
                module.total_downtime = "0.0"

            module.save()
            instance = module
            response_obj['id'] = module.pk
{'fraction_non_alphanumeric': 0.1056348176970745, 'fraction_numerical': 0.0008646779074794639, 'mean_word_length': 2.6882806271591817, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 24, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4586952', 'n_tokens_mistral': 3827, 'n_tokens_neox': 3743, 'n_words': 696}