Best Python code snippet using pom_python
views.py
Source:views.py
1# -*- coding: utf-8 -*-2# Create your views here.3#from django.template.loader import get_template 4#from django.template import Template, Context 5#from django.http import Http404, HttpResponse 6from django.shortcuts import render_to_response7from django.utils import timezone8from sample_board.models import DjangoBoard9from django.views.decorators.csrf import csrf_exempt10from django.http import HttpResponseRedirect11from sample_board.pagingHelper import pagingHelper12#from django.core.urlresolvers import reverse13# íê¸!!14#===========================================================================================15rowsPerPage = 2 16 17def home(request): 18 #OK19 #url = '/listSpecificPageWork?current_page=1' 20 #return HttpResponseRedirect(url) 21 boardList = DjangoBoard.objects.order_by('-id')[0:2] 22 current_page =123 totalCnt = DjangoBoard.objects.all().count() 24 25 pagingHelperIns = pagingHelper();26 totalPageList = pagingHelperIns.getTotalPageList( totalCnt, rowsPerPage)27 print 'totalPageList', totalPageList28 29 return render_to_response('listSpecificPage.html', {'boardList': boardList, 'totalCnt': totalCnt, 30 'current_page':current_page ,'totalPageList':totalPageList} ) 31 32#===========================================================================================33def show_write_form(request):34 return render_to_response('writeBoard.html') 35#===========================================================================================36@csrf_exempt37def DoWriteBoard(request):38 br = DjangoBoard (subject = request.POST['subject'],39 name = request.POST['name'],40 mail = request.POST['email'],41 memo = request.POST['memo'],42 created_date = timezone.now(),43 hits = 044 )45 br.save()46 47 # ë¤ì ì¡°í 48 url = '/listSpecificPageWork?current_page=1' 49 return HttpResponseRedirect(url) 50 51#===========================================================================================52def viewWork(request):53 pk= request.GET['memo_id'] 54 #print 'pk='+ pk55 boardData = DjangoBoard.objects.get(id=pk)56 #print boardData.memo57 58 # Update DataBase59 print 'boardData.hits', boardData.hits60 DjangoBoard.objects.filter(id=pk).update(hits = boardData.hits + 1)61 62 return render_to_response('viewMemo.html', {'memo_id': request.GET['memo_id'], 63 'current_page':request.GET['current_page'], 64 'searchStr': request.GET['searchStr'], 65 'boardData': boardData } ) 66 67#===========================================================================================68def listSpecificPageWork(request): 69 current_page = request.GET['current_page']70 totalCnt = DjangoBoard.objects.all().count() 71 72 print 'current_page=', current_page73 74 # íì´ì§ë¥¼ ê°ì§ê³ ë²ì ë°ì´í°ë¥¼ ì¡°ííë¤ => raw SQL ì¬ì©í¨75 boardList = DjangoBoard.objects.raw('SELECT Z.* FROM(SELECT X.*, ceil( rownum / %s ) as page FROM ( SELECT ID,SUBJECT,NAME, CREATED_DATE, MAIL,MEMO,HITS \76 FROM SAMPLE_BOARD_DJANGOBOARD ORDER BY ID DESC ) X ) Z WHERE page = %s', [rowsPerPage, current_page])77 78 print 'boardList=',boardList, 'count()=', totalCnt79 80 # ì ì²´ íì´ì§ë¥¼ 구í´ì ì ë¬...81 pagingHelperIns = pagingHelper();82 83 totalPageList = pagingHelperIns.getTotalPageList( totalCnt, rowsPerPage)84 85 print 'totalPageList', totalPageList86 87 return render_to_response('listSpecificPage.html', {'boardList': boardList, 'totalCnt': totalCnt, 88 'current_page':int(current_page) ,'totalPageList':totalPageList} )89#===========================================================================================90def listSpecificPageWork_to_update(request):91 memo_id = request.GET['memo_id']92 current_page = request.GET['current_page']93 searchStr = request.GET['searchStr']94 95 #totalCnt = DjangoBoard.objects.all().count()96 print 'memo_id', memo_id97 print 'current_page', current_page98 print 'searchStr', searchStr99 100 boardData = DjangoBoard.objects.get(id=memo_id)101 102 return render_to_response('viewForUpdate.html', {'memo_id': request.GET['memo_id'], 103 'current_page':request.GET['current_page'], 104 'searchStr': request.GET['searchStr'], 105 'boardData': boardData } ) 106#===========================================================================================107@csrf_exempt108def updateBoard(request):109 memo_id = request.POST['memo_id']110 current_page = request.POST['current_page']111 searchStr = request.POST['searchStr'] 112 113 print '#### updateBoard ######'114 print 'memo_id', memo_id115 print 'current_page', current_page116 print 'searchStr', searchStr117 118 # Update DataBase119 DjangoBoard.objects.filter(id=memo_id).update(120 mail= request.POST['mail'],121 subject= request.POST['subject'],122 memo= request.POST['memo']123 )124 125 # Display Page => POST ìì²ì redirection!126 url = '/listSpecificPageWork?current_page=' + str(current_page)127 return HttpResponseRedirect(url) 128 129#===========================================================================================130def DeleteSpecificRow(request):131 memo_id = request.GET['memo_id']132 current_page = request.GET['current_page']133 print '#### DeleteSpecificRow ######'134 print 'memo_id', memo_id135 print 'current_page', current_page136 137 p = DjangoBoard.objects.get(id=memo_id)138 p.delete()139 140 # Display Page 141 # ë§ì§ë§ ë©ëª¨ë¥¼ ìì íë ê²½ì°, íì´ì§ë¥¼ íë ì¤ì.142 totalCnt = DjangoBoard.objects.all().count() 143 pagingHelperIns = pagingHelper();144 145 totalPageList = pagingHelperIns.getTotalPageList( totalCnt, rowsPerPage)146 print 'totalPages', totalPageList147 148 if( int(current_page) in totalPageList):149 print 'current_page No Change'150 current_page=current_page151 else:152 current_page= int(current_page)-1153 print 'current_page--' 154 155 url = '/listSpecificPageWork?current_page=' + str(current_page)156 return HttpResponseRedirect(url) 157#===========================================================================================158@csrf_exempt159def searchWithSubject(request):160 searchStr = request.POST['searchStr']161 print 'searchStr', searchStr162 163 url = '/listSearchedSpecificPageWork?searchStr=' + searchStr +'&pageForView=1'164 return HttpResponseRedirect(url) 165 166 167#=========================================================================================== 168def listSearchedSpecificPageWork(request):169 searchStr = request.GET['searchStr']170 pageForView = request.GET['pageForView']171 print 'listSearchedSpecificPageWork:searchStr', searchStr, 'pageForView=', pageForView172 173 #boardList = DjangoBoard.objects.filter(subject__contains=searchStr)174 #print 'boardList=',boardList175 176 totalCnt = DjangoBoard.objects.filter(subject__contains=searchStr).count()177 print 'totalCnt=',totalCnt178 179 pagingHelperIns = pagingHelper();180 totalPageList = pagingHelperIns.getTotalPageList( totalCnt, rowsPerPage)181 182 # like 구문 ì ì©ë°©ë² 183 boardList = DjangoBoard.objects.raw("""SELECT Z.* FROM ( SELECT X.*, ceil(rownum / %s) as page \184 FROM ( SELECT ID,SUBJECT,NAME, CREATED_DATE, MAIL,MEMO,HITS FROM SAMPLE_BOARD_DJANGOBOARD \185 WHERE SUBJECT LIKE '%%'||%s||'%%' ORDER BY ID DESC) X ) Z WHERE page = %s""", [rowsPerPage, searchStr, pageForView])186 187 print'boardList=',boardList188 189 return render_to_response('listSearchedSpecificPage.html', {'boardList': boardList, 'totalCnt': totalCnt, 190 'pageForView':int(pageForView) ,'searchStr':searchStr, 'totalPageList':totalPageList} ) 191 192#===========================================================================================...
pagination.py
Source:pagination.py
1"""2å页ç»ä»¶3"""456class Pagination(object):7 def __init__(self, current_page, all_count, base_url, query_params, per_page=20, pager_page_count=11):8 """9 å页åå§å10 :param current_page: å½å页ç 11 :param per_page: æ¯é¡µæ¾ç¤ºæ°æ®æ¡æ°12 :param all_count: æ°æ®åºä¸æ»æ¡æ°13 :param base_url: åºç¡URL14 :param query_params: QueryDict对象ï¼å
é¨å«ææå½åURLçåæ¡ä»¶15 :param pager_page_count: 页é¢ä¸æå¤æ¾ç¤ºç页ç æ°é16 """17 self.base_url = base_url18 try:19 self.current_page = int(current_page)20 if self.current_page <= 0:21 raise Exception()22 except Exception as e:23 self.current_page = 124 self.query_params = query_params25 self.per_page = per_page26 self.all_count = all_count27 self.pager_page_count = pager_page_count28 pager_count, b = divmod(all_count, per_page)29 if b != 0:30 pager_count += 131 self.pager_count = pager_count3233 half_pager_page_count = int(pager_page_count / 2)34 self.half_pager_page_count = half_pager_page_count3536 @property37 def start(self):38 """39 æ°æ®è·åå¼èµ·å§ç´¢å¼40 :return:41 """42 return (self.current_page - 1) * self.per_page4344 @property45 def end(self):46 """47 æ°æ®è·åå¼ç»æç´¢å¼48 :return:49 """50 return self.current_page * self.per_page5152 def page_html(self):53 """54 çæHTML页ç 55 :return:56 """57 # å¦ææ°æ®æ»é¡µç pager_count<11 pager_page_count58 if self.pager_count < self.pager_page_count:59 pager_start = 160 pager_end = self.pager_count61 else:62 # æ°æ®é¡µç å·²ç»è¶
è¿1163 # å¤æï¼ å¦æå½å页 <= 5 half_pager_page_count64 if self.current_page <= self.half_pager_page_count:65 pager_start = 166 pager_end = self.pager_page_count67 else:68 # å¦æï¼ å½å页+5 > æ»é¡µç 69 if (self.current_page + self.half_pager_page_count) > self.pager_count:70 pager_end = self.pager_count71 pager_start = self.pager_count - self.pager_page_count + 172 else:73 pager_start = self.current_page - self.half_pager_page_count74 pager_end = self.current_page + self.half_pager_page_count7576 page_list = []7778 if self.current_page <= 1:79 prev = '<li><a href="#">ä¸ä¸é¡µ</a></li>'80 else:81 self.query_params['page'] = self.current_page - 182 prev = '<li><a href="%s?%s">ä¸ä¸é¡µ</a></li>' % (self.base_url, self.query_params.urlencode())83 page_list.append(prev)84 for i in range(pager_start, pager_end + 1):85 self.query_params['page'] = i86 if self.current_page == i:87 tpl = '<li class="active"><a href="%s?%s">%s</a></li>' % (88 self.base_url, self.query_params.urlencode(), i,)89 else:90 tpl = '<li><a href="%s?%s">%s</a></li>' % (self.base_url, self.query_params.urlencode(), i,)91 page_list.append(tpl)9293 if self.current_page >= self.pager_count:94 nex = '<li><a href="#">ä¸ä¸é¡µ</a></li>'95 else:96 self.query_params['page'] = self.current_page + 197 nex = '<li><a href="%s?%s">ä¸ä¸é¡µ</a></li>' % (self.base_url, self.query_params.urlencode(),)98 page_list.append(nex)99 page_str = "".join(page_list)
...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!