Subversion Repositories SmartDukaan

Rev

Rev 264 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
144 ashish 1
'''
2
Created on 12-May-2010
3
 
4
@author: gaurav
5
'''
6
from datastore.DataDefinition import * #Phones, init, PhoneItem
7
 
8
from elixir import *
173 ashish 9
import datetime
144 ashish 10
 
11
 
12
class DataHelper:
264 ashish 13
    """
14
    Documentation for class Datahelper
15
    This class contains various methods to access the database tables
16
    crawl_id is used to retain past data for comparison, on each new crawl a new crawl_id is generated
17
    """
144 ashish 18
    def __init__(self):
238 ashish 19
        #init()
20
        pass
21
 
22
    def initxy(self):
264 ashish 23
        """
24
        Documentation for method initxy
25
        It calls a method init() so that when one needs to access the helper methods of 
26
        this class to access database, the database tables are in the scope 
27
        """
144 ashish 28
        init()
238 ashish 29
        #pass
144 ashish 30
 
264 ashish 31
    '''
144 ashish 32
    def add_vendor(self,name,url):
238 ashish 33
        try:
34
            v = Vendor.query.filter_by(name=name)
35
            v = v.filter_by(url=url).one()
36
            return
37
        except:
38
            v = Vendor()
39
            v.v_name = name
40
            v.v_url = url
41
            session.commit() 
144 ashish 42
 
43
    def get_all_vendors(self):
44
        vi = Vendor.query.all()
45
        return vi
46
 
264 ashish 47
    def get_all_phones(self):
48
        phones = PhoneItem.query.all()
49
        return phones
144 ashish 50
 
51
    def add_new_phone(self, url, name, source):
238 ashish 52
        try:
53
            pi = PhoneItem.query.filter_by(name=name)
54
            pi = pi.filter_by(url=url)
55
            pi = pi.filter_by(source=source).one()
56
            return
57
        except:
58
            pi = PhoneItem()
59
            pi.name = name
60
            pi.url = url
61
            pi.source = source
62
            session.commit()
63
 
264 ashish 64
    def add_mobstoreurl(self,url):
65
        try:
66
            ai = themobilestoreurls.query.filter_by(url=url).one()
67
            return
68
        except:
69
            ai = themobilestoreurls()
70
            ai.url = url
71
            session.commit()
72
 
73
    def get_allmobstoreurls(self):
74
        ai = themobilestoreurls.query.all()
75
        return ai
76
 
77
    def add_new_mobstorephone(self,name,shown_pr,final_pr):
78
        try:
79
            ai = themobilestorephones.query.filter_by(name=name)
80
            ai = ai.filter_by(shown_price=shown_pr)
81
            ai = ai.filter_by(final_price=final_pr).one()
82
            return
83
        except:    
84
            ai = themobilestorephones()
85
            ai.name = name
86
            ai.shown_price = shown_pr
87
            ai.final_price = final_pr
88
            session.commit()
89
 
90
    def get_allmobstorephones(self):
91
        ai = themobilestorephones.query.all()
92
        return ai
93
 
94
    def add_pricesbolourl(self,url):
95
        try:
96
            ai = pricesbolourls.query.filter_by(url=url).one()
97
            return
98
        except:
99
            ai = pricesbolourls()
100
            ai.url = url
101
            session.commit()    
102
 
103
    def get_allpricesbolourl(self):
104
        vi = pricesbolourls.query.all()
105
        return vi
106
 
107
 
108
    def set_all_crawled(self,bval):
109
        for ph in self.get_all_phones():
110
            ph.is_crawled = bval
111
        session.commit()    
112
 
113
    def set_crawled(self,url,bval):
114
        for ph in self.get_all_phones():
115
            if ph.url == url:
116
                ph.is_crawled = bval
117
        session.commit()                     
118
 
144 ashish 119
    def add_price(self,url,price):
238 ashish 120
        try:
121
            p = prices.query.filter_by(price=price)
122
            p = p.filter_by(url=url).one()
123
            return
124
        except:
125
            for ph in self.get_all_phones():
126
                if ph.url == url:
127
                    ph.price = price
264 ashish 128
            session.commit()        
129
    '''
130
 
131
    def add_newcrawler(self):
132
        """
133
        Documentation for method add_newcrawler 
134
        This method is used to add a new crawlid in crawl's table
135
        """
