def build_choice(): event_types = EventType.objects.all() choices = Choices() choices.choices_clear() for event_type in event_types: dub = (event_type.abbr,event_type.label) choices.add_choices(dub) # print choices.choices return choices
def build_users(org): users = User.objects.filter(org=org) user_choices = Choices() user_choices.choices_clear() i=0 for user in users: i += 1 dub = (str(i),user.email) user_choices.add_choices(dub) # print user_choices.choices return user_choices
def post(self): data = request.get_json() quiz_id = data['url'].split('/')[-1] # split "/quiz/10" by "/" and get "10". answers_correct_row = [quiz for quiz in Choices.select(Choices.id).dicts().join(QuizQuestions).group_by() .where((Choices.is_correct == True) & (QuizQuestions.quiz_id == quiz_id))] answers_correct_db = {id_['id'] for id_ in answers_correct_row} answers_user = {int(v) for k, v in data.items() if k != 'url'} correct_answers_count = len(answers_correct_db) - len(answers_user.difference(answers_correct_db)) percent = (100 * correct_answers_count)//len(answers_user) return jsonify({'score': percent})
def create(): if request.method == 'POST': username = current_user.username choice1 = request.form['choice1'] choice2 = request.form['choice2'] choice3 = request.form['choice3'] choice4 = request.form['choice4'] selections = Choices(username, choice1, choice2, choice3, choice4) db.session.add(selections) db.session.commit() return redirect(url_for('bracketcreated')) return render_template("create.html")
def insert_data_to_db(loaded_json): for pizza in loaded_json: pizza_to_insert = Pizza( title=pizza['title'], description=pizza['description'] ) for choice in pizza['choices']: choices_to_insert = Choices(choice_title=choice['title'], choice_price=choice['price'], pizza=pizza_to_insert) db.session.add(choices_to_insert) db.session.add(pizza_to_insert) db.session.commit()
def build_choice(): event_types = EventType.objects.all() choices = Choices() choices.choices_clear() for event_type in event_types: dub = (event_type.abbr, event_type.label) choices.add_choices(dub) # print choices.choices return choices
def build_users(org): users = User.objects.filter(org=org) user_choices = Choices() user_choices.choices_clear() i = 0 for user in users: i += 1 dub = (str(i), user.email) user_choices.add_choices(dub) # print user_choices.choices return user_choices
class TreeAddForm(forms.Form): edit_address_street = forms.CharField(max_length=200, required=True, initial="Enter an Address or Intersection") geocode_address = forms.CharField(widget=forms.HiddenInput, max_length=255, required=True) edit_address_city = forms.CharField(max_length=100, required=False, initial="Enter a City") edit_latitude = forms.CharField(max_length=100, required=False, initial="Latitude") edit_longitude = forms.CharField(max_length=100, required=False, initial="Longitude") edit_address_zip = USZipCodeField(widget=forms.HiddenInput, required=False) lat = forms.FloatField(widget=forms.HiddenInput,required=True) lon = forms.FloatField(widget=forms.HiddenInput,required=True) species_name = forms.CharField(required=False, initial="Enter a Species Name") species_id = forms.CharField(widget=forms.HiddenInput, required=False) dbh = forms.FloatField(required=False, label="Trunk size") crown_width = forms.FloatField(required=False, label="Crown Diameter") dbh_type = forms.ChoiceField(required=False, widget=forms.RadioSelect, choices=[('diameter', 'Diameter'), ('circumference', 'Circumference')]) height = forms.FloatField(required=False, label="Tree height") canopy_height = forms.IntegerField(required=False) plot_width = forms.ChoiceField(required=False, label="Enter the planting site length", choices=[('1','1'),('2','2'),('3','3'),('4','4'),('5','5'),('6','6'),('7','7'),('8','8'),('9','9'),('10','10'),('11','11'),('12','12'),('13','13'),('14','14'),('15','15'),('99','15+')]) plot_length = forms.ChoiceField(required=False, label="Enter the planting site width", choices=[('1','1'),('2','2'),('3','3'),('4','4'),('5','5'),('6','6'),('7','7'),('8','8'),('9','9'),('10','10'),('11','11'),('12','12'),('13','13'),('14','14'),('15','15'),('99','15+')]) plot_width_in = forms.ChoiceField(required=False, choices=[('1','1'),('2','2'),('3','3'),('4','4'),('5','5'),('6','6'),('7','7'),('8','8'),('9','9'),('10','10'),('11','11')]) plot_length_in = forms.ChoiceField(required=False, choices=[('1','1'),('2','2'),('3','3'),('4','4'),('5','5'),('6','6'),('7','7'),('8','8'),('9','9'),('10','10'),('11','11')]) plot_type = forms.TypedChoiceField(choices=Choices().get_field_choices('plot_type'), required=False) power_lines = forms.TypedChoiceField(choices=Choices().get_field_choices('powerline_conflict_potential'), required=False) sidewalk_damage = forms.ChoiceField(choices=Choices().get_field_choices('sidewalk_damage'), required=False) condition = forms.ChoiceField(choices=Choices().get_field_choices('condition'), required=False) canopy_condition = forms.ChoiceField(choices=Choices().get_field_choices('canopy_condition'), required=False) fauna = forms.MultipleChoiceField(choices=Choices().get_field_choices('fauna'), required=False, widget=forms.CheckboxSelectMultiple) target = forms.ChoiceField(required=False, choices=[('add', 'I want to add another tree'), ('edit', 'I\'m done!')], initial='edit', widget=forms.RadioSelect) def __init__(self, *args, **kwargs): super(TreeAddForm, self).__init__(*args, **kwargs) if not self.fields['plot_type'].choices[0][0] == '': self.fields['plot_type'].choices.insert(0, ('','Select One...' ) ) self.fields['power_lines'].choices.insert(0, ('','Select One...' ) ) self.fields['sidewalk_damage'].choices.insert(0, ('','Select One...' ) ) self.fields['condition'].choices.insert(0, ('','Select One...' ) ) self.fields['canopy_condition'].choices.insert(0, ('','Select One...' ) ) self.fields['plot_width'].choices.insert(0, ('','Select Feet...' ) ) self.fields['plot_width_in'].choices.insert(0, ('','Select Inches...' ) ) self.fields['plot_length'].choices.insert(0, ('','Select Feet...' ) ) self.fields['plot_length_in'].choices.insert(0, ('','Select Inches...' ) ) def clean(self): cleaned_data = self.cleaned_data height = cleaned_data.get('height') canopy_height = cleaned_data.get('canopy_height') try: point = Point(cleaned_data.get('lon'),cleaned_data.get('lat'),srid=4326) nbhood = Neighborhood.objects.filter(geometry__contains=point) except: raise forms.ValidationError("This tree is missing a location. Enter an address in Step 1 and update the map to add a location for this tree.") if nbhood.count() < 1: raise forms.ValidationError("The selected location is outside our area. Please specify a location within the " + settings.REGION_NAME + " region.") if height > 300: raise forms.ValidationError("Height is too large.") if canopy_height > 300: raise forms.ValidationError("Canopy height is too large.") if canopy_height and height and canopy_height > height: raise forms.ValidationError("Canopy height cannot be larger than tree height.") return cleaned_data def save(self,request): from django.contrib.gis.geos import Point plot = Plot() plot.data_owner = request.user address = self.cleaned_data.get('edit_address_street') if address: plot.address_street = address plot.geocoded_address = address city = self.cleaned_data.get('edit_address_city') geo_address = self.cleaned_data.get('geocode_address') if geo_address: plot.geocoded_address = geo_address if city: plot.address_city = city zip_ = self.cleaned_data.get('edit_address_zip') if zip_: plot.address_zip = zip_ plot_width = self.cleaned_data.get('plot_width') plot_width_in = self.cleaned_data.get('plot_width_in') if plot_width: plot.width = float(plot_width) if plot_width_in: plot.width = plot.width + (float(plot_width_in) / 12) plot_length = self.cleaned_data.get('plot_length') plot_length_in = self.cleaned_data.get('plot_length_in') if plot_length: plot.length = float(plot_length) if plot_length_in: plot.length = plot.length + (float(plot_length_in) / 12) plot_type = self.cleaned_data.get('plot_type') if plot_type: plot.type = plot_type power_lines = self.cleaned_data.get('power_lines') if power_lines != "": plot.powerline_conflict_potential = power_lines sidewalk_damage = self.cleaned_data.get('sidewalk_damage') if sidewalk_damage: plot.sidewalk_damage = sidewalk_damage import_event, created = ImportEvent.objects.get_or_create(file_name='site_add',) plot.import_event = import_event pnt = Point(self.cleaned_data.get('lon'),self.cleaned_data.get('lat'),srid=4326) plot.geometry = pnt plot.last_updated_by = request.user plot.save() species = self.cleaned_data.get('species_id') height = self.cleaned_data.get('height') canopy_height = self.cleaned_data.get('canopy_height') dbh = self.cleaned_data.get('dbh') crown_width = self.cleaned_data.get('crown_width') dbh_type = self.cleaned_data.get('dbh_type') condition = self.cleaned_data.get('condition') canopy_condition = self.cleaned_data.get('canopy_condition') new_tree = Tree() if species: spp = Species.objects.filter(symbol=species) if spp: new_tree.species=spp[0] if crown_width: new_tree.crown_width = crown_width if height: new_tree.height = height if canopy_height: new_tree.canopy_height = canopy_height if dbh: if dbh_type == 'circumference': dbh = dbh / math.pi new_tree.dbh = dbh if condition: new_tree.condition = condition if canopy_condition: new_tree.canopy_condition = canopy_condition new_tree.import_event = import_event new_tree.last_updated_by = request.user new_tree.plot = plot new_tree.save() #print new_tree.__dict__ fauna = self.cleaned_data.get('fauna') if fauna: print 'fauna',fauna fauna_dict = dict(Choices().get_field_choices('fauna')) for f in fauna: fauna = TreeFauna() fauna.reported_by = request.user fauna.key = f fauna.value = datetime.now() fauna.fauna = fauna_dict[f] # or random string fauna.tree = new_tree fauna.save() return plot
def save(self,request): from django.contrib.gis.geos import Point plot = Plot() plot.data_owner = request.user address = self.cleaned_data.get('edit_address_street') if address: plot.address_street = address plot.geocoded_address = address city = self.cleaned_data.get('edit_address_city') geo_address = self.cleaned_data.get('geocode_address') if geo_address: plot.geocoded_address = geo_address if city: plot.address_city = city zip_ = self.cleaned_data.get('edit_address_zip') if zip_: plot.address_zip = zip_ plot_width = self.cleaned_data.get('plot_width') plot_width_in = self.cleaned_data.get('plot_width_in') if plot_width: plot.width = float(plot_width) if plot_width_in: plot.width = plot.width + (float(plot_width_in) / 12) plot_length = self.cleaned_data.get('plot_length') plot_length_in = self.cleaned_data.get('plot_length_in') if plot_length: plot.length = float(plot_length) if plot_length_in: plot.length = plot.length + (float(plot_length_in) / 12) plot_type = self.cleaned_data.get('plot_type') if plot_type: plot.type = plot_type power_lines = self.cleaned_data.get('power_lines') if power_lines != "": plot.powerline_conflict_potential = power_lines sidewalk_damage = self.cleaned_data.get('sidewalk_damage') if sidewalk_damage: plot.sidewalk_damage = sidewalk_damage import_event, created = ImportEvent.objects.get_or_create(file_name='site_add',) plot.import_event = import_event pnt = Point(self.cleaned_data.get('lon'),self.cleaned_data.get('lat'),srid=4326) plot.geometry = pnt plot.last_updated_by = request.user plot.save() species = self.cleaned_data.get('species_id') height = self.cleaned_data.get('height') canopy_height = self.cleaned_data.get('canopy_height') dbh = self.cleaned_data.get('dbh') crown_width = self.cleaned_data.get('crown_width') dbh_type = self.cleaned_data.get('dbh_type') condition = self.cleaned_data.get('condition') canopy_condition = self.cleaned_data.get('canopy_condition') new_tree = Tree() if species: spp = Species.objects.filter(symbol=species) if spp: new_tree.species=spp[0] if crown_width: new_tree.crown_width = crown_width if height: new_tree.height = height if canopy_height: new_tree.canopy_height = canopy_height if dbh: if dbh_type == 'circumference': dbh = dbh / math.pi new_tree.dbh = dbh if condition: new_tree.condition = condition if canopy_condition: new_tree.canopy_condition = canopy_condition new_tree.import_event = import_event new_tree.last_updated_by = request.user new_tree.plot = plot new_tree.save() #print new_tree.__dict__ fauna = self.cleaned_data.get('fauna') if fauna: print 'fauna',fauna fauna_dict = dict(Choices().get_field_choices('fauna')) for f in fauna: fauna = TreeFauna() fauna.reported_by = request.user fauna.key = f fauna.value = datetime.now() fauna.fauna = fauna_dict[f] # or random string fauna.tree = new_tree fauna.save() return plot
def post(self): data = request.get_json() if data['quiz_name'] == '' or data['quiz_description'] == '': return jsonify({'message': 'no data'}) else: quiz_name = data['quiz_name'] quiz_description = data['quiz_description'] QuizNames.add(quiz_name=quiz_name, description=quiz_description) quiz_id = QuizNames.get(QuizNames.quiz_name == quiz_name) for key, value in data.items(): if 'question' in key: QuizQuestions.add(quiz_id=quiz_id, question_text=value['question-text']) question = QuizQuestions.get( QuizQuestions.question_text == value['question-text']) Choices.add(question_id=question.id, choice_text=value['choice']['answer-1']) Choices.add(question_id=question.id, choice_text=value['choice']['answer-2']) Choices.add(question_id=question.id, choice_text=value['choice']['answer-3']) Choices.add(question_id=question.id, choice_text=value['choice']['answer-4']) correct_answer_json = value['choice'][ 'is-correct'] # get correct answer name correct_answer_db = Choices.get( Choices.choice_text == value['choice'] [correct_answer_json] ) # get answer_id by correct_answer value. Choices.update({ Choices.is_correct: True }).where(Choices.id == correct_answer_db.id).execute( ) # update selected answer with "is_correct" flag. return jsonify(data)
class TreeAddForm(forms.Form): edit_address_street = forms.CharField( max_length=200, required=True, initial="Enter an Address or Intersection") geocode_address = forms.CharField(widget=forms.HiddenInput, max_length=255, required=True) edit_address_city = forms.CharField(max_length=200, required=False, initial="Enter a City") edit_address_zip = USZipCodeField(widget=forms.HiddenInput, required=False) lat = forms.FloatField(widget=forms.HiddenInput, required=True) lon = forms.FloatField(widget=forms.HiddenInput, required=True) initial_map_location = forms.CharField(max_length=200, required=False, widget=forms.HiddenInput) species_name = forms.CharField(required=False, initial="Enter a Species Name") species_other1 = forms.CharField(required=False, initial="Genus") species_other2 = forms.CharField(required=False, initial="species") species_id = forms.CharField(widget=forms.HiddenInput, required=False) dbh = forms.FloatField(required=False, label="Trunk size") dbh_type = forms.ChoiceField(required=False, widget=forms.RadioSelect, choices=[('diameter', 'Diameter'), ('circumference', 'Circumference')]) height = forms.FloatField(required=False, label="Tree height") canopy_height = forms.IntegerField(required=False) plot_width = forms.ChoiceField(required=False, choices=[('1', '1'), ('2', '2'), ('3', '3'), ('4', '4'), ('5', '5'), ('6', '6'), ('7', '7'), ('8', '8'), ('9', '9'), ('10', '10'), ('11', '11'), ('12', '12'), ('13', '13'), ('14', '14'), ('15', '15'), ('99', '15+')]) plot_length = forms.ChoiceField(required=False, choices=[('1', '1'), ('2', '2'), ('3', '3'), ('4', '4'), ('5', '5'), ('6', '6'), ('7', '7'), ('8', '8'), ('9', '9'), ('10', '10'), ('11', '11'), ('12', '12'), ('13', '13'), ('14', '14'), ('15', '15'), ('99', '15+')]) plot_width_in = forms.ChoiceField(required=False, choices=[('1', '1'), ('2', '2'), ('3', '3'), ('4', '4'), ('5', '5'), ('6', '6'), ('7', '7'), ('8', '8'), ('9', '9'), ('10', '10'), ('11', '11')]) plot_length_in = forms.ChoiceField(required=False, choices=[('1', '1'), ('2', '2'), ('3', '3'), ('4', '4'), ('5', '5'), ('6', '6'), ('7', '7'), ('8', '8'), ('9', '9'), ('10', '10'), ('11', '11')]) plot_type = forms.TypedChoiceField( choices=Choices().get_field_choices('plot_type'), required=False) power_lines = forms.TypedChoiceField( choices=Choices().get_field_choices('powerline_conflict_potential'), required=False) sidewalk_damage = forms.ChoiceField( choices=Choices().get_field_choices('sidewalk_damage'), required=False) condition = forms.ChoiceField( choices=Choices().get_field_choices('condition'), required=False) canopy_condition = forms.ChoiceField( choices=Choices().get_field_choices('canopy_condition'), required=False) target = forms.ChoiceField( required=False, choices=[('addsame', 'I want to add another tree using the same tree details'), ('add', 'I want to add another tree with new details'), ('edit', 'I\'m done!')], initial='edit', widget=forms.RadioSelect) owner_additional_id = forms.CharField(required=False) def __init__(self, *args, **kwargs): super(TreeAddForm, self).__init__(*args, **kwargs) if not self.fields['plot_type'].choices[0][0] == '': self.fields['plot_type'].choices.insert(0, ('', 'Select One...')) self.fields['power_lines'].choices.insert(0, ('', 'Select One...')) self.fields['sidewalk_damage'].choices.insert( 0, ('', 'Select One...')) self.fields['condition'].choices.insert(0, ('', 'Select One...')) self.fields['canopy_condition'].choices.insert( 0, ('', 'Select One...')) self.fields['plot_width'].choices.insert(0, ('', 'Select Feet...')) self.fields['plot_width_in'].choices.insert( 0, ('', 'Select Inches...')) self.fields['plot_length'].choices.insert(0, ('', 'Select Feet...')) self.fields['plot_length_in'].choices.insert( 0, ('', 'Select Inches...')) def clean(self): cleaned_data = self.cleaned_data height = cleaned_data.get('height') canopy_height = cleaned_data.get('canopy_height') try: point = Point(cleaned_data.get('lon'), cleaned_data.get('lat'), srid=4326) nbhood = Neighborhood.objects.filter(geometry__contains=point) except: raise forms.ValidationError( "This tree is missing a location. Enter an address in Step 1 and update the map to add a location for this tree." ) if nbhood.count() < 1: raise forms.ValidationError( "The selected location is outside our area. Please specify a location within the " + settings.REGION_NAME + " region.") if height > 300: raise forms.ValidationError("Height is too large.") if canopy_height > 300: raise forms.ValidationError("Canopy height is too large.") if canopy_height and height and canopy_height > height: raise forms.ValidationError( "Canopy height cannot be larger than tree height.") # initial_map_location is an optional field so only trigger the validation if it was specified if cleaned_data.get('initial_map_location'): initial_map_location = cleaned_data.get( 'initial_map_location').split(',') initial_point = Point(float(initial_map_location[1]), float(initial_map_location[0]), srid=4326) if point == initial_point: raise forms.ValidationError( "We need a more precise location for the tree. Please move the tree marker from the default location for this address to the specific location of the tree planting site. " ) return cleaned_data def save(self, request): from django.contrib.gis.geos import Point plot = Plot() plot.data_owner = request.user address = self.cleaned_data.get('edit_address_street') if address: plot.address_street = address plot.geocoded_address = address city = self.cleaned_data.get('edit_address_city') geo_address = self.cleaned_data.get('geocode_address') if geo_address: plot.geocoded_address = geo_address if city: plot.address_city = city zip_ = self.cleaned_data.get('edit_address_zip') if zip_: plot.address_zip = zip_ plot_width = self.cleaned_data.get('plot_width') plot_width_in = self.cleaned_data.get('plot_width_in') if plot_width: plot.width = float(plot_width) if plot_width_in: plot.width = plot.width + (float(plot_width_in) / 12) plot_length = self.cleaned_data.get('plot_length') plot_length_in = self.cleaned_data.get('plot_length_in') if plot_length: plot.length = float(plot_length) if plot_length_in: plot.length = plot.length + (float(plot_length_in) / 12) plot_type = self.cleaned_data.get('plot_type') if plot_type: plot.type = plot_type power_lines = self.cleaned_data.get('power_lines') if power_lines != "": plot.powerline_conflict_potential = power_lines sidewalk_damage = self.cleaned_data.get('sidewalk_damage') if sidewalk_damage: plot.sidewalk_damage = sidewalk_damage owner_additional_id = self.cleaned_data.get('owner_additional_id') if owner_additional_id: plot.owner_additional_id = owner_additional_id import_event, created = ImportEvent.objects.get_or_create( file_name='site_add', ) plot.import_event = import_event pnt = Point(self.cleaned_data.get('lon'), self.cleaned_data.get('lat'), srid=4326) plot.geometry = pnt plot.last_updated_by = request.user plot.save() species = self.cleaned_data.get('species_id') species_other1 = self.cleaned_data.get('species_other1') species_other2 = self.cleaned_data.get('species_other2') height = self.cleaned_data.get('height') canopy_height = self.cleaned_data.get('canopy_height') dbh = self.cleaned_data.get('dbh') dbh_type = self.cleaned_data.get('dbh_type') condition = self.cleaned_data.get('condition') canopy_condition = self.cleaned_data.get('canopy_condition') #TODO: fix this if species or height or canopy_height or dbh or condition or canopy_condition: # print species, height, canopy_height, dbh, condition, canopy_condition if species: spp = Species.objects.filter(id=species) if spp: new_tree = Tree(species=spp[0]) else: new_tree = Tree() else: new_tree = Tree() if species_other1: new_tree.species_other1 = species_other1 if species_other2: new_tree.species_other2 = species_other2 if height: new_tree.height = height if canopy_height: new_tree.canopy_height = canopy_height if dbh: if dbh_type == 'circumference': new_tree.dbh = dbh / math.pi else: new_tree.dbh = dbh if condition: new_tree.condition = condition if canopy_condition: new_tree.canopy_condition = canopy_condition new_tree.import_event = import_event new_tree.last_updated_by = request.user new_tree.plot = plot new_tree.save() #print new_tree.__dict__ return plot