def showEditKnowledge(request, knowledge_id): user = users.models.getKnowledgeUser(request.user) kn = get_object_or_404(knowledge.models.Knowledge, pk=knowledge_id) if user != kn.author: return HttpResponseForbidden('access denied.') success = False if request.method == 'GET': form = KnowledgeForm(instance=kn) if request.method == 'POST': form = KnowledgeForm(request.POST, request.FILES, instance=kn) print('---- validating form') if form.is_valid(): print(request.FILES) print(request.POST) print('---- valid form') kn = form.save() print('---- edit knowledge :' + str(kn)) success = True Log.log_action(request, 'دانش ' + kn.subject + ' ویرایش شد.') else: print('---- invalid form') return render( request, 'knowledge/add-knowledge.html', addUserInfoContext( request, { 'page_title': 'edit knowledge', 'form': form, 'success': success, 'action': 'ویرایش', }))
def reportAbuseAj(request): print('report abuse') if request.method == 'GET': raise Http404 if request.method == 'POST': url = request.POST.dict().get('url') pres = request.POST.dict().get('name') reason = request.POST.dict().get('reason') remove_id = request.POST.dict().get('remove_id') remove_type = request.POST.dict().get('type') print(request.POST.dict()) # print('abuse reported name: ' + pres + ' url: ' + url + ' rem_id:' + str(remove_id) + ' t' + str(type(remove_id)) + ' rem_type:' + str(remove_type) + ' t' + str(type(remove_type))) print('abuse reported name: ' + pres + ' url: ' + url) print(' rem_id:' + str(remove_id) ) print(' t' + str(type(remove_id))) print(' rem_type:' + str(remove_type)) if (not pres) or (not url): return HttpResponseForbidden('invalid request') abuse = users.models.ReportAbuseRequest() abuse.user = users.models.getKnowledgeUser(request.user) abuse.ref = remove_type abuse.remove_id = remove_id abuse.abusedName = pres abuse.url = url abuse.reason = reason abuse.save() Log.log_action(request,'درخواست استفاده نادرست ثبت شد.') print('abuse reported') print(abuse) return HttpResponse(' درخواست با موفقیت ثبت شد.') return None
def loginView(request): if request.method == 'GET': return render(request, 'user/login.html', { 'page_title': '»Login', }) user = authenticate(username=request.POST['username'], password=request.POST['password']) if user is not None: # the password verified for the user if user.is_active: baseUrl = urlReverse('base-view') nextUrl = request.GET.get('next', baseUrl) login(request, user) print('---- login user: '******' redirect to ' + nextUrl) Log.log_action(request, "ورود به سامانه") return HttpResponseRedirect(nextUrl) else: # print("The password is valid, but the account has been disabled!") return render(request, 'user/login.html', { 'page_title': 'Login', 'invalidLogin': True, 'errorMessage': 'Account has been deactivated.' }) else: # the authentication system was unable to verify the username and password return render(request, 'user/login.html', { 'page_title': 'Login', 'invalidLogin': True, 'errorMessage': 'Invalid username or password.' })
def addCommentAJ(request, knowledge_id): print('add comment AJ') if request.method == 'GET': raise Http404 kn = get_object_or_404(knowledge.models.Knowledge, pk=knowledge_id) user = users.models.getKnowledgeUser(request.user) txt = request.POST['text'] time = datetime.now() # print('add comment user:'******' text:"' +txt + '" time:' + str(time)) comment = knowledge.models.Comment() comment.knowledge = kn comment.author = user comment.text = txt comment.date = time comment.save() Log.log_action(request, 'ببه دانش ' + kn.subject + ' نظر داده شد.') return JsonResponse({ 'comment': KnowledgeHtmlFactory.CommentFactory(comment), })
def showSpecialprivilageRequest(request): success = False if request.method == 'GET': form = SpecialPrivilegeForm() if request.method == 'POST': form = SpecialPrivilegeForm(request.POST) print('---- validating form') if form.is_valid(): print('---- valid form') mod = form.save(commit=False) kuser = users.models.getKnowledgeUser(request.user) mod.user = kuser mod.url = urlReverse('show-user-profile', kwargs={'user_id': kuser.id}) mod.save() success = True Log.log_action(request,'درخواست دسترسی ویژه ثبت شد.') else : print('---- invalid form') return render(request, 'user/show-special-privilage-request.html', addUserInfoContext(request, { 'page_title': 'Special privilege request', 'form': form, 'success': success }))
def showAddKnowledge(request): user = users.models.getKnowledgeUser(request.user) success = False if request.method == 'GET': form = KnowledgeForm() if request.method == 'POST': form = KnowledgeForm(request.POST, request.FILES) print('---- validating form') if form.is_valid(): print(request.FILES) print(request.POST) print('---- valid form') kn = form.save(commit=False) kn.author = user if kn.access > user.privilege: return HttpResponseForbidden('access denied.') print('---- add author to knowledge :' + str(user)) success = True kn.save() Log.log_action(request, 'دانش ' + kn.subject + ' ساخته شد.') else: print('---- invalid form') return render( request, 'knowledge/add-knowledge.html', addUserInfoContext( request, { 'page_title': 'Add knowledge', 'form': form, 'success': success, 'action': 'افزودن', }))
def reportAbuseAj(request): print('report abuse') if request.method == 'GET': raise Http404 if request.method == 'POST': url = request.POST.dict().get('url') pres = request.POST.dict().get('name') reason = request.POST.dict().get('reason') remove_id = request.POST.dict().get('remove_id') remove_type = request.POST.dict().get('type') print(request.POST.dict()) # print('abuse reported name: ' + pres + ' url: ' + url + ' rem_id:' + str(remove_id) + ' t' + str(type(remove_id)) + ' rem_type:' + str(remove_type) + ' t' + str(type(remove_type))) print('abuse reported name: ' + pres + ' url: ' + url) print(' rem_id:' + str(remove_id)) print(' t' + str(type(remove_id))) print(' rem_type:' + str(remove_type)) if (not pres) or (not url): return HttpResponseForbidden('invalid request') abuse = users.models.ReportAbuseRequest() abuse.user = users.models.getKnowledgeUser(request.user) abuse.ref = remove_type abuse.remove_id = remove_id abuse.abusedName = pres abuse.url = url abuse.reason = reason abuse.save() Log.log_action(request, 'درخواست استفاده نادرست ثبت شد.') print('abuse reported') print(abuse) return HttpResponse(' درخواست با موفقیت ثبت شد.') return None
def showDismissKUser(request): print('dismiss users') if request.method == 'GET': kusers = users.models.KUser.objects.all() print('send page for get method') return render(request, 'user/show-dismiss-user.html', addUserInfoContext(request, { 'page_title': 'Dismiss', 'kusers': kusers, })) elif request.method == 'POST': success_mes = '<div class="form-success">کاربران اخراج شدند.</div>' print(request.POST) ids = json.loads(request.POST['ids']) print('ids:') print(ids) print(type(ids)) kusers = users.models.KUser.objects.filter(pk__in=ids) print(kusers) man = getRequestKUser(request) for kuser in kusers: if kuser.privilege > man.privilege: print('insufficient access') return JsonResponse({'message': '<div class="form-error">دسترسی مجاز نیست.</div>', 'success': False}) for kuser in kusers: kuser.fire() Log.log_action(request,'کاربر ' + kuser.user.username + ' اخراج شد.') return JsonResponse({'message': success_mes, 'success': True}) return None
def addRelationAJ(request, knowledge_id): print('add relationship AJ') if request.method == 'GET': raise Http404 kn = get_object_or_404(knowledge.models.Knowledge, pk=knowledge_id) add_relation_form = InterKnowledgeRelationshipForm(request.POST) relation = None success = False print('---- validating form') if add_relation_form.is_valid(): print('---- valid form') relation = add_relation_form.save() Log.log_action(request,'رابطه ' + relation.abuse_presentation() + ' ساخته شد.') success = True else : print('---- invalid form') context = { 'knowledge': kn, 'success': success, 'add_relation_form': add_relation_form } t = loader.get_template('knowledge/util/add-relation.html') resp = t.render(context) but = KnowledgeHtmlFactory.RelationButtonFactory(relation, kn) if success else '' return JsonResponse({ 'form': resp, 'button': but })
def showRegisterKUser(request): success = False if request.method == 'GET': form = KUserForm() if request.method == 'POST': form = KUserForm(request.POST) print('---- validating form') if form.is_valid(): print('---- valid form') try: print('register user for') form.save() print('form saved') Log.log_action(request,'کاربر جدید ساخنه شد.') success = True except : print("Unexpected error:", sys.exc_info()[0]) success = False else : print('---- invalid form') return render(request, 'user/register.html', addUserInfoContext(request, { 'page_title': 'Register', 'form': form, 'success': success }))
def showEditKnowledge(request, knowledge_id): user = users.models.getKnowledgeUser(request.user) kn = get_object_or_404(knowledge.models.Knowledge, pk=knowledge_id) if user != kn.author: return HttpResponseForbidden('access denied.') success = False if request.method == 'GET': form = KnowledgeForm(instance=kn) if request.method == 'POST': form = KnowledgeForm(request.POST, request.FILES, instance=kn) print('---- validating form') if form.is_valid(): print(request.FILES) print(request.POST) print('---- valid form') kn = form.save() print('---- edit knowledge :' + str(kn)) success = True Log.log_action(request, 'دانش ' + kn.subject + ' ویرایش شد.') else : print('---- invalid form') return render(request, 'knowledge/add-knowledge.html', addUserInfoContext(request, { 'page_title': 'edit knowledge', 'form': form, 'success': success, 'action': 'ویرایش', }))
def rateKnowledgeAJ(request, knowledge_id): user = users.models.getKnowledgeUser(request.user) kn = get_object_or_404(knowledge.models.Knowledge, pk=knowledge_id) if user.privilege < kn.access: return HttpResponseForbidden('access denied.') vote = request.GET['vote'] kn.rate(user, (vote == 'up')) # try: # rate = knowledge.models.Rate.objects.get(knowledge=kn, voter=user) # except: # rate = knowledge.models.Rate() # rate.knowledge = kn # rate.voter = user # # rate.up = # rate.save() # # print('rate:') # print(rate) Log.log_action(request, 'به دانش ' + kn.subject + ' رتبه داده شد.') return HttpResponseRedirect( urlReverse('show-knowledge', kwargs={'knowledge_id': kn.id}))
def showAddKnowledge(request): user = users.models.getKnowledgeUser(request.user) success = False if request.method == 'GET': form = KnowledgeForm() if request.method == 'POST': form = KnowledgeForm(request.POST, request.FILES) print('---- validating form') if form.is_valid(): print(request.FILES) print(request.POST) print('---- valid form') kn = form.save(commit=False) kn.author = user if kn.access > user.privilege: return HttpResponseForbidden('access denied.') print('---- add author to knowledge :' + str(user)) success = True kn.save() Log.log_action(request, 'دانش ' + kn.subject + ' ساخته شد.') else : print('---- invalid form') return render(request, 'knowledge/add-knowledge.html', addUserInfoContext(request, { 'page_title': 'Add knowledge', 'form': form, 'success': success, 'action': 'افزودن', }))
def rateKnowledgeAJ(request, knowledge_id): user = users.models.getKnowledgeUser(request.user) kn = get_object_or_404(knowledge.models.Knowledge, pk=knowledge_id) if user.privilege < kn.access: return HttpResponseForbidden('access denied.') vote = request.GET['vote'] kn.rate(user, (vote == 'up')) # try: # rate = knowledge.models.Rate.objects.get(knowledge=kn, voter=user) # except: # rate = knowledge.models.Rate() # rate.knowledge = kn # rate.voter = user # # rate.up = # rate.save() # # print('rate:') # print(rate) Log.log_action(request, 'به دانش ' + kn.subject + ' رتبه داده شد.') return HttpResponseRedirect(urlReverse('show-knowledge', kwargs={'knowledge_id': kn.id}))
def showKnowledge(request, knowledge_id): user = users.models.getKnowledgeUser(request.user) kn = get_object_or_404(knowledge.models.Knowledge, pk=knowledge_id) if user.privilege < kn.access: return HttpResponseForbidden('access denied.') rate_up = knowledge.models.Rate.objects.filter(knowledge=kn, up=True).count() rate_down = knowledge.models.Rate.objects.filter(knowledge=kn, up=False).count() user_rate = '' try: rt = knowledge.models.Rate.objects.get(knowledge=kn, voter=user) if rt and rt.up: user_rate = 'up' else: user_rate = 'down' except: pass print('show knowledge:') print(kn) Log.log_action(request, 'دانش ' + kn.subject + ' مشاهده شد.') return render(request, 'knowledge/show-knowledge.html', addUserInfoContext(request, { 'page_title': kn.subject, 'knowledge': kn, 'add_relation_form': InterKnowledgeRelationshipForm(), 'add_tag_form': TagForm(), 'rate_up': rate_up, 'rate_down': rate_down, 'user_rate': user_rate, }))
def addRelationAJ(request, knowledge_id): print('add relationship AJ') if request.method == 'GET': raise Http404 kn = get_object_or_404(knowledge.models.Knowledge, pk=knowledge_id) add_relation_form = InterKnowledgeRelationshipForm(request.POST) relation = None success = False print('---- validating form') if add_relation_form.is_valid(): print('---- valid form') relation = add_relation_form.save() Log.log_action(request, 'رابطه ' + relation.abuse_presentation() + ' ساخته شد.') success = True else: print('---- invalid form') context = { 'knowledge': kn, 'success': success, 'add_relation_form': add_relation_form } t = loader.get_template('knowledge/util/add-relation.html') resp = t.render(context) but = KnowledgeHtmlFactory.RelationButtonFactory(relation, kn) if success else '' return JsonResponse({'form': resp, 'button': but})
def showSource(request, source_id): src = get_object_or_404(knowledge.models.Source, pk=source_id) print('show source: ') print(src) Log.log_action(request, 'منبع ' + src.subject + ' مشاهده شد.') return render(request, 'knowledge/show-source.html', addUserInfoContext(request, { 'page_title': src.subject, 'source': src, }))
def showSource(request, source_id): src = get_object_or_404(knowledge.models.Source, pk=source_id) print('show source: ') print(src) Log.log_action(request, 'منبع ' + src.subject + ' مشاهده شد.') return render( request, 'knowledge/show-source.html', addUserInfoContext(request, { 'page_title': src.subject, 'source': src, }))
def removeKnowledgeAj(request): if request.method == 'GET': raise Http404 if request.method == 'POST': id = request.POST.dict().get('id') print('remove knowledge id: ' + id) knowledge.models.Knowledge.objects.filter(pk=id).delete() Log.log_action(request,'دانش حذف شد.') return HttpResponse('حذف با موفقیت انجام شد.') return None
def removeRelationAj(request): if request.method == 'GET': raise Http404 if request.method == 'POST': id = request.POST.dict().get('id') print('remove tag id: ' + id) knowledge.models.InterknowledgeRelationship.objects.filter(pk=id).delete() Log.log_action(request,'رابطه حذف شد.') return HttpResponse('حذف با موفقیت انجام شد.') return None
def removeKnowledgeAj(request): if request.method == 'GET': raise Http404 if request.method == 'POST': id = request.POST.dict().get('id') print('remove knowledge id: ' + id) knowledge.models.Knowledge.objects.filter(pk=id).delete() Log.log_action(request, 'دانش حذف شد.') return HttpResponse('حذف با موفقیت انجام شد.') return None
def removeRelationAj(request): if request.method == 'GET': raise Http404 if request.method == 'POST': id = request.POST.dict().get('id') print('remove tag id: ' + id) knowledge.models.InterknowledgeRelationship.objects.filter( pk=id).delete() Log.log_action(request, 'رابطه حذف شد.') return HttpResponse('حذف با موفقیت انجام شد.') return None
def searchUser(request): print('search user') if request.method == 'POST': raise Http404 kusers = knowledge.engine.SearchEngine.searchUser(request.GET) Log.log_action(request, "جستجوی کاربران") return render(request, 'user/search-user.html', addUserInfoContext(request, { 'page_title': 'Search user', 'kusers': kusers, }))
def showAddTagType(request): success = False if request.method == 'GET': form = TagTypeForm() if request.method == 'POST': form = TagTypeForm(request.POST) print('---- validating form') if form.is_valid(): print('---- valid form') tagt = form.save() success = True Log.log_action(request,'نوع برچسب ' + tagt.presentation() + ' ساخته شد.') else : print('---- invalid form') return render(request, 'knowledge/add-tag-type.html', addUserInfoContext(request, { 'page_title': 'Add Tag type', 'form': form, 'success': success }))
def showAddSource(request): success = False if request.method == 'GET': form = SourceForm() if request.method == 'POST': form = SourceForm(request.POST) print('---- validating form') if form.is_valid(): print('---- valid form') sc = form.save() success = True Log.log_action(request, 'منبع ' + sc.subject + ' ساخنه شد.') else : print('---- invalid form') return render(request, 'knowledge/add-source.html', addUserInfoContext(request, { 'page_title': 'Add source', 'form': form, 'success': success }))
def addTagAJ(request, knowledge_id): print('add tag AJ') if request.method == 'GET': raise Http404 kn = get_object_or_404(knowledge.models.Knowledge, pk=knowledge_id) add_tag_form = TagForm(request.POST) print(kn) tag = None success = False print('---- validating form') if add_tag_form.is_valid(): print('---- valid form') tag = add_tag_form.save(commit=False) tag.knowledge = kn try: tag.save() Log.log_action(request, '' + tag.abuse_presentation() + ' ساخته شد.') success = True except: success = False add_tag_form.add_error(None, 'تگ مورد نظر موجود هست.') add_tag_form.is_valid() else : print('---- invalid form') context = { 'knowledge': kn, 'success': success, 'add_tag_form': add_tag_form } t = loader.get_template('knowledge/util/add-tag.html') resp = t.render(context) but = KnowledgeHtmlFactory.TagButtonFactory(tag) if success else '' return JsonResponse({ 'form': resp, 'button': but })
def showReportUserActivity(request): print('report user activity') if request.method == 'GET': kusers = users.models.KUser.objects.all() print('send page for get method') return render(request, 'user/show-report-user-activity.html', addUserInfoContext(request, { 'page_title': 'report user activity', 'kusers': kusers, })) elif request.method == 'POST': logs = knowledge.engine.ReportEngine.reportUserActivity(request.POST) Log.log_action(request, 'درخواست گزارش فعالیت کارمندان') context = { 'logs': logs, } t = loader.get_template('user/report-user-activity-result.html') resp = t.render(context) return JsonResponse({'result': resp, 'success': True}) return None
def addTagAJ(request, knowledge_id): print('add tag AJ') if request.method == 'GET': raise Http404 kn = get_object_or_404(knowledge.models.Knowledge, pk=knowledge_id) add_tag_form = TagForm(request.POST) print(kn) tag = None success = False print('---- validating form') if add_tag_form.is_valid(): print('---- valid form') tag = add_tag_form.save(commit=False) tag.knowledge = kn try: tag.save() Log.log_action(request, '' + tag.abuse_presentation() + ' ساخته شد.') success = True except: success = False add_tag_form.add_error(None, 'تگ مورد نظر موجود هست.') add_tag_form.is_valid() else: print('---- invalid form') context = { 'knowledge': kn, 'success': success, 'add_tag_form': add_tag_form } t = loader.get_template('knowledge/util/add-tag.html') resp = t.render(context) but = KnowledgeHtmlFactory.TagButtonFactory(tag) if success else '' return JsonResponse({'form': resp, 'button': but})
def showAddTagType(request): success = False if request.method == 'GET': form = TagTypeForm() if request.method == 'POST': form = TagTypeForm(request.POST) print('---- validating form') if form.is_valid(): print('---- valid form') tagt = form.save() success = True Log.log_action(request, 'نوع برچسب ' + tagt.presentation() + ' ساخته شد.') else: print('---- invalid form') return render( request, 'knowledge/add-tag-type.html', addUserInfoContext(request, { 'page_title': 'Add Tag type', 'form': form, 'success': success }))
def showAddSource(request): success = False if request.method == 'GET': form = SourceForm() if request.method == 'POST': form = SourceForm(request.POST) print('---- validating form') if form.is_valid(): print('---- valid form') sc = form.save() success = True Log.log_action(request, 'منبع ' + sc.subject + ' ساخنه شد.') else: print('---- invalid form') return render( request, 'knowledge/add-source.html', addUserInfoContext(request, { 'page_title': 'Add source', 'form': form, 'success': success }))
def showKnowledge(request, knowledge_id): user = users.models.getKnowledgeUser(request.user) kn = get_object_or_404(knowledge.models.Knowledge, pk=knowledge_id) if user.privilege < kn.access: return HttpResponseForbidden('access denied.') rate_up = knowledge.models.Rate.objects.filter(knowledge=kn, up=True).count() rate_down = knowledge.models.Rate.objects.filter(knowledge=kn, up=False).count() user_rate = '' try: rt = knowledge.models.Rate.objects.get(knowledge=kn, voter=user) if rt and rt.up: user_rate = 'up' else: user_rate = 'down' except: pass print('show knowledge:') print(kn) Log.log_action(request, 'دانش ' + kn.subject + ' مشاهده شد.') return render( request, 'knowledge/show-knowledge.html', addUserInfoContext( request, { 'page_title': kn.subject, 'knowledge': kn, 'add_relation_form': InterKnowledgeRelationshipForm(), 'add_tag_form': TagForm(), 'rate_up': rate_up, 'rate_down': rate_down, 'user_rate': user_rate, }))
def addCommentAJ(request, knowledge_id): print('add comment AJ') if request.method == 'GET': raise Http404 kn = get_object_or_404(knowledge.models.Knowledge, pk=knowledge_id) user = users.models.getKnowledgeUser(request.user) txt = request.POST['text'] time = datetime.now() # print('add comment user:'******' text:"' +txt + '" time:' + str(time)) comment = knowledge.models.Comment() comment.knowledge = kn comment.author = user comment.text = txt comment.date = time comment.save() Log.log_action(request,'ببه دانش ' + kn.subject + ' نظر داده شد.') return JsonResponse({ 'comment': KnowledgeHtmlFactory.CommentFactory(comment), })