def get_fixture_subject(self): try: fixture_subject = Setting.get_by_key_name('fixture_subject').status from model.proficiency import Proficiency this_subject = Proficiency.get_by_key_name(fixture_subject) assert this_subject is not None return Proficiency.get_by_key_name(fixture_subject) except: DEFAULT_SUBJECT = "Smart Grid" logging.error('unable to load fixture subject - returning default: %s', DEFAULT_SUBJECT) return DEFAULT_SUBJECT
def load(self): save = [] logging.info('loading fixture') self.fixture_offset = Setting.get_by_key_name('fixture_offset') if self.fixture_offset.status == "update_stats": #instead of 'create_account' logging.warning('Load Fixtures Cron Job Hit Twice In a Row') print "error -- current status: ", self.fixture_offset.status return False this_account, this_user, this_quiz_taker = self.get_fixture() scores = Scores() import random correct_prob = random.randint(80,95) FIXTURE_PROFICIENCY = self.get_fixture_subject() from model.proficiency import Proficiency this_proficiency = random.sample( Proficiency.gql("WHERE status = 'public'").fetch(1000), 1 )[0] save_scores = scores.make_scores(this_user, this_proficiency, correct_prob, SCORE_NUM = 10) memcache.set('current_fixture', ( this_account, this_user, this_quiz_taker ), 600000) self.fixture_offset.status = "update_stats" print this_user.nickname save.append(self.fixture_offset) save.extend(save_scores) db.put(save) # open fixture.xml, go to offset. # load one name, email pair. register. FIXTURE_PROFICIENCY = self.get_fixture_subject()
def sponsor_settings(self): PLEDGE_NUM = 500 # get employer this_employer = Employer.get_by_key_name(self.request.get('sponsor')) # save message this_employer.sponsorship_message = self.request.get('sponsorship_message') #save quiz subjects this_employer.quiz_subjects = [ self.request.get('quiz_subject') ] # also save it in the profile_image from model.user import Profile this_user = Profile.get_by_key_name(this_employer.unique_identifier) from model.proficiency import Proficiency this_proficiency = Proficiency.get_by_key_name(self.request.get('quiz_subject')) # TODO: Should changing your sponsored subject cancel out your existing auto pledges? # old_pledges = AutoPledge.gql("WHERE employer = :1 AND proficiency != :2", % (this_employer, this_proficiency)) #this_user.sponsored_subjects.append( Proficiency.get_by_key_name(self.request.get('quiz_subject')) ) -- Multiple Entries this_user.sponsored_subjects = [ this_proficiency.key() ] # create auto_pledge from model.employer import AutoPledge # save sponsor account new_pledge = AutoPledge(key_name = this_employer.unique_identifier + "_" + this_proficiency.name, employer = this_employer, proficiency = this_proficiency, count = PLEDGE_NUM) db.put([this_employer, this_user, new_pledge]) return "OK"
def pqmember(self, *args): from model.user import Profile pq = Profile.get_by_key_name("PlopQuiz") from model.proficiency import Proficiency ps = Proficiency.gql("WHERE status = 'public'").fetch(1000) from model.user import SubjectMember for p in ps: db.put(SubjectMember(key_name = pq.unique_identifier + "_" + p.name, user = pq, subject = p ))
def get_featured_quiz(): try: from model.dev import Setting featured_subject = Setting.get_by_key_name('fixture_subject').status except: from model.proficiency import Proficiency featured_subject = Proficiency.gql("WHERE status = 'public' ORDER BY status, modified DESC" ).get() return featured_subject
def get(self): #from model.proficiency import SubjectProfile #subjects = SubjectProfile.all().fetch(1000) #template_values = {"subjects": subjects} from model.proficiency import Proficiency proficiencies = Proficiency.all().fetch(1000) template_values = {'proficiencies': proficiencies} path = tpl_path(DEV_PATH + 'edit_subjects.html') self.response.out.write(template.render(path, template_values))
def get_quiz_subjects(self): from model.proficiency import Proficiency quiz_subjects = Proficiency.gql("WHERE status = 'public' ORDER BY status, modified DESC" ).fetch(6) for p in quiz_subjects: if p.name == eval(self.request.get('subject'))[0]: quiz_subjects.remove(p) quiz_subjects.insert(0, p) continue return quiz_subjects[0:5]
def NewTopic(self): # set moderation status for raw item this_subject = Proficiency.gql("WHERE name = :1", self.request.get('subject_name')).get() new_topic = ProficiencyTopic(name = self.request.get('topic_name'), proficiency = this_subject) db.put([this_subject, new_topic]) if self.request.get('no_template'): return "OK" from utils.webapp import template from utils.utils import tpl_path template_values = {"subject": this_subject, "new_topic":new_topic} path = tpl_path(EDITOR_PATH + 'item_topic.html') return template.render(path, template_values)
def update_subject_blurb(self): from model.proficiency import Proficiency subject_name = self.request.get('subject_name') this_subject = Proficiency.get_by_key_name(subject_name) if this_subject is None: logging.error('no subject found when saving blurb for subject_name %s ', subject_name) return "no subject found" this_subject.blurb = self.request.get('new_blurb') db.put(this_subject) return "OK"
def extra_subjects(member_subjects, these_subjects, offset=0): extra_subjects = Proficiency.gql("WHERE status = 'public' ORDER BY status, modified DESC").fetch(100) current_count = 0 for s in extra_subjects: if current_count >= MIN_SUBJECT_LENGTH: break elif s.name not in member_subjects: these_subjects.append(s) current_count += 1 else: continue # subject already in these_subjects return these_subjects
def upload_subject_img(self): subject_name = self.request.path.split('/subject_img/')[1].replace('%20',' ') from model.proficiency import Proficiency this_subject = Proficiency.get_by_key_name(subject_name) new_image = this_subject.new_image(self.request.get('subject_img')) db.put([this_subject, new_image]) logging.info('saved new image for subject %s' % (this_subject.name)) from utils.webapp import template path = tpl_path(EDITOR_PATH +'load_subject_images.html') template_values = {'s': {"subject": this_subject, "is_member": "admin" }} # Only admins can upload photos, for now. return template.render(path, template_values)
def remove_link(self): from model.proficiency import Proficiency, Link subject_name = self.request.get('subject_name') this_subject = Proficiency.get_by_key_name(subject_name) this_link = Link.get( self.request.get('link_key') ) logging.info('removed link: %s', this_link.url ) db.delete(this_link) db.put(this_subject) from utils.webapp import template path = tpl_path(EDITOR_PATH +'subject/links_list.html') template_values = {'s': {"subject": this_subject, "is_member": "admin" }} # Only admins can edit links, for now. return template.render(path, template_values)
def delete_subject_image(self): from model.proficiency import Proficiency, SubjectImage subject_name = self.request.get('subject_name') this_subject = Proficiency.get_by_key_name(subject_name) this_img = SubjectImage.get( self.request.get('img_key') ) logging.info('removed img for subject %s', this_subject.name ) db.delete(this_img) db.put(this_subject) from utils.webapp import template path = tpl_path(EDITOR_PATH +'load_subject_images.html') template_values = {'s': {"subject": this_subject, "is_member": "admin" }} # Only admins can edit links, for now. return template.render(path, template_values)
def get(self): proficiencies = Proficiency.gql("WHERE status = :1", "public"); proficiencies = proficiencies.fetch(1000) buy_buttons = [] for p in proficiencies: p.checkout_button = checkout.render_quiz_button(self, p.tag(), p.name) buy_buttons.append( { 'tag': p.tag().lower(), 'html' : p.checkout_button}) prof_json = encode(proficiencies) template_values = {'proficiencies' : proficiencies, 'prof_json': prof_json, 'buy_buttons': encode(buy_buttons), 'load': 2000} path = tpl_path(STORE_PATH + 'proficiency.html') self.response.out.write(template.render(path, template_values))
def make_scores(self, *args): if len(args) < 2: return "Specify A Proficiency, and Correct Ratio" from utils.appengine_utilities.sessions import Session self.session = Session() from model.proficiency import Proficiency this_proficiency = Proficiency.get_by_key_name(args[0]) correct_prob = args[1] if not self.session['user']: return "Not Logged In" this_user = self.session['user'] from dev.fixtures import Scores scores = Scores() save_scores = scores.make_scores(this_user, this_proficiency, correct_prob, SCORE_NUM = 10) db.put(save_scores)
def working(self, *args): from model.proficiency import Proficiency, ProficiencyTopic q = Proficiency.get_by_key_name("QuiztheBill") save = [] pts = ProficiencyTopic.all().fetch(1000) for p in pts: if p.subject is None: p.subject = q print p.name save.append(p) db.put(save)
def get_test(self): if len(self.request.path.split('/sponsor/')[1]) > 0: employer = Employer.gql('WHERE name = :1', self.request.path.split('/quiz/')[1].lower()) try: these_proficiencies = employer.get().proficiencies except: return None proficiencies = [] for p in these_proficiencies: this_p = Proficiency.get_by_key_name(p) proficiencies.append(this_p.name) return [proficiencies, employer.get()] #except: return [proficiency.name for proficiency in all_proficiencies.fetch(4)] if self.request.get('proficiencies'): proficiencies = self.request.get('proficiencies') return [eval(proficiencies,{"__builtins__":None},{}), self.get_default_vendor()] return None
def change_video(self): from model.proficiency import Proficiency subject_name = self.request.get('subject_name') this_subject = Proficiency.get_by_key_name(subject_name) if "p=" not in self.request.get('new_video_url'): logging.info('video url %s is not recognizable as video playlist link', self.request.get('new_video_url')) return "error" video_code = self.request.get('new_video_url').split("p=")[1] this_subject.video_html = video_code logging.info('changed video for subject %s to %s' % (this_subject.name,video_code) ) db.put(this_subject) from utils.webapp import template path = tpl_path(EDITOR_PATH +'subject/video_object.html') template_values = {'s': {"subject": this_subject, "is_member": "admin" }} # Only admins can edit links, for now. return template.render(path, template_values)
def add_link(self): from model.proficiency import Proficiency, Link subject_name = self.request.get('subject_name') this_subject = Proficiency.get_by_key_name(subject_name) try: new_link = Link(key_name = subject_name + "_" + self.request.get('link_url'), url = self.request.get('link_url'), title = self.request.get('link_title'), subject = this_subject ) except BadValueError: return "error" db.put([this_subject,new_link]) logging.info('new link with url %s and title %s for subject %s' % (this_subject.name, str(new_link.url), new_link.title ) ) from utils.webapp import template path = tpl_path(EDITOR_PATH +'subject/links_list.html') template_values = {'s': {"subject": this_subject, "is_member": "admin" }} # Only admins can edit links, for now. return template.render(path, template_values)
def get_html(self): import urllib template_values = {'text': urllib.unquote( self.request.get('text') ) } from model.proficiency import Proficiency if self.request.get('subject_key'): template_values['subject_key'] = self.request.get('subject_key') else: subjects = Proficiency.gql("WHERE status = 'public'").fetch(1000) template_values['subjects'] = subjects if self.request.get('topic_key'): template_values['topic_key'] = self.request.get('topic_key') if self.request.get('topic_name'): from model.proficiency import ProficiencyTopic p = ProficiencyTopic.gql("WHERE name = :1", self.request.get('topic_name')).get() template_values['topic_key'] = p.key() path = tpl_path(DEV_PATH +'ubiquity_builder.html') response = simplejson.dumps(template.render(path, template_values)) self.response.out.write(jsonp(self.request.get("callback"), response))
def join_subject(self): from utils.appengine_utilities.sessions import Session self.session = Session() from model.proficiency import Proficiency this_subject = Proficiency.gql("WHERE name = :1", self.request.get('subject_name') ).get() from model.user import SubjectMember admin_status = False from google.appengine.api import users user = users.get_current_user() if user: admin_status = True this_membership = SubjectMember(keyname = self.session['user'].unique_identifier + "_" + this_subject.name, user = self.session['user'], subject = this_subject, is_admin = admin_status) logging.info('user %s joined subject %s'% (self.session['user'].unique_identifier, this_subject.name) ) db.put([this_membership]) from utils.webapp import template path = tpl_path(EDITOR_PATH +'load_member_section.html') template_values = {'s': {"subject": this_subject, "is_member": "contributor" }} # Only admins can edit links, for now. return template.render(path, template_values)
def add_auto_pledge(self, *args): if not args: return "Specify A Business Identifier, Proficiency Name, and Number of Pledges." if len(args) > 3: return "Specify A Business Identifier, Proficiency Name, and Number of Pledges." business_name = args[0] from model.employer import Employer this_employer = Employer.get_by_key_name(business_name) if not this_employer: return "employer does not exist" proficiency_name = args[1] from model.proficiency import Proficiency #import string -- Capwords sucks, darnit. #this_proficiency = Proficiency.get_by_key_name(string.capwords(proficiency_name)) this_proficiency = Proficiency.get_by_key_name(proficiency_name) pledge_num = int(args[2]) from model.employer import AutoPledge new_pledge = AutoPledge(employer = this_employer, proficiency = this_proficiency, count = pledge_num) new_pledge.put() return encode(new_pledge)
def change_rights(self): from utils.appengine_utilities.sessions import Session self.session = Session() from model.proficiency import Proficiency from model.user import SubjectMember subject_name = self.request.get('subject_name') this_subject = Proficiency.get_by_key_name(subject_name) this_change = self.request.get('rights_action') this_membership = SubjectMember.gql("WHERE subject = :1 AND user = :2", this_subject, self.session['user']).get() if this_change == "make_admin": logging.info('make admin') this_membership.is_admin = True if this_change == "remove_admin": this_membership.is_admin = False db.put([this_subject,this_membership]) logging.info('user %s has had admin status set to %s for subject %s' % (self.session['user'].unique_identifier, str(this_membership.is_admin), this_subject.name)) from utils.webapp import template path = tpl_path(EDITOR_PATH +'subject/admin_rights.html') template_values = {'s': {"subject": this_subject}} return template.render(path, template_values)
def submit(self, args): from utils.appengine_utilities.sessions import Session session = Session() if not session['user']: return False from .model.account import Account from .model.user import Profile sponsor_type = "personal" # or corporate package = args[0] award_type = args[1] raw_target = [args[2]] # for more than one user. TODO: Front-end. target = [] activated = [] single_target = False for u in raw_target: # for now, it really is just for one person. t = Profile.get(u) if len(raw_target) > 1: single_target = t #if single_target == session['user'].key(): return False # can't sponsor yourself target.append(t.key()) activated.append(False) from model.account import SponsorPledge new_pledge = SponsorPledge( #specify key name if uniqueness is important, but its not high priority for now. sponsor = session['user'], sponsor_type = sponsor_type, package = package, award_type = award_type, target = target, type = type, activated = activated) # if only a single person is receiving the pledge if single_target: new_pledge.single_target = single_target # if any subject was chosen if args[3] != "any_subject": from model.proficiency import Proficiency new_pledge.proficiency = Proficiency.get(args[3]) db.put(new_pledge) return True
def create_new_subject(self): from utils.appengine_utilities.sessions import Session self.session = Session() from model.proficiency import Proficiency existing_subject = Proficiency.gql("WHERE name = :1", self.request.get('subject_name') ).get() if existing_subject is not None: logging.warning("user %s attempted to create duplicate subject with name %s" %(self.session['user'].unique_identifier, self.request.get('subject_name')) ) return "exists" this_subject = Proficiency(key_name = self.request.get('subject_name'), name = self.request.get('subject_name')) from model.user import SubjectMember this_membership = SubjectMember(keyname = self.session['user'].unique_identifier + "_" + this_subject.name, user = self.session['user'], subject = this_subject, status = "public", is_admin = True) logging.info('user %s created subject %s'% (self.session['user'].unique_identifier, this_subject.name) ) db.put([this_subject, this_membership]) from utils.webapp import template path = tpl_path(EDITOR_PATH +'subject_container.html') from editor.methods import get_subjects_for_user template_values = { 'subjects' : get_subjects_for_user(self.session['user'])} return template.render(path, template_values)
def get(self): self.proficiencies = {} # Create random list of three quiz items. if self.request.get('proficiencies'): quiz_items = [] for p in eval(self.request.get('proficiencies')): # TODO make these keys for easy lookup -- these are proficiencies, not topics. this_p = Proficiency.gql("WHERE name = :1", p) q = QuizItem.gql("WHERE proficiency = :1", this_p.get()) # use topic for key quiz_items.extend(q.fetch(1000)) # Query all quiz items else: q = db.GqlQuery("SELECT * FROM QuizItem") quiz_items = q.fetch(1000) # Load Fixture Data if Necessary q = db.GqlQuery("SELECT * FROM QuizItem") quiz_items = q.fetch(1000) import random for item in quiz_items: self.load_item(item, random) self.load_array(random) template_values = {"quiz_items": self.quiz_array } logging.debug(template_values) path = tpl_path(DEMO_PATH + 'ad.html') self.response.out.write(template.render(path, template_values))
def SubmitItem(self): from model.proficiency import Proficiency, ProficiencyTopic this_subject = Proficiency.gql("WHERE name = :1", self.request.get('subject_name') ).get() logging.info('submitting item') from utils.appengine_utilities.sessions import Session session = Session() if len( self.request.get('item_key') ) < 1: this_item = QuizItem(pending_proficiency = this_subject) if session['user']: this_item.author= session['user'] else: this_item = QuizItem.get(self.request.get('item_key')) if self.request.get('item_status') == "approved": this_item.active = True this_item.pending_proficiency = None this_item.proficiency = this_subject if self.request.get('item_status') == "not_approved": this_item.active = False this_item.pending_proficiency = this_subject this_item.proficiency = None this_item.topic = ProficiencyTopic.get( self.request.get('topic_key') ) this_item.index = self.request.get('correct_answer') this_item.answers = [a.strip("'") for a in self.request.get('answers').split(",")] this_item.content = self.request.get('item_text') save = [ this_subject, this_item] #if session['user']: save.append(session['user']) db.put( save ) logging.info('saving new quiz item %s with subject %s and index %s' % (this_item.__dict__, self.request.get('subject_name'), self.request.get('correct_answer') )) if self.request.get('ubiquity'): return "OK" from utils.webapp import template from utils.utils import tpl_path from editor.methods import get_membership, get_user_items template_values = {"subject": this_subject, 'subject_membership': get_membership(session['user'], this_subject), 'user_items': get_user_items(session['user'], this_subject), } path = tpl_path(EDITOR_PATH + 'quiz_item_editor_template.html') return template.render(path, template_values)
def get_subjects_for_user(this_user, offset=0): # Can this be refactored? """ This current method places member subjects first, and then loads non-member subjects. This is just a prototype and a later implementation may be different. """ memberships = this_user.member_subjects.fetch(100) member_subjects = [] for m in memberships: member_subjects.append(m.subject.name) these_subjects = Proficiency.gql("WHERE name IN :1 AND status = 'public' ORDER BY status, modified DESC", member_subjects).fetch(1000) # In case there aren't enough member subjects, add more if len(these_subjects) - offset < MIN_SUBJECT_LENGTH: these_subjects = extra_subjects(member_subjects, these_subjects, offset=offset) subject_list = [] if len(these_subjects[offset:offset+5]) > 0: these_subjects = these_subjects[offset:offset+5] for s in these_subjects[:5]: is_member = False for m in memberships: if m.subject.name == s.name: if m.is_admin: is_member = "admin" else: is_member = "contributor" subject_list.append({"subject": s, "is_member": is_member}) return subject_list
def get_html(self): from model.proficiency import Proficiency subjects = Proficiency.gql("WHERE status = 'public'").fetch(1000) template_values = {'text': self.request.get('text'), 'subjects': subjects} path = tpl_path(DEV_PATH +'ubiquity_builder.html') self.response.out.write(template.render(path, template_values))
def get_subject(self): # Get subject from path import string #string.capwords() subject_name = self.request.path.split('/edit/')[1].replace("%20"," ") #TODO - instead of capwords, make all subject names lowercase self.this_subject = Proficiency.get_by_key_name(subject_name) assert self.this_subject is not None