Python Django framework completes a complete Forum (4. Other py file source code required by the project), djangopy
These files are stored in a newly created utils Folder:
Automatically Generated verification code:
Check_code.py:
"Manufacturing verification code" import randomfrom PIL import Image, ImageDraw, ImageFont, ImageFilter_letter_cases = "abcdefghjkmnpqrstuvwxy" # lowercase letters to remove potentially interfering I, l, o, z_upper_cases = _ letter_cases.upper () # uppercase letter _ numbers = ''. join (map (str, range (3, 10) # Number init_chars = ''. join (_ letter_cases, _ upper_cases, _ numbers) def create_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 Verification Code image @ param size: size of the image, format (width, height), default value: (120, 30) @ param chars: allowed character set combination, Format String @ param img_type: the default format for saving images is GIF. The options are GIF, JPEG, TIFF, and PNG @ param mode. The default format is RGB @ param bg_color, the default value is white @ param fg_color: foreground color and Verification Code character color. The default value is blue # 0000FF @ param font_size: Verification Code font size @ param font_type: The Verification Code font. The default value is AE _AlArabiya.ttf @ param: number of Characters in the verification code @ param draw_lines: Indicates whether to draw interference lines @ param n_lines: The number range of interference lines. Format tuples. The default value is (1, 2 ), valid only when draw_lines is True @ param draw_points: whether to draw interference points @ param point_chance: the probability of occurrence of interference points. The value range is [0,100] @ return: [0]: PIL Image instance @ return: [1]: the string "width, height = size # width and height # Create Image img = Image. new (mode, size, bg_color) draw = ImageDraw. draw (img) # create brush def get_chars (): "generate a string of the given length, return the List format" "return random. sample (chars, length) def create_lines (): "Draw interference line" line_num = random. randint (* n_line) # Number of interfering lines for I in range (line_num): # Starting Point begin = (random. randint (0, size [0]), random. randint (0, size [1]) # end Point end = (random. randint (0, size [0]), random. randint (0, size [1]) draw. line ([begin, end], fill = (0, 0, 0) def create_points (): "" chance = min (100, max (0, int (point_chance) # The size limit is [0,100] for w in range (width): for h in range (height): tmp = random. randint (0,100) if tmp> 100-chance: draw. point (w, h), fill = (0, 0, 0) def create_strs (): "Draw Verification Code character" c_chars = get_chars () strs = '% s' % ''. join (c_chars) # separate each character with spaces. font = 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) if draw_lines: create_lines () if draw_points: create_points () strs = create_strs () # graphic distortion parameter params = [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) # create distortion img = img. filter (ImageFilter. EDGE_ENHANCE_MORE) # filter, boundary enhancement (greater threshold) return img, strs
Pagination. py file of the page paging function:
From django. utils. safestring import mark_safeclass Pagination (object): "" Pagination "def _ init _ (self, current_page, data_count, per_page_count = 10, pager_num = 7): try: self. current_page = int (current_page) failed t Exception as e: self. current_page = 1 self. data_count = data_count self. per_page_count = per_page_count self. pager_num = pager_num @ property def start (self): return (self. current_page-1 )* Self. per_page_count @ property def end (self): return self. current_page * self. per_page_count @ property def total_count (self): v, y = divmod (self. data_count, self. per_page_count) if y: v + = 1 return v def page_str (self, base_url): page_list = [] if self. total_count <self. pager_num: start_index = 1 end_index = self. total_count + 1 else: if self. current_page <= (self. pager_num + 1)/2: start_index = 1 end_index = self. pager_num + 1 else: start_index = self. current_page-(self. pager_num-1)/2 end_index = self. current_page + (self. pager_num + 1)/2 if (self. current_page + (self. pager_num-1)/2)> self. total_count: end_index = self. total_count + 1 start_index = self. total_count-self. pager_num + 1 if self. current_page = 1: prev = '<li> <a class = "page" href = "javascript: void (0);"> previous page </a> </Li> 'else: prev = '<li> <a class = "page" href = "% s? P = % s "> previous page </a> </li> '% (base_url, self. current_page-1,) page_list.append (prev) for I in range (int (start_index), int (end_index): if I = 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) if self. current_page = self. total_count: nex = '<li> <a class = "page" href = "javascript: void (0);"> next page </a> </li> 'else: nex = '<li> <a class = "page" href = "% s? P = % s "> next page </a> </li> '% (base_url, self. current_page + 1,) page_list.append (nex) page_str = mark_safe ("". join (page_list) return page_str