<commit_before>from datetime import datetime
from django.core.paginator import Paginator, PageNotAnInteger, EmptyPage
from django.shortcuts import render, redirect
from django.template.loader import render_to_string

from .forms import jsForm, FCMForm, FCMCONCEPTForm, FiltersForm, chartisForm, SortMapsForm, FCMEDGEForm
from .models import FCM
from .models import FCM_CONCEPT
from .models import FCM_CONCEPT_INFO
from .models import FCM_EDGES_IN_FCM_CONCEPT
from .models import FCM_EDGE_INFO
from . models import Tags
from django.contrib import messages
from bs4 import BeautifulSoup
from django.shortcuts import get_object_or_404
from django import forms
import json, pdb, urllib2

from django.http import HttpResponseForbidden
from django.contrib.auth.decorators import login_required
from django.db.models import Q
from django.db import DatabaseError
from django.core.exceptions import ObjectDoesNotExist

# Create your views here.
def index(request):
    context = {'a_var': "no_value"}
    return render(request, 'fcm_app/index.html', context)


def browse(request):
    if request.method == 'GET':
        if 'hasFilters' in request.GET:
            if bool(request.GET['hasFilters']) is True:
                pass
            else:
                request.method = 'GET'
        else:
            request.method = 'POST'
    else:
        request.session['filter-post'] = request.POST

    if request.method == 'POST':
        filter_form = FiltersForm(request.session['filter-post'])
        if filter_form.is_valid():
            filtered_title_and_or_description = filter_form.cleaned_data['filtered_title_and_or_description']
            filtered_year = filter_form.cleaned_data['filtered_year']
            filtered_country = filter_form.cleaned_data['filtered_country']
            filtered_getmine = filter_form.cleaned_data['filtered_getmine']
            filtered_tags = filter_form.cleaned_data['filtered_tags']
            filtered_sorting_type = filter_form.cleaned_data['filtered_sorting_type']
            filtered_sorting_order = filter_form.cleaned_data['filtered_sorting_order']


            if request.user.is_authenticated:
                all_fcms = FCM.objects.filter(Q(status='1') | Q(user=request.user)).order_by('-creation_date')
            else:
                all_fcms = FCM.objects.filter(Q(status='1')).order_by('-creation_date')
            if filtered_year != "-":
                all_fcms = all_fcms.filter(creation_date__year=filtered_year)
            if filtered_country != "-":
                all_fcms = all_fcms.filter(country=filtered_country)
            all_fcms = all_fcms.filter(Q(title__icontains=filtered_title_and_or_description) | Q(
                description__icontains=filtered_title_and_or_description)).distinct()

            if filtered_tags:
                queryset_list = []
                for element in filtered_tags:
                    try:
                        queryset_list.append(Tags.objects.get(pk=str(element)).fcm_set.all())
                    except DatabaseError:
                        pass
                    except ObjectDoesNotExist:
                        pass

                results_union = FCM.objects.none()
                for q in queryset_list:
                    results_union = (results_union | q )
                results_union = results_union.distinct()
                all_fcms = results_union & all_fcms

            if 'filtered_getmine' in request.POST:  #check if the checkbox is checked or not
                filtered_getmine = request.POST['filtered_getmine']
            else:
                filtered_getmine = False
            if filtered_getmine:
                all_fcms = all_fcms.filter(manual='1')

            if filtered_sorting_type == 'creation_date':
                if filtered_sorting_order == 'ASC':
                    all_fcms = all_fcms.order_by('creation_date')
                else:
                    all_fcms = all_fcms.order_by('-creation_date')
            elif filtered_sorting_type == 'title':
                if filtered_sorting_order == 'ASC':
                    all_fcms = all_fcms.order_by('title')
                else:
                    all_fcms = all_fcms.order_by('-title')

            data = {'filtered_title_and_or_description': filtered_title_and_or_description,
                    'filtered_year': filtered_year,
                    'filtered_country': filtered_country,
                    'filtered_getmine': filtered_getmine,
                    'filtered_tags': filtered_tags,
                    'filtered_sorting_type': filtered_sorting_type,
                    'filtered_sorting_order': filtered_sorting_order}
            filter_form = FiltersForm(initial=data)
            paginator = Paginator(all_fcms, 9)
            page = request.GET.get('page')
            try:
                all_fcms = paginator.page(page)
            except PageNotAnInteger:
                # If page is not an integer, deliver first page.
                all_fcms = paginator.page(1)
            except EmptyPage:
                # If page is out of range (e.g. 9999), deliver last page of results.
                all_fcms = paginator.page(paginator.num_pages)
            return render(request, 'fcm_app/browse.html',
                          {"all_fcms": all_fcms, "filter_form": filter_form, "filter_tags":filtered_tags})
        else:
            all_fcms = FCM.objects.filter(Q(status='1') | Q(user=request.user)).order_by('-creation_date')
            return render(request, 'fcm_app/browse.html', {"all_fcms": all_fcms, "filter_form": filter_form})
    else:

        #all_fcms = FCM.objects.all()
        if request.user.is_authenticated:
            all_fcms = FCM.objects.filter(Q(status='1') | Q(user=request.user)).order_by('-creation_date')
        else:
            all_fcms = FCM.objects.filter(Q(status='1')).order_by('-creation_date')
        filter_form = FiltersForm()
        paginator = Paginator(all_fcms, 9)
        page = request.GET.get('page')
        try:
            all_fcms = paginator.page(page)
        except PageNotAnInteger:
            # If page is not an integer, deliver first page.
            all_fcms = paginator.page(1)
        except EmptyPage:
            # If page is out of range (e.g. 9999), deliver last page of results.
            all_fcms = paginator.page(paginator.num_pages)
        return render(request, 'fcm_app/browse.html', {"all_fcms": all_fcms, "filter_form": filter_form})


