author | Lennard de Rijk <ljvderijk@gmail.com> |
Sat, 24 Oct 2009 15:14:11 -0700 | |
changeset 3036 | c703f3802841 |
parent 2764 | 6ed5a3e26692 |
permissions | -rw-r--r-- |
2431
800a020c9bcf
Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
1 |
#!/usr/bin/python2.5 |
800a020c9bcf
Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
2 |
# |
800a020c9bcf
Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
3 |
# Copyright 2009 the Melange authors. |
800a020c9bcf
Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
4 |
# |
800a020c9bcf
Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
5 |
# Licensed under the Apache License, Version 2.0 (the "License"); |
800a020c9bcf
Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
6 |
# you may not use this file except in compliance with the License. |
800a020c9bcf
Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
7 |
# You may obtain a copy of the License at |
800a020c9bcf
Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
8 |
# |
800a020c9bcf
Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
9 |
# http://www.apache.org/licenses/LICENSE-2.0 |
800a020c9bcf
Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
10 |
# |
800a020c9bcf
Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
11 |
# Unless required by applicable law or agreed to in writing, software |
800a020c9bcf
Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
12 |
# distributed under the License is distributed on an "AS IS" BASIS, |
800a020c9bcf
Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
13 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
800a020c9bcf
Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
14 |
# See the License for the specific language governing permissions and |
800a020c9bcf
Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
15 |
# limitations under the License. |
800a020c9bcf
Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
16 |
|
800a020c9bcf
Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
17 |
"""Survey (Model) query functions. |
800a020c9bcf
Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
18 |
""" |
800a020c9bcf
Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
19 |
|
800a020c9bcf
Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
20 |
__authors__ = [ |
800a020c9bcf
Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
21 |
'"Daniel Diniz" <ajaksu@gmail.com>', |
800a020c9bcf
Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
22 |
'"James Levy" <jamesalexanderlevy@gmail.com>', |
800a020c9bcf
Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
23 |
'"Lennard de Rijk" <ljvderijk@gmail.com>', |
800a020c9bcf
Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
24 |
] |
800a020c9bcf
Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
25 |
|
800a020c9bcf
Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
26 |
|
800a020c9bcf
Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
27 |
import logging |
800a020c9bcf
Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
28 |
|
800a020c9bcf
Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
29 |
from google.appengine.ext import db |
800a020c9bcf
Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
30 |
|
800a020c9bcf
Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
31 |
from soc.cache import sidebar |
800a020c9bcf
Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
32 |
from soc.logic.models import linkable as linkable_logic |
2462
2d5d2961f774
Added getRecordLogic method to SurveyLogic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
2460
diff
changeset
|
33 |
from soc.logic.models import survey_record as survey_record_logic |
2431
800a020c9bcf
Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
34 |
from soc.logic.models.user import logic as user_logic |
800a020c9bcf
Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
35 |
from soc.logic.models import work |
800a020c9bcf
Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
36 |
from soc.models.program import Program |
800a020c9bcf
Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
37 |
from soc.models import student_project |
800a020c9bcf
Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
38 |
from soc.models.survey import Survey |
2444
6276c3340c30
Separated two survey types out of the main survey model.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
2443
diff
changeset
|
39 |
from soc.models.grading_project_survey import GradingProjectSurvey |
2568
5c456c4d0a0b
Fixed imports for Survey logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
2524
diff
changeset
|
40 |
from soc.models.grading_record import GradingRecord |
2444
6276c3340c30
Separated two survey types out of the main survey model.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
2443
diff
changeset
|
41 |
from soc.models.project_survey import ProjectSurvey |
2431
800a020c9bcf
Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
42 |
from soc.models.survey import SurveyContent |
800a020c9bcf
Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
43 |
from soc.models.survey_record import SurveyRecord |
800a020c9bcf
Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
44 |
from soc.models.work import Work |
800a020c9bcf
Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
45 |
|
800a020c9bcf
Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
46 |
|
800a020c9bcf
Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
47 |
class Logic(work.Logic): |
800a020c9bcf
Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
48 |
"""Logic methods for the Survey model. |
800a020c9bcf
Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
49 |
""" |
800a020c9bcf
Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
50 |
|
800a020c9bcf
Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
51 |
def __init__(self, model=Survey, base_model=Work, |
2462
2d5d2961f774
Added getRecordLogic method to SurveyLogic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
2460
diff
changeset
|
52 |
scope_logic=linkable_logic, |
2d5d2961f774
Added getRecordLogic method to SurveyLogic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
2460
diff
changeset
|
53 |
record_logic=survey_record_logic.logic): |
2431
800a020c9bcf
Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
54 |
"""Defines the name, key_name and model for this entity. |
2462
2d5d2961f774
Added getRecordLogic method to SurveyLogic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
2460
diff
changeset
|
55 |
|
2d5d2961f774
Added getRecordLogic method to SurveyLogic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
2460
diff
changeset
|
56 |
params: |
2d5d2961f774
Added getRecordLogic method to SurveyLogic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
2460
diff
changeset
|
57 |
record_logic: SurveyRecordLogic (or subclass) instance for this Survey |
2431
800a020c9bcf
Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
58 |
""" |
800a020c9bcf
Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
59 |
|
2462
2d5d2961f774
Added getRecordLogic method to SurveyLogic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
2460
diff
changeset
|
60 |
self.record_logic = record_logic |
2d5d2961f774
Added getRecordLogic method to SurveyLogic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
2460
diff
changeset
|
61 |
|
2431
800a020c9bcf
Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
62 |
super(Logic, self).__init__(model=model, base_model=base_model, |
800a020c9bcf
Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
63 |
scope_logic=scope_logic) |
800a020c9bcf
Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
64 |
|
800a020c9bcf
Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
65 |
def createSurvey(self, survey_fields, schema, survey_content=False): |
800a020c9bcf
Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
66 |
"""Create a new survey from prototype. |
800a020c9bcf
Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
67 |
|
800a020c9bcf
Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
68 |
params: |
800a020c9bcf
Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
69 |
survey_fields = dict of survey field items (see SurveyContent model) |
800a020c9bcf
Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
70 |
schema = metadata about survey fields (SurveyContent.schema) |
800a020c9bcf
Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
71 |
survey_content = existing SurveyContent entity |
800a020c9bcf
Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
72 |
""" |
800a020c9bcf
Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
73 |
|
800a020c9bcf
Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
74 |
if not survey_content: |
800a020c9bcf
Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
75 |
survey_content = SurveyContent() |
800a020c9bcf
Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
76 |
else: |
800a020c9bcf
Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
77 |
# wipe clean existing dynamic properties if they exist |
800a020c9bcf
Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
78 |
for prop in survey_content.dynamic_properties(): |
800a020c9bcf
Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
79 |
delattr(survey_content, prop) |
800a020c9bcf
Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
80 |
|
800a020c9bcf
Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
81 |
for name, value in survey_fields.items(): |
800a020c9bcf
Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
82 |
setattr(survey_content, name, value) |
800a020c9bcf
Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
83 |
|
800a020c9bcf
Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
84 |
survey_content.schema = str(schema) |
800a020c9bcf
Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
85 |
|
800a020c9bcf
Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
86 |
db.put(survey_content) |
800a020c9bcf
Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
87 |
|
800a020c9bcf
Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
88 |
return survey_content |
800a020c9bcf
Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
89 |
|
2460
b8b0c631bcfe
Added getSurveyForContent method to SurveyLogic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
2459
diff
changeset
|
90 |
def getSurveyForContent(self, survey_content): |
b8b0c631bcfe
Added getSurveyForContent method to SurveyLogic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
2459
diff
changeset
|
91 |
"""Returns the Survey belonging to the given SurveyContent. |
b8b0c631bcfe
Added getSurveyForContent method to SurveyLogic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
2459
diff
changeset
|
92 |
|
b8b0c631bcfe
Added getSurveyForContent method to SurveyLogic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
2459
diff
changeset
|
93 |
params: |
b8b0c631bcfe
Added getSurveyForContent method to SurveyLogic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
2459
diff
changeset
|
94 |
survey_content: the SurveyContent to retrieve the Survey for. |
b8b0c631bcfe
Added getSurveyForContent method to SurveyLogic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
2459
diff
changeset
|
95 |
|
b8b0c631bcfe
Added getSurveyForContent method to SurveyLogic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
2459
diff
changeset
|
96 |
returns: |
b8b0c631bcfe
Added getSurveyForContent method to SurveyLogic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
2459
diff
changeset
|
97 |
Survey or subclass if possible else None. |
b8b0c631bcfe
Added getSurveyForContent method to SurveyLogic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
2459
diff
changeset
|
98 |
""" |
b8b0c631bcfe
Added getSurveyForContent method to SurveyLogic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
2459
diff
changeset
|
99 |
|
b8b0c631bcfe
Added getSurveyForContent method to SurveyLogic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
2459
diff
changeset
|
100 |
fields = {'survey_content': survey_content} |
b8b0c631bcfe
Added getSurveyForContent method to SurveyLogic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
2459
diff
changeset
|
101 |
|
b8b0c631bcfe
Added getSurveyForContent method to SurveyLogic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
2459
diff
changeset
|
102 |
return self.getForFields(fields, unique=True) |
b8b0c631bcfe
Added getSurveyForContent method to SurveyLogic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
2459
diff
changeset
|
103 |
|
2462
2d5d2961f774
Added getRecordLogic method to SurveyLogic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
2460
diff
changeset
|
104 |
def getRecordLogic(self): |
2d5d2961f774
Added getRecordLogic method to SurveyLogic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
2460
diff
changeset
|
105 |
"""Returns SurveyRecordLogic that belongs to this SurveyLogic. |
2d5d2961f774
Added getRecordLogic method to SurveyLogic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
2460
diff
changeset
|
106 |
""" |
2d5d2961f774
Added getRecordLogic method to SurveyLogic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
2460
diff
changeset
|
107 |
|
2d5d2961f774
Added getRecordLogic method to SurveyLogic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
2460
diff
changeset
|
108 |
return self.record_logic |
2d5d2961f774
Added getRecordLogic method to SurveyLogic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
2460
diff
changeset
|
109 |
|
2431
800a020c9bcf
Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
110 |
def getDebugUser(self, survey, this_program): |
800a020c9bcf
Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
111 |
"""Debugging method impersonates other roles. |
800a020c9bcf
Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
112 |
|
800a020c9bcf
Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
113 |
Tests taking survey, saving response, and grading. |
800a020c9bcf
Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
114 |
|
800a020c9bcf
Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
115 |
params: |
800a020c9bcf
Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
116 |
survey = survey entity |
800a020c9bcf
Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
117 |
this_program = program scope of survey |
800a020c9bcf
Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
118 |
""" |
800a020c9bcf
Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
119 |
|
800a020c9bcf
Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
120 |
if 'mentor' in survey.taking_access: |
800a020c9bcf
Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
121 |
from soc.models.mentor import Mentor |
800a020c9bcf
Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
122 |
role = Mentor.get_by_key_name( |
800a020c9bcf
Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
123 |
this_program.key().name() + "/org_1/test") |
800a020c9bcf
Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
124 |
|
800a020c9bcf
Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
125 |
if 'student' in survey.taking_access: |
800a020c9bcf
Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
126 |
from soc.models.student import Student |
800a020c9bcf
Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
127 |
role = Student.get_by_key_name( |
800a020c9bcf
Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
128 |
this_program.key().name() + "/test") |
800a020c9bcf
Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
129 |
|
800a020c9bcf
Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
130 |
if role: return role.user |
800a020c9bcf
Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
131 |
|
800a020c9bcf
Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
132 |
def getKeyValuesFromEntity(self, entity): |
800a020c9bcf
Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
133 |
"""See base.Logic.getKeyNameValues. |
800a020c9bcf
Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
134 |
""" |
800a020c9bcf
Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
135 |
|
800a020c9bcf
Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
136 |
return [entity.prefix, entity.scope_path, entity.link_id] |
800a020c9bcf
Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
137 |
|
800a020c9bcf
Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
138 |
def getKeyValuesFromFields(self, fields): |
800a020c9bcf
Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
139 |
"""See base.Logic.getKeyValuesFromFields. |
800a020c9bcf
Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
140 |
""" |
800a020c9bcf
Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
141 |
|
800a020c9bcf
Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
142 |
return [fields['prefix'], fields['scope_path'], fields['link_id']] |
800a020c9bcf
Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
143 |
|
800a020c9bcf
Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
144 |
def getKeyFieldNames(self): |
800a020c9bcf
Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
145 |
"""See base.Logic.getKeyFieldNames. |
800a020c9bcf
Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
146 |
""" |
800a020c9bcf
Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
147 |
|
800a020c9bcf
Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
148 |
return ['prefix', 'scope_path', 'link_id'] |
800a020c9bcf
Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
149 |
|
800a020c9bcf
Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
150 |
def getScope(self, entity): |
800a020c9bcf
Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
151 |
"""Gets Scope for entity. |
800a020c9bcf
Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
152 |
|
800a020c9bcf
Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
153 |
params: |
800a020c9bcf
Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
154 |
entity = Survey entity |
800a020c9bcf
Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
155 |
""" |
800a020c9bcf
Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
156 |
|
800a020c9bcf
Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
157 |
if getattr(entity, 'scope', None): |
800a020c9bcf
Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
158 |
return entity.scope |
800a020c9bcf
Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
159 |
|
800a020c9bcf
Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
160 |
import soc.models.program |
800a020c9bcf
Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
161 |
import soc.models.organization |
800a020c9bcf
Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
162 |
import soc.models.user |
800a020c9bcf
Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
163 |
import soc.models.site |
800a020c9bcf
Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
164 |
|
800a020c9bcf
Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
165 |
# use prefix to generate dict key |
2764
6ed5a3e26692
Cleaned up Survey Logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
2689
diff
changeset
|
166 |
scope_types = { |
6ed5a3e26692
Cleaned up Survey Logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
2689
diff
changeset
|
167 |
"program": soc.models.program.Program, |
6ed5a3e26692
Cleaned up Survey Logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
2689
diff
changeset
|
168 |
"org": soc.models.organization.Organization, |
6ed5a3e26692
Cleaned up Survey Logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
2689
diff
changeset
|
169 |
"user": soc.models.user.User, |
6ed5a3e26692
Cleaned up Survey Logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
2689
diff
changeset
|
170 |
"site": soc.models.site.Site} |
2431
800a020c9bcf
Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
171 |
|
800a020c9bcf
Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
172 |
# determine the type of the scope |
800a020c9bcf
Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
173 |
scope_type = scope_types.get(entity.prefix) |
800a020c9bcf
Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
174 |
|
800a020c9bcf
Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
175 |
if not scope_type: |
800a020c9bcf
Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
176 |
# no matching scope type found |
800a020c9bcf
Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
177 |
raise AttributeError('No Matching Scope type found for %s' % entity.prefix) |
800a020c9bcf
Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
178 |
|
800a020c9bcf
Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
179 |
# set the scope and update the entity |
800a020c9bcf
Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
180 |
entity.scope = scope_type.get_by_key_name(entity.scope_path) |
800a020c9bcf
Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
181 |
entity.put() |
800a020c9bcf
Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
182 |
|
800a020c9bcf
Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
183 |
# return the scope |
800a020c9bcf
Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
184 |
return entity.scope |
800a020c9bcf
Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
185 |
|
2689
18d8486fd411
Renamed hasAtLeastOneRecord to hasRecord.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
2684
diff
changeset
|
186 |
def hasRecord(self, survey_entity): |
2684
08ec7ca16dce
Added hasAtLeastOneRecord method to Survey Logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
2590
diff
changeset
|
187 |
"""Returns True iff the given Survey has at least one SurveyRecord. |
08ec7ca16dce
Added hasAtLeastOneRecord method to Survey Logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
2590
diff
changeset
|
188 |
|
08ec7ca16dce
Added hasAtLeastOneRecord method to Survey Logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
2590
diff
changeset
|
189 |
Args: |
08ec7ca16dce
Added hasAtLeastOneRecord method to Survey Logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
2590
diff
changeset
|
190 |
survey_entity: a Survey instance |
08ec7ca16dce
Added hasAtLeastOneRecord method to Survey Logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
2590
diff
changeset
|
191 |
""" |
08ec7ca16dce
Added hasAtLeastOneRecord method to Survey Logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
2590
diff
changeset
|
192 |
|
08ec7ca16dce
Added hasAtLeastOneRecord method to Survey Logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
2590
diff
changeset
|
193 |
fields = {'survey': survey_entity} |
08ec7ca16dce
Added hasAtLeastOneRecord method to Survey Logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
2590
diff
changeset
|
194 |
|
08ec7ca16dce
Added hasAtLeastOneRecord method to Survey Logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
2590
diff
changeset
|
195 |
record_logic = self.getRecordLogic() |
08ec7ca16dce
Added hasAtLeastOneRecord method to Survey Logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
2590
diff
changeset
|
196 |
return record_logic.getQueryForFields(fields).count(1) > 0 |
08ec7ca16dce
Added hasAtLeastOneRecord method to Survey Logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
2590
diff
changeset
|
197 |
|
2524
4b39352e5a07
try to fix the surveys-have-no-scope issue
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
2500
diff
changeset
|
198 |
def _onCreate(self, entity): |
4b39352e5a07
try to fix the surveys-have-no-scope issue
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
2500
diff
changeset
|
199 |
"""Set the scope of the survey. |
4b39352e5a07
try to fix the surveys-have-no-scope issue
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
2500
diff
changeset
|
200 |
""" |
4b39352e5a07
try to fix the surveys-have-no-scope issue
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
2500
diff
changeset
|
201 |
|
4b39352e5a07
try to fix the surveys-have-no-scope issue
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
2500
diff
changeset
|
202 |
self.getScope(entity) |
4b39352e5a07
try to fix the surveys-have-no-scope issue
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
2500
diff
changeset
|
203 |
super(Logic, self)._onCreate(entity) |
4b39352e5a07
try to fix the surveys-have-no-scope issue
Sverre Rabbelier <sverre@rabbelier.nl>
parents:
2500
diff
changeset
|
204 |
|
2500
ecc16ffe174b
Style fixes and a forgotten parameter in Survey(Record) logic.
Daniel Diniz <ajaksu@gmail.com>
parents:
2480
diff
changeset
|
205 |
|
2443
2e86dbd47907
Seperated Logic for ProjectSurvey and GradingProject surveys.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
2442
diff
changeset
|
206 |
class ProjectLogic(Logic): |
2e86dbd47907
Seperated Logic for ProjectSurvey and GradingProject surveys.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
2442
diff
changeset
|
207 |
"""Logic class for ProjectSurvey. |
2e86dbd47907
Seperated Logic for ProjectSurvey and GradingProject surveys.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
2442
diff
changeset
|
208 |
""" |
2e86dbd47907
Seperated Logic for ProjectSurvey and GradingProject surveys.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
2442
diff
changeset
|
209 |
|
2e86dbd47907
Seperated Logic for ProjectSurvey and GradingProject surveys.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
2442
diff
changeset
|
210 |
def __init__(self, model=ProjectSurvey, |
2462
2d5d2961f774
Added getRecordLogic method to SurveyLogic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
2460
diff
changeset
|
211 |
base_model=Survey, scope_logic=linkable_logic, |
2d5d2961f774
Added getRecordLogic method to SurveyLogic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
2460
diff
changeset
|
212 |
record_logic=survey_record_logic.project_logic): |
2443
2e86dbd47907
Seperated Logic for ProjectSurvey and GradingProject surveys.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
2442
diff
changeset
|
213 |
"""Defines the name, key_name and model for this entity. |
2e86dbd47907
Seperated Logic for ProjectSurvey and GradingProject surveys.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
2442
diff
changeset
|
214 |
""" |
2e86dbd47907
Seperated Logic for ProjectSurvey and GradingProject surveys.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
2442
diff
changeset
|
215 |
|
2459
accd8ba94b44
Calling the correct super in Project and Grading Survey Logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
2455
diff
changeset
|
216 |
super(ProjectLogic, self).__init__(model=model, base_model=base_model, |
2462
2d5d2961f774
Added getRecordLogic method to SurveyLogic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
2460
diff
changeset
|
217 |
scope_logic=scope_logic, |
2d5d2961f774
Added getRecordLogic method to SurveyLogic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
2460
diff
changeset
|
218 |
record_logic=record_logic) |
2443
2e86dbd47907
Seperated Logic for ProjectSurvey and GradingProject surveys.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
2442
diff
changeset
|
219 |
|
2e86dbd47907
Seperated Logic for ProjectSurvey and GradingProject surveys.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
2442
diff
changeset
|
220 |
|
2e86dbd47907
Seperated Logic for ProjectSurvey and GradingProject surveys.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
2442
diff
changeset
|
221 |
class GradingProjectLogic(ProjectLogic): |
2590
f0d0c9e7b2d4
Fix indention and add missing dot in docstring in survey models.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
2568
diff
changeset
|
222 |
"""Logic class for GradingProjectSurvey. |
2443
2e86dbd47907
Seperated Logic for ProjectSurvey and GradingProject surveys.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
2442
diff
changeset
|
223 |
""" |
2e86dbd47907
Seperated Logic for ProjectSurvey and GradingProject surveys.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
2442
diff
changeset
|
224 |
|
2e86dbd47907
Seperated Logic for ProjectSurvey and GradingProject surveys.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
2442
diff
changeset
|
225 |
def __init__(self, model=GradingProjectSurvey, |
2462
2d5d2961f774
Added getRecordLogic method to SurveyLogic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
2460
diff
changeset
|
226 |
base_model=ProjectSurvey, scope_logic=linkable_logic, |
2d5d2961f774
Added getRecordLogic method to SurveyLogic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
2460
diff
changeset
|
227 |
record_logic=survey_record_logic.grading_logic): |
2443
2e86dbd47907
Seperated Logic for ProjectSurvey and GradingProject surveys.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
2442
diff
changeset
|
228 |
"""Defines the name, key_name and model for this entity. |
2e86dbd47907
Seperated Logic for ProjectSurvey and GradingProject surveys.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
2442
diff
changeset
|
229 |
""" |
2e86dbd47907
Seperated Logic for ProjectSurvey and GradingProject surveys.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
2442
diff
changeset
|
230 |
|
2459
accd8ba94b44
Calling the correct super in Project and Grading Survey Logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
2455
diff
changeset
|
231 |
super(GradingProjectLogic, self).__init__(model=model, |
accd8ba94b44
Calling the correct super in Project and Grading Survey Logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
2455
diff
changeset
|
232 |
base_model=base_model, |
2500
ecc16ffe174b
Style fixes and a forgotten parameter in Survey(Record) logic.
Daniel Diniz <ajaksu@gmail.com>
parents:
2480
diff
changeset
|
233 |
scope_logic=scope_logic, |
ecc16ffe174b
Style fixes and a forgotten parameter in Survey(Record) logic.
Daniel Diniz <ajaksu@gmail.com>
parents:
2480
diff
changeset
|
234 |
record_logic=record_logic) |
2443
2e86dbd47907
Seperated Logic for ProjectSurvey and GradingProject surveys.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
2442
diff
changeset
|
235 |
|
2442
dd1f94c3594c
Start on adding ProjectSurvey and GradingProjectSurvey.
Daniel Diniz <ajaksu@gmail.com>
parents:
2439
diff
changeset
|
236 |
|
2431
800a020c9bcf
Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff
changeset
|
237 |
logic = Logic() |
2443
2e86dbd47907
Seperated Logic for ProjectSurvey and GradingProject surveys.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
2442
diff
changeset
|
238 |
project_logic = ProjectLogic() |
2e86dbd47907
Seperated Logic for ProjectSurvey and GradingProject surveys.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
2442
diff
changeset
|
239 |
grading_logic = GradingProjectLogic() |