<commit_before>from __future__ import print_function, division, absolute_import

"""Used to Access things in template like this:
(BUT NOW OUT OF DATE)

    x: {{ g.identity.name }}
    security: {{ security.__dict__ }}

"""

import os
import hashlib
import datetime
import time
import logging

import uuid
import hashlib
import hmac
import base64

import urlparse

import simplejson as json

import sqlalchemy
from sqlalchemy import orm

#from redis import StrictRedis
import redis
Redis = redis.StrictRedis()


from flask import (Flask, g, render_template, url_for, request, make_response,
                   redirect, flash, abort)

from wqflask import app


from pprint import pformat as pf

from wqflask import pbkdf2

from wqflask.database import db_session

from wqflask import model

from utility import Bunch, Struct, after
from utility.tools import LOG_SQL, LOG_SQL_ALCHEMY

import logging
from utility.logger import getLogger
logger = getLogger(__name__)

from base.data_set import create_datasets_list

import requests
from utility.elasticsearch_tools import get_elasticsearch_connection, get_user_by_unique_column, save_user

THREE_DAYS = 60 * 60 * 24 * 3
#THREE_DAYS = 45

def timestamp():
    return datetime.datetime.utcnow().isoformat()


class AnonUser(object):
    cookie_name = 'anon_user_v8'

    def __init__(self):
        self.cookie = request.cookies.get(self.cookie_name)
        if self.cookie:
            logger.debug("already is cookie")
            self.anon_id = verify_cookie(self.cookie)

        else:
            logger.debug("creating new cookie")
            self.anon_id, self.cookie = create_signed_cookie()
        self.key = "anon_collection:v1:{}".format(self.anon_id)

        @after.after_this_request
        def set_cookie(response):
            response.set_cookie(self.cookie_name, self.cookie)

    def add_collection(self, new_collection):
        collection_dict = dict(name = new_collection.name,
                               created_timestamp = datetime.datetime.utcnow().strftime('%b %d %Y %I:%M%p'),
                               changed_timestamp = datetime.datetime.utcnow().strftime('%b %d %Y %I:%M%p'),
                               num_members = new_collection.num_members,
                               members = new_collection.get_members())

        Redis.set(self.key, json.dumps(collection_dict))
        Redis.expire(self.key, 60 * 60 * 24 * 5)

    def delete_collection(self, collection_name):
        existing_collections = self.get_collections()
        updated_collections = []
        for i, collection in enumerate(existing_collections):
            if collection['name'] == collection_name:
                continue
            else:
                this_collection = {}
                this_collection['id'] = collection['id']
                this_collection['name'] = collection['name']
                this_collection['created_timestamp'] = collection['created_timestamp'].strftime('%b %d %Y %I:%M%p')
                this_collection['changed_timestamp'] = collection['changed_timestamp'].strftime('%b %d %Y %I:%M%p')
                this_collection['num_members'] = collection['num_members']
                this_collection['members'] = collection['members']
                updated_collections.append(this_collection)

        Redis.set(self.key, json.dumps(updated_collections))

    def get_collections(self):
        json_collections = Redis.get(self.key)
        if json_collections == None or json_collections == "None":
            return []
        else:
            collections = json.loads(json_collections)
            for collection in collections:
                collection['created_timestamp'] = datetime.datetime.strptime(collection['created_timestamp'], '%b %d %Y %I:%M%p')
                collection['changed_timestamp'] = datetime.datetime.strptime(collection['changed_timestamp'], '%b %d %Y %I:%M%p')
            return collections

    def import_traits_to_user(self):
        collections_list = json.loads(Redis.get(self.key))
        for collection in collections_list:
            uc = model.UserCollection()
            uc.name = collection['name']
            collection_exists = g.user_session.user_ob.get_collection_by_name(uc.name)
            if collection_exists:
                continue
            else:<commit_msg>Fix assumption of existing collection<commit_after>from __future__ import print_function, division, absolute_import

"""Used to Access things in template like this:
(BUT NOW OUT OF DATE)

    x: {{ g.identity.name }}
    security: {{ security.__dict__ }}

"""

