Merge lp:~dylanmccall/harvest/harvest-dylan-m into lp:harvest
- harvest-dylan-m
- Merge into trunk
Status: | Merged |
---|---|
Merged at revision: | 186 |
Proposed branch: | lp:~dylanmccall/harvest/harvest-dylan-m |
Merge into: | lp:harvest |
Diff against target: |
806 lines (+706/-1) 11 files modified
INSTALL (+1/-1) harvest/common/url_tools.py (+32/-0) harvest/filters/__init__.py (+23/-0) harvest/filters/containers.py (+97/-0) harvest/filters/filters.py (+318/-0) harvest/opportunities/filters.py (+53/-0) harvest/opportunities/urls.py (+4/-0) harvest/opportunities/views.py (+34/-0) harvest/opportunities/wrappers.py (+90/-0) harvest/settings.py (+4/-0) harvest/templates/opportunities/opportunities_filter.html (+50/-0) |
To merge this branch: | bzr merge lp:~dylanmccall/harvest/harvest-dylan-m |
Related bugs: |
Reviewer | Review Type | Date Requested | Status |
---|---|---|---|
Daniel Holbach | Approve | ||
James Westby | Approve | ||
Review via email:
|
Commit message
Description of the change
Introducing a new view at /opportunities/
This view presents all packages and all opportunities tracked by Harvest, where the user can filter them using a list of properties. First the list of source packages is filtered, then the list of opportunities related to those packages is filtered.
User interface is just a proof of concept so far, with very few avenues for interaction (beyond toggling some filters in a rudimentary way). The back-end lets us quickly define new properties to filter packages and opportunities by, and these are instantly reflected in the output.
Everything happens through static pages at the moment, with parameters passed in the query string. Any interaction that involves clicking a link, including expanding a package to show its related opportunities, is a full page load away.
![](/+icing/build/overlay/assets/skins/sam/images/close.gif)
Dylan McCall (dylanmccall) wrote : | # |
![](/+icing/build/overlay/assets/skins/sam/images/close.gif)
Daniel Holbach (dholbach) wrote : | # |
> Oh, I forgot. This also adds a dependency on debug-toolbar. That change can be
> reverted (it's just in settings.py and INSTALL), but it also doesn't hurt.
> It's very useful for tracking how changes affect performance.
>
> Note that the middleware adds a whole ton of data to the page as it is sent
> (when enabled), so it does have a performance hit of its own. It only turns on
> if it's being accessed through 127.0.0.1.
Maybe we can ask people to add that stuff to local_settings.py or we require YES_I_AM_
About the rest of the new code: HOLY COW! You put quite a bit of work into this! :)
FilterContainer:
~~~~~~~~~~~~~~~~
minor only, but the description of set_filters() says "Add a set of filters to be children of this one." although self.filters_dict is reset at the beginning. Would an add_*() function be useful or should the description just be modified a bit?
I think I'd rename get() to find(), not sure which is more common. Both work for me. :-)
113 + #the first bit is the one this instance stores
114 + if name_bits[0] in self.filters_dict:
115 + result = self.filters_
116 + else:
117 + result = None
Lines 116 and 117 can be removed.
119 + if isinstance(result, FilterContainer) and len(name_bits)>1:
120 + return result.
121 + else:
122 + #we have reached the end of the list. Return the result (which may be None)
123 + return result
121 can be removed and 123 unidented one level.
FilterSystem:
~~~~~~~~~~~~~
Does every call to get_parameters() produce a copy? Do we need a get()ter there?
Filter:
~~~~~~~
Do we need explicit getters and setters() for the very simple members of Filter?
Do you think it's useful to add a few very quick examples to some of descriptions? ie: to me it wasn't immediately obvious what the "toggling" is about.
![](/+icing/build/overlay/assets/skins/sam/images/close.gif)
Daniel Holbach (dholbach) wrote : | # |
(minor) Maybe FilterSystem.
Can FilterSystem and FilterContainer be merged? Are they used separately and in different ways? I must admit FilterSystem is not quite clear to me yet. Still digging through it. :)
![](/+icing/build/overlay/assets/skins/sam/images/close.gif)
Daniel Holbach (dholbach) wrote : | # |
Filter: I'm not sure what get_value() and get_value_string*() and get_parameters_
![](/+icing/build/overlay/assets/skins/sam/images/close.gif)
Daniel Holbach (dholbach) wrote : | # |
Can we get PackageListWrap
![](/+icing/build/overlay/assets/skins/sam/images/close.gif)
James Westby (james-w) wrote : | # |
Wow!
Thanks Dylan.
31 + url_params = list()
seems to be superfluous.
Would there ever be a desire for
36 +def current_
to remove parameters? That can be something that is added later if needed
though.
46 \ No newline at end of file
453 \ No newline at end of file
673 \ No newline at end of file
Fixing those would be nice.
149 + #note that this currently stores parameters that aren't relevant to the FilterSystem
150 + #we need to do that so we don't eat them in get_url_
151 + self.set_
is that still needed with the code to get the original params in get_url_
Where you mark a method as abstract and don't implement in then consider
raise NotImplementedE
which will give an error if the subclass doesn't implement it, instead of
silently doing nothing.
244 + @param choices_dict: Optional value to be used instead of internal value
that's not a parameter to that method, same in the next method.
Where you use mark_safe you should add a comment stating why that string is
known to be safe.
Do the default parameters make sense? I'm not sure we should be defaulting to
"ged" at least.
Overall this is some seriously great code, nice work!
Thanks,
James
![](/+icing/build/overlay/assets/skins/sam/images/close.gif)
Dylan McCall (dylanmccall) wrote : | # |
Yay! Thanks for the comments. I'll respond to them here.
There is a bit of superfluous initialization (by Python standards) going
on, indeed. Thanks for pointing them out! I was still in Vala mode when
I started this, desperately clinging to the comfort of strong typing ;)
So, in order from the top!
---FilterContai
under filters/filters.py, and FilterSystem under
filters go inside of. It should be The object that the rest of
the application uses to manipulate filters. The current HTTP
request is handed to that object and it sorts out the rest.
Thankfully, that means the thing can be poked at repeatedly
until the wackiness subsides, without affecting the rest of the
code.
FilterGroup is used to group filters that work with a specific
collection of objects (there's a pkg and opp group at the
moment). It is possible to enable and disable filters inside a
in the past. It will still only ever be called once, but I
needn't enforce that; it just adds complexity for no particular
reason. I renamed set_filters to add_filters and I'm
functions starting with set_ and get_ were making my head hurt,
anyway.
used anywhere any more. I'll strip that out. Similar
lot_ of copying going on for one request. It can probably be
sped up somewhere.
method of many other types, even though it is definitely not
like those. Good point. Changed it to find()!
Daniel, you're awesome at naming functions! :)
Thanks for spotting the leftover set_parameters stuff, James.
It's all gone now! (*phew*)
---Filters---
The default parameters are just for testing purposes, and indeed
make no sense. I'm using gedit as a consistent query to test
against, so when it looks exactly like the mockup I'll know! (It
also needs some kind of weird default, because it doesn't handle
big lists of packages very gracefully).
pkg:name is set as such because there is no interface to change
it yet, except editing the URL. Normally, of course, it would be
blank.
Javascript is easy, but doing it without needs a pretty
convoluted HTML form. I may just do the Javascript solution for
now).
- 201. By Dylan McCall
-
Cleanup, as discussed at <https:/
/code.edge. launchpad. net/~dylanmccal l/harvest/ harvest- dylan-m/ +merge/ 28262> Added newlines at end of files, where they were missing.
harvest/filters:
Finished docstrings
Removing "#abstract" comment where it makes no senseharvest/
filters/ containers. py:
Renamed some methods for clarityharvest/
filters/ filters. py:
raising exception for methods that need to be implemented
Rejigged get_values, get_value_string and get_parameters_for_value. Now a filter can be serialized and it's all a little bit simpler - 202. By Dylan McCall
-
Fixed a bug where serialize_value, given an empty set for value, used the current value instead.
![](/+icing/build/overlay/assets/skins/sam/images/close.gif)
Daniel Holbach (dholbach) wrote : | # |
Thanks a lot for your continued work on this and sorry for not replying earlier. I've been quite busy with other things. Sorry.
> The setters and getters found themselves in such quantity
> because the implementation of these things can fluctuate. A lot
> of that fluctuation has been reduced with the version you're
> seeing here, though. The other reason is I find it elegant to
> have the outside world only access an object through methods;
> never through properties. (That way the rules for accessing a
> given property are self-documenting). If that's silly, let me
> know!
In cases where setters and getters just do the minimal amount of work ("self.bla = bla" and "return bla"), I'd expect that accessing the object's property would just work. If additional work needs to be done, I'd try to do stuff in the constructor or in a function that does some kind of computation or other work.
I don't know if that's applicable here.
> The reason I have visible_packages and hidden_packages in
> wrappers.py is that there will be a bunch of other stuff there
> in the near future. For example, stuff that summarizes the two
> collections of packages. Some of this demands processing that
> may or may not happen, so it makes sense to call the appropriate
> functions from the template as appropriate (I think…).
Can you maybe explain what kind of summary you'd like to see there?
I personally feel it's a bit more work to get it right, but it might be beneficial to tune the queries in views.py right, which would also save us from loading too many objects into memory.
> PackageWrapper is similar; it has little at the moment, but in
> the future could be used to access what categories of
> opportunities lie within, including hidden ones. Throwing that
> logic in the template (even with template tags) feels like a
> horrible act, and probably wouldn't be as efficient.
> Having said that, PackageWrapper feels a bit more wrong because
> there is already a perfectly good SourcePackage object we can
> add data to. I just feel squeamish throwing that stuff directly
> at a SourcePackage model instance. I know it won't save it to
> the database or anything, but it feels wrong somehow.
>
> I kept an eye on performance when I put that together. It does
> grab the entire list of source packages from the database and
> turn them into new Python objects, but in the end we're just
> doing the same database hit that would happen later. Debug
> Toolbar says we hit the database once for each model; nothing
> blatantly redundant is happening. Always room for better
> performance, though!
I was under the impression that we wouldn't have to have all opportunities and source packages in memory if the query just asked for a specific subset. Maybe I'm wrong. I'll go and find out.
> Okay, that's a lot of writing, but this has been really helpful to get
> my thoughts straightened. It feels a lot smoother than it did this
> morning!
Y...
![](/+icing/build/overlay/assets/skins/sam/images/close.gif)
Dylan McCall (dylanmccall) wrote : | # |
I did some pondering and poking, and I'm further convinced to change my use of accessors. Turns out the most Pythonic way is to use plain instance variables and implement property() as appropriate, which lets us specify our own getters and setters for those variables (or just a getter). That Java course nearly corrupted me!
There is a bit of divergence in filters.py in the gsoc-client-stuff branch, so I'm a little reluctant to make that change in this branch. (Merge conflicts — even simple ones — invariably give me headaches). Are you okay if I do it in a new branch from gsoc-client-stuff?
> I was under the impression that we wouldn't have to have all
> opportunities and source packages in memory if the query just asked
> for a specific subset. Maybe I'm wrong. I'll go and find out.
I mean to say that all the source packages which have been met by the package filtergroup are examined in a similar way. It isn't an extensive thing, but we end up accessing the queryset, which gives us a list of all the references it has found, both hidden and visible packages. (The opportunities remain a queryset until the template asks for them). I should move that to be smarter about hidden packages, only storing their count via a query.
With gsoc-client-stuff, I'm working on asynchronously loading results, package info and hidden packages. This means implementing new views on the same query data as before. So, that's why I am a little picky about having one object to manage all that; all these views can just talk to the same thing in a somewhat balanced way. Having said that, it's mostly a stop-gap and if all goes well it should be really easy to yank it out for something quicker and more cool once the dust is settled. To be honest I'm not sure what the final result is going to look like in views.py, so I am reluctant to devote too much to a specific approach yet ;)
![](/+icing/build/overlay/assets/skins/sam/images/close.gif)
Daniel Holbach (dholbach) wrote : | # |
> That Java course nearly
> corrupted me!
BIG HUGS! I'm sure you'll survive, though! :-)
> There is a bit of divergence in filters.py in the gsoc-client-stuff branch, so
> I'm a little reluctant to make that change in this branch. (Merge conflicts —
> even simple ones — invariably give me headaches). Are you okay if I do it in a
> new branch from gsoc-client-stuff?
Sure. I'm also happy for small branches and small merge proposals to go in as it should be much quicker, easier to review, etc.
> > I was under the impression that we wouldn't have to have all
> > opportunities and source packages in memory if the query just asked
> > for a specific subset. Maybe I'm wrong. I'll go and find out.
>
> I mean to say that all the source packages which have been met by the package
> filtergroup are examined in a similar way. It isn't an extensive thing, but we
> end up accessing the queryset, which gives us a list of all the references it
> has found, both hidden and visible packages. (The opportunities remain a
> queryset until the template asks for them). I should move that to be smarter
> about hidden packages, only storing their count via a query.
As I told you in a separate discussion my main concern was merely the complexity of new classes we add. If we (and particularly new contributors) try to fix upcoming bugs, it'll be harder and harder to dive through various classes which sometimes have very similar names to figure out where something is done.
Having said that, we can surely merge this for now and simplify over time and as we get a better idea of what we want to get done.
> With gsoc-client-stuff, I'm working on asynchronously loading results, package
> info and hidden packages. This means implementing new views on the same query
> data as before. So, that's why I am a little picky about having one object to
> manage all that; all these views can just talk to the same thing in a somewhat
> balanced way. Having said that, it's mostly a stop-gap and if all goes well it
> should be really easy to yank it out for something quicker and more cool once
> the dust is settled. To be honest I'm not sure what the final result is going
> to look like in views.py, so I am reluctant to devote too much to a specific
> approach yet ;)
Gotcha. Thanks for your work on this and consideration.
Preview Diff
1 | === modified file 'INSTALL' | |||
2 | --- INSTALL 2010-03-02 10:42:19 +0000 | |||
3 | +++ INSTALL 2010-06-27 21:07:25 +0000 | |||
4 | @@ -1,4 +1,4 @@ | |||
6 | 1 | 1. sudo apt-get install python-django python-launchpadlib python-django-openid-auth bzr | 1 | 1. sudo apt-get install python-django python-launchpadlib python-django-openid-auth bzr python-django-debug-toolbar |
7 | 2 | 2 | ||
8 | 3 | --- | 3 | --- |
9 | 4 | Optional for postgres usage: | 4 | Optional for postgres usage: |
10 | 5 | 5 | ||
11 | === added file 'harvest/common/url_tools.py' | |||
12 | --- harvest/common/url_tools.py 1970-01-01 00:00:00 +0000 | |||
13 | +++ harvest/common/url_tools.py 2010-06-27 21:07:25 +0000 | |||
14 | @@ -0,0 +1,32 @@ | |||
15 | 1 | def new_url_with_parameters(url, params_dict): | ||
16 | 2 | """ | ||
17 | 3 | Returns a new URL with an added query string, described by params_dict. | ||
18 | 4 | @param params_dict: a dictionary with all the parameters to add | ||
19 | 5 | @param path: url to add the parameters to | ||
20 | 6 | @return: the url (a string) with given parameters | ||
21 | 7 | """ | ||
22 | 8 | #Derived from <http://djangosnippets.org/snippets/1627/> | ||
23 | 9 | |||
24 | 10 | def param_bit(key, value): | ||
25 | 11 | if value: | ||
26 | 12 | return "%s=%s" % (key, value) | ||
27 | 13 | else: | ||
28 | 14 | return "%s" % key | ||
29 | 15 | |||
30 | 16 | if len(params_dict): | ||
31 | 17 | url_params = list() | ||
32 | 18 | url += "?%s" % "&".join([param_bit(key, value) for (key, value) in params_dict.items()]) | ||
33 | 19 | |||
34 | 20 | return url | ||
35 | 21 | |||
36 | 22 | def current_url_with_parameters(request, new_params_dict): | ||
37 | 23 | """ | ||
38 | 24 | Returns the current URL with some parameters changed, which are | ||
39 | 25 | described in new_params_dict. The rest of the query string remains | ||
40 | 26 | intact. | ||
41 | 27 | """ | ||
42 | 28 | params = request.GET.copy() #this includes parameters that aren't used by the FilterSystem | ||
43 | 29 | params.update(new_params_dict) | ||
44 | 30 | url = request.path | ||
45 | 31 | return new_url_with_parameters(url, params) | ||
46 | 32 | |||
47 | 0 | 33 | ||
48 | === added directory 'harvest/filters' | |||
49 | === added file 'harvest/filters/__init__.py' | |||
50 | --- harvest/filters/__init__.py 1970-01-01 00:00:00 +0000 | |||
51 | +++ harvest/filters/__init__.py 2010-06-27 21:07:25 +0000 | |||
52 | @@ -0,0 +1,23 @@ | |||
53 | 1 | """ | ||
54 | 2 | Here, we define a very abstract filtering system. | ||
55 | 3 | This system is entirely decoupled from presentation. A filter is simply a query | ||
56 | 4 | operation intended for a particular type of model, which can be configured and | ||
57 | 5 | turned on / off via structured input. | ||
58 | 6 | |||
59 | 7 | There are, of course, some rules: different types of filters can be added, | ||
60 | 8 | some of which store extra information (like a line of text that the user can | ||
61 | 9 | input), and filters can be grouped like radio buttons where only one filter | ||
62 | 10 | in a group can be selected. | ||
63 | 11 | |||
64 | 12 | The included classes provide most important features for free, but to be of | ||
65 | 13 | any use they need to be extended to fit the intended application. For example, | ||
66 | 14 | the get_query class is unimplemented; it should return a Q object that does | ||
67 | 15 | the work of selecting whatever the filter is intended to select. | ||
68 | 16 | |||
69 | 17 | Groups of filters in no way refer to how they get placed in a page, or how | ||
70 | 18 | they are interacted with. We leave that entirely up to templates. It is also | ||
71 | 19 | something else's job to figure out how to draw filters, and how to form the | ||
72 | 20 | links they talk to us with. | ||
73 | 21 | |||
74 | 22 | All This Does is filter things. | ||
75 | 23 | """ | ||
76 | 0 | 24 | ||
77 | === added file 'harvest/filters/containers.py' | |||
78 | --- harvest/filters/containers.py 1970-01-01 00:00:00 +0000 | |||
79 | +++ harvest/filters/containers.py 2010-06-27 21:07:25 +0000 | |||
80 | @@ -0,0 +1,97 @@ | |||
81 | 1 | from harvest.common.url_tools import current_url_with_parameters | ||
82 | 2 | |||
83 | 3 | class FilterContainer(object): #abstract | ||
84 | 4 | """ | ||
85 | 5 | A class that contains Filter objects, which can be retrieved with the | ||
86 | 6 | find method. | ||
87 | 7 | |||
88 | 8 | The added Filter objects are referred to as "children." They are expected | ||
89 | 9 | to exist for the entire life of the container. | ||
90 | 10 | """ | ||
91 | 11 | |||
92 | 12 | def __init__(self, filters_set): | ||
93 | 13 | self.filters_dict = dict() #refers to Filter objects by their unique IDs | ||
94 | 14 | self.add_filters(filters_set) | ||
95 | 15 | |||
96 | 16 | def add_filters(self, filters_set): #final | ||
97 | 17 | """ | ||
98 | 18 | Adds a set of filters to be children of this one and informs | ||
99 | 19 | each child that it belongs to this container. | ||
100 | 20 | @param filter_set: a set of Filter objects | ||
101 | 21 | """ | ||
102 | 22 | for child in set(filters_set): | ||
103 | 23 | self.filters_dict[child.get_id()] = child | ||
104 | 24 | child.set_container(self) | ||
105 | 25 | |||
106 | 26 | def find(self, full_name): #final | ||
107 | 27 | """ | ||
108 | 28 | Finds a filter inside this object or one of its children, based | ||
109 | 29 | on that filter's full name in the format container:child. | ||
110 | 30 | @param full_name: an object's full name | ||
111 | 31 | @return: the object described by full_name, or None | ||
112 | 32 | """ | ||
113 | 33 | result = None | ||
114 | 34 | name_bits = full_name.split(':',1) | ||
115 | 35 | |||
116 | 36 | #the first bit is the one this instance stores | ||
117 | 37 | if name_bits[0] in self.filters_dict: | ||
118 | 38 | result = self.filters_dict[name_bits[0]] | ||
119 | 39 | |||
120 | 40 | if isinstance(result, FilterContainer) and len(name_bits)>1: | ||
121 | 41 | result = result.find(name_bits[1]) #send remaining bits to child | ||
122 | 42 | |||
123 | 43 | #we have reached the end of the list. Return the result (which may be None) | ||
124 | 44 | return result | ||
125 | 45 | |||
126 | 46 | |||
127 | 47 | |||
128 | 48 | class FilterSystem(FilterContainer): | ||
129 | 49 | """ | ||
130 | 50 | This is the single all-knowing root object that should contain all | ||
131 | 51 | other filters for an application. From this object it is possible to | ||
132 | 52 | find a filter using its full name, which is the same name used to | ||
133 | 53 | serialize a filter's state in an HTTP query string. | ||
134 | 54 | |||
135 | 55 | Before an instance of this object can be safely used, | ||
136 | 56 | update_from_http should be called with the current HttpRequest. | ||
137 | 57 | """ | ||
138 | 58 | |||
139 | 59 | def __init__(self, filters_set, default_parameters = dict()): | ||
140 | 60 | FilterContainer.__init__(self, filters_set) | ||
141 | 61 | self.request = None #current http request | ||
142 | 62 | self.default_parameters = default_parameters | ||
143 | 63 | |||
144 | 64 | def update_from_http(self, request): #final | ||
145 | 65 | """ | ||
146 | 66 | Call this method to update the state of all filters based on an | ||
147 | 67 | HttpRequest object. The request object will be stored for other uses. | ||
148 | 68 | @param request: current HttpRequest object | ||
149 | 69 | """ | ||
150 | 70 | self.request = request | ||
151 | 71 | #this contains parameters that aren't relevant to the FilterSystem | ||
152 | 72 | self._set_parameters(request.GET) | ||
153 | 73 | |||
154 | 74 | def _set_parameters(self, parameters): #final | ||
155 | 75 | """ | ||
156 | 76 | Updates the state of all filters based on given parameters. | ||
157 | 77 | @param parameters: dictionary of parameters, for example from request.GET | ||
158 | 78 | """ | ||
159 | 79 | new_params = parameters.copy() | ||
160 | 80 | for key in self.default_parameters: | ||
161 | 81 | #apply default parameters for keys that have not been set | ||
162 | 82 | if not key in new_params: new_params[key] = self.default_parameters[key] | ||
163 | 83 | |||
164 | 84 | for key in new_params: | ||
165 | 85 | filter_object = self.find(key) | ||
166 | 86 | if filter_object: | ||
167 | 87 | filter_object.set_value(new_params[key]) | ||
168 | 88 | |||
169 | 89 | def get_url_with_parameters(self, parameters): #final | ||
170 | 90 | """ | ||
171 | 91 | Returns a new URL where the given parameters will be applied. | ||
172 | 92 | To generate parameters, see Filter.serialize. | ||
173 | 93 | @param parameters: a dictionary of new parameters | ||
174 | 94 | @return: the current url with the given parameters added to the query string | ||
175 | 95 | """ | ||
176 | 96 | return current_url_with_parameters(self.request, parameters) | ||
177 | 97 | |||
178 | 0 | 98 | ||
179 | === added file 'harvest/filters/filters.py' | |||
180 | --- harvest/filters/filters.py 1970-01-01 00:00:00 +0000 | |||
181 | +++ harvest/filters/filters.py 2010-06-27 21:07:25 +0000 | |||
182 | @@ -0,0 +1,318 @@ | |||
183 | 1 | #FIXME: Make ChoiceFilter a bit nicer so we don't need to call super() and all that bother from harvest.opportunities.filters. | ||
184 | 2 | #TODO: adjust Filter.render() methods for custom template tags, with django.template.Template | ||
185 | 3 | |||
186 | 4 | from containers import FilterContainer, FilterSystem | ||
187 | 5 | from django.utils.safestring import mark_safe | ||
188 | 6 | from copy import copy | ||
189 | 7 | |||
190 | 8 | class Filter(object): #abstract, extend in application | ||
191 | 9 | """ | ||
192 | 10 | The abstract base class for all other filters. | ||
193 | 11 | |||
194 | 12 | Every Filter's main objective is to process QuerySets with the method | ||
195 | 13 | Filter.process_queryset. Filter, or one of its subclasses, should be | ||
196 | 14 | extended with a new version of process_queryset that does something useful | ||
197 | 15 | for a specific application. For example, process_queryset could return | ||
198 | 16 | queryset.filter(foo="bar") so using the filter will limit the queryset | ||
199 | 17 | to objects where foo=bar. | ||
200 | 18 | |||
201 | 19 | Every Filter has a unique id and can belong to a single container. | ||
202 | 20 | |||
203 | 21 | A Filter can be assigned a new value from a string and its internal value | ||
204 | 22 | (which could be of any type) can be serialized back to a string. | ||
205 | 23 | The object itself can be serialized as a key / value pair, where the key | ||
206 | 24 | is completely unique. | ||
207 | 25 | |||
208 | 26 | Every Filter can be rendered, which outputs markup (currently html) | ||
209 | 27 | describing it for a user interface. | ||
210 | 28 | """ | ||
211 | 29 | |||
212 | 30 | #TODO: figure out get_system and get_full_name when set_container is called and store the value | ||
213 | 31 | |||
214 | 32 | def __init__(self, id_str): #final | ||
215 | 33 | self.id_str = id_str #local name | ||
216 | 34 | self.container = None #immediate container (FilterContainer) | ||
217 | 35 | |||
218 | 36 | def get_id(self): #final | ||
219 | 37 | """ | ||
220 | 38 | @return: the local id of this filter, unique to its container. | ||
221 | 39 | """ | ||
222 | 40 | return self.id_str | ||
223 | 41 | |||
224 | 42 | def set_container(self, container): #final | ||
225 | 43 | """ | ||
226 | 44 | Specify that this filter belongs to a specific container. This will | ||
227 | 45 | replace any container it currently believes it belongs to. | ||
228 | 46 | @param container: a FilterContainer object that holds this Filter | ||
229 | 47 | """ | ||
230 | 48 | #it would make sense to raise an exception if self.container != None | ||
231 | 49 | self.container = container | ||
232 | 50 | |||
233 | 51 | def get_container(self): #final | ||
234 | 52 | """ | ||
235 | 53 | @return: the container that this filter belongs to | ||
236 | 54 | """ | ||
237 | 55 | return self.container | ||
238 | 56 | |||
239 | 57 | def get_system(self): #final | ||
240 | 58 | """ | ||
241 | 59 | @return: the FilterSystem that this filter ultimately belongs to | ||
242 | 60 | """ | ||
243 | 61 | container = self.get_container() | ||
244 | 62 | system = None | ||
245 | 63 | if isinstance(container, Filter): | ||
246 | 64 | system = container.get_system() | ||
247 | 65 | elif isinstance(container, FilterSystem): | ||
248 | 66 | system = container | ||
249 | 67 | return system | ||
250 | 68 | |||
251 | 69 | def get_full_name(self): #final | ||
252 | 70 | """ | ||
253 | 71 | Returns the filter's full name, which should make sense from anywhere | ||
254 | 72 | in the application. This name is in the format parent:child:child. | ||
255 | 73 | @return: the filter's full name, which is a string | ||
256 | 74 | """ | ||
257 | 75 | full_name = self.get_id() | ||
258 | 76 | container = self.get_container() | ||
259 | 77 | if isinstance(container, Filter): | ||
260 | 78 | full_name = "%s:%s" % (container.get_full_name(), full_name) | ||
261 | 79 | return full_name | ||
262 | 80 | |||
263 | 81 | def set_value(self, value): #abstract | ||
264 | 82 | """ | ||
265 | 83 | Extend this to take a value passed down from the top, probably | ||
266 | 84 | from FilterSystem.update_from_http, and do something with it. | ||
267 | 85 | @param value: a new value, as a string | ||
268 | 86 | """ | ||
269 | 87 | raise NotImplementedError(self.set_value) | ||
270 | 88 | |||
271 | 89 | def get_value(self): #abstract | ||
272 | 90 | """ | ||
273 | 91 | @return: a copy of this filter's value in its native format | ||
274 | 92 | """ | ||
275 | 93 | raise NotImplementedError(self.get_value) | ||
276 | 94 | |||
277 | 95 | def serialize_value(self, value): #abstract | ||
278 | 96 | """ | ||
279 | 97 | The inverse of set_value. Returns the given value as a string that could | ||
280 | 98 | be added to an HTTP query string. | ||
281 | 99 | @param value: the value to serialize, in a format native to this Filter (see get_value) | ||
282 | 100 | @return: a unicode string formatted for set_value | ||
283 | 101 | """ | ||
284 | 102 | raise NotImplementedError(self.serialize_value) | ||
285 | 103 | |||
286 | 104 | def serialize(self, value = None): #final | ||
287 | 105 | """ | ||
288 | 106 | Creates a dictionary of parameters to describe this object, either | ||
289 | 107 | as-is or with a new value. | ||
290 | 108 | The result can be sent to FilterSystem.get_url_with_parameters. | ||
291 | 109 | @param value: a different value to use, in a format native to this Filter (see get_value) | ||
292 | 110 | @return: a dictionary of key:value pairs referring to the object and its value. | ||
293 | 111 | """ | ||
294 | 112 | if value == None: value = self.get_value() | ||
295 | 113 | key = self.get_full_name() | ||
296 | 114 | value_str = self.serialize_value(value) | ||
297 | 115 | return {key : value_str} | ||
298 | 116 | |||
299 | 117 | def get_container_toggle_parameters(self): #final | ||
300 | 118 | """ | ||
301 | 119 | Helper method to get the parameter for toggling this filter's state in | ||
302 | 120 | its container, if there is one. | ||
303 | 121 | @return: a dictionary of key:value pairs to generate new GET parameters | ||
304 | 122 | """ | ||
305 | 123 | container = self.get_container() | ||
306 | 124 | params = dict() | ||
307 | 125 | if isinstance(container, FilterGroup): | ||
308 | 126 | params = container.serialize(container.get_value_with_selection(self.get_id())) | ||
309 | 127 | return params | ||
310 | 128 | |||
311 | 129 | def process_queryset(self, queryset): #abstract | ||
312 | 130 | """ | ||
313 | 131 | Extend this to manipulate a given queryset and then return it. | ||
314 | 132 | For example, queryset.filter(name__startswith = self.value) | ||
315 | 133 | @param queryset: a queryset to operate on | ||
316 | 134 | @return: a queryset based on the given one | ||
317 | 135 | """ | ||
318 | 136 | raise NotImplementedError(self.process_queryset) | ||
319 | 137 | |||
320 | 138 | def render(self): #final | ||
321 | 139 | """ | ||
322 | 140 | @return: the default rendering of the filter itself in given context | ||
323 | 141 | """ | ||
324 | 142 | return self.render_html() | ||
325 | 143 | |||
326 | 144 | def render_html(self): | ||
327 | 145 | """ | ||
328 | 146 | Extend this to return the html output for the filter itself. | ||
329 | 147 | The output should be very simple and semantically meaningful, | ||
330 | 148 | with no specific concern about formatting. It will be | ||
331 | 149 | placed within other tags that describe its context, and it is | ||
332 | 150 | up to the template to describe style. | ||
333 | 151 | @return: a unicode string containing html representing this filter | ||
334 | 152 | """ | ||
335 | 153 | system = self.get_system() | ||
336 | 154 | toggle_params = self.get_container_toggle_parameters() | ||
337 | 155 | href = system.get_url_with_parameters(toggle_params) | ||
338 | 156 | |||
339 | 157 | return mark_safe(u'<a href="%s">(%s)</a>' | ||
340 | 158 | % (href, self.get_id())) | ||
341 | 159 | |||
342 | 160 | |||
343 | 161 | |||
344 | 162 | |||
345 | 163 | class EditFilter(Filter): #abstract, extend in application | ||
346 | 164 | """ | ||
347 | 165 | This Filter has a simple string value which can be edited by the user. | ||
348 | 166 | |||
349 | 167 | Serialized as stored ("value") | ||
350 | 168 | """ | ||
351 | 169 | |||
352 | 170 | def __init__(self, id_str): | ||
353 | 171 | Filter.__init__(self, id_str) | ||
354 | 172 | self.input_str = "" | ||
355 | 173 | |||
356 | 174 | def set_value(self, value): #overrides Filter | ||
357 | 175 | self.input_str = value | ||
358 | 176 | |||
359 | 177 | def get_value(self): #overrides Filter | ||
360 | 178 | return self.input_str | ||
361 | 179 | |||
362 | 180 | def serialize_value(self, value): #overrides Filter | ||
363 | 181 | return value | ||
364 | 182 | |||
365 | 183 | def render_html(self): | ||
366 | 184 | system = self.get_system() | ||
367 | 185 | toggle_params = self.get_container_toggle_parameters() | ||
368 | 186 | href = system.get_url_with_parameters(toggle_params) | ||
369 | 187 | |||
370 | 188 | return mark_safe(u'<a href="%s">%s: %s</a>' | ||
371 | 189 | % (href, self.get_id(), self.get_value())) | ||
372 | 190 | |||
373 | 191 | |||
374 | 192 | class SetFilter(Filter): #abstract, extend in application | ||
375 | 193 | """ | ||
376 | 194 | Holds a set of strings, with no repetition. | ||
377 | 195 | |||
378 | 196 | Serialized as a comma-separated list ("dog,cat,horse,mouse") | ||
379 | 197 | """ | ||
380 | 198 | |||
381 | 199 | def __init__(self, id_str): | ||
382 | 200 | Filter.__init__(self, id_str) | ||
383 | 201 | self.selected_set = set() | ||
384 | 202 | |||
385 | 203 | def set_value(self, value): #overrides Filter | ||
386 | 204 | self.selected_set = set([s for s in value.split(",") if self.id_allowed(s)]) | ||
387 | 205 | |||
388 | 206 | def get_value(self): #overrides Filter | ||
389 | 207 | return self.selected_set.copy() | ||
390 | 208 | |||
391 | 209 | def serialize_value(self, value): #overrides Filter | ||
392 | 210 | return ",".join(value) | ||
393 | 211 | |||
394 | 212 | def get_value_with_selection(self, item_id): #final | ||
395 | 213 | """ | ||
396 | 214 | Returns the current value of this object with the selection referred to | ||
397 | 215 | by item_id toggled on or off, depending on its current state. | ||
398 | 216 | @param item_id: id for the item to toggle | ||
399 | 217 | @return: the value of this SetFilter with the given item toggled on or off | ||
400 | 218 | """ | ||
401 | 219 | select = self.get_value() | ||
402 | 220 | if item_id in select: | ||
403 | 221 | select.remove(item_id) | ||
404 | 222 | else: | ||
405 | 223 | select.add(item_id) | ||
406 | 224 | return select | ||
407 | 225 | |||
408 | 226 | def id_selected(self, item_id): | ||
409 | 227 | return item_id in self.selected_set | ||
410 | 228 | |||
411 | 229 | def id_allowed(self, item_id): | ||
412 | 230 | return True | ||
413 | 231 | |||
414 | 232 | |||
415 | 233 | class ChoiceFilter(SetFilter): #abstract, extend in application | ||
416 | 234 | """ | ||
417 | 235 | Has a dictionary of items, with names and values of any type. These can be | ||
418 | 236 | selected or deselected by the input, which is a set of strings, as in | ||
419 | 237 | SetFilter. In that set, any items which refer to choices that do not exist | ||
420 | 238 | are ignored. | ||
421 | 239 | |||
422 | 240 | Serialized as a comma-separated list, like SetFilter. | ||
423 | 241 | """ | ||
424 | 242 | |||
425 | 243 | def __init__(self, id_str, choices_dict): | ||
426 | 244 | SetFilter.__init__(self, id_str) | ||
427 | 245 | self.choices_dict = choices_dict | ||
428 | 246 | |||
429 | 247 | def id_allowed(self, item_id): #overrides SetFilter | ||
430 | 248 | return item_id in self.choices_dict | ||
431 | 249 | |||
432 | 250 | def get_selected_items(self): | ||
433 | 251 | return [self.choices_dict[s] for s in self.selected_set] | ||
434 | 252 | |||
435 | 253 | def render_html(self): #overrides Filter | ||
436 | 254 | choices = "" | ||
437 | 255 | |||
438 | 256 | for c in self.choices_dict: | ||
439 | 257 | c_render = self._render_html_choice(c) | ||
440 | 258 | if self.id_selected(c): | ||
441 | 259 | c_render = "<b>%s</b>" % c_render | ||
442 | 260 | choices += "<li>%s</li>" % c_render | ||
443 | 261 | |||
444 | 262 | system = self.get_system() | ||
445 | 263 | toggle_params = self.get_container_toggle_parameters() | ||
446 | 264 | self_href = system.get_url_with_parameters(toggle_params) | ||
447 | 265 | |||
448 | 266 | return mark_safe(u'<a href="%s">%s</a>:<ul>%s</ul>' % (self_href, self.get_id(), choices)) | ||
449 | 267 | |||
450 | 268 | def _render_html_choice(self, item_id): | ||
451 | 269 | system = self.get_system() | ||
452 | 270 | toggle_params = self.serialize(self.get_value_with_selection(item_id)) | ||
453 | 271 | item_href = system.get_url_with_parameters(toggle_params) | ||
454 | 272 | |||
455 | 273 | return mark_safe(u'<a href="%s">%s</a>' % (item_href, item_id)) | ||
456 | 274 | |||
457 | 275 | |||
458 | 276 | |||
459 | 277 | |||
460 | 278 | class FilterGroup(FilterContainer, SetFilter): #final | ||
461 | 279 | """ | ||
462 | 280 | A collection of other Filters, which are selected (enabled) according to the | ||
463 | 281 | rules of SetFilter. | ||
464 | 282 | |||
465 | 283 | The do_queryset method mixes the output from all selected Filters, so only | ||
466 | 284 | the one for this FilterGroup needs to be (or should be) called. | ||
467 | 285 | |||
468 | 286 | Serialized as a comma-separated list, like SetFilter. | ||
469 | 287 | """ | ||
470 | 288 | |||
471 | 289 | def __init__(self, id_str, filters_set): | ||
472 | 290 | FilterContainer.__init__(self, filters_set) | ||
473 | 291 | SetFilter.__init__(self, id_str) | ||
474 | 292 | |||
475 | 293 | def id_allowed(self, item_id): #overrides SetFilter | ||
476 | 294 | return item_id in self.filters_dict | ||
477 | 295 | |||
478 | 296 | def get_selected_filters(self, filter_id): | ||
479 | 297 | return [self.filters_dict[s] for s in self.selected_set] | ||
480 | 298 | |||
481 | 299 | def process_queryset(self, queryset): #overrides Filter | ||
482 | 300 | for f in self.selected_set: | ||
483 | 301 | queryset = self.filters_dict[f].process_queryset(queryset) #returns something like QuerySet.filter(blah) | ||
484 | 302 | return queryset | ||
485 | 303 | |||
486 | 304 | def render_html(self): #overrides Filter | ||
487 | 305 | filters = "" | ||
488 | 306 | |||
489 | 307 | for f in self.filters_dict: | ||
490 | 308 | f_render = self._render_html_filter(f) | ||
491 | 309 | if self.id_selected(f): | ||
492 | 310 | f_render = "<em>%s</em>" % f_render | ||
493 | 311 | filters += "<li>%s</li>" % f_render | ||
494 | 312 | |||
495 | 313 | return mark_safe(u'%s:<ul>%s</ul>' % (self.get_id(), filters)) | ||
496 | 314 | |||
497 | 315 | def _render_html_filter(self, filter_id): | ||
498 | 316 | f = self.filters_dict[filter_id] | ||
499 | 317 | return f.render_html() | ||
500 | 318 | |||
501 | 0 | 319 | ||
502 | === added file 'harvest/opportunities/filters.py' | |||
503 | --- harvest/opportunities/filters.py 1970-01-01 00:00:00 +0000 | |||
504 | +++ harvest/opportunities/filters.py 2010-06-27 21:07:25 +0000 | |||
505 | @@ -0,0 +1,53 @@ | |||
506 | 1 | from harvest.filters import filters, containers | ||
507 | 2 | import models | ||
508 | 3 | |||
509 | 4 | class PkgNameFilter(filters.EditFilter): | ||
510 | 5 | def process_queryset(self, queryset): | ||
511 | 6 | return queryset.filter(name__startswith = self.get_value()) | ||
512 | 7 | |||
513 | 8 | class PkgSetFilter(filters.ChoiceFilter): | ||
514 | 9 | def __init__(self, id_str): | ||
515 | 10 | choices_dict = dict() | ||
516 | 11 | for s in models.PackageSet.objects.all(): | ||
517 | 12 | choices_dict[s.name] = s | ||
518 | 13 | super(PkgSetFilter, self).__init__(id_str, choices_dict) | ||
519 | 14 | |||
520 | 15 | def process_queryset(self, queryset): | ||
521 | 16 | return queryset.filter(packagesets__in=self.get_selected_items()) | ||
522 | 17 | |||
523 | 18 | |||
524 | 19 | |||
525 | 20 | class OppFeaturedFilter(filters.Filter): | ||
526 | 21 | def process_queryset(self, queryset): | ||
527 | 22 | return queryset.filter(opportunitylist__featured=True) | ||
528 | 23 | |||
529 | 24 | class OppListFilter(filters.ChoiceFilter): | ||
530 | 25 | def __init__(self, id_str): | ||
531 | 26 | choices_dict = dict() | ||
532 | 27 | for l in models.OpportunityList.objects.all(): | ||
533 | 28 | choices_dict[l.name] = l | ||
534 | 29 | super(OppListFilter, self).__init__(id_str, choices_dict) | ||
535 | 30 | |||
536 | 31 | def process_queryset(self, queryset): | ||
537 | 32 | return queryset.filter(opportunitylist__in=self.get_selected_items()) | ||
538 | 33 | |||
539 | 34 | |||
540 | 35 | #we don't really need to create a special type here, but it may be handy | ||
541 | 36 | class HarvestFilters(containers.FilterSystem): | ||
542 | 37 | def __init__(self): | ||
543 | 38 | super(HarvestFilters, self).__init__( | ||
544 | 39 | [ | ||
545 | 40 | filters.FilterGroup("pkg", [ | ||
546 | 41 | PkgNameFilter("name"), | ||
547 | 42 | PkgSetFilter("set") | ||
548 | 43 | ] ), | ||
549 | 44 | filters.FilterGroup("opp", [ | ||
550 | 45 | OppFeaturedFilter("featured"), | ||
551 | 46 | OppListFilter("list") | ||
552 | 47 | ] ) | ||
553 | 48 | ], | ||
554 | 49 | default_parameters = { "pkg" : "name,set", | ||
555 | 50 | "pkg:name" : "ged", | ||
556 | 51 | "pkg:set" : "ubuntu-desktop" } | ||
557 | 52 | ) | ||
558 | 53 | |||
559 | 0 | 54 | ||
560 | === modified file 'harvest/opportunities/urls.py' | |||
561 | --- harvest/opportunities/urls.py 2010-03-08 16:33:21 +0000 | |||
562 | +++ harvest/opportunities/urls.py 2010-06-27 21:07:25 +0000 | |||
563 | @@ -12,6 +12,10 @@ | |||
564 | 12 | 12 | ||
565 | 13 | url(r'^source-package/(?P<sourcepackage_slug>[-\w+.]+)/$', 'opportunities.views.sourcepackage_detail', name='sourcepackage_detail'), | 13 | url(r'^source-package/(?P<sourcepackage_slug>[-\w+.]+)/$', 'opportunities.views.sourcepackage_detail', name='sourcepackage_detail'), |
566 | 14 | url(r'^source-package/$', 'opportunities.views.sourcepackage_list', name='sourcepackage_list'), | 14 | url(r'^source-package/$', 'opportunities.views.sourcepackage_list', name='sourcepackage_list'), |
567 | 15 | |||
568 | 16 | url(r'^filter', | ||
569 | 17 | 'opportunities.views.opportunities_filter', | ||
570 | 18 | name='opportunities_filter'), | ||
571 | 15 | 19 | ||
572 | 16 | url(r'^by-type', | 20 | url(r'^by-type', |
573 | 17 | 'opportunities.views.opportunities_by_type', | 21 | 'opportunities.views.opportunities_by_type', |
574 | 18 | 22 | ||
575 | === modified file 'harvest/opportunities/views.py' | |||
576 | --- harvest/opportunities/views.py 2010-06-08 15:46:42 +0000 | |||
577 | +++ harvest/opportunities/views.py 2010-06-27 21:07:25 +0000 | |||
578 | @@ -13,6 +13,9 @@ | |||
579 | 13 | import models | 13 | import models |
580 | 14 | import forms | 14 | import forms |
581 | 15 | 15 | ||
582 | 16 | from filters import HarvestFilters | ||
583 | 17 | from wrappers import PackageWrapper, PackageListWrapper | ||
584 | 18 | |||
585 | 16 | def opportunity_index(request): | 19 | def opportunity_index(request): |
586 | 17 | sources_list = models.SourcePackage.objects.all() | 20 | sources_list = models.SourcePackage.objects.all() |
587 | 18 | paginator = Paginator(sources_list, 50) | 21 | paginator = Paginator(sources_list, 50) |
588 | @@ -120,6 +123,37 @@ | |||
589 | 120 | extra_context = {'opportunities': opportunities}, | 123 | extra_context = {'opportunities': opportunities}, |
590 | 121 | ) | 124 | ) |
591 | 122 | 125 | ||
592 | 126 | def opportunities_filter(request): | ||
593 | 127 | filters = HarvestFilters() | ||
594 | 128 | filters.update_from_http(request) | ||
595 | 129 | filters_pkg = filters.find('pkg') | ||
596 | 130 | filters_opp = filters.find('opp') | ||
597 | 131 | |||
598 | 132 | packages_list = models.SourcePackage.objects.distinct() | ||
599 | 133 | packages_list = filters_pkg.process_queryset(packages_list) | ||
600 | 134 | |||
601 | 135 | #opportunities_list is filtered right away to only check opportunities belonging to selected packages | ||
602 | 136 | opportunities_list = models.Opportunity.objects.distinct().filter(sourcepackage__in=packages_list) | ||
603 | 137 | opportunities_list = filters_opp.process_queryset(opportunities_list) | ||
604 | 138 | #TODO: need to filter out opportunities with valid=False again | ||
605 | 139 | #TODO: would it be more efficient to group opportunities by their sourcepackages first, then run filters_opp.process_queryset() for each of those groups? | ||
606 | 140 | |||
607 | 141 | pkg_list_wrapper = PackageListWrapper(request, packages_list, opportunities_list) | ||
608 | 142 | |||
609 | 143 | context = { | ||
610 | 144 | 'grouping': 'package', | ||
611 | 145 | 'packages_list': pkg_list_wrapper, | ||
612 | 146 | 'filters_pkg' : filters_pkg, | ||
613 | 147 | 'filters_opp' : filters_opp | ||
614 | 148 | } | ||
615 | 149 | |||
616 | 150 | return render( | ||
617 | 151 | 'opportunities/opportunities_filter.html', | ||
618 | 152 | context, | ||
619 | 153 | context_instance=RequestContext(request)) | ||
620 | 154 | |||
621 | 155 | #TODO: package_filter_detail(request, sourcepackage, opportunities_list) | ||
622 | 156 | |||
623 | 123 | def opportunities_by_type(request): | 157 | def opportunities_by_type(request): |
624 | 124 | types_list = models.OpportunityList.objects.filter(active=True) | 158 | types_list = models.OpportunityList.objects.filter(active=True) |
625 | 125 | paginator = Paginator(types_list, 50) | 159 | paginator = Paginator(types_list, 50) |
626 | 126 | 160 | ||
627 | === added file 'harvest/opportunities/wrappers.py' | |||
628 | --- harvest/opportunities/wrappers.py 1970-01-01 00:00:00 +0000 | |||
629 | +++ harvest/opportunities/wrappers.py 2010-06-27 21:07:25 +0000 | |||
630 | @@ -0,0 +1,90 @@ | |||
631 | 1 | from django.db.models import Count | ||
632 | 2 | from harvest.common.url_tools import current_url_with_parameters | ||
633 | 3 | |||
634 | 4 | class PackageWrapper(object): | ||
635 | 5 | """ | ||
636 | 6 | Describes a visible source package, for specific use in a | ||
637 | 7 | template. | ||
638 | 8 | """ | ||
639 | 9 | |||
640 | 10 | def __init__(self, request, package, visible_opportunities = None, expanded = False): | ||
641 | 11 | self.request = request | ||
642 | 12 | self.package = package | ||
643 | 13 | self.visible_opportunities = visible_opportunities | ||
644 | 14 | self.expanded = expanded | ||
645 | 15 | |||
646 | 16 | def real(self): | ||
647 | 17 | return self.package | ||
648 | 18 | |||
649 | 19 | def get_expand_toggle_url(self): | ||
650 | 20 | parameter = {'expand_pkg' : self.package.name} | ||
651 | 21 | url = current_url_with_parameters(self.request, parameter) | ||
652 | 22 | return url | ||
653 | 23 | |||
654 | 24 | #FIXME: get_visible_opportunities and get_hidden_opportunities feel | ||
655 | 25 | # wasteful. Could we do exclude and filter in a single | ||
656 | 26 | # operation? Does it affect performance? | ||
657 | 27 | def get_visible_opportunities(self): | ||
658 | 28 | """ | ||
659 | 29 | Returns opportunities that belong to the given package and are | ||
660 | 30 | in opportunities_list. | ||
661 | 31 | """ | ||
662 | 32 | #also check if valid? | ||
663 | 33 | return self.visible_opportunities | ||
664 | 34 | |||
665 | 35 | def get_hidden_opportunities(self): | ||
666 | 36 | """ | ||
667 | 37 | Returns opportunities that belong to the given package but have | ||
668 | 38 | been hidden from view | ||
669 | 39 | """ | ||
670 | 40 | opps_visible = self.get_visible_opportunities() | ||
671 | 41 | return self.package.opportunity_set.exclude(pk__in=opps_visible) | ||
672 | 42 | |||
673 | 43 | class PackageListWrapper(object): | ||
674 | 44 | """ | ||
675 | 45 | Object describing a list of source packages and opportunities, to | ||
676 | 46 | be used by a template. It contains UI-specific variables and simple | ||
677 | 47 | helper functions for doing final queries to access these lists. | ||
678 | 48 | """ | ||
679 | 49 | |||
680 | 50 | def __init__(self, request, packages_list, opportunities_list): | ||
681 | 51 | expand_list = None #list of packages to show in detail | ||
682 | 52 | if 'expand_pkg' in request.GET: | ||
683 | 53 | expand_list = request.GET['expand_pkg'].split(',') | ||
684 | 54 | |||
685 | 55 | related_packages = set(opportunities_list.values_list('sourcepackage', flat=True)) | ||
686 | 56 | |||
687 | 57 | self.visible_packages_list = list() | ||
688 | 58 | self.hidden_packages_list = list() | ||
689 | 59 | |||
690 | 60 | #Create a PackageWrapper around every source package. | ||
691 | 61 | #Includes a less detailed wrapper for hidden packages. | ||
692 | 62 | for package in packages_list: | ||
693 | 63 | if package.pk in related_packages: | ||
694 | 64 | opps = None | ||
695 | 65 | expand = False | ||
696 | 66 | |||
697 | 67 | if expand_list: expand = (package.name in expand_list) | ||
698 | 68 | opps = opportunities_list.filter(sourcepackage=package) | ||
699 | 69 | |||
700 | 70 | package_wrapper = PackageWrapper(request, package, | ||
701 | 71 | visible_opportunities = opps, | ||
702 | 72 | expanded = expand) | ||
703 | 73 | self.visible_packages_list.append(package_wrapper) | ||
704 | 74 | |||
705 | 75 | else: | ||
706 | 76 | package_wrapper = PackageWrapper(request, package) | ||
707 | 77 | self.hidden_packages_list.append(package_wrapper) | ||
708 | 78 | |||
709 | 79 | def get_visible_packages(self): | ||
710 | 80 | """ | ||
711 | 81 | Returns list of packages that are are visible. | ||
712 | 82 | These are any packages that contain opportunities. | ||
713 | 83 | """ | ||
714 | 84 | return self.visible_packages_list | ||
715 | 85 | |||
716 | 86 | def get_hidden_packages(self): | ||
717 | 87 | """ | ||
718 | 88 | Returns list of packages that have been hidden from view. | ||
719 | 89 | """ | ||
720 | 90 | return self.hidden_packages_list | ||
721 | 0 | \ No newline at end of file | 91 | \ No newline at end of file |
722 | 1 | 92 | ||
723 | === modified file 'harvest/settings.py' | |||
724 | --- harvest/settings.py 2010-06-01 16:16:19 +0000 | |||
725 | +++ harvest/settings.py 2010-06-27 21:07:25 +0000 | |||
726 | @@ -7,6 +7,7 @@ | |||
727 | 7 | TEMPLATE_DEBUG = DEBUG | 7 | TEMPLATE_DEBUG = DEBUG |
728 | 8 | STATIC_SERVE = True | 8 | STATIC_SERVE = True |
729 | 9 | PROJECT_NAME = 'harvest' | 9 | PROJECT_NAME = 'harvest' |
730 | 10 | INTERNAL_IPS = ('127.0.0.1',) #for testing | ||
731 | 10 | 11 | ||
732 | 11 | from common import utils | 12 | from common import utils |
733 | 12 | VERSION_STRING = utils.get_harvest_version( | 13 | VERSION_STRING = utils.get_harvest_version( |
734 | @@ -70,6 +71,7 @@ | |||
735 | 70 | 'django.contrib.sessions.middleware.SessionMiddleware', | 71 | 'django.contrib.sessions.middleware.SessionMiddleware', |
736 | 71 | 'django.contrib.auth.middleware.AuthenticationMiddleware', | 72 | 'django.contrib.auth.middleware.AuthenticationMiddleware', |
737 | 72 | 'django.middleware.locale.LocaleMiddleware', | 73 | 'django.middleware.locale.LocaleMiddleware', |
738 | 74 | 'debug_toolbar.middleware.DebugToolbarMiddleware', #for testing | ||
739 | 73 | ) | 75 | ) |
740 | 74 | 76 | ||
741 | 75 | ROOT_URLCONF = 'harvest.urls' | 77 | ROOT_URLCONF = 'harvest.urls' |
742 | @@ -98,7 +100,9 @@ | |||
743 | 98 | 'django.contrib.sites', | 100 | 'django.contrib.sites', |
744 | 99 | 'django.contrib.admin', | 101 | 'django.contrib.admin', |
745 | 100 | 'django_openid_auth', | 102 | 'django_openid_auth', |
746 | 103 | 'debug_toolbar', #for testing | ||
747 | 101 | 'opportunities', | 104 | 'opportunities', |
748 | 105 | 'filters', | ||
749 | 102 | 'common', | 106 | 'common', |
750 | 103 | ) | 107 | ) |
751 | 104 | 108 | ||
752 | 105 | 109 | ||
753 | === added file 'harvest/templates/opportunities/opportunities_filter.html' | |||
754 | --- harvest/templates/opportunities/opportunities_filter.html 1970-01-01 00:00:00 +0000 | |||
755 | +++ harvest/templates/opportunities/opportunities_filter.html 2010-06-27 21:07:25 +0000 | |||
756 | @@ -0,0 +1,50 @@ | |||
757 | 1 | {% extends "base.html" %} | ||
758 | 2 | {% load i18n %} | ||
759 | 3 | |||
760 | 4 | {% block title %}{% trans "Opportunity Index" %} - {{ block.super }}{% endblock %} | ||
761 | 5 | |||
762 | 6 | {% block content %} | ||
763 | 7 | <div class="mainpage"> | ||
764 | 8 | |||
765 | 9 | <h1>{% trans "Opportunities" %}</h1> | ||
766 | 10 | |||
767 | 11 | <div class="filters" style="background-color:#E0F1FF; float:left; width:15em;"> | ||
768 | 12 | {{filters_pkg.render}} | ||
769 | 13 | {{filters_opp.render}} | ||
770 | 14 | </div> | ||
771 | 15 | |||
772 | 16 | <div class="results" style="float:left;"> | ||
773 | 17 | {% if packages_list %} | ||
774 | 18 | <ul> | ||
775 | 19 | {% for pkg in packages_list.get_visible_packages %} | ||
776 | 20 | <li><a href="{{ pkg.get_expand_toggle_url }}">{{ pkg.real.name }}</a> | ||
777 | 21 | {% if pkg.expanded %} | ||
778 | 22 | <ul> | ||
779 | 23 | {% for opportunity in pkg.get_visible_opportunities %} | ||
780 | 24 | {% include "opportunities/opportunity_detail.inc.html" %} | ||
781 | 25 | {% endfor %} | ||
782 | 26 | |||
783 | 27 | {% with pkg.get_hidden_opportunities.count as hidden_count %} | ||
784 | 28 | {% ifnotequal hidden_count 0 %} | ||
785 | 29 | <li><small>{{ hidden_count }} {{ hidden_count|pluralize:"opportunity,opportunities"}} hidden</small></li> | ||
786 | 30 | {% endifnotequal %} | ||
787 | 31 | {% endwith %} | ||
788 | 32 | </ul> | ||
789 | 33 | {% endif %} | ||
790 | 34 | </li> | ||
791 | 35 | {% endfor %} | ||
792 | 36 | |||
793 | 37 | {% with packages_list.get_hidden_packages|length as hidden_count %} | ||
794 | 38 | {% ifnotequal hidden_count 0 %} | ||
795 | 39 | <li><small>{{ hidden_count }} package{{ hidden_count|pluralize:"s"}} {{ hidden_count|pluralize:"has,have"}} no matching opportunities</small></li> | ||
796 | 40 | {% endifnotequal %} | ||
797 | 41 | {% endwith %} | ||
798 | 42 | </ul> | ||
799 | 43 | |||
800 | 44 | {% else %} | ||
801 | 45 | <p>{% trans "There are currently no opportunities in Harvest. :(" %}</p> | ||
802 | 46 | {% endif %} | ||
803 | 47 | </div> | ||
804 | 48 | |||
805 | 49 | </div> | ||
806 | 50 | {% endblock %} |
Oh, I forgot. This also adds a dependency on debug-toolbar. That change can be reverted (it's just in settings.py and INSTALL), but it also doesn't hurt. It's very useful for tracking how changes affect performance.
Note that the middleware adds a whole ton of data to the page as it is sent (when enabled), so it does have a performance hit of its own. It only turns on if it's being accessed through 127.0.0.1.