Example #1
0
    def index(request, groups):
        """Index comment"""
        response_headers = [
            ('Content-Type', 'text/html; charset=utf-8'),
        ]
        request_method = request.environ.get('REQUEST_METHOD')

        if request_method == 'GET':
            comment_entities = CommentRepository.get_all()
            item_template = get_template('comment/item.html')
            content = [
                item_template.safe_substitute(
                    **{
                        'id': comment.id,
                        'first_name': comment.first_name,
                        'second_name': comment.second_name,
                        'last_name': comment.last_name,
                        'phone': comment.phone,
                        'email': comment.email,
                        'text': comment.text,
                    }) for comment in comment_entities
            ]

            response_context = {'content': "".join(content)}
            response_body = get_template('comment/index.html').safe_substitute(
                **response_context)
            response_status = 200
            return (response_body, response_headers, response_status)
        else:
            return method_not_allowed(request, groups)
Example #2
0
    def regions(request, groups):
        """Show regions"""
        response_headers = [
            ('Content-Type', 'text/html; charset=utf-8'),
        ]
        request_method = request.environ.get('REQUEST_METHOD')

        if request_method == 'GET':
            region_entities = RegionRepository.get_stat()
            item_template = get_template('comment/region_tr.html')
            content = [
                item_template.safe_substitute(
                    **{
                        'id': region.id,
                        'name': region.name,
                        'comment_count': region.comment_count,
                    }) for region in region_entities
            ]

            response_context = {'content': "".join(content)}
            response_body = get_template(
                'comment/region_stat.html').safe_substitute(**response_context)
            response_status = 200
            return (response_body, response_headers, response_status)
        else:
            return method_not_allowed(request, groups)
Example #3
0
    def add(request, groups):
        """Add new comment"""
        response_headers = [
            ('Content-Type', 'text/html; charset=utf-8'),
        ]
        request_method = request.environ.get('REQUEST_METHOD')

        if request_method == 'POST':
            last_name = get_item(request.form_input.get('last_name', []), None)
            first_name = get_item(request.form_input.get('first_name', []),
                                  None)
            second_name = get_item(request.form_input.get('second_name', []),
                                   None)
            city = get_item(request.form_input.get('city', []), None)
            phone = get_item(request.form_input.get('phone', []), None)
            email = get_item(request.form_input.get('email', []), None)
            text = get_item(request.form_input.get('text', []), None)

            try:
                city_id = city
            except ValueError:
                city_id = None

            result = None
            if last_name and first_name and text:
                comment_entity = CommentEntity(
                    _last_name=last_name,
                    _first_name=first_name,
                    _text=text,
                    _city_id=city_id,
                    _second_name=second_name,
                    _phone=phone,
                    _email=email,
                )
                result = CommentRepository.create(comment_entity)

            if result:
                return redirect(url='/comment/success/')
            else:
                return redirect(url='/comment/fail/')
        elif request_method == 'GET':
            regions = RegionRepository.get_all()
            option_template = get_template('comment/option.html')
            region_options = [
                option_template.safe_substitute(**{
                    'id': region.id,
                    'name': region.name
                }) for region in regions
            ]

            response_context = {'region_options': "".join(region_options)}
            response_body = get_template('comment/add.html').safe_substitute(
                **response_context)
            response_status = 200
            return (response_body, response_headers, response_status)
        else:
            return method_not_allowed(request, groups)
Example #4
0
 def render_message(seft, user_data):
     file_text_path = 'templates/email_message.txt'
     file_html_path = 'templates/email_message.html'
     plain_txt = get_template(file_text_path)
     html_txt = get_template(file_html_path)
     if isinstance(user_data, dict):
         plain_ = render_context(plain_txt, user_data)
         html_ = render_context(html_txt, user_data)
         return (plain_, html_)
     return (None, None)
Example #5
0
 def fail(request, groups):
     """Fail comment"""
     response_headers = [
         ('Content-Type', 'text/html; charset=utf-8'),
     ]
     response_body = get_template('comment/fail.html').safe_substitute()
     response_status = 200
     return (response_body, response_headers, response_status)
import os
import sys
from service.readdoc_service import Case
from utils.template import get_template
import json
from utils.convert2json import recommendation2json

if __name__ == '__main__':
    template = get_template('utils/targetinfo.txt')
    similarity = 0.97
    cases = []
    path = os.path.abspath('../data')
    #print('path', path)
    for root, dirs, files in os.walk(path):
        for dirName in dirs:
            #print('dirname', dirName)
            case = Case(template, root, dirName, similarity)
            cases.append(case.get_case())
    resultFile = os.path.join(os.path.abspath('.'), 'testJson.json')
    with open(resultFile, 'w+', encoding='utf-8') as jsonFile:
        jsonContent = json.dumps(cases,
                                 default=recommendation2json,
                                 ensure_ascii=False,
                                 indent=4)
        jsonFile.write(jsonContent)