136
        cl = crawl()
137
        now = datetime.datetime.now()
138
        cl.crawled_date = str(now)
139
        session.commit()
144 ashish 140
 
264 ashish 141
    def get_latestcrawler(self):
142
        """
143
        Documentation for method get_latestcrawler 
144
        This method is used to get the latest crawler from the crawl's table
145
        """
146
        cl = crawl.query.all()
147
        sz = len(cl)
148
        cl = crawl.query.filter_by(id=sz).one()
149
        return cl
150
 
151
    def get_latestcrawlerid(self):
152
        """
153
        Documentation for method get_latestcrawlerid 
154
        This method is used to get the latest crawler-id from the crawl's table
155
        """
156
        cl = crawl.query.all()
157
        sz = len(cl)
158
        cl = crawl.query.filter_by(id=sz).one().id
159
        return cl
160
 
144 ashish 161
    def add_infiphone(self,name,shown_price,final_price):
264 ashish 162
        """
163
        Documentation for method add_infiphone 
164
        This method is used to add a phone in infibeam's table
165
        """
238 ashish 166
        try:
264 ashish 167
            ai = infibeam_data.query.filter_by(name=name)
168
            cid = self.get_latestcrawlerid()
169
            ai = ai.filter_by(crawl_id=cid).one()
238 ashish 170
            return
171
        except:
172
            ai = infibeam_data()
173
            ai.name = name
174
            ai.shown_price = shown_price
175
            ai.final_price = final_price
264 ashish 176
            cid = self.get_latestcrawlerid()
177
            ai.crawl_id = cid
238 ashish 178
            session.commit()
179
 
264 ashish 180
    def get_all_infibeam_data(self):
181
        """
182
        Documentation for method get_all_infibeam_data 
183
        This method is used to retrieve all the phones in infibeam's table
184
        """
185
        cid = self.get_latestcrawlerid()
274 ashish 186
        phones = infibeam_data.query.filter_by(crawl_id=cid).all()
264 ashish 187
        return phones
188
 
150 ashish 189
    def add_univervendor(self,name,site):
264 ashish 190
        """
191
        Documentation for method add_univervendor 
192
        This method is used to add a vendor in univercell's table
193
        """
238 ashish 194
        try:
195
            ai = univercell_data.query.filter_by(name=name)
264 ashish 196
            ai = ai.filter_by(site=site)
197
            cid = self.get_latestcrawlerid()
198
            ai = ai.filter_by(crawl_id=cid).one()
238 ashish 199
            return
200
        except:
201
            ai = univercell_data()
202
            ai.v_name = name
203
            ai.v_site = site
264 ashish 204
            cid = self.get_latestcrawlerid()
205
            ai.crawl_id = cid
238 ashish 206
            session.commit()
207
 
150 ashish 208
    def get_all_univervendors(self):
264 ashish 209
        """
210
        Documentation for method get_all_univervendor 
211
        This method is used to retrieve all the vendors in univercell's table
212
        """
213
        cid = self.get_latestcrawlerid()
274 ashish 214
        vi = univercell_data.query.filter_by(crawl_id=cid).all()
150 ashish 215
        return vi
216
 
217
    def add_new_univerphone(self,name,shown_price,final_price):
264 ashish 218
        """
219
        Documentation for method add_univerphone 
220
        This method is used to add a phone in univercell's table
221
        """
238 ashish 222
        try:
223
            ai = univercell_items.query.filter_by(name=name)
224
            ai = ai.filter_by(shown_price=shown_price)
264 ashish 225
            ai = ai.filter_by(final_price=final_price)
226
            cid = self.get_latestcrawlerid()
227
            ai = ai.filter_by(crawl_id=cid).one()
238 ashish 228
            return
229
        except:
230
            ai = univercell_items()
231
            ai.p_title = name
232
            ai.p_shown_price = shown_price
233
            ai.p_final_price = final_price
264 ashish 234
            cid = self.get_latestcrawlerid()
235
            ai.crawl_id = cid
238 ashish 236
            session.commit()
237
 
264 ashish 238
    def get_all_univercell_phones(self):
239
        """
240
        Documentation for method get_all_univercell_phones 
241
        This method is used to retrieve all the phones in univercell's table
242
        """
243
        cid = self.get_latestcrawlerid()
274 ashish 244
        phones = univercell_items.query.filter_by(crawl_id=cid).all()
