16 |
16 |
17 """Views for Organizations. |
17 """Views for Organizations. |
18 """ |
18 """ |
19 |
19 |
20 __authors__ = [ |
20 __authors__ = [ |
|
21 '"Madhusudan.C.S" <madhusudancs@gmail.com>', |
21 '"Augie Fackler" <durin42@gmail.com>', |
22 '"Augie Fackler" <durin42@gmail.com>', |
22 '"Sverre Rabbelier" <sverre@rabbelier.nl>', |
23 '"Sverre Rabbelier" <sverre@rabbelier.nl>', |
23 '"Lennard de Rijk" <ljvderijk@gmail.com>', |
24 '"Lennard de Rijk" <ljvderijk@gmail.com>', |
24 ] |
25 ] |
25 |
26 |
26 |
27 |
27 import itertools |
28 import itertools |
28 |
29 |
29 from django import forms |
30 from django import forms |
|
31 from django.utils import simplejson |
30 from django.utils.translation import ugettext |
32 from django.utils.translation import ugettext |
31 |
33 |
32 from soc.logic import cleaning |
34 from soc.logic import cleaning |
33 from soc.logic import dicts |
35 from soc.logic import dicts |
34 from soc.logic import accounts |
36 from soc.logic import accounts |
382 content = lists.getListContent(request, params, filter) |
384 content = lists.getListContent(request, params, filter) |
383 contents = [content] |
385 contents = [content] |
384 |
386 |
385 return self._list(request, params, contents, page_name) |
387 return self._list(request, params, contents, page_name) |
386 |
388 |
|
389 def _getMapData(self, student_project_params, filter=None): |
|
390 """Constructs the JSON object required to generate a |
|
391 google map on organization home page. |
|
392 |
|
393 Args: |
|
394 student_project_logic: logic for student_project to obtain entities |
|
395 filter: a dict for the properties that the entities should have |
|
396 |
|
397 Returns: |
|
398 A json object containing map data with the following structure. |
|
399 [ |
|
400 { |
|
401 'type': 'mentor', |
|
402 'name': MentorName, |
|
403 'city': CityName, |
|
404 'ccTLD': ccTLD, |
|
405 'students': [{'name': Name, 'city': CityName, 'ccTLD': ccTLD, |
|
406 'summary': Summary, 'url': URL}, |
|
407 {'name': Name, 'city': CityName, 'ccTLD': ccTLD, |
|
408 'summary': Summary, 'url': URL}, ] |
|
409 }, |
|
410 { |
|
411 'type': 'none', |
|
412 'name': MentorName, |
|
413 'student': {'name': Name, 'city': CityName, 'ccTLD': ccTLD, |
|
414 'summary': Summary, 'url': URL} |
|
415 } |
|
416 ] |
|
417 """ |
|
418 student_project_logic = student_project_params['logic'] |
|
419 |
|
420 map_data = [] |
|
421 mentors = {} |
|
422 student_only = [] |
|
423 |
|
424 # get all the student_project entities for this organization |
|
425 student_project_entities = student_project_logic.getForFields(filter) |
|
426 |
|
427 # construct a dictionary of mentors. For each mentor construct a |
|
428 # list of 3-tuple containing student, project title and url. This is |
|
429 # mainly done to track and pair all students and mentors who |
|
430 # have allowed to publish their locations. |
|
431 for entity in student_project_entities: |
|
432 |
|
433 if entity.mentor.publish_location and entity.mentor not in mentors: |
|
434 # if mentor has allowed to publish his location add it to the |
|
435 # mentors dictionary |
|
436 mentors[entity.mentor] = [] |
|
437 |
|
438 if entity.student.publish_location: |
|
439 # if student has allowed to publish his location, add it to the |
|
440 # corresponding mentor list, otherwise add it to the 'none' list |
|
441 if entity.mentor in mentors: |
|
442 mentors[entity.mentor].append( |
|
443 (entity.student, entity.title, |
|
444 redirects.getPublicRedirect(entity, student_project_params))) |
|
445 else: |
|
446 student_only.append( |
|
447 (entity.student, entity.title, |
|
448 redirects.getPublicRedirect(entity, student_project_params), |
|
449 entity.mentor)) |
|
450 |
|
451 # from the index built in the form of mentors dictionary we now build |
|
452 # the map_data for all the mentors who have allowed to publish their |
|
453 # location |
|
454 for mentor in mentors: |
|
455 mentor_map = { |
|
456 'type': 'mentor', |
|
457 'name': mentor.name(), |
|
458 'city': mentor.res_city, |
|
459 'ccTLD': mentor.ccTld(), |
|
460 'students': [] |
|
461 } |
|
462 |
|
463 for student, summary, url in mentors[mentor]: |
|
464 student_map = { |
|
465 'name': student.name(), |
|
466 'city': student.res_city, |
|
467 'ccTLD': student.ccTld(), |
|
468 'summary': summary, |
|
469 'url': url |
|
470 } |
|
471 |
|
472 mentor_map['students'].append(student_map) |
|
473 |
|
474 map_data.append(mentor_map) |
|
475 |
|
476 # construct the 'type': 'none' dictionary for those students, whose |
|
477 # mentors haven't allowed to publish location |
|
478 for student, summary, url, mentor in student_only: |
|
479 nomentor_map = { |
|
480 'type': 'none', |
|
481 'name': mentor.name(), |
|
482 'student': { |
|
483 'name': student.name(), |
|
484 'city': student.res_city, |
|
485 'ccTLD': student.ccTld(), |
|
486 'summary': summary, |
|
487 'url': url |
|
488 } |
|
489 } |
|
490 |
|
491 map_data.append(nomentor_map) |
|
492 |
|
493 return simplejson.dumps(map_data) |
|
494 |
387 def _public(self, request, entity, context): |
495 def _public(self, request, entity, context): |
388 """See base.View._public(). |
496 """See base.View._public(). |
389 """ |
497 """ |
390 |
498 |
391 from soc.views.models import student_project as student_project_view |
499 from soc.views.models import student_project as student_project_view |
416 contents.append(ap_list) |
524 contents.append(ap_list) |
417 |
525 |
418 # construct the list and put it into the context |
526 # construct the list and put it into the context |
419 context['list'] = soc.logic.lists.Lists(contents) |
527 context['list'] = soc.logic.lists.Lists(contents) |
420 |
528 |
421 return super(View, self)._public(request=request, |
529 # obtain data to construct the organization map as json object |
422 entity=entity, context=context) |
530 context['org_map_data'] = self._getMapData(ap_params, filter) |
|
531 |
|
532 return super(View, self)._public(request=request, entity=entity, |
|
533 context=context) |
423 |
534 |
424 def _getExtraMenuItems(self, role_description, params=None): |
535 def _getExtraMenuItems(self, role_description, params=None): |
425 """Used to create the specific Organization menu entries. |
536 """Used to create the specific Organization menu entries. |
426 |
537 |
427 For args see group.View._getExtraMenuItems(). |
538 For args see group.View._getExtraMenuItems(). |