def go(): problem = get_or_none(Problem, id=id) if not problem: return {"success": False, "error": u"존재하지 않는 문제입니다."} checker = ObjectPermissionChecker(request.user) if not checker.has_perm('edit_problem', problem) and problem.user != request.user: return {"success": False, "error": u"권한이 없습니다."} if request.method != "POST": return {"success": False, "error": u"POST 접근하셔야 합니다."} file = request.FILES["file"] md5 = md5file(file) target_path = os.path.join("judge-attachments", md5, file.name) storage = DefaultStorage() storage.save(target_path, file) new_attachment = Attachment(problem=problem, file=target_path) new_attachment.save() # 해당 오브젝트에 대해 아무 퍼미션이나 있으면 처리됨. 문제의 경우 PUBLISHED 일 때는 이 권한을 사용하지 않아서 안전하다 visible_users = get_users_with_perms(problem, with_group_users=False) visible_groups = get_groups_with_perms(problem) publish("problem-attachment-%s" % datetime.now().strftime('%s.%f'), "problem", "problem-attachment", actor=request.user, target=problem, timestamp=datetime.now(), visible_users=visible_users, visible_groups=visible_groups, verb=u"문제 {target}에 첨부파일 %s 을 추가했습니다." % file.name) return {"success": True}
def go(): problem = get_or_none(Problem, id=id) if not problem: return {"success": False, "error": u"존재하지 않는 문제입니다."} if not request.user.is_superuser and problem.user != request.user: return {"success": False, "error": u"권한이 없습니다."} if request.method != "POST": return {"success": False, "error": u"POST 접근하셔야 합니다."} file = request.FILES["file"] md5 = md5file(file) target_path = os.path.join("judge-attachments", md5, file.name) storage = DefaultStorage() storage.save(target_path, file) new_attachment = Attachment(problem=problem, file=target_path) new_attachment.save() publish( "problem-attachment-%s" % datetime.now().strftime("%s.%f"), "problem", "problem-attachment", actor=request.user, target=problem, timestamp=datetime.now(), admin_only=True, verb=u"문제 {target}에 첨부파일 %s 을 추가했습니다." % file.name, ) return {"success": True}
def delete_attachment(request, id): attachment = get_object_or_404(Attachment, id=id) problem = attachment.problem checker = ObjectPermissionChecker(request.user) if not checker.has_perm('edit_problem', problem) and problem.user != request.user: raise Http404 old_id = attachment.id old_filename = attachment.file.name attachment.file.delete(False) attachment.delete() # 해당 오브젝트에 대해 아무 퍼미션이나 있으면 처리됨. 문제의 경우 PUBLISHED 일 때는 이 권한을 사용하지 않아서 안전하다 visible_users = get_users_with_perms(problem, with_group_users=False) visible_groups = get_groups_with_perms(problem) publish("problem-attachment-delete-%s" % datetime.now().strftime('%s.%f'), "problem", "problem-attachment", actor=request.user, target=problem, timestamp=datetime.now(), visible_users=visible_users, visible_groups=visible_groups, verb=u"문제 {target}에서 첨부파일 %s 을 삭제했습니다." % os.path.basename(old_filename)) return HttpResponse("[]")
def refresh(problem, user): # TODO: 언젠가.. 최적화한다. -_- # PUBLISHED 가 아니면 Solver 인스턴스는 존재하지 않는다. if problem.state != Problem.PUBLISHED: return # Solver 인스턴스를 찾음. 없으면 만듬. instance = get_or_none(Solver, problem=problem, user=user) if not instance: instance = Solver(problem=problem, user=user) instance.save() # 이 사람의 서브미션 목록을 찾는다 submissions = Submission.objects.filter(problem=problem, is_public=True, user=user).order_by("id") accepted = submissions.filter(state=Submission.ACCEPTED) # 풀었나? 못 풀었나? prev_solved = instance.solved if accepted.count() == 0: instance.solved = False instance.incorrect_tries = submissions.count() instance.fastest_submission = instance.shortest_submission = None else: instance.solved = True first = accepted[0] instance.when = first.submitted_on incorrect = submissions.filter(id__lt=first.id) instance.incorrect_tries = incorrect.count() instance.fastest_submission = accepted.order_by("time")[0] instance.shortest_submission = accepted.order_by("length")[0] instance.save() if instance.solved != prev_solved: # 유저 프로필에 푼 문제 수 업데이트 profile = user.get_profile() profile.solved_problems = Solver.objects.filter( user=user, solved=True).count() profile.save() # 처음으로 풀었을 경우 알림을 보낸다 id = "solved-%d-%d" % (problem.id, user.id) if instance.solved: # 풀었다! publish(id, "solved", "judge", target=problem, actor=user, timestamp=instance.fastest_submission.submitted_on, verb=u"%d번의 시도만에 문제 {target}를 해결했습니다." % (instance.incorrect_tries + 1)) else: # 리저지 등 관계로 풀었던 문제를 못푼게 됨. depublish(id) # TODO: 가장 빠른 솔루션, 가장 짧은 솔루션이 등장했을 경우 # newsfeed entry를 보낸다 return instance
def edit_handler(sender, **kwargs): instance = kwargs["instance"] publish("wiki-edit-%d" % instance.id, "wiki", "wiki-edit", actor=instance.user, target=instance.revision_for, timestamp=instance.created_on, verb=u"위키 페이지 {target}을 편집했습니다.")
def refresh(problem, user): # TODO: 언젠가.. 최적화한다. -_- # PUBLISHED 가 아니면 Solver 인스턴스는 존재하지 않는다. if problem.state != Problem.PUBLISHED: return # Solver 인스턴스를 찾음. 없으면 만듬. instance = get_or_none(Solver, problem=problem, user=user) if not instance: instance = Solver(problem=problem, user=user) instance.save() # 이 사람의 서브미션 목록을 찾는다 submissions = Submission.objects.filter(problem=problem, is_public=True, user=user).order_by("id") accepted = submissions.filter(state=Submission.ACCEPTED) # 풀었나? 못 풀었나? prev_solved = instance.solved if accepted.count() == 0: instance.solved = False instance.incorrect_tries = submissions.count() instance.fastest_submission = instance.shortest_submission = None else: instance.solved = True first = accepted[0] instance.when = first.submitted_on incorrect = submissions.filter(id__lt=first.id) instance.incorrect_tries = incorrect.count() instance.fastest_submission = accepted.order_by("time")[0] instance.shortest_submission = accepted.order_by("length")[0] instance.save() if instance.solved != prev_solved: # 유저 프로필에 푼 문제 수 업데이트 profile = user.get_profile() profile.solved_problems = Solver.objects.filter(user=user, solved=True).count() profile.save() # 처음으로 풀었을 경우 알림을 보낸다 id = "solved-%d-%d" % (problem.id, user.id) if instance.solved: # 풀었다! publish(id, "solved", "judge", target=problem, actor=user, timestamp=instance.fastest_submission.submitted_on, verb=u"%d번의 시도만에 문제 {target}를 해결했습니다." % (instance.incorrect_tries + 1)) else: # 리저지 등 관계로 풀었던 문제를 못푼게 됨. depublish(id) # TODO: 가장 빠른 솔루션, 가장 짧은 솔루션이 등장했을 경우 # newsfeed entry를 보낸다 return instance
def problem_revision_edit_handler(sender, **kwargs): instance = kwargs["instance"] publish("problem-edit-%d" % instance.id, "problem", "problem-edit", actor=instance.user, target=instance.revision_for, timestamp=instance.created_on, admin_only=True, verb=u"문제 {target}을 편집했습니다.")
def saved_problem(sender, **kwargs): instance = kwargs["instance"] if instance.state == Problem.PUBLISHED: id = "new-problem-%d" % instance.id if not has_activity(key=id): publish(id, "newproblem", "judge", actor=instance.user, action_object=instance, verb=u"온라인 저지에 새 문제 {action_object}를 " u"공개했습니다.") else: activity = get_activity(key=id) activity.actor = instance.user activity.save()
def post_handler(sender, **kwargs): instance, created = kwargs["instance"], kwargs["created"] if not created: return profile = instance.user.get_profile() profile.posts += 1 profile.save() publish("forum-post-%d" % instance.id, "posts", "posted", actor=instance.user, target=instance.category, action_object=instance, timestamp=instance.created_on, verb=u"{target}에 글 {action_object}를 " u"썼습니다.")
def saved_problem(sender, **kwargs): instance = kwargs["instance"] if instance.state == Problem.PUBLISHED: id = "new-problem-%d" % instance.id if not has_activity(key=id): publish(id, "newproblem", "judge", actor=instance.user, action_object=instance, verb=u"온라인 저지에 새 문제 {action_object}를 " u"공개했습니다.") else: activity = get_activity(key=id) activity.actor = instance.user activity.save() assign_perm('judge.edit_problem', instance.user, instance) # make life easier..
def problem_revision_edit_handler(sender, **kwargs): instance = kwargs["instance"] # 해당 오브젝트에 대해 아무 퍼미션이나 있으면 처리됨. 문제의 경우 PUBLISHED 일 때는 이 권한을 사용하지 않아서 안전하다 visible_users = get_users_with_perms(instance.revision_for, with_group_users=False) visible_groups = get_groups_with_perms(instance.revision_for) print visible_users print visible_groups publish("problem-edit-%d" % instance.id, "problem", "problem-edit", actor=instance.user, target=instance.revision_for, timestamp=instance.created_on, visible_users=visible_users, visible_groups=visible_groups, verb=u"문제 {target}을 편집했습니다.")
def post_handler(sender, **kwargs): instance, created = kwargs["instance"], kwargs["created"] if not created: return profile = instance.user.get_profile() profile.posts += 1 profile.save() everyone = Group.objects.get(name='everyone') if not 'read_post' in get_perms(everyone, instance.category): return publish("forum-post-%d" % instance.id, "posts", "posted", actor=instance.user, target=instance.category, action_object=instance, timestamp=instance.created_on, verb=u"{target}에 글 {action_object}를 " u"썼습니다.")
def comment_handler(sender, **kwargs): instance, created = kwargs["instance"], kwargs["created"] profile = instance.user.get_profile() target = instance.content_object if created: publish("comment-%d" % instance.id, "posts", "commented", actor=instance.user, action_object=instance, target=target, timestamp=instance.submit_date, verb=u"{target}에 새 댓글을 달았습니다: " u"{action_object}") profile.posts += 1 elif instance.is_removed: depublish("comment-%d" % instance.id) profile.posts -= 1 profile.save()
def delete_attachment(request, id): attachment = get_object_or_404(Attachment, id=id) problem = attachment.problem if not request.user.is_superuser and problem.user != request.user: raise Http404 old_id = attachment.id old_filename = attachment.file.name attachment.file.delete(False) attachment.delete() publish("problem-attachment-delete-%s" % datetime.now().strftime('%s.%f'), "problem", "problem-attachment", actor=request.user, target=problem, timestamp=datetime.now(), admin_only=True, verb=u"문제 {target}에서 첨부파일 %s 을 삭제했습니다." % os.path.basename(old_filename)) return HttpResponse("[]")
def post_handler(sender, **kwargs): instance, created = kwargs["instance"], kwargs["created"] if not created: return profile = instance.user.get_profile() profile.posts += 1 profile.save() # 해당 오브젝트에 대해 아무 퍼미션이나 있으면 처리됨 visible_users = get_users_with_perms(instance.category, with_group_users=False) visible_groups = get_groups_with_perms(instance.category) publish("forum-post-%d" % instance.id, "posts", "posted", actor=instance.user, target=instance.category, action_object=instance, timestamp=instance.created_on, visible_users=visible_users, visible_groups=visible_groups, verb=u"{target}에 글 {action_object}를 " u"썼습니다.")
def comment_handler(sender, **kwargs): instance, created = kwargs["instance"], kwargs["created"] profile = instance.user.get_profile() target = instance.content_object ctype = ContentType.objects.get_for_model(target) visible_users = None visible_groups = None if ctype.name == 'post': print 'post' print target.category visible_users = get_users_with_perms(target.category, with_group_users=False) visible_groups = get_groups_with_perms(target.category) if ctype.name == 'problem' and target.state != Problem.PUBLISHED: visible_users = get_users_with_perms(target, with_group_users=False) visible_groups = get_groups_with_perms(target) print visible_users print visible_groups if created: publish("comment-%d" % instance.id, "posts", "commented", actor=instance.user, action_object=instance, target=target, timestamp=instance.submit_date, visible_users=visible_users, visible_groups=visible_groups, verb=u"{target}에 새 댓글을 달았습니다: " u"{action_object}") profile.posts += 1 elif instance.is_removed: depublish("comment-%d" % instance.id) profile.posts -= 1 profile.save()