def action_get_results_by_section(requestedSection, date, requestedBrand): try: brand = requestedBrand surveyProps = configparsers.loadPropertyFile('survey_'+brand) sectionContent = None dateFormat = None if date is not None and date is not "": dateFormat = date.isoformat() sections = memcache.get(key='admin:'+requestedBrand+':'+requestedSection+':count', namespace='razorfish') if sections is not None: logging.debug("Returning admin:"+requestedBrand+':'+requestedSection+":count from MEMCACHE") else: survey_class = utils.determine_survey(brand) section_module = utils.determine_sections(brand) surveys = list() if hasattr(section_module, requestedSection) and inspect.isclass(getattr(section_module, requestedSection)): sectionClass = getattr(section_module, requestedSection) section = dict() section['id'] = sectionClass.kind().lower() section['name'] = sectionClass.kind() section['index'] = surveyProps.get(sectionClass.kind(), 'index') section['questions'] = list() for key, value in sectionClass.properties().items(): question = dict() question['id'] = key question['name'] = value.verbose_name logging.debug('getting results for '+str(key)) results = datastore.get_results_by_count_section_question(sectionClass, key, dateFormat, requestedBrand) logging.debug(results) question['results'] = results[0] question['percentages'] = results[1] question['options_length'] = len(value.choices) section['questions'].append(question) surveys.append(section) sections = sorted(surveys, key=itemgetter('index'), reverse=False) # Save to memcache #memcache.set(key='admin:'+requestedBrand+':'+requestedSection+':count', value=sections, time=86400, namespace='razorfish') return sections except (BadValueError,AttributeError,TypeError,ValueError,CapabilityDisabledError), e: logging.error('action_get_results_by_section()') logging.error(e) raise e
def set_all_arguments(user_id, brand, arguments): try: user = user_model.User.get_by_key_name(str(user_id)) survey_class = utils.determine_survey(brand) logging.debug(survey_class) section_module = utils.determine_sections(brand) logging.debug(section_module) survey = survey_class(user_id=user, brand=brand) survey.put() # Iterate through each available Section Class for name, obj in inspect.getmembers(section_module): logging.debug(name) if inspect.isclass(obj): # Create an instance of the Section Class section = obj(parent=survey) sectionProperties = section.properties() for propKey, propValue in sectionProperties.items(): if propKey in arguments: try: # Set the property as the attribute and attribute value if propValue.data_type is int: setattr(section, propKey, int(arguments[propKey])) elif propValue.data_type is db.Text: encodedText = arguments[propKey].encode('ascii', 'xmlcharrefreplace') setattr(section, propKey, db.Text(encodedText)) except Exception, e: logging.error(e) raise e # Add the Section to the datastore def txn_section(): section.put() db.run_in_transaction(txn_section) # If the Survey instance has the Section... if hasattr(survey, str(section.kind()).lower()): # Add the Section to the Survey setattr(survey, str(section.kind()).lower(), section) # Update the Survey in the datastore survey.put()