import os
import hashlib
import datetime
import time
import logging

import uuid
import hashlib
import hmac
import base64

import urlparse

import simplejson as json

import sqlalchemy
from sqlalchemy import orm

#from redis import StrictRedis
import redis
Redis = redis.StrictRedis()


from flask import (Flask, g, render_template, url_for, request, make_response,
                   redirect, flash, abort)

from wqflask import app


from pprint import pformat as pf

from wqflask import pbkdf2

from wqflask.database import db_session

from wqflask import model

from utility import Bunch, Struct, after
from utility.tools import LOG_SQL, LOG_SQL_ALCHEMY

import logging
from utility.logger import getLogger
logger = getLogger(__name__)

from base.data_set import create_datasets_list

import requests
from utility.elasticsearch_tools import get_elasticsearch_connection, get_user_by_unique_column, save_user

THREE_DAYS = 60 * 60 * 24 * 3
#THREE_DAYS = 45

def timestamp():
    return datetime.datetime.utcnow().isoformat()


class AnonUser(object):
    cookie_name = 'anon_user_v8'

    def __init__(self):
        self.cookie = request.cookies.get(self.cookie_name)
        if self.cookie:
            logger.debug("already is cookie")
            self.anon_id = verify_cookie(self.cookie)

        else:
            logger.debug("creating new cookie")
            self.anon_id, self.cookie = create_signed_cookie()
        self.key = "anon_collection:v1:{}".format(self.anon_id)

        @after.after_this_request
        def set_cookie(response):
            response.set_cookie(self.cookie_name, self.cookie)

    def add_collection(self, new_collection):
        collection_dict = dict(name = new_collection.name,
                               created_timestamp = datetime.datetime.utcnow().strftime('%b %d %Y %I:%M%p'),
                               changed_timestamp = datetime.datetime.utcnow().strftime('%b %d %Y %I:%M%p'),
                               num_members = new_collection.num_members,
                               members = new_collection.get_members())

        Redis.set(self.key, json.dumps(collection_dict))
        Redis.expire(self.key, 60 * 60 * 24 * 5)

    def delete_collection(self, collection_name):
        existing_collections = self.get_collections()
        updated_collections = []
        for i, collection in enumerate(existing_collections):
            if collection['name'] == collection_name:
                continue
            else:
                this_collection = {}
                this_collection['id'] = collection['id']
                this_collection['name'] = collection['name']
                this_collection['created_timestamp'] = collection['created_timestamp'].strftime('%b %d %Y %I:%M%p')
                this_collection['changed_timestamp'] = collection['changed_timestamp'].strftime('%b %d %Y %I:%M%p')
                this_collection['num_members'] = collection['num_members']
                this_collection['members'] = collection['members']
                updated_collections.append(this_collection)

        Redis.set(self.key, json.dumps(updated_collections))

    def get_collections(self):
        json_collections = Redis.get(self.key)
        if json_collections == None or json_collections == "None":
            return []
        else:
            collections = json.loads(json_collections)
            for collection in collections:
                collection['created_timestamp'] = datetime.datetime.strptime(collection['created_timestamp'], '%b %d %Y %I:%M%p')
                collection['changed_timestamp'] = datetime.datetime.strptime(collection['changed_timestamp'], '%b %d %Y %I:%M%p')
            return collections

    def import_traits_to_user(self):
        result = Redis.get(self.key)
        collections_list = json.loads(result if result else "[]")
        for collection in collections_list:
            uc = model.UserCollection()
            uc.name = collection['name']
            collection_exists = g.user_session.user_ob.get_collection_by_name(uc.name)
            if collection_exists:
                continue
            else:
{'fraction_non_alphanumeric': 0.09631241410902427, 'fraction_numerical': 0.004809894640403115, 'mean_word_length': 2.806887532693984, 'pattern_counts': {'":': 2, '<': 3, '<?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': '2113508', 'n_tokens_mistral': 2518, 'n_tokens_neox': 2415, 'n_words': 570}