<commit_before># vim: ai ts=4 sts=4 et sw=4

from django.http import HttpResponse, HttpResponseRedirect, HttpResponseServerError
from django.shortcuts import render_to_response, get_object_or_404
from mysite.profile.models import Person, ProjectExp, Tag, TagType, Link_ProjectExp_Tag, Link_Project_Tag, Link_SF_Proj_Dude_FM
from mysite.search.models import Project
import StringIO
import datetime
import urllib
import simplejson
import re

# Add a contribution {{{

def add_project_exp_web(request):
    # {{{
    username = request.POST.get('u', '')
    project_name = request.POST.get('project_name', '')
    description = request.POST.get('description', '')
    url = request.POST.get('url', '')
    format = request.POST.get('format', 'html')

    notif = ''
    if username and project_name and description and url:
        ProjectExp.create_from_text(username, project_name, description, url)
        notif = "Added experience with %s to %s's profile." % (
                project_name, username)
    else:
        notif = "Er, like, bad input: {project_name: %s, username: %s}" % (
                project_name, username)

    if format == 'json':
        return HttpResponse(simplejson.dumps([{'notification': notif}]))

    data = profile_data_from_username(username)
    data['notification'] = notif

    return HttpResponseRedirect('/people/?' +
            urllib.urlencode({'u': username}))
    #}}}
add_contribution_web = add_project_exp_web

def add_contribution(username, project_name, url='', description=''):
    # {{{
    pass
    # }}}

# }}}

def xp_slurper_display_input_form(request, notification=None):
    return render_to_response('profile/xp_slurper.html', {
        'notification': notification})

def exp_scraper_scrape_web(request):
    # {{{
    # Check input
    input_username = request.GET.get('u', None)
    if input_username is None:
        return exp_scraper_display_input_form(request, "Please enter a username.")
    
    # FIXME: get or get_or_create?
    exp_scraper_scrape(Person.objects.get(username=input_username))

    person = Person.objects.get(username=input_username)

    return display_person(person, "main")
    # }}}

def exp_scraper_scrape(person):
    # {{{
    #person.fetch_projects_from_sourceforge()
    #Execute script on server: person.fetch_contrib_data_from_ohloh_for_user_and_projects()
    person.fetch_contrib_data_from_ohloh()
    person.save()
    # }}}

# Display profile {{{
def profile_data_from_username(username, fetch_ohloh_data = False):
    # {{{
    person, person_created = Person.objects.get_or_create(<commit_msg>The XP Slurper will now create a Person object for the entered username if none already exists.<commit_after># vim: ai ts=4 sts=4 et sw=4

from django.http import HttpResponse, HttpResponseRedirect, HttpResponseServerError
from django.shortcuts import render_to_response, get_object_or_404
from mysite.profile.models import Person, ProjectExp, Tag, TagType, Link_ProjectExp_Tag, Link_Project_Tag, Link_SF_Proj_Dude_FM
from mysite.search.models import Project
import StringIO
import datetime
import urllib
import simplejson
import re

# Add a contribution {{{

def add_project_exp_web(request):
    # {{{
    username = request.POST.get('u', '')
    project_name = request.POST.get('project_name', '')
    description = request.POST.get('description', '')
    url = request.POST.get('url', '')
    format = request.POST.get('format', 'html')

    notif = ''
    if username and project_name and description and url:
        ProjectExp.create_from_text(username, project_name, description, url)
        notif = "Added experience with %s to %s's profile." % (
                project_name, username)
    else:
        notif = "Er, like, bad input: {project_name: %s, username: %s}" % (
                project_name, username)

    if format == 'json':
        return HttpResponse(simplejson.dumps([{'notification': notif}]))

    data = profile_data_from_username(username)
    data['notification'] = notif

    return HttpResponseRedirect('/people/?' +
            urllib.urlencode({'u': username}))
    #}}}
add_contribution_web = add_project_exp_web

def add_contribution(username, project_name, url='', description=''):
    # {{{
    pass
    # }}}

# }}}

def xp_slurper_display_input_form(request, notification=None):
    return render_to_response('profile/xp_slurper.html', {
        'notification': notification})

def exp_scraper_scrape_web(request):
    # {{{
    # Check input
    input_username = request.GET.get('u', None)
    if input_username is None:
        return exp_scraper_display_input_form(request, "Please enter a username.")
    
    Person.objects.get_or_create(username=input_username)
    exp_scraper_scrape()

    person = Person.objects.get(username=input_username)

    return display_person(person, "main")
    # }}}

def exp_scraper_scrape(person):
    # {{{
    #person.fetch_projects_from_sourceforge()
    #Execute script on server: person.fetch_contrib_data_from_ohloh_for_user_and_projects()
    person.fetch_contrib_data_from_ohloh()
    person.save()
    # }}}

# Display profile {{{
def profile_data_from_username(username, fetch_ohloh_data = False):
    # {{{
    person, person_created = Person.objects.get_or_create(
{'fraction_non_alphanumeric': 0.11462300076161462, 'fraction_numerical': 0.002284843869002285, 'mean_word_length': 4.1, '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': '6924305', 'n_tokens_mistral': 1661, 'n_tokens_neox': 1617, 'n_words': 348}