264 ashish 245
        return phones
238 ashish 246
 
173 ashish 247
    def add_naaptolurl(self,url):
264 ashish 248
        """
249
        Documentation for method add_naaptolurl 
250
        This method is used to add a url for phone in naaptol's table
251
        These are taken from sitemap.xml
252
        """
238 ashish 253
        try:
264 ashish 254
            n = naaptolurls.query.filter_by(url=url)
255
            cid = self.get_latestcrawlerid()
256
            n = n.filter_by(crawl_id=cid).one()
238 ashish 257
            return
258
        except:
264 ashish 259
            n = naaptolurls()
260
            n.url = url
261
            cid = self.get_latestcrawlerid()
262
            n.crawl_id = cid
238 ashish 263
            session.commit()
173 ashish 264
 
264 ashish 265
    def get_allnaaptolurls(self):
266
        """
267
        Documentation for method get_allnaaptolurls 
268
        This method is used to retrieve all the url for phones in naaptol's table
269
        """
270
        cid = self.get_latestcrawlerid()
274 ashish 271
        ai = naaptolurls.query.filter_by(crawl_id=cid).all()
264 ashish 272
        return ai
273
 
173 ashish 274
    def add_morenaaptolurl(self,url):
264 ashish 275
        """
276
        Documentation for method add_morenaaptolurl 
277
        This method is used to add a url for phone in naaptol's table
278
        These are the urls generated by replacing 'price' with 'features'
279
        which were redirected
280
        """
238 ashish 281
        try:
264 ashish 282
            n = morenaaptolurls.query.filter_by(url=url)
283
            cid = self.get_latestcrawlerid()
284
            n = n.filter_by(crawl_id=cid).one()
238 ashish 285
            return
286
        except:
287
            try:
264 ashish 288
                n = morenaaptolurls.query.filter_by(url=url)
289
                cid = self.get_latestcrawlerid()
290
                n = n.filter_by(crawl_id=cid).one()
238 ashish 291
                return
292
            except:
264 ashish 293
                n = morenaaptolurls()
294
                n.url = url
295
                cid = self.get_latestcrawlerid()
296
                n.crawl_id = cid
238 ashish 297
                session.commit()        
173 ashish 298
 
299
    def get_allmorenaaptolurls(self):
264 ashish 300
        """
301
        Documentation for method get_allmorenaaptolurls 
302
        This method is used to retrieve all the extra urls for phones in naaptol's table
303
        """
304
        cid = self.get_latestcrawlerid()
274 ashish 305
        ai = morenaaptolurls.query.filter_by(crawl_id=cid).all()
173 ashish 306
        return ai
264 ashish 307
 
238 ashish 308
 
173 ashish 309
    def add_new_naaptolphone(self,name,range):
264 ashish 310
        """
311
        Documentation for method add_new_naaptolphone 
312
        This method is used to add a phone in naaptol's table
313
        """
173 ashish 314
        temp = name.lower()
264 ashish 315
        cid = self.get_latestcrawlerid()
173 ashish 316
        if temp.find("null") != -1:
317
            return
318
        for n in self.get_allnaaptolphones(): 
319
            if n.name == name:
320
                if n.range == range:
264 ashish 321
                    if n.crawl_id == cid:
173 ashish 322
                        return                            
323
        ai = naaptolphones()
324
        ai.name = name
325
        ai.range = range
264 ashish 326
        ai.crawl_id = self.get_latestcrawlerid()
173 ashish 327
        session.commit()
328
 
264 ashish 329
    def get_naaptolphone(self, name, range):
330
        """
331
        Documentation for method get_naaptolphone 
332
        This method is used to retrieve a phone in naaptol's table
333
        given its name and range
334
        """
335
        query = naaptolphones.query.filter_by(name=name)
336
        query = query.filter_by(range=range)
337
        cid = self.get_latestcrawlerid()
338
        query = query.filter_by(crawl_id=cid)
339
        return query.one()
340
 
341
    def get_allnaaptolphones(self):
342
        """
343
        Documentation for method get_allnaaptolphones 
344
        This method is used to retrieve all the phone in naaptol's table
345
        """
346
        cid = self.get_latestcrawlerid()
274 ashish 347
        ai = naaptolphones.query.filter_by(crawl_id=cid).all()
264 ashish 348
        return ai    