@login_required
def import_fcm(request):
    storage = messages.get_messages(request)
    storage.used = True
    if request.method == 'POST':
        form = FCMForm(request.POST, request.FILES)
        if form.is_valid():
            print(request.user)
            user = request.user
            soup = BeautifulSoup(form.cleaned_data['map_html'], "html.parser")  # vazo i lxml  i html.parser
            if len(soup.find("table", class_="yimagetable")) > 0:
                print("src in html: " + soup.find("img", class_="yimage")['src'])
                print("image name: " + form.cleaned_data['map_image'].name)
                if urllib2.unquote(soup.find("img", class_="yimage")['src']) == form.cleaned_data['map_image'].name:
                    if user.is_authenticated():
                        fcm = FCM(user=user,
                                  title=form.cleaned_data['title'],
                                  country = form.cleaned_data['country'],
                                  status = form.cleaned_data['status'],
                                  description=form.cleaned_data['description'],
                                  creation_date=datetime.now(),
                                  map_image=form.cleaned_data['map_image'],
                                  map_html=form.cleaned_data['map_html'])
                        fcm.save()
                        tags = form.cleaned_data['tags']
                        for tag_element in tags:
                            try:
                                new_tag = Tags(name=str(tag_element))
                                new_tag.save()
                            except DatabaseError:<commit_msg>fixes<commit_after>from datetime import datetime
from django.core.paginator import Paginator, PageNotAnInteger, EmptyPage
from django.shortcuts import render, redirect
from django.template.loader import render_to_string

from .forms import jsForm, FCMForm, FCMCONCEPTForm, FiltersForm, chartisForm, SortMapsForm, FCMEDGEForm
from .models import FCM
from .models import FCM_CONCEPT
from .models import FCM_CONCEPT_INFO
from .models import FCM_EDGES_IN_FCM_CONCEPT
from .models import FCM_EDGE_INFO
from . models import Tags
from django.contrib import messages
from bs4 import BeautifulSoup
from django.shortcuts import get_object_or_404
from django import forms
import json, pdb
import urllib.parse as urllib

from django.http import HttpResponseForbidden
from django.contrib.auth.decorators import login_required
from django.db.models import Q
from django.db import DatabaseError
from django.core.exceptions import ObjectDoesNotExist

# Create your views here.
def index(request):
    context = {'a_var': "no_value"}
    return render(request, 'fcm_app/index.html', context)


