21 '"Sverre Rabbelier" <sverre@rabbelier.nl>', |
21 '"Sverre Rabbelier" <sverre@rabbelier.nl>', |
22 '"Lennard de Rijk" <ljvderijk@gmail.com>', |
22 '"Lennard de Rijk" <ljvderijk@gmail.com>', |
23 ] |
23 ] |
24 |
24 |
25 |
25 |
|
26 from django import http |
26 from django import forms |
27 from django import forms |
|
28 from django.utils import simplejson |
27 from django.utils.translation import ugettext |
29 from django.utils.translation import ugettext |
28 |
30 |
|
31 from soc.logic import allocations |
29 from soc.logic import cleaning |
32 from soc.logic import cleaning |
30 from soc.logic import dicts |
33 from soc.logic import dicts |
31 from soc.logic.helper import timeline as timeline_helper |
34 from soc.logic.helper import timeline as timeline_helper |
32 from soc.logic.models import host as host_logic |
35 from soc.logic.models import host as host_logic |
33 from soc.logic.models import mentor as mentor_logic |
36 from soc.logic.models import mentor as mentor_logic |
160 page_name: the page name displayed in templates as page and header title |
163 page_name: the page name displayed in templates as page and header title |
161 params: a dict with params for this View, not used |
164 params: a dict with params for this View, not used |
162 """ |
165 """ |
163 |
166 |
164 program = program_logic.logic.getFromKeyFields(kwargs) |
167 program = program_logic.logic.getFromKeyFields(kwargs) |
165 |
168 slots = program.slots |
166 filter = { |
169 |
167 'scope': program, |
170 if request.method == 'POST' and 'result' in request.POST: |
168 } |
171 result = request.POST['result'] |
169 |
172 |
170 query = org_logic.logic.getQueryForFields(filter=filter) |
173 from_json = simplejson.loads(result).iteritems() |
171 entities = org_logic.logic.getAll(query) |
174 |
172 data = [i.toDict() for i in entities] |
175 # filter out all orgs where the link_id is 'undefined' |
|
176 orgs = dict( ((k,v) for k, v in from_json if k != 'undefined')) |
|
177 |
|
178 items = orgs.iteritems() |
|
179 |
|
180 # whether the org has it's allocations locked |
|
181 locked = ((k, v['slots']) for k, v in items if v['locked']) |
|
182 |
|
183 # the adjustement for the org |
|
184 adjusted = ((k, v['adjustment']) for k, v in items if v['adjustment']) |
|
185 |
|
186 locked_slots = dict(locked) |
|
187 adjusted_slots = dict(adjusted) |
|
188 else: |
|
189 filter = { |
|
190 'scope': program, |
|
191 } |
|
192 |
|
193 query = org_logic.logic.getQueryForFields(filter=filter) |
|
194 entities = [i.toDict() for i in org_logic.logic.getAll(query)] |
|
195 |
|
196 # group orgs by link_id |
|
197 orgs = dict( ((i['link_id'], i) for i in entities) ) |
|
198 |
|
199 # default to no orgs locked nor adjusted |
|
200 locked_slots = adjusted_slots = {} |
|
201 |
|
202 # TODO(Lennard): use real data here |
|
203 applications = dict( ((i, [1, 2]) for i in orgs.keys()) ) |
|
204 mentors = dict( ((i, 1000) for i in orgs.keys()) ) |
|
205 |
|
206 # TODO: Use configuration variables here |
|
207 max_slots_per_org = 40 |
|
208 min_slots_per_org = 2 |
|
209 iterative = False |
|
210 |
|
211 allocator = allocations.Allocator(orgs, applications, mentors, |
|
212 slots, max_slots_per_org, |
|
213 min_slots_per_org, iterative) |
|
214 |
|
215 result = allocator.allocate(locked_slots, adjusted_slots) |
|
216 |
|
217 data = [] |
|
218 |
|
219 for link_id, count in result.iteritems(): |
|
220 org = orgs[link_id] |
|
221 data.append({ |
|
222 'link_id': link_id, |
|
223 'slots': count, |
|
224 'locked': org['locked'] if 'locked' in org else 0, |
|
225 'adjustment': org['adjustment'] if 'adjustment' in org else 0, |
|
226 }) |
173 |
227 |
174 return self.json(request, data) |
228 return self.json(request, data) |
175 |
229 |
176 @decorators.merge_params |
230 @decorators.merge_params |
177 @decorators.check_access |
231 @decorators.check_access |
179 params=None, **kwargs): |
233 params=None, **kwargs): |
180 """View that allows to assign slots to orgs. |
234 """View that allows to assign slots to orgs. |
181 """ |
235 """ |
182 |
236 |
183 from soc.views.models import organization as organization_view |
237 from soc.views.models import organization as organization_view |
|
238 |
184 params = organization_view.view.getParams() |
239 params = organization_view.view.getParams() |
|
240 params['list_template'] = 'soc/program/allocation/allocation.html' |
185 params['list_heading'] = 'soc/program/allocation/heading.html' |
241 params['list_heading'] = 'soc/program/allocation/heading.html' |
186 params['list_row'] = 'soc/program/allocation/row.html' |
242 params['list_row'] = 'soc/program/allocation/row.html' |
|
243 params['list_pagination'] = 'soc/list/no_pagination.html' |
187 |
244 |
188 program = program_logic.logic.getFromKeyFields(kwargs) |
245 program = program_logic.logic.getFromKeyFields(kwargs) |
189 |
246 |
190 filter = { |
247 filter = { |
191 'scope': program, |
248 'scope': program, |
|
249 'status': 'active', |
192 } |
250 } |
193 |
251 |
194 content = lists.getListContent(request, params, filter=filter) |
252 content = lists.getListContent(request, params, filter=filter) |
195 contents = [content] |
253 contents = [content] |
196 |
254 |
197 return self._list(request, params, contents, page_name) |
255 context = { |
|
256 'total_slots': program.slots, |
|
257 'uses_json': True, |
|
258 'uses_slot_allocator': True |
|
259 } |
|
260 |
|
261 return self._list(request, params, contents, page_name, context) |
198 |
262 |
199 def _editPost(self, request, entity, fields): |
263 def _editPost(self, request, entity, fields): |
200 """See base._editPost(). |
264 """See base._editPost(). |
201 """ |
265 """ |
202 |
266 |