349
 
173 ashish 350
    def add_new_ntonlinesp(self,nid,name,price):
264 ashish 351
        """
352
        Documentation for method add_new_ntonlinesp 
353
        This method is used to add a online-supplier for a particular phone
354
        in naaptol's table
355
        """
238 ashish 356
        try:
357
            n = ntonlinesp.query.filter_by(nid=nid)
264 ashish 358
            cid = self.get_latestcrawlerid()
359
            n = n.filter_by(crawl_id=cid)
238 ashish 360
            n = n.filter_by(name=name).one()
361
            return
362
        except:    
363
            ai = ntonlinesp()
364
            ai.nid = nid
365
            ai.name = name
366
            ai.price = price
264 ashish 367
            cid = self.get_latestcrawlerid()
368
            ai.crawl_id = cid
238 ashish 369
            session.commit()
370
 
264 ashish 371
    def get_allntonlinesp(self):
372
        """
373
        Documentation for method get_allntonlinesp 
374
        This method is used to retrieve all the online-suppliers in naaptol's table
375
        """
376
        cid = self.get_latestcrawlerid()
274 ashish 377
        ai = ntonlinesp.query.filter_by(crawl_id=cid).all()
264 ashish 378
        return ai
379
 
380
    def get_ntonlinespbynid(self,nid):
381
        """
382
        Documentation for method get_allntonlinespbynid 
383
        This method is used to retrieve all the online-supplier for a particular phone 
384
        in naaptol's table given the id of phone in naaptolphones
385
        """
386
        cid = self.get_latestcrawlerid()
274 ashish 387
        ai = ntonlinesp.query.filter_by(crawl_id=cid).all()
264 ashish 388
        return ai
389
 
173 ashish 390
    def add_new_ntofflinesp(self,nid,name,price):
264 ashish 391
        """
392
        Documentation for method add_new_ntofflinesp 
393
        This method is used to add a offline-supplier for a particular phone
394
        in naaptol's table
395
        """
238 ashish 396
        try:
397
            n = ntofflinesp.query.filter_by(nid=nid)
264 ashish 398
            cid = self.get_latestcrawlerid()
399
            n = n.filter_by(crawl_id=cid)
238 ashish 400
            n = n.filter_by(name=name).one()
401
            return
402
        except:    
403
            ai = ntofflinesp()
404
            ai.nid = nid
405
            ai.name = name
406
            ai.price = price
264 ashish 407
            cid = self.get_latestcrawlerid()
408
            ai.crawl_id = cid
238 ashish 409
            session.commit()
410
 
173 ashish 411
 
264 ashish 412
    def get_allntofflinesp(self):
413
        """
414
        Documentation for method get_allntofflinesp 
415
        This method is used to retrieve all the offline-suppliers in naaptol's table
416
        """
417
        cid = self.get_latestcrawlerid()
274 ashish 418
        ai = ntofflinesp.query.filter_by(crawl_id=cid).all()
173 ashish 419
        return ai
420
 
264 ashish 421
    def get_ntofflinespbynid(self,nid):
422
        """
423
        Documentation for method get_allntolinespbynid 
424
        This method is used to retrieve all the offline-supplier for a particular phone 
425
        in naaptol's table given the id of phone in naaptolphones
426
        """
427
        ai = ntofflinesp.query.filter_by(nid=nid)
428
        cid = self.get_latestcrawlerid()
274 ashish 429
        ai = ai.filter_by(crawl_id=cid).all()
173 ashish 430
        return ai
431
 
264 ashish 432
    def add_new_mobstorephone_new(self,name,shown_pr,final_pr,extra_info):
433
        """
434
        Documentation for method add_new_mobstorephone_new 
435
        This method is used to add a phone in themobilestore's table
436
        """
437
        try:
438
            ai = themobilestorephones_new.query.filter_by(name=name)
439
            cid = self.get_latestcrawlerid()
440
            ai = ai.filter_by(crawl_id=cid).one()
441
            return
442
        except:    
443
            ai = themobilestorephones_new()
444
            ai.name = name
445
            ai.shown_price = shown_pr
446
            ai.final_price = final_pr
447
            ai.extra_info = extra_info
448
            cid = self.get_latestcrawlerid()
449
            ai.crawl_id=cid
450
            session.commit()
173 ashish 451
 
