These files are placed in a new Utils folder:
Automatically generate verification code:
check_code.py:
"""Manufacturing Verification Code"""ImportRandom fromPILImportImage, Imagedraw, Imagefont, imagefilter_letter_cases="Abcdefghjkmnpqrstuvwxy" #lowercase letters To remove i,l,o,z that may interfere_upper_cases = _letter_cases.upper ()#Uppercase Letters_numbers ="'. Join (Map (str, RANGE (3, 10)))#DigitalInit_chars ="'. Join ((_letter_cases, _upper_cases, _numbers))defCreate_validate_code (size= (120, 30), chars=Init_chars, Img_type="GIF", Mode="RGB", Bg_color= (255, 255, 255), Fg_color= (0, 0, 255), Font_size=18, Font_type="Monaco.ttf", Length=4, Draw_lines=True, N_line= (1, 2), Draw_points=True, Point_chance=2): """@todo: Generate CAPTCHA image @param size: The size of the picture, format (wide, high), default to (+), @param chars: Allowed character set, format string @param img_type: Picture saved Format, default is GIF, optional gif,jpeg,tiff,png @param mode: Picture mode, default is RGB @param bg_color: Background color, default is white @param fg_color: foreground color, captcha character color , the default is blue #0000ff @param font_size: captcha font size @param font_type: captcha font, default to Ae_alarabiya.ttf @param length: Number of captcha characters @p Aram Draw_lines: Whether the interference line @param N_lines: The number range of interference lines, the format tuple, the default is (1, 2), only valid when Draw_lines is true @param draw_points: whether to draw the interference point @ param point_chance: Probability of disturbance point occurrence, size range [0] @return: [0]: PIL image instance @return: [1]: string in captcha picture"""width, height= Size#Wide Height #Create a graphicIMG =image.new (mode, size, bg_color) draw= Imagedraw.draw (IMG)#Create a brush defget_chars ():"""generates a string of the given length, returning the list format""" returnrandom.sample (chars, length)defcreate_lines ():"""Draw Interference Lines"""Line_num= Random.randint (*n_line)#number of interfering lines forIinchRange (line_num):#starting pointBegin = (Random.randint (0, size[0]), Random.randint (0, size[1])) #End PointEnd = (Random.randint (0, size[0]), Random.randint (0, size[1]) draw.line ([begin, end], fill=(0, 0, 0))defcreate_points ():"""Draw Interference points"""Chance= min (max (0, Int (point_chance)))#size limit in [0, +] forWinchRange (width): forHinchRange (height): tmp= Random.randint (0, 100) ifTMP > 100-Chance:draw.point ((W, h), fill=(0, 0, 0))defcreate_strs ():"""plotting Captcha characters"""C_chars=get_chars () STRs='%s'%' '. Join (C_chars)#spaces are separated by a space before and after each characterFont=Imagefont.truetype (Font_type, font_size) font_width, Font_height=font.getsize (STRs) Draw.text (((Width-Font_width)/3, (height-font_height)/3), STRs, Font=font, fill=Fg_color)return "'. Join (C_chars)ifDraw_lines:create_lines ()ifdraw_points:create_points () STRs=create_strs ()#graphic Warp Parametersparams = [1-float (Random.randint (1, 2))/100, 0, 0, 0,1-float (Random.randint (1, 10))/100, Float (random.randint (1, 2))/500, 0.001, Float (random.randint (1, 2))/500] img= Img.transform (Size, image.perspective, params)#Creating Distortionsimg= Img.filter (Imagefilter.edge_enhance_more)#filter, Edge enhancement (threshold value) returnIMG, STRs
pagination.py file for page paging function:
fromDjango.utils.safestringImportMark_safeclasspagination (object):"""page Out""" def __init__(Self, current_page, Data_count, per_page_count=10, pager_num=7): Try: Self.current_page=Int (current_page)exceptException as E:self.current_page= 1Self.data_count=Data_count Self.per_page_count=Per_page_count Self.pager_num=Pager_num @propertydefStart (self):return(self.current_page-1) *Self.per_page_count @propertydefEnd (self):returnSelf.current_page *Self.per_page_count @propertydefTotal_count (self): V, y=Divmod (Self.data_count, Self.per_page_count)ify:v+ = 1returnvdefpage_str (Self, base_url): Page_list= [] ifSelf.total_count <Self.pager_num:start_index= 1End_index= Self.total_count + 1Else: ifSelf.current_page <= (self.pager_num + 1)/2: Start_index= 1End_index= Self.pager_num + 1Else: Start_index= Self.current_page-(self.pager_num-1)/2End_index= Self.current_page + (self.pager_num + 1)/2if(Self.current_page + (self.pager_num-1)/2) >Self.total_count:end_index= Self.total_count + 1Start_index= Self.total_count-self.pager_num + 1ifSelf.current_page = = 1: Prev='<li><a class= "page" href= "javascript:void (0);" > Prev </a></li>' Else: Prev='<li><a class= "page" href= "%s?p=%s" > Prev </a></li>'% (Base_url, self.current_page-1,) page_list.append (prev) forIinchRange (int (start_index), int (end_index)):ifi = =self.current_page:temp='<li class= "active" ><a class= "page active" href= "%s?p=%s" >%s</a></li>'%(Base_url, I, i)Else: Temp='<li><a class= "page" href= "%s?p=%s" >%s</a></li>'%(Base_url, I, i) page_list.append (temp)ifSelf.current_page = =Self.total_count:nex='<li><a class= "page" href= "javascript:void (0);" > Next </a></li>' Else: Nex='<li><a class= "page" href= "%s?p=%s" > Next </a></li>'% (Base_url, Self.current_page + 1,) page_list.append (NEX) Page_str= Mark_safe ("". Join (page_list))returnPage_str
Python's Django Framework completes a complete forum (4. Other PY files required by the project source code)