实训项目第二周2

以下是论坛方面的代码

# -*- coding: utf-8 -*-  
from __future__ import unicode_literals  
  
from django.shortcuts import get_object_or_404, render, render_to_response  
  
# Create your views here.  
from django.http import HttpResponseRedirect, HttpResponse  
from django.urls import reverse  
from django.utils import timezone  
from django.template import loader, RequestContext  
from django.views import generic  
from django import forms  
  
from .models import Problem, User, SubmitCode, AcceptedCode, Discussion, Response  
  
class DiscussionForm(forms.Form):  
    title = forms.CharField()  
    text = forms.CharField(widget=forms.Textarea)  
    code = forms.CharField(widget=forms.Textarea)  
  
  
  
# coding page , submit code , get result  
class CodingView(generic.TemplateView):  
    template_name = 'judgeOL/coding.html'  
  
    def get_context_data(self, **kwargs):  
        context = super(CodingView, self).get_context_data(**kwargs)  
        context['problem_id'] = self.kwargs['problem_id']  
        return context  
  
  
# bbs page, so many discussions  
class DiscussionView(generic.ListView):  
    model = Discussion  
    template_name = 'judgeOL/discussions.html'  
    context_object_name = 'discussion_list'  
 
    @staticmethod  
    def get_discussions(problem_id):  
        return Discussion.objects.get(problem_id=problem_id)  
  
    def get_context_data(self, **kwargs):  
        context = super(DiscussionView, self).get_context_data(**kwargs)  
        context['problem_id'] = self.kwargs['problem_id']  
        context['problem_name'] = Problem.objects.get(pk=self.kwargs['problem_id']).name  
        return context  
  
  
# every discussions have a lot of responses  
class ResponseView(generic.ListView):  
    model = Response  
    template_name = 'judgeOL/responses.html'  
    context_object_name = 'response_list'  
  
    def get_queryset(self):  
        return Response.objects.all().filter(discussion_id=self.kwargs['discussion_id'])  
  
    def get_context_data(self, **kwargs):  
        context = super(ResponseView, self).get_context_data(**kwargs)  
        context['discussion'] = Discussion.objects.get(pk=self.kwargs['discussion_id'])  
        context['problem_id'] = self.kwargs['problem_id']  
        return context  
  
  
class EditView(generic.TemplateView):  
    template_name = 'judgeOL/edit.html'  
  
    def get_context_data(self, **kwargs):  
        context = super(EditView, self).get_context_data(**kwargs)  
        context['problem_id'] = self.kwargs['problem_id']  
        return context  
  
def new_discussion(request, problem_id):  
    if request.method == 'GET':  
        return HttpResponseRedirect(reverse('judgeOL:discussions', args={problem_id}))  
    else:  
        try:  
            name = request.POST['title']  
            pub_text = request.POST['text']  
            pub_code = request.POST['code']  
            user_id = request.session['user_id']  
            Discussion.objects.create(user_id=user_id, problem_id=problem_id, name=name, pub_text=pub_text,  
                                      pub_code=pub_code, pub_date=timezone.now(), vote_count=1, view_count=1)  
            return HttpResponseRedirect(reverse('judgeOL:discussions', args={problem_id}))  
        except KeyError:  
            return HttpResponse('key error')  
  
        """ 
        discussion_form = DiscussionForm(request.POST) 
        if discussion_form.is_valid(): 
            name = discussion_form.cleaned_data['title'] 
            pub_text = discussion_form.cleaned_data['pub_text'] 
            pub_code = discussion_form.cleaned_data['pub_code'] 
            user_id = request.session['user_id'] 
            Discussion.objects.create(user_id=user_id, problem_id=problem_id, name=name, pub_text=pub_text, 
                                      pub_code=pub_code, pub_date=timezone.now(), vote_count=1, view_count=1) 
            return HttpResponse(reverse('judgeOL:discussions', args={problem_id})) 
        else: 
            return HttpResponse('judgeOL/error.html') 
      

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值