264 ashish 452
    def get_allmobstorephones_new(self):
453
        """
454
        Documentation for method get_allmobstorephone_new 
455
        This method is used to retrieve all the phones in themobilestore's table
456
        """
457
        cid = self.get_latestcrawlerid()
274 ashish 458
        ai = themobilestorephones_new.query.filter_by(crawl_id=cid).all()
173 ashish 459
        return ai
460
 
238 ashish 461
    def add_babuchakurl(self,url,no_pages):
264 ashish 462
        """
463
        Documentation for method add_babuchakurl 
464
        This method is used to add a url for vendor in babuchak's table
465
        """
238 ashish 466
        try:
264 ashish 467
            ai = babuchak_urls.query.filter_by(url=url)
468
            cid = self.get_latestcrawlerid()
469
            ai = ai.filter_by(crawl_id=cid).one()
238 ashish 470
            return
471
        except:    
472
            ai = babuchak_urls()
473
            ai.url = url
264 ashish 474
            ai.no_pages = no_pages
475
            cid = self.get_latestcrawlerid()
476
            ai.crawl_id=cid 
238 ashish 477
            session.commit()
173 ashish 478
 
238 ashish 479
    def get_allbabuchakurls(self):
264 ashish 480
        """
481
        Documentation for method get_allbabuchakurls 
482
        This method is used to retrieve all the vendor-urls in babuchak's table
483
        """
484
        cid = self.get_latestcrawlerid()
274 ashish 485
        ai = babuchak_urls.query.filter_by(crawl_id=cid).all()
238 ashish 486
        return ai
487
 
488
    def add_babuchakphoneurl(self,url):
264 ashish 489
        """
490
        Documentation for method add_babuchakphoneurl 
491
        This method is used to add a url for phone in babuchak's table
492
        """
238 ashish 493
        try:
264 ashish 494
            ai = babuchak_phoneurls.query.filter_by(url=url)
495
            cid = self.get_latestcrawlerid()
496
            ai = ai.filter_by(crawl_id=cid).one()
238 ashish 497
            return
498
        except:    
499
            ai = babuchak_phoneurls()
500
            ai.url = url 
264 ashish 501
            cid = self.get_latestcrawlerid()
502
            ai.crawl_id=cid
238 ashish 503
            session.commit()
504
 
505
    def get_allbabuchakphoneurls(self):
264 ashish 506
        """
507
        Documentation for method get_allbabuchakphoneurls 
508
        This method is used to retrieve all the phone-urls in babuchak's table
509
        """
510
        cid = self.get_latestcrawlerid()
274 ashish 511
        ai = babuchak_phoneurls.query.filter_by(crawl_id=cid).all()
238 ashish 512
        return ai
513
 
514
    def add_babuchakphone(self,name,shown_price,final_price):
264 ashish 515
        """
516
        Documentation for method add_babuchakphone 
517
        This method is used to add a phone in babuchak's table
518
        """
238 ashish 519
        try:
264 ashish 520
            ai = babuchak_phones.query.filter_by(name=name)
521
            cid = self.get_latestcrawlerid()
522
            ai = ai.filter_by(crawl_id=cid).one()
238 ashish 523
            return
524
        except:
525
            ai = babuchak_phones()
526
            ai.name = name
527
            ai.shown_price = shown_price
528
            ai.final_price = final_price    
264 ashish 529
            cid = self.get_latestcrawlerid()
530
            ai.crawl_id=cid
238 ashish 531
            session.commit()
532
 
533
    def get_allbabuchakphones(self):
264 ashish 534
        """
535
        Documentation for method get_allbabuchakphones 
536
        This method is used to retrieve all the phone in babuchak's table
537
        """
538
        cid = self.get_latestcrawlerid()
274 ashish 539
        ai = babuchak_phones.query.filter_by(crawl_id=cid).all()
238 ashish 540
        return ai       
264 ashish 541
 
542
    def add_ipbasic(self,name,site):
543
        """
544
        Documentation for method add_ipbasic 
545
        This method is used to add a url for phone in indiaplaza's table
546
        """
547
        try:
548
            ai = indiaplaza_data.query.filter_by(name=name)
549
            ai = ai.filter_by(site=site).one()
550
            cid = self.get_latestcrawlerid()
551
            ai = ai.filter_by(crawl_id=cid).one()
552
            return
553
        except:
554
            ai = indiaplaza_data()
