def instance_entities(data): """ Given a kind and some json, call insert on that kind and return the results. A little safer. """ fields = ('id', 'created', 'modified', 'entity_id', 'previous_id', 'status', 'available') entities = [] if 'cards' in data: for card_data in data['cards']: kind = card_data.get('kind') if kind in card_map: entities.push( card_map[kind](omit(card_data, fields)) ) if 'units' in data: entities = entities + [ Unit(omit(unit_data, fields)) for unit_data in data['units'] ] if 'subjects' in data: entities = entities + [ Subject(omit(subject_data, fields)) for subject_data in data['subjects'] ] return entities
def create(): s = Subject( category=request.form['category'] ) if s.save(): flash("Subject created") else: flash("Subject not created", errors = s.errors)
def post(self): '''POST /subjects/ -> add a new subject ''' data = subjectResource.parser.parse_args() subject_name = data.get('name') subject_age = data.get('age') subject = Subject(subject_name, subject_age) subject.save() return subject.json(), 201
def create_subject(name, description, user): s = Subject() s.name = name s.description = description s.user_id = user.id session = db_session.create_session() session.add(s) session.commit() return s
def create_alert(): if request.method == 'POST': subject_url = request.form['subject_url'] course = Course.find_by_url(subject_url) subject = Subject(subject_url, course.tag_name, course.query) subject.load_price() subject.save_to_mongo() alert_name = request.form['name'] price_limit = request.form['price_limit'] Alert(alert_name, subject._id, price_limit, session['email']).save_to_mongo() # What happens if it's a GET request return render_template("alerts/new_alert.html")
def load_request(xacml): if not isinstance(xacml, dict): xacml = json.loads(xacml) user_attrs = xacml["Request"]["AccessSubject"][0]["Attribute"] subject = Subject() for attr in user_attrs: if "Issuer" in attr.keys(): subject.add_attribute(attr["AttributeId"], attr["Value"], attr["Issuer"], attr["DataType"], attr["IncludeInResult"]) else: subject.add_attribute(attr["AttributeId"], attr["Value"], None, attr["DataType"], attr["IncludeInResult"]) action_attrs = xacml["Request"]["Action"][0]["Attribute"] action = Action() for attr in action_attrs: action.add_attribute(attr["AttributeId"], attr["Value"]) resource = Resource() if len(xacml["Request"]["Resource"]) == 1: resource_attrs = xacml["Request"]["Resource"][0]["Attribute"] for attr in resource_attrs: resource.add_attribute(attr["AttributeId"], attr["Value"], attr["DataType"], attr["IncludeInResult"]) else: resource_ids = [] resource_values = [] resource_data_types = [] resource_include_results = [] for i in range(0, len(xacml["Request"]["Resource"])): resource_attrs = xacml["Request"]["Resource"][i]["Attribute"] for attr in resource_attrs: resource_ids.append(attr["AttributeId"]) resource_values.append(attr["Value"]) resource_data_types.append(attr["DataType"]) resource_include_results.append(attr["IncludeInResult"]) resource.add_attribute(resource_ids, resource_values, resource_data_types, resource_include_results) return subject, action, resource
# This file shall only run once upon initiation # To be placed in Procfile -> release: section # if there are no subjects in database yet, # create a list of preset subjects. print("Loading environment variables from .env") from dotenv import load_dotenv load_dotenv() from models.subject import Subject exist_subjects = Subject.select() if not len(exist_subjects): presets = [ "Language", "Mathematics", "Coding", "Accounting", "Life Skills" ] for p in presets: subject = Subject(category=p) if subject.save(): print(f"Subject {p} saved.") else: print(f"Unable to create subject {p}.")
def add_subject(self, **kwargs): subject = Subject(subject_name=kwargs["subject_name"], ) self.db.session.add(subject) self.db.session.commit()
import sys, os sys.path.append(os.path.join(os.getcwd(), "..")) from models.collections import Collections from models.subject import Subject from persistence import mongoInterface mongoInterface.setup() collections = Collections() collections.save(mongoInterface) for collectionId, subjectsIds in collections.getCollections(): print "############################" print "# Preprocessing collection: " + collectionId print "############################\n" for subjectId in subjectsIds: try: subject = Subject(subjectId) subject.fetch() subject.parse() subject.save(mongoInterface) except (OSError, Exception) as e: print "Error: ", e mongoInterface.close()
def get_relative(self, subject_a, relationship): subj = Subject(subject_a, self.df) return subj.get_relative(relationship)
def get_relationship(self, subject_a, subject_b): subj = Subject(subject_a, self.df) relationship = subj.get_relationship(subject_b) chain = subj.get_relationship_chain(subject_b) return relationship, chain
def list_subjects(bot, message: Message): subjects = Subject().all() subject_table = create_subjects_prettytable(subjects) message.reply_text(f"```{subject_table.get_string()}```",parse_mode='markdown')
def __load_subject__(self, model_type, subject): name = subject["name"] file = subject["file"] transform = subject["transform"] path = os.path.join(SUBJECTS_DIR, model_type, file) return Subject(name, transform, path)
def respond_to_card_route(request, card_id): """ Record and process a learner's response to a card. NEXT STATE POST Respond Card -> GET Learn Card ...when not ready -> GET Choose Unit ...when ready, but still units -> GET View Subject Tree ...when ready and done """ # TODO-3 simplify this method. # perhaps smaller methods or move to model layer? db_conn = request['db_conn'] current_user = get_current_user(request) if not current_user: return abort(401) card = get_card_by_kind(db_conn, card_id) if not card: return abort(404) # Make sure the card is the current one context = get_learning_context(current_user) if context.get('card', {}).get('entity_id') != card['entity_id']: return abort(400) r = seq_update(db_conn, current_user, card, request['params'].get('response')) errors, response, feedback = (r.get('errors'), r.get('response'), r.get('feedback')) if errors: return 400, { 'errors': errors, 'ref': 'wtyOJPoy4bh76OIbYp8mS3LP', } subject = Subject(context.get('subject')) unit = Unit(context.get('unit')) status = judge(db_conn, unit, current_user) # If we are done with this current unit... if status == "done": buckets = traverse(db_conn, current_user, subject) # If there are units to be diagnosed... if buckets['diagnose']: unit = buckets['diagnose'][0] next_card = choose_card(db_conn, current_user, unit) next_ = { 'method': 'GET', 'path': '/s/cards/{card_id}/learn'.format( card_id=next_card['entity_id']), } set_learning_context(current_user, card=next_card.data, unit=unit.data, next=next_) # If there are units to be learned or reviewed... elif buckets['learn'] or buckets['review']: next_ = { 'method': 'GET', 'path': '/s/subjects/{subject_id}/units'.format( subject_id=subject['entity_id']), } set_learning_context(current_user, card=None, unit=None, next=next_) # If we are out of units... else: next_ = { 'method': 'GET', 'path': '/s/subjects/{subject_id}/tree'.format( subject_id=subject['entity_id']), } set_learning_context(current_user, card=None, unit=None, next=next_) # If we are still reviewing, learning or diagnosing this unit... else: next_card = choose_card(db_conn, current_user, unit) if next_card: next_ = { 'method': 'GET', 'path': '/s/cards/{card_id}/learn'.format( card_id=next_card['entity_id']), } set_learning_context(current_user, card=next_card.data, next=next_) else: next_ = {} set_learning_context(current_user, next=next_) return 200, { 'response': deliver_response(response), 'feedback': feedback, 'next': next_, }