<commit_before>from collections import Counter
import json
import settings
from datetime import datetime

from django.utils import timezone
from django.contrib.auth.decorators import login_required
from django.shortcuts import render, redirect, get_object_or_404
from django.http import HttpResponse
from django.views.decorators.csrf import csrf_exempt
from django.core.exceptions import ObjectDoesNotExist

from xbrowse_server.gene_lists.models import GeneList
from xbrowse_server import server_utils
from xbrowse.reference.utils import get_coding_regions_from_gene_structure
from xbrowse.core import genomeloc
from xbrowse_server.base.forms import EditFamilyForm, EditFamilyCauseForm
from xbrowse_server.base.models import Project, Family, FamilySearchFlag, ProjectGeneList, CausalVariant, ANALYSIS_STATUS_CHOICES
from xbrowse_server.decorators import log_request
from xbrowse_server.base.lookups import get_saved_variants_for_family
from xbrowse_server.api.utils import add_extra_info_to_variants_family
from xbrowse_server import json_displays
from xbrowse_server import sample_management
from xbrowse_server.mall import get_reference, get_datastore, get_coverage_store
from django.conf import settings
from django.core.exceptions import PermissionDenied


@login_required
@log_request('families')
def families(request, project_id):
    project = get_object_or_404(Project, project_id=project_id)
    if not project.can_view(request.user):
        raise PermissionDenied

    families_json = json_displays.family_list(project.get_families())
    
    return render(request, 'family/families.html', {
        'project': project,
        'families_json': json.dumps(families_json),
        'analysis_statuses':  json.dumps(dict(ANALYSIS_STATUS_CHOICES)),
        'is_manager': 'true' if project.can_admin(request.user) else 'false',
    })


@login_required
@log_request('family_home')
def family_home(request, project_id, family_id):

    project = get_object_or_404(Project, project_id=project_id)
    family = get_object_or_404(Family, project=project, family_id=family_id)
    if not project.can_view(request.user):
        raise PermissionDenied

    else:
        #phenotips_supported=False
        #if not (settings.PROJECTS_WITHOUT_PHENOTIPS is None or project_id in settings.PROJECTS_WITHOUT_PHENOTIPS):
        #    phenotips_supported=True
        phenotips_supported=True
        if settings.PROJECTS_WITHOUT_PHENOTIPS is not None and project_id in settings.PROJECTS_WITHOUT_PHENOTIPS:
          phenotips_supported=False

        analysis_status_json = family.get_analysis_status_json()
        analysis_status_choices = dict(ANALYSIS_STATUS_CHOICES)
        analysis_status_desc_and_icon = analysis_status_choices[family.analysis_status]
        return render(request, 'family/family_home.html', {
            'phenotips_supported':phenotips_supported,
            'project': project,
            'family': family,
            'user_can_edit': family.can_edit(request.user),
            'user_is_admin': project.can_admin(request.user),
            'saved_variants': FamilySearchFlag.objects.filter(family=family).order_by('-date_saved'),
            'analysis_status_desc_and_icon': analysis_status_desc_and_icon,
            'analysis_status_json': analysis_status_json,
        })


@login_required
@log_request('family_edit')
@csrf_exempt
def edit_family(request, project_id, family_id):
    error = None

    project = get_object_or_404(Project, project_id=project_id)
    family = get_object_or_404(Family, project=project, family_id=family_id)
    if not project.can_admin(request.user):
        raise PermissionDenied

    if request.method == 'POST':
        form = EditFamilyForm(request.POST, request.FILES)
        if form.is_valid():
            family.short_description = form.cleaned_data['short_description']
            family.about_family_content = form.cleaned_data['about_family_content']
            family.analysis_summary_content = form.cleaned_data['analysis_summary_content']

            if family.analysis_status != form.cleaned_data['analysis_status']:
                print("Analysis status changed to: %s" % form.cleaned_data['analysis_status'])
                if family.analysis_status not in ('Q', 'I'):
                    settings.EVENTS_COLLECTION.insert({
                            'event_type': 'family_analysis_status_changed', 'project_id': project_id, 'family_id': family_id, 'date': datetime.now(), 
                            'username': request.user.username, 'email': request.user.email,
                            'from': family.analysis_status, 'to': form.cleaned_data['analysis_status'] })

                family.analysis_status = form.cleaned_data['analysis_status']
                family.analysis_status_date_saved = timezone.now()
                family.analysis_status_saved_by = request.user

            if 'pedigree_image' in request.FILES:
                family.pedigree_image = request.FILES['pedigree_image']

            family.save()

            return redirect('family_home', project_id=project.project_id, family_id=family.family_id)<commit_msg>Fixed timestamp in event log<commit_after>from collections import Counter
import json
import settings
from datetime import datetime

