def rateuser(request, username): if request.method == 'POST': # If the form has been submitted... form = forms1.ratingForm(request.POST) # A form bound to the POST data if form.is_valid(): # All validation rules pass rate_receiver = form.cleaned_data['rate_receiver'] rating = form.cleaned_data['rating'] review = form.cleaned_data['review'] if models.User.objects.filter(username = rate_receiver).exists(): ratin = models.Rating() ratin.create_rating(rate_receiver, username, rating, review) user = findUser(rate_receiver) user.ratings += 1 user.save() ratin.save() return HttpResponseRedirect('/user/uprofile/%s/'%username) else: form = forms1.ratingForm() return render(request, 'rate/rateuser.html', {'form' : form, 'message' : 'Usertoratedoesntexist'}) else: form = forms1.ratingForm() else: form = forms1.ratingForm() # An unbound form return render(request, 'rate/rateuser.html', {'form': form, 'username' : username, 'user' : findUser(username)})
def test_empty(self): form_data = {'rate_receiver': '', 'rating' : 'socute', 'review' : 'good service'} form = ratingForm(data=form_data) self.assertEqual(form.is_valid(), False)
def ratingView(function=None, uuid=None): # Universal vars viewName = 'Rating' viewURL = 'ratingBP.ratingView' listColumns = ['Rating', 'Description', 'Probability', 'Impact'] templateView = 'rating/rating.html' # View kwargs kwargs = { 'title': viewName + ' list', 'maxDataTableWidth': '700', 'minDataTableWidth': '500', 'details': False } # Cruds listCrud = crud.ratingListData getCrud = crud.getRating postCrud = crud.postRating putCrud = crud.putRating deleteCrud = crud.deleteRating postForm = ratingForm() postData = { 'value': postForm.value.data, 'desc': postForm.desc.data, 'probability': postForm.probability.data, 'impact': postForm.impact.data } putForm = ratingForm() putData = { 'value': putForm.value.data, 'desc': putForm.desc.data, 'probability': putForm.probability.data, 'impact': putForm.impact.data } # put variables putExecs = [ 'data = getCrud(uuid)', 'impact=data.impact.uuid if data.impact else ""', 'probability=data.probability.uuid if data.probability else ""', 'putForm = ratingForm(value=data.value,desc=data.desc,impact=impact,probability=probability)', 'impacts = impactCrud.impactSelectData()', 'impacts.insert(0,("","Select Impact"))', 'probabilies = probabilityCrud.probabilitySelectData()', 'probabilies.insert(0,("","Probability"))', 'putForm.probability.choices = probabilies', 'putForm.impact.choices = impacts' ] # Post variables postExecs = [ 'impacts = impactCrud.impactSelectData()', 'impacts.insert(0,("","Select Impact"))', 'probabilies = probabilityCrud.probabilitySelectData()', 'probabilies.insert(0,("","Select Probability"))', 'postForm.probability.choices = probabilies', 'postForm.impact.choices = impacts' ] # -------------------------------------------------------------------------------------------- # CRUD Views (Do not touch!) # Build list of all rows if function == None: kwargs['listColumns'] = listColumns kwargs['listData'] = listCrud() return render_template('dataTable.html', **kwargs) # Create new row elif function == 'new': # Function kwargs kwargs = { 'contentTitle': 'Add new {}'.format(viewName), 'submitStay': True } for r in postExecs: exec(r) if postForm.validate_on_submit(): req = postCrud(data=postData) if 'success' in req: successMessage(req['success']) if not postForm.submitStay.data: return redirect(url_for(viewURL)) else: return redirect(url_for(viewURL) + '/new') elif 'error' in req: errorMessage(req['error']) return render_template(templateView, form=postForm, **kwargs) # View single row details elif function == 'details' and uuid != None: # Function kwargs data = getCrud(uuid) kwargs = { 'contentTitle': '{} details'.format(viewName), 'details': True, 'detailsData': data, 'submitStay': False, 'modifiedUser': getUser(data.modifiedBy), 'createdUser': getUser(data.createdBy) } return render_template(templateView, **kwargs) # Edit single row elif function == 'edit' and uuid != None: # Function kwargs kwargs = { 'contentTitle': 'Edit {}'.format(viewName), 'submitStay': False } for r in putExecs: exec(r) if putForm.validate_on_submit(): req = putCrud(data=putData, uuid=uuid) if 'success' in req: successMessage(req['success']) return redirect(url_for(viewURL)) elif 'error' in req: errorMessage(req['error']) return render_template(templateView, form=putForm, **kwargs) # Delete single row elif function == 'delete' and uuid != None: req = deleteCrud(uuid) if 'success' in req: successMessage(req['success']) elif 'error' in req: errorMessage(req['error']) return redirect(url_for(viewURL))
def blog(request, id_blog): blogsRecientes = Blog.objects.filter(status='P').order_by('-time')[:4] blog = get_object_or_404(Blog, id=id_blog) cate = Blog.categoria.all() numCalifblog = rating.objects.filter(Blog=id_blog).aggregate( Count('Blog')).values()[0] sumCalifblog = rating.objects.filter(Blog=blog.id).aggregate( Sum('calificacion')).values()[0] if sumCalifblog > 0: numStarsblog = (sumCalifblog) / numCalifblog Star = [i + 1 for i in range(numStarsblog)] else: Star = [i + 1 for i in range(0)] if blog.comentar: comenta = comentarios.objects.filter( Blog=blog.id).order_by('fecha_pub').reverse()[:5] if request.method == "POST": form = ComentarioForm(request.POST) formR = ratingForm(request.POST) if formR.is_valid(): calificacion = formR.cleaned_data['calificacion'] ctR = rating() ctR.Blog = Blog.objects.get(id=id_blog) ctR.calificacion = calificacion ctR.save() # info = 'inicializando' if form.is_valid(): nombre = form.cleaned_data['nombre'] cuerpo = form.cleaned_data['cuerpo'] ct = comentarios() ct.nombre = nombre ct.Blog = Blog.objects.get(id=id_blog) ct.cuerpo = cuerpo ct.save() # info = 'se guardo satisfactoriamente' return TemplateResponse( request, "blog.html", { 'form': ComentarioForm(), 'ct': ct, 'id_blog': id_blog, 'blog': blog, 'cate': cate, 'blogsRecientes': blogsRecientes, 'comentarios': comenta, 'Star': Star }) # else: # info = ' informacion con datos incorrectos' form = ComentarioForm() ctx = { 'form': form, 'id_blog': id_blog, 'blog': blog, 'cate': cate, 'blogsRecientes': blogsRecientes, 'comentarios': comenta, 'Star': Star } return render_to_response('blog.html', ctx, context_instance=RequestContext(request)) else: form = ComentarioForm() ctx = { 'form': form, 'id_blog': id_blog, 'blog': blog, 'cate': cate, 'blogsRecientes': blogsRecientes, 'comentarios': comenta, 'Star': Star } return render_to_response('blog.html', ctx, context_instance=RequestContext(request)) else: comenta = '' return TemplateResponse( request, "blog.html", { 'blog': blog, 'cate': cate, 'blogsRecientes': blogsRecientes, 'comentarios': comenta, 'Star': Star })
def test_forms(self): form_data = {'rate_receiver': 'cutepuppy', 'rating' : 'socute', 'review' : 'good service'} form = ratingForm(data=form_data) self.assertEqual(form.is_valid(), True)