~manishsinha/cheers/insertion

« back to all changes in this revision

Viewing changes to cheers/datastore.py

  • Committer: Manish Sinha
  • Date: 2010-10-27 07:32:00 UTC
  • Revision ID: manishsinha.tech@gmail.com-20101027073200-25u7hi42yng3dj3n
Implemented the Register trophy method

Show diffs side-by-side

added added

removed removed

Lines of Context:
22
22
import os
23
23
import sqlite3
24
24
import config
 
25
import random
 
26
import datetime
25
27
 
26
28
class SqliteStore():
27
29
    """ Implement the data storage based using sqlite as backend
46
48
        # Try creating the tables
47
49
        try:
48
50
            self.__cur.execute(config.create_trophy_command)
 
51
        except sqlite3.OperationalError:
 
52
            print("Trophy Table existed")
 
53
        
 
54
        try:
49
55
            self.__cur.execute(config.create_desc_command)
 
56
        except sqlite3.OperationalError:
 
57
            print("Description Table existed")
 
58
        
 
59
        try:
50
60
            self.__cur.execute(config.create_app_command)
 
61
        except sqlite3.OperationalError:
 
62
            print("App Table existed")
 
63
        
 
64
        try:
51
65
            self.__cur.execute(config.create_set_command)
 
66
        except sqlite3.OperationalError:
 
67
            print("Set Table existed")
 
68
            
 
69
        try:
52
70
            self.__cur.execute(config.create_history_command)
53
 
            print("All tables create successfully")
54
71
        except sqlite3.OperationalError:
55
 
            print("Table existed")
 
72
            print("History Table existed")
 
73
            
56
74
            
57
75
        self.__res_cursor=None
58
76
 
61
79
        fetch_trp = config.fetch_trophy_where %("id",)
62
80
        res_trophy = self.__cur.execute(fetch_trp, { "value":json_data["id"] })
63
81
        if(len(res_history.fetchall()) == 0 and len(res_trophy.fetchall()) == 0):
64
 
            try:
65
 
                # Inserting Description
66
 
                # Searching and Inserting Sets
67
 
                # Searching and Inserting Apps
 
82
            
 
83
            # Searching and Inserting Sets
 
84
            set_list = self.__cur.execute(config.fetch_set_byname, (json_data["setname"],))
 
85
            matched_set = set_list.fetchone()
 
86
            if matched_set is None:
 
87
                # Does not exist, create one
 
88
                print("Set '%s' does not exist. Creating." %(json_data["setname"],))
 
89
                setid = int(random.random()*10000000)
 
90
                self.__cur.execute(config.insert_set_command, (setid, json_data["setname"], json_data["seticon"]))
 
91
            else:
 
92
                # Exists. Use it
 
93
                print("Set '%s' already exists. Will be reused" %(matched_set[1],))
 
94
                setid = matched_set[0]
 
95
            
 
96
            # Searching and Inserting Apps
 
97
            app_list = self.__cur.execute(config.fetch_app_byapp, (json_data["app"],))
 
98
            matched_app = app_list.fetchone()
 
99
            if matched_app is None:
 
100
                # Does not exist, create one
 
101
                print("App '%s' does not exist. Creating." %(json_data["app"],))
 
102
                appid = int(random.random()*10000000)
 
103
                self.__cur.execute(config.insert_app_command, (appid, json_data["app"], json_data["appname"]))
 
104
            else:
 
105
                # Exists. Use it
 
106
                print("App '%s' already exists. Will be reused" %(matched_app[1],))
 
107
                appid = matched_app[0]
 
108
            
 
109
            # Inserting Description
 
110
            desc_list = json_data["desc"]
 
111
            for desc in desc_list:
 
112
                self.__cur.execute(config.insert_desc_command, (json_data["id"], desc["lang"], desc["desc"]))
 
113
            
 
114
            timestamp = int(datetime.datetime(1970,01,01).today().strftime("%s"))
 
115
             
 
116
            try:    
68
117
                # Inserting Trophy
 
118
                self.__cur.execute(config.insert_trophy_command, (  json_data["id"], \
 
119
                                                                    json_data["title"], \
 
120
                                                                    json_data["iconpath"], \
 
121
                                                                    appid, \
 
122
                                                                    setid, \
 
123
                                                                    int(json_data["priority"]), \
 
124
                                                                    bool(json_data["unlocked"]), \
 
125
                                                                    timestamp, \
 
126
                                                                    json_data["stockicon"]))
 
127
                
69
128
                # Inserting History
 
129
                self.__cur.execute(config.insert_history_command, (json_data["id"], timestamp))
 
130
                
 
131
                self.__conn.commit()
70
132
                pass
71
133
            except:
72
 
                print("Insertion Failed")
 
134
                raise
73
135
            
74
136
 
75
137
    def AwardTrophy(self, id):