555
            ai.v_name = name
556
            ai.v_site = site
557
            cid = self.get_latestcrawlerid()
558
            ai.crawl_id=cid
559
            session.commit()
560
 
173 ashish 561
    def get_all_ipbasic(self):
264 ashish 562
        """
563
        Documentation for method get_all_ipbasic 
564
        This method is used to retrieve all phone-urls in indiaplaza's table
565
        """
566
        cid = self.get_latestcrawlerid()
274 ashish 567
        vi = indiaplaza_data.query.filter_by(crawl_id=cid).all()
173 ashish 568
        return vi
569
 
570
    def add_ipextra(self,name,shown_price,final_price,guarantee,shipinfo):
264 ashish 571
        """
572
        Documentation for method add_ipextra 
573
        This method is used to add a phone in indiaplaza's table
574
        """
238 ashish 575
        try:
576
            ai = indiaplaza_items.query.filter_by(p_name=name)
577
            ai = ai.filter_by(p_shown_price=shown_price)
578
            ai = ai.filter_by(p_final_price=final_price)
579
            ai = ai.filter_by(p_guaranteeinfo=guarantee)
264 ashish 580
            cid = self.get_latestcrawlerid()
581
            ai = ai.filter_by(crawl_id=cid)
238 ashish 582
            ai = ai.filter_by(p_shipinfo=shipinfo).one()
583
            return
584
        except:
585
            ai = indiaplaza_items()
586
            ai.p_name = name
587
            ai.p_shown_price = shown_price
588
            ai.p_final_price = final_price
589
            ai.p_guaranteeinfo = guarantee
590
            ai.p_shipinfo = shipinfo
264 ashish 591
            cid = self.get_latestcrawlerid()
592
            ai.crawl_id=cid
238 ashish 593
            session.commit()
173 ashish 594
 
595
    def get_all_indiaplaza_phones(self):
264 ashish 596
        """
597
        Documentation for method get_all_indiaplaza_phones 
598
        This method is used to retrieve all the phones in indiaplaza's table
599
        """
600
        cid = self.get_latestcrawlerid()
274 ashish 601
        phones = indiaplaza_items.query.filter_by(crawl_id=cid).all()
173 ashish 602
        return phones
264 ashish 603
 
604
    def set_extra_vars(self,var,val,desc):    
605
        """
606
        Documentation for method set_extra_vars  
607
        This method is used to add a variable for a particular supplier.
608
        Some suppliers has no fixed count of number of pages on which they have data
609
        so to deal with that this function is used to set variables. 
610
        """
611
        try:
612
            tm = extra_vars.query.filter_by(var=var).one()
613
            tm.val = val
614
            tm.desc = desc
615
        except:
616
            tm = extra_vars()
617
            tm.var = var
618
            tm.val = val
619
            tm.desc = desc       
620
 
621
    def get_extra_vars(self,var):
622
        """
623
        Documentation for method get_extra_vars  
624
        This method is used to retrieve a variable's value given its name 
625
        """
626
        try:
627
            #print "in try"
628
            tm = extra_vars.query.filter_by(var=var).one()
629
        except:
630
            #print "in except"
631
            tm = "EMPTY"
632
        return tm.val
173 ashish 633
 
264 ashish 634
    def add_supplier(self,name,site):
635
        """
636
        Documentation for method add_suppliers 
637
        This method is used to add a supplier in supplier's table
638
        """
639
        for s in self.get_all_suppliers():
640
            if s.name == name:
641
                now = datetime.datetime.now()
642
                s.last_crawled = str(now)
643
                session.commit()
644
                return             
645
        ai = suppliers()
646
        ai.name = name
647
        ai.site = site
648
        now = datetime.datetime.now()
649
        ai.last_crawled = str(now)
650
        session.commit()
651
 
173 ashish 652
    def get_all_suppliers(self):
264 ashish 653
        """
654
        Documentation for method get_all_suppliers 
655
        This method is used to retrieve all the suppliers from the supplier's table
656
        """
173 ashish 657
        sup = suppliers.query.all()
658
        return sup
659
 
660
    def get_suppId(self,name):
264 ashish 661
        """
662
        Documentation for method get_suppId 
663
        This method is used to retrieve id of the supplier given his name
664
        from the supplier's table
665
        """
173 ashish 666
        for s in self.get_all_suppliers():