def browse(request):
    if request.method == 'GET':
        if 'hasFilters' in request.GET:
            if bool(request.GET['hasFilters']) is True:
                pass
            else:
                request.method = 'GET'
        else:
            request.method = 'POST'
    else:
        request.session['filter-post'] = request.POST

    if request.method == 'POST':
        filter_form = FiltersForm(request.session['filter-post'])
        if filter_form.is_valid():
            filtered_title_and_or_description = filter_form.cleaned_data['filtered_title_and_or_description']
            filtered_year = filter_form.cleaned_data['filtered_year']
            filtered_country = filter_form.cleaned_data['filtered_country']
            filtered_getmine = filter_form.cleaned_data['filtered_getmine']
            filtered_tags = filter_form.cleaned_data['filtered_tags']
            filtered_sorting_type = filter_form.cleaned_data['filtered_sorting_type']
            filtered_sorting_order = filter_form.cleaned_data['filtered_sorting_order']


            if request.user.is_authenticated:
                all_fcms = FCM.objects.filter(Q(status='1') | Q(user=request.user)).order_by('-creation_date')
            else:
                all_fcms = FCM.objects.filter(Q(status='1')).order_by('-creation_date')
            if filtered_year != "-":
                all_fcms = all_fcms.filter(creation_date__year=filtered_year)
            if filtered_country != "-":
                all_fcms = all_fcms.filter(country=filtered_country)
            all_fcms = all_fcms.filter(Q(title__icontains=filtered_title_and_or_description) | Q(
                description__icontains=filtered_title_and_or_description)).distinct()

            if filtered_tags:
                queryset_list = []
                for element in filtered_tags:
                    try:
                        queryset_list.append(Tags.objects.get(pk=str(element)).fcm_set.all())
                    except DatabaseError:
                        pass
                    except ObjectDoesNotExist:
                        pass

                results_union = FCM.objects.none()
                for q in queryset_list:
                    results_union = (results_union | q )
                results_union = results_union.distinct()
                all_fcms = results_union & all_fcms

            if 'filtered_getmine' in request.POST:  #check if the checkbox is checked or not
                filtered_getmine = request.POST['filtered_getmine']
            else:
                filtered_getmine = False
            if filtered_getmine:
                all_fcms = all_fcms.filter(manual='1')

            if filtered_sorting_type == 'creation_date':
                if filtered_sorting_order == 'ASC':
                    all_fcms = all_fcms.order_by('creation_date')
                else:
                    all_fcms = all_fcms.order_by('-creation_date')
            elif filtered_sorting_type == 'title':
                if filtered_sorting_order == 'ASC':
                    all_fcms = all_fcms.order_by('title')
                else:
                    all_fcms = all_fcms.order_by('-title')

            data = {'filtered_title_and_or_description': filtered_title_and_or_description,
                    'filtered_year': filtered_year,
                    'filtered_country': filtered_country,
                    'filtered_getmine': filtered_getmine,
                    'filtered_tags': filtered_tags,
                    'filtered_sorting_type': filtered_sorting_type,
                    'filtered_sorting_order': filtered_sorting_order}
            filter_form = FiltersForm(initial=data)
            paginator = Paginator(all_fcms, 9)
            page = request.GET.get('page')
            try:
                all_fcms = paginator.page(page)
            except PageNotAnInteger:
                # If page is not an integer, deliver first page.
                all_fcms = paginator.page(1)
            except EmptyPage:
                # If page is out of range (e.g. 9999), deliver last page of results.
                all_fcms = paginator.page(paginator.num_pages)
            return render(request, 'fcm_app/browse.html',
                          {"all_fcms": all_fcms, "filter_form": filter_form, "filter_tags":filtered_tags})
        else:
            all_fcms = FCM.objects.filter(Q(status='1') | Q(user=request.user)).order_by('-creation_date')
            return render(request, 'fcm_app/browse.html', {"all_fcms": all_fcms, "filter_form": filter_form})
    else:

        #all_fcms = FCM.objects.all()
        if request.user.is_authenticated:
            all_fcms = FCM.objects.filter(Q(status='1') | Q(user=request.user)).order_by('-creation_date')
        else:
            all_fcms = FCM.objects.filter(Q(status='1')).order_by('-creation_date')
        filter_form = FiltersForm()
        paginator = Paginator(all_fcms, 9)
        page = request.GET.get('page')
        try:
            all_fcms = paginator.page(page)
        except PageNotAnInteger:
            # If page is not an integer, deliver first page.
            all_fcms = paginator.page(1)
        except EmptyPage:
            # If page is out of range (e.g. 9999), deliver last page of results.
            all_fcms = paginator.page(paginator.num_pages)
        return render(request, 'fcm_app/browse.html', {"all_fcms": all_fcms, "filter_form": filter_form})


@login_required
def import_fcm(request):
    storage = messages.get_messages(request)
    storage.used = True
    if request.method == 'POST':
        form = FCMForm(request.POST, request.FILES)
        if form.is_valid():
            print(request.user)
            user = request.user
            soup = BeautifulSoup(form.cleaned_data['map_html'], "html.parser")  # vazo i lxml  i html.parser
            if len(soup.find("table", class_="yimagetable")) > 0:
                print("src in html: " + soup.find("img", class_="yimage")['src'])
                print("image name: " + form.cleaned_data['map_image'].name)
                if urllib.unquote(soup.find("img", class_="yimage")['src']) == form.cleaned_data['map_image'].name:
                    if user.is_authenticated():
                        fcm = FCM(user=user,
                                  title=form.cleaned_data['title'],
                                  country = form.cleaned_data['country'],
                                  status = form.cleaned_data['status'],
                                  description=form.cleaned_data['description'],
                                  creation_date=datetime.now(),
                                  map_image=form.cleaned_data['map_image'],
                                  map_html=form.cleaned_data['map_html'])
                        fcm.save()
                        tags = form.cleaned_data['tags']
                        for tag_element in tags:
                            try:
                                new_tag = Tags(name=str(tag_element))
                                new_tag.save()
                            except DatabaseError:
{'fraction_non_alphanumeric': 0.09396383186705767, 'fraction_numerical': 0.002932551319648094, 'mean_word_length': 2.0453953488372094, 'pattern_counts': {'":': 18, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 14, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2831744', 'n_tokens_mistral': 4739, 'n_tokens_neox': 4488, 'n_words': 886}