def view_feedback(request, name): me = Student.from_request(request) # First, try to get the challenge. try: challenge = Challenge.objects.get(slug=name) except exceptions.ObjectDoesNotExist: raise Http404() # Now get all of the SOSses related to this user and challenge. feedback = SOS.objects.filter(challenge=challenge, student=me) # Mark feedback as helpful or not. if "helpful" in request.GET or "unhelpful" in request.GET: helpful = True if "unhelpful" in request.GET: helpful = False f_id = request.GET["helpful" if helpful else "unhelpful"] if f_id.isdigit(): try: f = Feedback.objects.get(id=int(f_id)) if f.sos in feedback and f.helpful is None: f.helpful = helpful f.save() LogEntry.log(request, "Marked as %shelpful"%("" if helpful else "un")) except exceptions.ObjectDoesNotExist: LogEntry.log(request, "Feedback voting naughtiness.") return render(request, "feedback.html", {'challenge': challenge, 'feedback': feedback})
def view_feedback(request, name): me = Student.from_request(request) if "alerts" not in request.session: request.session["alerts"] = [] if not me: request.session["alerts"].append(("alert-error","Please sign in first.")) return redirect("sign-in") # First, try to get the challenge. try: challenge = Challenge.objects.get(slug=name) except exceptions.ObjectDoesNotExist: raise Http404() # Now get all of the SOSses related to this user and challenge. feedback = SOS.objects.filter(challenge=challenge, student=me) # Mark feedback as helpful or not. if "helpful" in request.GET or "unhelpful" in request.GET: helpful = True if "unhelpful" in request.GET: helpful = False f_id = request.GET["helpful" if helpful else "unhelpful"] if f_id.isdigit(): f = Feedback.objects.get(id=int(f_id)) if f.sos in feedback and f.helpful is None: f.helpful = helpful f.save() if helpful: f.author.award_xp(50) me.award_xp(5) LogEntry.log(request, "Marked as %shelpful"%("" if helpful else "un")) return render(request, "feedback.html", {'challenge': challenge, 'feedback': feedback})
def do_asm_challenge(request, student, challenge): code = request.POST.get("code") if "code" in request.POST else "" # Save this in the database whether it compiles or not. CR = ChallengeResponse() CR.student = student CR.challenge = challenge CR.code = code CR.is_correct = False CR.save() # Only try to autograde if the program compiles. if assembler.assemble_and_store(request, "challenge", code, challenge.pattern, challenge.preamble, challenge.postamble): completed = student in challenge.completed_by.all() results = autograde.grade(challenge, student, code, completed) # Save its correctness in the database. CR.is_correct = True if results else False if results: CR.rom_size, CR.runtime = results CR.save() # Award XP if the program is correct. if results and not completed: challenge.completed_by.add(student) challenge.save() student.award_xp(challenge.xp) student.save() request.session['alerts'].append( ('alert-success', '''Congratulations! You completed this challenge and earned %d XP.''' % challenge.xp)) LogEntry.log(request, "Completed %s" % challenge.slug) elif results: request.session['alerts'].append( ('alert-success', '''This solution was %d bytes in size and executed %d lines of code.''' % results)) LogEntry.log( request, "%s: %d, %d" % (challenge.slug, results[0], results[1])) # Is there an SOS involved? if "sos" in request.POST and "help" in request.POST: for s in SOS.objects.filter(challenge=challenge, student=student): s.active = False s.save() s = SOS() s.challenge = challenge s.submission = CR s.content = request.POST["help"] s.student = student s.save()
def view_thread(request, name, thread): me = Student.from_request(request) if "alerts" not in request.session: request.session["alerts"] = [] if not me: request.session["alerts"].append(("alert-error","Please sign in first.")) return redirect("sign-in") # First, try to get the challenge, then thread. try: board = DiscussionBoard.objects.get(slug=name) topic = DiscussionTopic.objects.get(id=thread, board=board) except exceptions.ObjectDoesNotExist: raise Http404() if not me.ta and (topic.hidden or board.restricted > me.level): raise Http404() # If this is a POST, we are either replying to someone or we are voting. # Manage permissions respectively and redirect. if request.method == "POST": if "thread" in request.POST and (me.level >= board.wrestricted or me.ta): p = DiscussionPost() p.topic = topic p.author = me p.content = str(request.POST.get("content")) p.save() topic.save() elif "upvote" in request.POST or "downvote" in request.POST: upvote = True if "downvote" in request.POST: upvote = False p_id = request.POST["upvote" if upvote else "downvote"] if p_id.isdigit() and (me.modpoints > 0 or me.ta): p = DiscussionPost.objects.get(id=int(p_id)) if upvote: p.upvotes += 1 else: p.downvotes += 1 request.session["alerts"].append(("alert-success","Post %s."%("upvoted" if upvote else "downvoted"))) LogEntry.log(request, "Thread %s"%("upvoted" if upvote else "downvoted")) p.save() me.modpoints -= 1 me.award_xp(1) me.save() return redirect( "thread", name=board.slug, thread=topic.id ) # Get all of the posts. Start on the last page by default. pagination = 20 posts = DiscussionPost.objects.filter(hidden=False, topic=topic) pages = (len(posts)/pagination)+1 page = pages-1 if "page" in request.GET and request.GET["page"].isdigit(): page = max(0,int(request.GET["page"])-1) return render( request, "forum_thread.html", {'board': board, 'topic': topic, 'student': me, 'posts': posts[pagination*page:pagination*(page+1)], 'alerts': request.session.pop('alerts', []), 'page': page+1, 'pages': pages })
def do_asm_challenge(request, student, challenge): code = request.POST.get("code") if "code" in request.POST else "" # Save this in the database whether it compiles or not. CR = ChallengeResponse() CR.student = student CR.challenge = challenge CR.code = code CR.is_correct = False CR.save() # Only try to autograde if the program compiles. if assembler.assemble_and_store(request, "challenge", code, challenge.pattern, challenge.preamble, challenge.postamble): completed = student in challenge.completed_by.all() results = autograde.grade( challenge, student, code, completed ) # Save its correctness in the database. CR.is_correct = True if results else False if results: CR.rom_size, CR.runtime = results CR.save() # Award XP if the program is correct. if results and not completed: challenge.completed_by.add(student) challenge.save() student.award_xp(challenge.xp) student.save() request.session['alerts'].append(('alert-success', '''Congratulations! You completed this challenge and earned %d XP.'''%challenge.xp)) LogEntry.log(request, "Completed %s"%challenge.slug) elif results: request.session['alerts'].append(('alert-success', '''This solution was %d bytes in size and executed %d lines of code.'''%results)) LogEntry.log(request, "%s: %d, %d"%(challenge.slug,results[0],results[1])) # Is there an SOS involved? if "sos" in request.POST and "help" in request.POST: for s in SOS.objects.filter(challenge=challenge, student=student): s.active = False s.save() s = SOS() s.challenge = challenge s.submission = CR s.content = request.POST["help"] s.student = student s.save()
def view_page(request, page=None): if page is None: return redirect( "help", page="index" ) if page.lower() != page: return redirect( "help", page.lower() ) try: p = Page.objects.get(name=page) LogEntry.log(request) return render(request, "help_page.html", {'content': p.content, 'title': p.name, 'alerts': request.session.pop('alerts', [])}) except exceptions.ObjectDoesNotExist: return HttpResponseRedirect(reverse("search")+"?query="+page )
def view_page(request, page=None): if page is None: return redirect("help", page="index") if page.lower() != page: return redirect("help", page.lower()) try: p = Page.objects.get(name=page) LogEntry.log(request) return render( request, "help_page.html", { 'content': p.content, 'title': p.name, 'alerts': request.session.pop('alerts', []) }) except exceptions.ObjectDoesNotExist: raise Http404()
def emit(self, record): try: from students.models import LogEntry import datetime time = record.asctime[:-4] + '.' + record.asctime[-3:] time2 = datetime.datetime.strptime(record.asctime, '%Y-%m-%d %H:%M:%S,%f') logentry = LogEntry(level=record.levelname, asctime=time2, module=record.module, message=record.message) logentry.save() except Exception as e: print e
def search(request): me = Student.from_request(request) LogEntry.log(request, "search for %s"%request) if "query" not in request.GET: return redirect("index") query = request.GET["query"] # Filter! pages = Page.objects.filter(content__icontains=query) results = [] for p in pages: i = p.content.lower().index(query.lower()) results.append( (p.name,"..."+p.content[max(0,i-50):min(len(p.content),i+50)]+"...") ) # TODO search through forum posts return render(request, "search.html", {'results': results, 'query': query, 'alerts': request.session.pop('alerts', []) })
def search(request): query = "" if "query" in request.GET: query = request.GET["query"] # Go through the text and find a digest that will allow the user to # find relevant data. pages = Page.objects.filter(content__icontains=query) results = [] for p in pages: i = p.content.index(query) results.append( (p.name,"..."+p.content[max(0,i-50):min(len(p.content),i+50)]+"...") ) # TODO search through forum posts and games too LogEntry.log(request, "search") return render(request, "search.html", {'results': results, 'query': query, 'alerts': request.session.pop('alerts', []) })
def view_feedback(request, name): me = Student.from_request(request) if "alerts" not in request.session: request.session["alerts"] = [] if not me: request.session["alerts"].append( ("alert-error", "Please sign in first.")) return redirect("sign-in") # First, try to get the challenge. try: challenge = Challenge.objects.get(slug=name) except exceptions.ObjectDoesNotExist: raise Http404() # Now get all of the SOSses related to this user and challenge. feedback = SOS.objects.filter(challenge=challenge, student=me) # Mark feedback as helpful or not. if "helpful" in request.GET or "unhelpful" in request.GET: helpful = True if "unhelpful" in request.GET: helpful = False f_id = request.GET["helpful" if helpful else "unhelpful"] if f_id.isdigit(): f = Feedback.objects.get(id=int(f_id)) if f.sos in feedback and f.helpful is None: f.helpful = helpful f.save() if helpful: f.author.award_xp(50) me.award_xp(5) LogEntry.log(request, "Marked as %shelpful" % ("" if helpful else "un")) return render(request, "feedback.html", { 'challenge': challenge, 'feedback': feedback })
def search(request): query = "" if "query" in request.GET: query = request.GET["query"] # Go through the text and find a digest that will allow the user to # find relevant data. pages = Page.objects.filter(content__icontains=query) results = [] for p in pages: i = p.content.index(query) results.append( (p.name, "..." + p.content[max(0, i - 50):min(len(p.content), i + 50)] + "...")) # TODO search through forum posts and games too LogEntry.log(request, "search") return render( request, "search.html", { 'results': results, 'query': query, 'alerts': request.session.pop('alerts', []) })
def view_thread(request, name, thread): me = Student.from_request(request) if "alerts" not in request.session: request.session["alerts"] = [] if not me: request.session["alerts"].append( ("alert-error", "Please sign in first.")) return redirect("sign-in") # First, try to get the challenge, then thread. try: board = DiscussionBoard.objects.get(slug=name) topic = DiscussionTopic.objects.get(id=thread, board=board) except exceptions.ObjectDoesNotExist: raise Http404() if not me.ta and (topic.hidden or board.restricted > me.level): raise Http404() # If this is a POST, we are either replying to someone or we are voting. # Manage permissions respectively and redirect. if request.method == "POST": if "thread" in request.POST and (me.level >= board.wrestricted or me.ta): p = DiscussionPost() p.topic = topic p.author = me p.content = str(request.POST.get("content")) p.save() topic.save() elif "upvote" in request.POST or "downvote" in request.POST: upvote = True if "downvote" in request.POST: upvote = False p_id = request.POST["upvote" if upvote else "downvote"] if p_id.isdigit() and (me.modpoints > 0 or me.ta): p = DiscussionPost.objects.get(id=int(p_id)) if upvote: p.upvotes += 1 else: p.downvotes += 1 request.session["alerts"].append( ("alert-success", "Post %s." % ("upvoted" if upvote else "downvoted"))) LogEntry.log( request, "Thread %s" % ("upvoted" if upvote else "downvoted")) p.save() me.modpoints -= 1 me.award_xp(1) me.save() return redirect("thread", name=board.slug, thread=topic.id) # Get all of the posts. Start on the last page by default. pagination = 20 posts = DiscussionPost.objects.filter(hidden=False, topic=topic) pages = (len(posts) / pagination) + 1 page = pages - 1 if "page" in request.GET and request.GET["page"].isdigit(): page = max(0, int(request.GET["page"]) - 1) return render( request, "forum_thread.html", { 'board': board, 'topic': topic, 'student': me, 'posts': posts[pagination * page:pagination * (page + 1)], 'alerts': request.session.pop('alerts', []), 'page': page + 1, 'pages': pages })
def view_challenge(request, name): me = Student.from_request(request) # First, try to get the challenge. try: challenge = Challenge.objects.get(slug=name) except exceptions.ObjectDoesNotExist: raise Http404() # Find the records for this submission. We need them whether we are viewing # or processing. my_size, my_speed = 0x10000, 0x10000 best_size, best_speed = 0x1000000, 0x1000000 completed = False records = CodeSubmission.objects.filter(challenge=challenge, is_correct=True).order_by('rom_size') if len(records) > 0: best_size = records[0].rom_size records = records.order_by('runtime') best_speed = records[0].runtime records = records.filter(student=me).order_by('rom_size') if len(records) > 0: my_size = records[0].rom_size records = records.order_by('runtime') my_speed = records[0].runtime completed = True # If it was a POST request, then we'll take care of that now. This takes # care of the side-effects and creates the challenge responses that will be # handled below. code = "" if request.method == "POST": try: old = CodeSubmission.objects.filter(student=me, challenge=challenge).order_by('-timestamp')[0] except: old = None code = request.POST.get("code") if "code" in request.POST else "" CR = CodeSubmission() CR.student = me CR.challenge = challenge CR.code = code CR.is_correct = False if challenge.autograde else None CR.save() # Convert the last code submission into a diff image, but only save it # if the diff is actually smaller than the full code. if old and len(SOS.objects.filter(submission=old)) == 0: old.parent = CR old_code = old.code old.code = "" for d in difflib.unified_diff( CR.code.splitlines(True), old_code.splitlines(True)): old.code += d if len(old.code) < len(old_code): old.save() # Only try to autograde if the program compiles. compiles = assembler.assemble_and_store(request, "challenge%s"%challenge.slug, code, challenge.pattern, challenge.preamble, challenge.postamble) if compiles and challenge.autograde: completed = me in challenge.completed_by.all() results = autograde.grade( challenge, me, code, completed ) # Save its correctness in the database. CR.is_correct = True if results else False if results: CR.rom_size, CR.runtime = results CR.save() # Award victory if the program is correct. if results and not completed: challenge.completed_by.add(me) challenge.save() request.session['alerts'].append(('alert-success', '''Congratulations! You completed this challenge! Can you make it more efficient?''')) LogEntry.log(request, "Completed %s"%challenge.slug) elif results: request.session['alerts'].append(('alert-success', '''This solution was %d bytes in size and executed %d instructions.'''%results)) LogEntry.log(request, "%s: %d, %d"%(challenge.slug,results[0],results[1])) # Is there an SOS involved? if "sos" in request.POST and "help" in request.POST: for s in SOS.objects.filter(challenge=challenge, student=me): s.active = False s.save() s = SOS() s.challenge = challenge s.submission = CR s.content = request.POST["help"] s.student = me s.save() # Handle the redirect. if "download" in request.POST and compiles: return redirect("rom") return redirect("challenge", challenge.slug) # Load the latest submission code to display it on the screen. subs = CodeSubmission.objects.filter(student=me, challenge=challenge).order_by('-timestamp') if len(subs) > 0: code = subs[0].code # Try to load up the best submissions by everyone. return render(request, "challenge.html", {'challenge': challenge, 'alerts': request.session.pop('alerts', []), 'code': code, 'my_size': my_size, 'my_speed': my_speed, 'best_size': best_size, 'best_speed': best_speed, 'completed': completed, 'feedback': SOS.objects.filter(challenge=challenge, student=me) } )
def view_sos(request, name): me = Student.from_request(request) # First, try to get the challenge. try: challenge = Challenge.objects.get(slug=name) except exceptions.ObjectDoesNotExist: raise Http404() # Make sure the student has completed it. if challenge not in me.challenge_set.all() and not me.ta: request.session['alerts'].append(('alert-error', '''You can't respond to SOS requests until you complete the challenge!''')) return redirect("index") if not challenge.autograde and not me.ta: request.session['alerts'].append(('alert-error', '''Only instructors and TAs can grade code submission projects.''')) return redirect("index") # If this is a POST request, we are responding. if request.method == "POST": if "id" in request.POST and request.POST["id"] == request.session.get("sos-id"): target = SOS.objects.get(id=int(request.session.pop("sos-id"))) fb = Feedback() fb.author = me fb.sos = target fb.content = str(request.POST.get("response")) fb.confident = True if request.POST.get("confident") else False fb.good = True if request.POST.get("good") else False fb.save() if request.POST.get("pass") and me.ta: target.active = False target.save() target.submission.correct = True challenge.completed_by.add( target.student ) target.submission.save() challenge.save() if (len( target.feedback_set.all() ) >= 3): target.active = False target.save() request.session["alerts"].append(("alert-success", """Thank you for helping your classmates by participating in the SOS system!""")) else: request.session["alerts"].append(("alert-error", """There was an error with your SOS submission.""")) LogEntry.log(request, "Botched SOS job.") return redirect("index") # Find all of the SOS requests for this challenge. all_sos = list( SOS.objects.filter(active=True, challenge=challenge).exclude(student=me) ) if len(all_sos) == 0: request.session["alerts"].append(("alert-error", "There are no active SOS requests for this challenge.")) return redirect("index") # Get a random challenge if you are not the TA. If you're the TA, get the # oldest one. if not me.ta: random.shuffle( all_sos ) target = None while len(all_sos) > 0 and target is None: x = all_sos.pop(0) feedbacks = Feedback.objects.filter( sos=x, author=me ) if len(feedbacks) == 0: target = x if target is None: request.session["alerts"].append(("alert-error", "You have already responded to all active SOS requests for this challenge!")) return redirect("index") # Now try to assemble the game and then load the page so that we can try to # help! request.session["sos-id"] = str(target.id) assembler.assemble_and_store(request, "challenge", target.submission.code, challenge.pattern, challenge.preamble, challenge.postamble) return render(request, "sos.html", {'challenge': challenge, 'alerts': request.session.pop('alerts', []), 'sos': target } )
def view_sos(request, name): me = Student.from_request(request) if "alerts" not in request.session: request.session["alerts"] = [] if not me: request.session["alerts"].append(("alert-error","Please sign in first.")) return redirect("sign-in") # First, try to get the challenge. try: challenge = Challenge.objects.get(slug=name) except exceptions.ObjectDoesNotExist: raise Http404() if challenge not in me.challenge_set.all(): request.session["alerts"].append(("alert-error", "You must complete this challenge before you can respond to an SOS.")) return redirect("challenge_list") # If this is a POST request, we are responding. if request.method == "POST": if "id" in request.POST and request.POST["id"] == request.session.get("sos-id"): target = SOS.objects.get(id=int(request.session.pop("sos-id"))) fb = Feedback() fb.author = me fb.sos = target fb.content = str(request.POST.get("response")) fb.confidence = True if request.POST.get("confident") else False fb.good = True if request.POST.get("good") else False fb.save() if (len( target.feedback_set.all() ) >= 3): target.active = False target.save() request.session["alerts"].append(("alert-success", """Thank you for helping your classmates by participating in the SOS system! (+10 XP)""")) me.award_xp(10) LogEntry.log(request, "SOS for %s"%(challenge.slug)) else: request.session["alerts"].append(("alert-error", """There was an error with your SOS submission.""")) LogEntry.log(request, "Botched SOS job.") return redirect("challenge_list") # Now find out if there are any SOS requests. all_sos = list( SOS.objects.filter(active=True, challenge=challenge).exclude(student=me) ) if len(all_sos) == 0: request.session["alerts"].append(("alert-error", "There are no active SOS requests for this challenge.")) return redirect("challenge_list") # Now find an SOS to respond to. Make sure that we haven't already responded # to it. random.shuffle( all_sos ) target = None while len(all_sos) > 0 and target is None: x = all_sos.pop() feedbacks = Feedback.objects.filter( sos=x, author=me ) if len(feedbacks) == 0: target = x if target is None: request.session["alerts"].append(("alert-error", "You have already responded to all active SOS requests for this challenge!")) return redirect("challenge_list") # Filtering is now over. Time to render. request.session["sos-id"] = str(target.id) if challenge.autograde: assembler.assemble_and_store(request, "challenge", target.submission.code, challenge.pattern, challenge.preamble, challenge.postamble) return render(request, "sos_asm.html", {'challenge': challenge, 'alerts': request.session.pop('alerts', []), 'sos': target } ) elif challenge.is_jam: return redirect("challenge_list") else: return redirect("challenge_list")
def webchat(request): me = Student.from_request(request) LogEntry.log(request, "chat") return render(request, "chat.html")
def saveLogEntry(e_type, e_desc): ev = LogEntry() ev.evt_type = e_type ev.evt_user = '******' ev.evt_desc = e_desc ev.save()
def webchat(request): LogEntry.log(request, "chat") return render(request, "chat.html")
def view_sos(request, name): me = Student.from_request(request) if "alerts" not in request.session: request.session["alerts"] = [] if not me: request.session["alerts"].append( ("alert-error", "Please sign in first.")) return redirect("sign-in") # First, try to get the challenge. try: challenge = Challenge.objects.get(slug=name) except exceptions.ObjectDoesNotExist: raise Http404() if challenge not in me.challenge_set.all(): request.session["alerts"].append(( "alert-error", "You must complete this challenge before you can respond to an SOS." )) return redirect("challenge_list") # If this is a POST request, we are responding. if request.method == "POST": if "id" in request.POST and request.POST["id"] == request.session.get( "sos-id"): target = SOS.objects.get(id=int(request.session.pop("sos-id"))) fb = Feedback() fb.author = me fb.sos = target fb.content = str(request.POST.get("response")) fb.confidence = True if request.POST.get("confident") else False fb.good = True if request.POST.get("good") else False fb.save() if (len(target.feedback_set.all()) >= 3): target.active = False target.save() request.session["alerts"].append( ("alert-success", """Thank you for helping your classmates by participating in the SOS system! (+10 XP)""")) me.award_xp(10) LogEntry.log(request, "SOS for %s" % (challenge.slug)) else: request.session["alerts"].append( ("alert-error", """There was an error with your SOS submission.""")) LogEntry.log(request, "Botched SOS job.") return redirect("challenge_list") # Now find out if there are any SOS requests. all_sos = list( SOS.objects.filter(active=True, challenge=challenge).exclude(student=me)) if len(all_sos) == 0: request.session["alerts"].append( ("alert-error", "There are no active SOS requests for this challenge.")) return redirect("challenge_list") # Now find an SOS to respond to. Make sure that we haven't already responded # to it. random.shuffle(all_sos) target = None while len(all_sos) > 0 and target is None: x = all_sos.pop() feedbacks = Feedback.objects.filter(sos=x, author=me) if len(feedbacks) == 0: target = x if target is None: request.session["alerts"].append(( "alert-error", "You have already responded to all active SOS requests for this challenge!" )) return redirect("challenge_list") # Filtering is now over. Time to render. request.session["sos-id"] = str(target.id) if challenge.autograde: assembler.assemble_and_store(request, "challenge", target.submission.code, challenge.pattern, challenge.preamble, challenge.postamble) return render( request, "sos_asm.html", { 'challenge': challenge, 'alerts': request.session.pop('alerts', []), 'sos': target }) elif challenge.is_jam: return redirect("challenge_list") else: return redirect("challenge_list")