667
            if s.name == name:
668
                return s.id
669
 
670
    def get_supp_byId(self,id):
264 ashish 671
        """
672
        Documentation for method get_supp_byId 
673
        This method is used to retrieve a supplier given his id
674
        from the supplier's table
675
        """
173 ashish 676
        for s in self.get_all_suppliers():
677
            if s.id == id:
678
                return s
679
 
680
    def get_supp_byName(self,name):
264 ashish 681
        """
682
        Documentation for method get_suppbyName 
683
        This method is used to retrieve a supplier given his name
684
        from the supplier's table
685
        """
173 ashish 686
        supps = []
687
        for s in self.get_all_suppliers():
688
            if s.name == name:
689
                supps.append(s)
690
        return supps        
691
 
692
    def get_supp_bySite(self,site):
264 ashish 693
        """
694
        Documentation for method get_suppbySite 
695
        This method is used to retrieve a supplier given his url
696
        from the supplier's table
697
        """
173 ashish 698
        supps = []
699
        for s in self.get_all_suppliers():
700
            if s.site == site:
701
                supps.append(s)
702
        return supps
703
 
264 ashish 704
    def add_models(self,brand,model):
705
        """
706
        Documentation for method add_models 
707
        This method is used to add a model in model's table
708
        """
709
        cid = self.get_latestcrawlerid()
710
        for m in self.get_all_models():
711
            if m.brand == brand:
712
                if m.model == model:
713
                    if m.crawl_id == cid:
714
                        return                     
715
        ai = models()
716
        ai.brand = brand
717
        ai.model = model
718
        ai.crawl_id = cid
719
        session.commit()
720
 
721
    def get_all_models(self):
722
        """
723
        Documentation for method get_all_models 
724
        This method is used to retrieve all the model from the model's table
725
        """
726
        cid = self.get_latestcrawlerid()
274 ashish 727
        mod = models.query.filter_by(crawl_id=cid).all()
264 ashish 728
        return mod
173 ashish 729
 
730
    def get_modId(self,brand,model):
264 ashish 731
        """
732
        Documentation for method get_modId 
733
        This method is used to retrieve the id of a model
734
        given its name from the model's table
735
        """
736
        cid = self.get_latestcrawlerid()
173 ashish 737
        for m in self.get_all_models():
738
            if m.brand == brand:
739
                if m.model == model:
264 ashish 740
                    if m.crawl_id == cid:
741
                        return m.id                     
173 ashish 742
 
743
    def get_modbyId(self,id):
264 ashish 744
        """
745
        Documentation for method get_modId 
746
        This method is used to retrieve a model given its id from the model's table
747
        """
748
        cid = self.get_latestcrawlerid()
173 ashish 749
        for m in self.get_all_models():
264 ashish 750
            if m.id == id and m.crawl_id == cid:
173 ashish 751
                return m                     
752
 
753
    def get_modbyModel(self,model):
264 ashish 754
        """
755
        Documentation for method get_modId 
756
        This method is used to retrieve a model given its name from the model's table
757
        """
758
        cid = self.get_latestcrawlerid()
173 ashish 759
        for m in self.get_all_models():
264 ashish 760
            if m.model == model and m.crawl_id == cid:
173 ashish 761
                return m
238 ashish 762
 
173 ashish 763
    def get_modbyBrand(self,brand):
264 ashish 764
        """
765
        Documentation for method get_modId 
766
        This method is used to retrieve a model given its brand-name from the model's table
767
        """
768
        cid = self.get_latestcrawlerid()
173 ashish 769
        mods = []
770
        for m in self.get_all_models():
264 ashish 771
            if m.brand == brand and m.crawl_id == cid:
173 ashish 772
                mods.append(m)
773
        return mods                     
774
 
264 ashish 775
    def add_prices(self,mobile_id,supplier_id,quoted_price,final_price,extra_info):
776
        """
777
        Documentation for method add_prices 
778
        This method is used to add all the info for a phone in prices's table
779
        """
780
        cid = self.get_latestcrawlerid()
781
        for p in self.get_all_prices():
782
            if p.mobile_id == mobile_id:
783
                if p.supplier_id == supplier_id:
784
                    if p.extra_info == extra_info and p.crawl_id == cid:
785
                        return                           
786
        ai = prices()
787
        ai.mobile_id = mobile_id
