Пример #1
0
def add_solution(request, JSON=True):
    problem_to_duplicate = Solution.objects.filter(id=request.GET.get('toid'))

    if request.method == 'POST':
        form = AddSolution(request.POST)
        if form.is_valid():
            posted = form.cleaned_data
            new_solution = Solution.objects.create_solution(
                owner_inst=request.user,
                source=Source.objects.get(id=1),
                category=Category.objects.get(
                    name=unicode(posted['category'])),
                problem=posted['problem'],
                problem_desc=posted['problem_desc'],
                solution=posted['solution'])
            user_score = User_score.objects.get(user=request.user)
            user_score.solutions_count = int(user_score.solutions_count) + 1
            user_score.save()
            new_solution_plus = Solution.objects.get(id=new_solution.id)
            if posted['tags']:
                tags = posted['tags']
                tags = re.findall(r'\S+', tags.replace(',', ' '))
                tag_list = []
                for tag in tags:
                    tag = tag.strip()
                    tag = tag[:50]
                    tag = tag.lower()
                    try:
                        selected_tag = Tag.objects.get(name=tag)
                        new_solution_plus.tags.add(selected_tag)
                        selected_tag.used_no = int(selected_tag.used_no) + 1
                        selected_tag.save()
                    except:
                        new_tag = Tag(name=tag, used_no=1)
                        new_tag.save()
                        new_solution_plus.tags.add(new_tag)

            uncomplete = False
            return uncomplete

    else:
        if problem_to_duplicate:
            form = AddSolution(
                initial={
                    'problem':
                    problem_to_duplicate[0].problem,
                    'problem_desc':
                    problem_to_duplicate[0].problem_desc,
                    'tags':
                    ','.join(problem_to_duplicate[0].tags.values_list(
                        'name', flat=True)),
                    'category':
                    problem_to_duplicate[0].category
                })
        else:
            form = AddSolution()
    return form
Пример #2
0
def edit_solution(request, solution_id):
  redirectTo = '/solutions/view/%s/'%solution_id
  solution = Solution.objects.get(id=solution_id)
  try:
    forked = True if request.GET['forked'] == 'True' else False
  except:
    forked = False
    
  if request.user == solution.owner:
    if request.method == 'POST':
      form = AddSolution(request.POST)
      if form.is_valid():
        posted = form.cleaned_data
        edited_solution = Solution.objects.filter(id=solution_id).update(**{'category': Category.objects.get(name=unicode(posted['category'])), 'problem': posted['problem'], 'problem_desc': posted['problem_desc'], 'solution': posted['solution']})
        Event.objects.create_event(operator=request.user, object_inst=request.user, operation='edit_solution', operation_result=Solution.objects.get(id=solution_id))
        former_tags = [unicode(tag) for tag in solution.tags.all()]
        if posted['tags']:
          tags=posted['tags']
          tags = re.findall(r'\S+', tags.replace(',',' '))
          solution.tags.clear()
          tag_list = []
          for tag in tags:
            tag = tag.strip()
            tag = tag[:50]
            tag = tag.lower()
            try:
              selected_tag = Tag.objects.get(name=tag)
              solution.tags.add(selected_tag)
              if not tag in former_tags:
                selected_tag.used_no = int(selected_tag.used_no) + 1
                selected_tag.save()
            except:
              new_tag = Tag(name=tag, used_no=1)
              new_tag.save()
              solution.tags.add(new_tag)
          for former_tag in former_tags:
            if not former_tag in tags:
              selected_tag = Tag.objects.get(name=former_tag)
              selected_tag.used_no = int(selected_tag.used_no) - 1
              selected_tag.save()
        else:
          solution.tags.clear()
          for former_tag in former_tags:
            selected_tag = Tag.objects.get(name=former_tag)
            selected_tag.used_no = int(selected_tag.used_no) - 1
            selected_tag.save()
          
      return HttpResponseRedirect(redirectTo)
    else:
      form = AddSolution(initial={'problem': solution.problem, 'problem_desc': solution.problem_desc, 'solution': solution.solution, 'category': solution.category, 'tags': ','.join( [unicode(tag) for tag in solution.tags.all()] )   })
      if not form:
        return HttpResponseRedirect(redirectTo)
      return render_to_response("edit_solutions.html", {'title': 'Edit Solution', 'solution_id': solution_id, 'form': form, 'forked': forked }, context_instance=RequestContext(request))
  else:
    return HttpResponseRedirect(redirectTo)
Пример #3
0
def add_solution(request, JSON=True):
  problem_to_duplicate = Solution.objects.filter(id=request.GET.get('toid'))

  if request.method =='POST':
    form = AddSolution(request.POST)
    if form.is_valid():
      posted = form.cleaned_data
      new_solution = Solution.objects.create_solution(owner_inst=request.user, source=Source.objects.get(id=1), category=Category.objects.get(name=unicode(posted['category'])), problem=posted['problem'], problem_desc=posted['problem_desc'], solution=posted['solution'])
      user_score = User_score.objects.get(user=request.user)
      user_score.solutions_count = int(user_score.solutions_count) + 1
      user_score.save()
      new_solution_plus = Solution.objects.get(id=new_solution.id)
      if posted['tags']:
        tags=posted['tags']
        tags = re.findall(r'\S+', tags.replace(',',' '))
        tag_list = []
        for tag in tags:
          tag = tag.strip()
          tag = tag[:50]
          tag = tag.lower()
          try:
            selected_tag = Tag.objects.get(name=tag)
            new_solution_plus.tags.add(selected_tag)
            selected_tag.used_no = int(selected_tag.used_no) + 1
            selected_tag.save()
          except:
            new_tag = Tag(name=tag, used_no=1)
            new_tag.save()
            new_solution_plus.tags.add(new_tag)

      uncomplete = False
      return uncomplete

  else:
    if problem_to_duplicate:
      form = AddSolution(initial={'problem': problem_to_duplicate[0].problem, 'problem_desc': problem_to_duplicate[0].problem_desc, 'tags': ','.join(problem_to_duplicate[0].tags.values_list('name', flat=True)), 'category': problem_to_duplicate[0].category})
    else:
      form = AddSolution()
  return form