from django.utils import timezone
from django.contrib.auth.decorators import login_required
from django.shortcuts import render, redirect, get_object_or_404
from django.http import HttpResponse
from django.views.decorators.csrf import csrf_exempt
from django.core.exceptions import ObjectDoesNotExist

from xbrowse_server.gene_lists.models import GeneList
from xbrowse_server import server_utils
from xbrowse.reference.utils import get_coding_regions_from_gene_structure
from xbrowse.core import genomeloc
from xbrowse_server.base.forms import EditFamilyForm, EditFamilyCauseForm
from xbrowse_server.base.models import Project, Family, FamilySearchFlag, ProjectGeneList, CausalVariant, ANALYSIS_STATUS_CHOICES
from xbrowse_server.decorators import log_request
from xbrowse_server.base.lookups import get_saved_variants_for_family
from xbrowse_server.api.utils import add_extra_info_to_variants_family
from xbrowse_server import json_displays
from xbrowse_server import sample_management
from xbrowse_server.mall import get_reference, get_datastore, get_coverage_store
from django.conf import settings
from django.core.exceptions import PermissionDenied


@login_required
@log_request('families')
def families(request, project_id):
    project = get_object_or_404(Project, project_id=project_id)
    if not project.can_view(request.user):
        raise PermissionDenied

    families_json = json_displays.family_list(project.get_families())
    
    return render(request, 'family/families.html', {
        'project': project,
        'families_json': json.dumps(families_json),
        'analysis_statuses':  json.dumps(dict(ANALYSIS_STATUS_CHOICES)),
        'is_manager': 'true' if project.can_admin(request.user) else 'false',
    })


@login_required
@log_request('family_home')
def family_home(request, project_id, family_id):

    project = get_object_or_404(Project, project_id=project_id)
    family = get_object_or_404(Family, project=project, family_id=family_id)
    if not project.can_view(request.user):
        raise PermissionDenied

    else:
        #phenotips_supported=False
        #if not (settings.PROJECTS_WITHOUT_PHENOTIPS is None or project_id in settings.PROJECTS_WITHOUT_PHENOTIPS):
        #    phenotips_supported=True
        phenotips_supported=True
        if settings.PROJECTS_WITHOUT_PHENOTIPS is not None and project_id in settings.PROJECTS_WITHOUT_PHENOTIPS:
          phenotips_supported=False

        analysis_status_json = family.get_analysis_status_json()
        analysis_status_choices = dict(ANALYSIS_STATUS_CHOICES)
        analysis_status_desc_and_icon = analysis_status_choices[family.analysis_status]
        return render(request, 'family/family_home.html', {
            'phenotips_supported':phenotips_supported,
            'project': project,
            'family': family,
            'user_can_edit': family.can_edit(request.user),
            'user_is_admin': project.can_admin(request.user),
            'saved_variants': FamilySearchFlag.objects.filter(family=family).order_by('-date_saved'),
            'analysis_status_desc_and_icon': analysis_status_desc_and_icon,
            'analysis_status_json': analysis_status_json,
        })


@login_required
@log_request('family_edit')
@csrf_exempt
def edit_family(request, project_id, family_id):
    error = None

    project = get_object_or_404(Project, project_id=project_id)
    family = get_object_or_404(Family, project=project, family_id=family_id)
    if not project.can_admin(request.user):
        raise PermissionDenied

    if request.method == 'POST':
        form = EditFamilyForm(request.POST, request.FILES)
        if form.is_valid():
            family.short_description = form.cleaned_data['short_description']
            family.about_family_content = form.cleaned_data['about_family_content']
            family.analysis_summary_content = form.cleaned_data['analysis_summary_content']

            if family.analysis_status != form.cleaned_data['analysis_status']:
                print("Analysis status changed to: %s" % form.cleaned_data['analysis_status'])
                if family.analysis_status not in ('Q', 'I'):
                    settings.EVENTS_COLLECTION.insert({
                            'event_type': 'family_analysis_status_changed', 'project_id': project_id, 'family_id': family_id, 'date': timezone.now(), 
                            'username': request.user.username, 'email': request.user.email,
                            'from': family.analysis_status, 'to': form.cleaned_data['analysis_status'] })

                family.analysis_status = form.cleaned_data['analysis_status']
                family.analysis_status_date_saved = timezone.now()
                family.analysis_status_saved_by = request.user

            if 'pedigree_image' in request.FILES:
                family.pedigree_image = request.FILES['pedigree_image']

            family.save()

            return redirect('family_home', project_id=project.project_id, family_id=family.family_id)
{'fraction_non_alphanumeric': 0.08076286854140313, 'fraction_numerical': 0.0035029677921572444, 'mean_word_length': 3.986899563318777, '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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2261077', 'n_tokens_mistral': 3114, 'n_tokens_neox': 2940, 'n_words': 565}