<commit_before>from search.forms import SearchForm
from django.views.generic.simple import direct_to_template
from profiles.models import Profile, Skill
from profiles.controllers import tag_clean
from itertools import chain

def strip_the_fucking_tags():
    for bad_tag in filter(lambda x: x[1][0] == ' ', Skill.objects.all().values_list()):
        bad_skill = Skill.objects.get(id = bad_tag[0])
        skills_check = Skill.objects.filter(name=tag_clean(bad_skill.name))
        if skills_check.count > 0:
            for poor_soul in Profile.objects.filter(skills__id=bad_skill.id):
                poor_soul.remove(bad_skill)
                poor_soul.add(skills_check.exclude(id=bad_skill.id)[0])
            bad_skill.delete()
        else:
            bad_skill.name = tag_clean(bad_skill.name)
            bad_skill.save()

def search(request):
    if request.method == "POST":
        searchform = SearchForm(request.POST)
        if searchform.is_valid():
            query = searchform.data.get('query')
            query_list = query.lower().split(',')
            strip_the_fucking_tags()
            if len(query_list) > 1:
                try:
                    qs = [Skill.objects.get(name__contains=qry).profile_set.all() for qry in query_list]
                except:
                    qs = []
                if qs != []:
                    results = list(set(qs[0]).intersection(*qs))
                else:
                    results = None
            else:
                try:
                    results = list(chain(Skill.objects.get(name__contains=query_list[0]).profile_set.all()))
                except Exception as e:
                    if e.__class__ == Skill.MultipleObjectsReturned:
                        results = list(chain(*[skill.profile_set.all() for skill in Skill.objects.filter(name__contains=query_list[0])]))
                    else:
                        results = None
            searchform = SearchForm({'query':query})
    else:
        searchform = SearchForm()
    return direct_to_template(request, 'search/grep.html', locals())<commit_msg>bug fixing, clean up<commit_after>from search.forms import SearchForm
from django.views.generic.simple import direct_to_template
from profiles.models import Profile, Skill
from profiles.controllers import tag_clean
from itertools import chain

def strip_the_fucking_tags():
    bad_skills_list = filter(lambda x: x[1][0] == ' ', Skill.objects.all().values_list())
    for bad_tag in bad_skills_list:
        bad_skill = Skill.objects.get(id = bad_tag[0])
        skills_check = Skill.objects.filter(name=tag_clean(bad_skill.name))
        if skills_check.count > 0:
            for poor_soul in Profile.objects.filter(skills__id=bad_skill.id):
                poor_soul.remove(bad_skill)
                poor_soul.add(skills_check.exclude(id=bad_skill.id)[0])
            bad_skill.delete()
        else:
            bad_skill.name = tag_clean(bad_skill.name)
            bad_skill.save()
    """
    for skill in Skill.objects.all():
        dupes = Skill.objects.filter(name=skill).exclude(id = skill.id)
        if dupes.count > 0:
            for dupe in dupes:
                poor_souls = dupe.profile_set.all()
                for ps in poor_souls:
                    ps.skills.remove(dupe)
                    ps.skills.add(skill)
    """

def search(request):
    if request.method == "POST":
        searchform = SearchForm(request.POST)
        if searchform.is_valid():
            query = searchform.data.get('query')
            query_list = query.lower().split(',')
            strip_the_fucking_tags()
            if len(query_list) > 1:
                try:
                    qs = [list(chain(*[skill.profile_set.all() for skill in Skill.objects.filter(name__contains=tag_clean(qry))])) for qry in query_list]
                    results = list(set(qs[0]).intersection(*qs))
                    #list(set([list(chain(*[skill.profile_set.all() for skill in Skill.objects.filter(name__contains=tag_clean(qry))])) for qry in query_list][0]).intersection(*qs)) # one liner for lulz
                except Exception as e:
                    results = None
            else:
                try:
                    results = list(chain(Skill.objects.get(name__contains=query_list[0]).profile_set.all()))
                except Exception as e:
                    if e.__class__ == Skill.MultipleObjectsReturned:
                        results = list(chain(*[skill.profile_set.all() for skill in Skill.objects.filter(name__contains=query_list[0])]))
                    else:
                        results = None
            searchform = SearchForm({'query':query})
    else:
        searchform = SearchForm()
    return direct_to_template(request, 'search/grep.html', locals())
{'fraction_non_alphanumeric': 0.11, 'fraction_numerical': 0.004166666666666667, 'mean_word_length': 2.1134889753566797, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 8, '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': '5407166', 'n_tokens_mistral': 1417, 'n_tokens_neox': 1392, 'n_words': 264}