def view_judge(request, judge_id): judge_id = int(judge_id) try: judge = Judge.objects.get(pk=judge_id) except Judge.DoesNotExist: return redirect_and_flash_error(request, "Judge not found") if request.method == 'POST': form = JudgeForm(request.POST,instance=judge) if form.is_valid(): try: form.save() except ValueError: return redirect_and_flash_error(request, "Judge information cannot be validated") return redirect_and_flash_success(request, "Judge {} updated successfully".format(form.cleaned_data['name'])) else: form = JudgeForm(instance=judge) base_url = '/judge/'+str(judge_id)+'/' scratch_url = base_url + 'scratches/view/' delete_url = base_url + 'delete/' links = [(scratch_url, 'Scratches for {}'.format(judge.name))] return render(request, 'common/data_entry.html', {'form': form, 'links': links, 'title': 'Viewing Judge: {}'.format(judge.name)})
def import_row(self, row, row_number): judge_name = row[0] judge_rank = row[1] try: judge_rank = round(float(judge_rank), 2) except ValueError: self.error("Judge rank is not a number", row) schools = [] for school_name in row[2:]: school_query = School.objects.filter(name__iexact=school_name) if school_query.exists(): school = school_query.first() else: school = School(name=school_name) try: self.create(school) except Exception: self.error("Invalid school '%s'" % school_name, row_number) schools.append(school.id) data = {"name": judge_name, "rank": judge_rank, "schools": schools} form = JudgeForm(data=data) if form.is_valid(): self.create(form) else: for _field, error_msgs in form.errors.items(): for error_msg in error_msgs: self.error("%s - %s" % (judge_name, error_msg), row_number)
def enter_judge(request): if request.method == 'POST': form = JudgeForm(request.POST) if form.is_valid(): try: form.save() except ValueError: cd = form.cleaned_data return redirect_and_flash_error(request, "Judge cannot be validated") return redirect_and_flash_success(request, "Judge {} created successfully".format(form.cleaned_data['name']), path="/") else: form = JudgeForm(first_entry=True) return render(request, 'common/data_entry.html', {'form': form, 'title': "Create Judge"})
def view_judge(request, judge_id): judge_id = int(judge_id) try: judge = Judge.objects.get(pk=judge_id) except Judge.DoesNotExist: return redirect_and_flash_error(request, "Judge not found") if request.method == "POST": form = JudgeForm(request.POST, instance=judge) if form.is_valid(): try: form.save() except ValueError: return redirect_and_flash_error( request, "Judge information cannot be validated") return redirect_and_flash_success( request, "Judge {} updated successfully".format( form.cleaned_data["name"])) else: form = JudgeForm(instance=judge) base_url = "/judge/" + str(judge_id) + "/" scratch_url = base_url + "scratches/view/" links = [(scratch_url, "Scratches for {}".format(judge.name))] return render( request, "common/data_entry.html", { "form": form, "links": links, "title": "Viewing Judge: {}".format(judge.name) })
def enter_judge(request): if request.method == "POST": form = JudgeForm(request.POST) if form.is_valid(): try: form.save() except ValueError: return redirect_and_flash_error(request, "Judge cannot be validated") return redirect_and_flash_success( request, "Judge {} created successfully".format( form.cleaned_data["name"]), path="/") else: form = JudgeForm(first_entry=True) return render(request, "common/data_entry.html", { "form": form, "title": "Create Judge" })
def import_judges(fileToImport): try: sh = xlrd.open_workbook( filename=None, file_contents=fileToImport.read()).sheet_by_index(0) except: return [ "ERROR: Please upload an .xlsx file. This file type is not compatible" ] judge_entries = 0 found_end = False errors = [] while not found_end: try: sh.cell(judge_entries, 0).value judge_entries += 1 except IndexError: found_end = True # Verify sheet has required number of columns try: sh.cell(0, 1).value except IndexError: errors.append("ERROR: Insufficient Columns in sheet. No Data Read") return errors for i in range(1, judge_entries): # 0 name # 1 rank # 2 phone # 3 provider # 4+ schools is_duplicate = False # Load and validate Judge's Name judge_name = sh.cell(i, 0).value if Judge.objects.filter(name=judge_name).first() is not None: errors.append(judge_name + ': duplicate judge name, overwriting') is_duplicate = True # Load and validate judge_rank judge_rank = sh.cell(i, 1).value try: judge_rank = Decimal(judge_rank) except TypeError or ValueError: errors.append(judge_name + ": Rank is not a number") continue if judge_rank > 100 or judge_rank < 0: errors.append(judge_name + ": Rank should be between 0-100") continue judge_phone = sh.cell(i, 2).value judge_provider = sh.cell(i, 3).value # iterate through schools until none are left cur_col = 4 schools = [] while True: try: judge_school = sh.cell(i, cur_col).value # If other judges have more schools but this judge doesn't, we get an empty string # If blank, keep iterating in case user has a random blank column for some reason if judge_school != '': try: # Get id from the name because JudgeForm requires we use id s = School.objects.get(name=judge_school).id schools.append(s) except IndexError: break except: try: s = School(name=judge_school) s.save() schools.append(s.id) except: errors.append(judge_name + ': Invalid School') continue except IndexError: break cur_col += 1 if is_duplicate: # overwrite the parameters for that judge judge = Judge.objects.get(name=judge_name) judge.rank = judge_rank judge.phone = judge_phone judge.provider = judge_provider judge.school = schools judge.save() else: form = JudgeForm( data={ 'name': judge_name, 'rank': judge_rank, 'phone': judge_phone, 'provider': judge_provider, 'schools': schools }) if form.is_valid(): form.save() else: print form.errors # print errors of console so they can actually be debugged errors.append(judge_name + ": Form invalid. Check inputs.") return errors
def import_judges(fileToImport): try: sh = xlrd.open_workbook( filename=None, file_contents=fileToImport.read()).sheet_by_index(0) except: return [ "ERROR: Please upload an .xlsx file. This filetype is not compatible" ] num_judges = 0 found_end = False judge_errors = [] while found_end == False: try: sh.cell(num_judges, 0).value num_judges += 1 except IndexError: found_end = True #Verify sheet has required number of columns try: sh.cell(0, 1).value except: team_errors.append( "ERROR: Insufficient Columns in sheet. No Data Read") return team_errors for i in range(1, num_judges): #Load and validate Judge's Name judge_name = sh.cell(i, 0).value try: Judge.objects.get(name=judge_name) judge_errors.append(judge_name + ": Duplicate Judge Name") continue except: pass #Load and validate judge_rank judge_rank = sh.cell(i, 1).value try: judge_rank = round(float(judge_rank), 2) except: judge_errors.append(judge_name + ": Rank not number") continue if judge_rank > 100 or judge_rank < 0: judge_errors.append(judge_name + ": Rank should be between 0-100") continue #iterate through schools until none are left cur_col = 2 schools = [] while (True): try: judge_school = sh.cell(i, cur_col).value #If other judges have more schools but this judge doesn't, we get an empty string #If blank, keep iterating in case user has a random blank column for some reason if (judge_school != ''): try: #Get id from the name because JudgeForm requires we use id s = School.objects.get(name__iexact=judge_school).id schools.append(s) except IndexError: break except: try: s = School(name=judge_school) s.save() schools.append(s.id) except: judge_errors.append(judge_name + ': Invalid School') continue except IndexError: break cur_col += 1 data = {'name': judge_name, 'rank': judge_rank, 'schools': schools} form = JudgeForm(data=data) if (form.is_valid()): form.save() else: error_messages = sum([error[1] for error in form.errors.items()], []) error_string = ', '.join(error_messages) judge_errors.append("%s: %s" % (judge_name, error_string)) return judge_errors
def import_judges(fileToImport): try: sh = xlrd.open_workbook(filename=None, file_contents=fileToImport.read()).sheet_by_index(0) except: return ["ERROR: Please upload an .xlsx file. This filetype is not compatible"] num_judges = 0 found_end = False judge_errors = [] while found_end == False: try: sh.cell(num_judges, 0).value num_judges += 1 except IndexError: found_end = True #Verify sheet has required number of columns try: sh.cell(0, 1).value except: team_errors.append("ERROR: Insufficient Columns in sheet. No Data Read") return team_errors for i in range(1, num_judges): #Load and validate Judge's Name judge_name = sh.cell(i, 0).value try: Judge.objects.get(name=judge_name) judge_errors.append(judge_name + ": Duplicate Judge Name") continue except: pass #Load and validate judge_rank judge_rank = sh.cell(i, 1).value try: judge_rank = round(float(judge_rank), 2) except: judge_errors.append(judge_name + ": Rank not number") continue if judge_rank > 100 or judge_rank < 0: judge_errors.append(judge_name + ": Rank should be between 0-100") continue #iterate through schools until none are left cur_col = 2 schools = [] while(True): try: judge_school = sh.cell(i, cur_col).value #If other judges have more schools but this judge doesn't, we get an empty string #If blank, keep iterating in case user has a random blank column for some reason if (judge_school != ''): try: #Get id from the name because JudgeForm requires we use id s = School.objects.get(name__iexact=judge_school).id schools.append(s) except IndexError: break except: try: s = School(name=judge_school) s.save() schools.append(s.id) except: judge_errors.append(judge_name + ': Invalid School') continue except IndexError: break cur_col += 1 data = {'name': judge_name, 'rank': judge_rank, 'schools': schools} form = JudgeForm(data=data) if (form.is_valid()): form.save() else: error_messages = sum([ error[1] for error in list(form.errors.items()) ], []) error_string = ', '.join(error_messages) judge_errors.append("%s: %s" % (judge_name, error_string)) return judge_errors