~dylanmccall/harvest/gsoc-client-stuff

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# -*- coding: utf-8 -*-
from django.contrib.auth.decorators import login_required
from django.core.paginator import Paginator, InvalidPage, EmptyPage
from django.utils.translation import ugettext as _
from django.db.models import Count
from django.shortcuts import get_object_or_404
from django.shortcuts import render_to_response as render
from django.http import HttpResponseRedirect

from django.template import RequestContext
from django.views.generic import list_detail

import models
import forms

from filters import HarvestFilters
from wrappers import PackageWrapper, PackageListWrapper

def opportunity_detail(request, opportunity_id):
    return list_detail.object_detail(
        request,
        queryset = models.Opportunity.objects.all(),
        object_id = opportunity_id,
        template_object_name = 'opportunity',
    )

@login_required
def opportunity_edit(request, opportunity_id):
    opportunity = get_object_or_404(models.Opportunity, id=opportunity_id)
    if request.method == "POST":
        form = forms.OpportunityForm(data=request.POST, instance=opportunity)
        if form.is_valid():
            if form.cleaned_data["reviewed"] != opportunity.reviewed:
                models.log_action(request.user, action="marked as reviewed", 
                                  opportunity=opportunity)
            if form.cleaned_data["applied"] != opportunity.applied:
                models.log_action(request.user, action="marked as applied", 
                                  opportunity=opportunity)
            if form.cleaned_data["experience"] != opportunity.experience:
                models.log_action(request.user, 
                                  action="changed experience to: %s" % form.cleaned_data["experience"],
                                  opportunity=opportunity)
            if form.cleaned_data["comment"] != opportunity.comment:
                if len(form.cleaned_data["comment"]) > 178:
                    action = "changed comment to: '%s'" % (form.cleaned_data["comment"][:177]+u"…")
                else:
                    action = "changed comment to: '%s'" % form.cleaned_data["comment"]
                models.log_action(request.user, action=action,
                                  opportunity=opportunity)
            form.save()
            return HttpResponseRedirect(request.POST["next"])
        else:
            request.user.message_set.create(message=_('Opportunity details could not be saved.'))
    else:
        form = forms.OpportunityForm(instance=opportunity)
    return render('opportunities/opportunity_edit.html', 
                    {'form': form, 
                     'opportunity':opportunity,
                     'user':request.user, 
                     'next': request.GET['next'],
                    }, RequestContext(request))


def _create_packages_list(request, filters_pkg, filters_opp):
    sourcepackages_list = models.SourcePackage.objects.distinct()
    sourcepackages_list = filters_pkg.process_queryset(sourcepackages_list)
    
    #opportunities_list is filtered right away to only check opportunities belonging to selected packages
    opportunities_list = models.Opportunity.objects.distinct().filter(sourcepackage__in=sourcepackages_list)
    opportunities_list = filters_opp.process_queryset(opportunities_list)
    
    #TODO: need to filter out opportunities with valid=False again
    #TODO: would it be more efficient to group opportunities by their sourcepackages first, then run filters_opp.process_queryset() for each of those groups?
    
    pkg_list_wrapper = PackageListWrapper(request, sourcepackages_list, opportunities_list)
    
    return pkg_list_wrapper


def opportunities_filter(request):
    filters = HarvestFilters()
    filters.update_from_http(request)
    filters_pkg = filters.find('pkg')
    filters_opp = filters.find('opp')
    
    pkg_list_wrapper = _create_packages_list(request, filters_pkg, filters_opp) 
    
    context = {
        'grouping': 'package',
        'packages_list': pkg_list_wrapper,
        'filters_pkg' : filters_pkg,
        'filters_opp' : filters_opp
    }

    return render(
        'opportunities/opportunities_filter.html',
        context,
        context_instance=RequestContext(request))


#TODO: package_filter_detail(request, sourcepackage, opportunities_list)


def opportunities_results(request):
    filters = HarvestFilters()
    filters.update_from_http(request)
    
    packages_list = _create_packages_list(request, filters.find('pkg'), filters.find('opp')) 
    
    context = {
        'grouping': 'package',
        'packages_list': packages_list
    }

    return render(
        'opportunities/results.html',
        context,
        context_instance=RequestContext(request))