aboutsummaryrefslogtreecommitdiff
path: root/wqflask/base/trait_collection.py
blob: 49b0eaea69b75f2956b720eada9c545b8eef0126 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#The TraitCollection class with encompass the UserCollection (collections
#created by registered and logged in users) and AnonCollection (collections created by users who
#aren't logged in) class. The former will represent and act on a database table while the latter
#will store its trait information in Redis


from __future__ import print_function, division, absolute_import

import uuid
import datetime

import simplejson as json

from flask import request
from flask.ext.sqlalchemy import SQLAlchemy

from wqflask import app

import sqlalchemy

from sqlalchemy import (Column, Integer, String, Table, ForeignKey, Unicode, Boolean, DateTime,
                        Text, Index)
from sqlalchemy.orm import relationship, backref

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

from wqflask.database import Base, init_db

class TraitCollection(object):


class AnonCollection(TraitCollection):
    
    def __init__(self, anon_id)
        self.anon_id = anon_id
        self.collection_members = Redis.smembers(self.anon_id)
        print("self.collection_members is:", self.collection_members)
        self.num_members = len(self.collection_members)
        

    @app.route("/collections/remove", methods=('POST',))
    def remove_traits(traits_to_remove):
        print("traits_to_remove:", traits_to_remove)
        for trait in traits_to_remove:
            Redis.srem(self.anon_id, trait)
        members_now = self.collection_members - traits_to_remove
        print("members_now:", members_now)
        print("Went from {} to {} members in set.".format(len(self.collection_members), len(members_now)))

        # We need to return something so we'll return this...maybe in the future
        # we can use it to check the results
        return str(len(members_now))

    @property
    def num_members(self):
        print("members are:", json.loads(self.members))
        return len(json.loads(self.members))

    #@property
    #def display_num_members(self):
    #    return display_collapsible(self.num_members)

    def members_as_set(self):
        return set(json.loads(self.members))