<commit_before># -*- coding: utf-8 -*-

import bottle
import json
import simplejson
import datetime
import re
import logging

import controls.auth

class Tags:

    def __init__(self, couchDB, ):
        self.couchDB = couchDB


    def tags_get( self, tag_name ):
        '''lists all the article with a named tag'''
        global_config_data = json.loads( self.couchDB.getDocValue( "global_config" ).text, 'utf8')
        authenticated = bottle.request.get_cookie("authenticated", secret = global_config_data["cookie_secret_key"])
        json_request =  '{'
        json_request +=     '"map" : "function(doc) { '
        json_request +=         'if( '
        json_request +=             'doc.document_type == \'blog_article\' '
        json_request +=             '&& doc.tags.indexOf(\'' + tag_name + '\')  !== -1 '
        json_request +=         ') { '
        json_request +=             'emit(\'title\', doc.title); '
        json_request +=         '} '
        json_request +=      '}" '
        json_request +=  '} '
        response = self.couchDB.getTempView( json_request )
        article_of_tag = artikle_data = json.loads( response.text, 'utf8' )

        block_article_of_tag = bottle.template(
            'block_tags',
            tag_name=tag_name,
            article_of_tag=article_of_tag)

        return bottle.template(
            'skeleton',
            uri_prefix="../",
            title="Schlagwort" + tag_name,
            authenticated=authenticated,
            main_area=block_article_of_tag)


    def all_tags_get( self ):
        '''show all used tags'''
        global_config_data = json.loads( self.couchDB.getDocValue( "global_config" ).text, 'utf8')
        authenticated = bottle.request.get_cookie("authenticated", secret = global_config_data["cookie_secret_key"])
        '''check and prepare password protection'''
        response = self.couchDB.getDocValue( "tag_statistics" )
        tag_statistics = json.loads(response.text, 'utf8')
        if "error" in tag_statistics:
            if tag_statistics["error"] == "not_found":
                logging.info( "Can not found document tag_statistics and create with default password" )
                json_code = self.gen_json_tag_statistics()
                tag_statistics = json.loads( json_code, 'utf8')
                response = self.couchDB.insertNamedDoc( "tag_statistics", json_code )
                response_data = json.loads(response.text, 'utf8')
                if "error" in tag_statistics:
                    flashed_message = response_data["error"] + ": " + response_data["reason"]
                    logging.error( flashed_message )
                    block_error = bottle.template(
                        'block_error',
                        flashed_message=flashed_message )

                    html_code = bottle.template(
                        'skeleton',
                        uri_prefix="../",
                        title= "Fehler",
                        authenticated=authenticated,
                        main_area=block_error)
                    return html_code
                else:
                    logging.info( response.text )
            else:
                flashed_message = tag_statistics["error"] + ": " + tag_statistics["reason"]
                logging.error( flashed_message )
                block_error = bottle.template(
                    'block_error',
                    flashed_message=flashed_message )

                html_code = bottle.template(
                    'skeleton',
                    uri_prefix="../",
                    title= "Fehler",
                    authenticated=authenticated,
                    main_area=block_error)
                return html_code
        else:
            current_time = datetime.datetime.now(datetime.timezone.utc)
            unix_timestamp = current_time.timestamp()
            '''if document older than 30 seconds (end of live)'''
            print( str( tag_statistics["last_update"] ) + " < " + str( ( unix_timestamp - 30 ) ) )
            if tag_statistics["last_update"] < ( unix_timestamp - 30 ) :
                logging.info( "The tag statistics document is older than 30 seconds (end of live). Refresh document now." )<commit_msg>bugfix<commit_after># -*- coding: utf-8 -*-

import bottle
import json
import simplejson
import datetime
import re
import logging

import controls.auth

class Tags:

    def __init__(self, couchDB, ):
        self.couchDB = couchDB


    def tags_get( self, tag_name ):
        '''lists all the article with a named tag'''
        global_config_data = json.loads( self.couchDB.getDocValue( "global_config" ).text, 'utf8')
        authenticated = bottle.request.get_cookie("authenticated", secret = global_config_data["cookie_secret_key"])
        json_request =  '{'
        json_request +=     '"map" : "function(doc) { '
        json_request +=         'if( '
        json_request +=             'doc.document_type == \'blog_article\' '
        json_request +=             '&& doc.tags.indexOf(\'' + tag_name + '\')  !== -1 '
        json_request +=         ') { '
        json_request +=             'emit(\'title\', doc.title); '
        json_request +=         '} '
        json_request +=      '}" '
        json_request +=  '} '
        response = self.couchDB.getTempView( json_request )
        article_of_tag = artikle_data = json.loads( response.text, 'utf8' )

        block_article_of_tag = bottle.template(
            'block_tags',
            tag_name=tag_name,
            article_of_tag=article_of_tag)

        return bottle.template(
            'skeleton',
            uri_prefix="../",
            title="Schlagwort" + tag_name,
            authenticated=authenticated,
            main_area=block_article_of_tag)


    def all_tags_get( self ):
        '''show all used tags'''
        global_config_data = json.loads( self.couchDB.getDocValue( "global_config" ).text, 'utf8')
        authenticated = bottle.request.get_cookie("authenticated", secret = global_config_data["cookie_secret_key"])
        '''check and prepare password protection'''
        response = self.couchDB.getDocValue( "tag_statistics" )
        tag_statistics = json.loads(response.text, 'utf8')
        if "error" in tag_statistics:
            if tag_statistics["error"] == "not_found":
                logging.info( "Can not found document tag_statistics." )
                flashed_message = response_data["error"] + ": " + response_data["reason"]
                logging.error( flashed_message )
                block_error = bottle.template(
                    'block_error',
                    flashed_message=flashed_message )

                html_code = bottle.template(
                    'skeleton',
                    uri_prefix="../",
                    title= "Fehler",
                    authenticated=authenticated,
                    main_area=block_error)
                return html_code
            else:
                flashed_message = tag_statistics["error"] + ": " + tag_statistics["reason"]
                logging.error( flashed_message )
                block_error = bottle.template(
                    'block_error',
                    flashed_message=flashed_message )

                html_code = bottle.template(
                    'skeleton',
                    uri_prefix="../",
                    title= "Fehler",
                    authenticated=authenticated,
                    main_area=block_error)
                return html_code
        else:
            current_time = datetime.datetime.now(datetime.timezone.utc)
            unix_timestamp = current_time.timestamp()
            '''if document older than 30 seconds (end of live)'''
            print( str( tag_statistics["last_update"] ) + " < " + str( ( unix_timestamp - 30 ) ) )
            if tag_statistics["last_update"] < ( unix_timestamp - 30 ) :
                logging.info( "The tag statistics document is older than 30 seconds (end of live). Refresh document now." )
{'fraction_non_alphanumeric': 0.10732919254658385, 'fraction_numerical': 0.0037267080745341614, 'mean_word_length': 1.8259038259038258, 'pattern_counts': {'":': 6, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7445525', 'n_tokens_mistral': 2234, 'n_tokens_neox': 2112, 'n_words': 439}