788
        ai.supplier_id = supplier_id
789
        ai.quoted_price = quoted_price
790
        ai.final_price = final_price
791
        ai.extra_info = extra_info
792
        ai.crawl_id = cid
793
        session.commit()
794
 
795
    def get_all_prices(self):
796
        """
797
        Documentation for method get_all_prices 
798
        This method is used to retrieve all the info for all the phones in prices's table
799
        """
800
        cid = self.get_latestcrawlerid()
274 ashish 801
        pr = prices.query.filter_by(crawl_id=cid).all()
264 ashish 802
        return pr
803
 
173 ashish 804
    def get_prbyId(self,id):
264 ashish 805
        """
806
        Documentation for method get_prbyId 
807
        This method is used to retrieve all the info for a phone given its id in prices's table
808
        """
809
        cid = self.get_latestcrawlerid()
173 ashish 810
        for p in self.get_all_prices():
264 ashish 811
            if p.id == id and p.crawl_id == cid:
173 ashish 812
                return p                     
813
 
814
    def get_prbySid(self,supplier_id):
264 ashish 815
        """
816
        Documentation for method get_prbySid 
817
        This method is used to retrieve all the info for a phone in prices's table 
818
        given its supplier-id 
819
        """
820
        cid = self.get_latestcrawlerid()
173 ashish 821
        prc = []
822
        for p in self.get_all_prices():
264 ashish 823
            if p.supplier_id == supplier_id and p.crawl_id == cid:
173 ashish 824
                prc.append(p)
825
        return prc
826
 
827
    def get_prbyMid(self,mobile_id):
264 ashish 828
        """
829
        Documentation for method get_prbySid 
830
        This method is used to retrieve all the info for a phone in prices's table 
831
        given its model-id 
832
        """
833
        cid = self.get_latestcrawlerid()
173 ashish 834
        prc = []
835
        for p in self.get_all_prices():
264 ashish 836
            if p.mobile_id == mobile_id and p.crawl_id == cid:
173 ashish 837
                prc.append(p)
838
        return prc
264 ashish 839
 
173 ashish 840
    def get_price_by_model(self, model_id, supplier_id):
264 ashish 841
        """
842
        Documentation for method get_prbySid 
843
        This method is used to retrieve all the info for a phone in prices's table 
844
        given its model-name 
845
        """
846
        cid = self.get_latestcrawlerid()
173 ashish 847
        query = prices.query.filter_by(mobile_id=model_id)
848
        query = query.filter_by(supplier_id=supplier_id)
264 ashish 849
        query = query.filter_by(crawl_id=cid)
173 ashish 850
        return query.one()
851
 
852
    def add_gs_info(self,mid,guaranteeinfo,shipinfo):
264 ashish 853
        """
854
        Documentation for method add_gs_info 
855
        This method is used to add guarantee and ship info for a phone 
856
        """
857
        cid = self.get_latestcrawlerid()
238 ashish 858
        try:
859
            gs = guarantee_info.query.filter_by(mid=mid)
860
            gs = gs.filter_by(guaranteeinfo=guaranteeinfo)
264 ashish 861
            gs = gs.filter_by(crawl_id=cid)
238 ashish 862
            gs = gs.filter_by(shipinfo=shipinfo).one()
863
            return
864
        except:
865
            gs = guarantee_info()
866
            gs.mid = mid
867
            gs.guaranteeinfo = guaranteeinfo 
868
            gs.shipinfo = shipinfo
264 ashish 869
            gs.crawl_id = cid
238 ashish 870
            session.commit() 
871
 
173 ashish 872
    def get_all_gs_info(self):
264 ashish 873
        """
874
        Documentation for method get_all_gs_info 
875
        This method is used to retrieve guarantee and ship info for all the phones 
876
        """
877
        cid = self.get_latestcrawlerid()
274 ashish 878
        gsi = guarantee_info.query.filter_by(crawl_id=cid).all()
173 ashish 879
        return gsi
880
 
881
    def get_gs_bymid(self,mid):
264 ashish 882
        """
883
        Documentation for method get_gs_bymid 
884
        This method is used to retrieve guarantee and ship info for a phones
885
        given its phone-id 
886
        """
887
        cid = self.get_latestcrawlerid()
888
        gsi = guarantee_info.query.filter_by(crawl_id=cid).one()
889
        return gsi  
890