summaryrefslogtreecommitdiff
path: root/app/deals/models.py
blob: 4f38cd1665728558cb657a3ed3f3d4449c97e338 (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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
import datetime
import os
from decimal import Decimal

from django.dispatch import receiver
from django.contrib.gis.db import models
from django.utils.html import format_html, format_html_join


class Brand(models.Model):
    name = models.CharField(max_length=200)

    class Meta:
        ordering = ('name',)

    def __str__(self):
        return self.name
    

class Deal(models.Model):
    brand = models.ForeignKey(Brand, on_delete=models.CASCADE, null=True)
    asin = models.CharField(max_length=200)
    category = models.CharField(max_length=200)
    brand_str = models.CharField(max_length=200)
    title = models.CharField(max_length=200, blank=True, null=True)
    prime_only = models.BooleanField(default=True)
    promo_type  = models.CharField(max_length=200, blank=True)
    deal_price = models.CharField(max_length=20, null=True)
    original_price = models.CharField(max_length=20, null=True)
    discount_percent_str = models.CharField(max_length=20, null=True)
    discount_percent = models.FloatField(null=True)
    url = models.CharField(max_length=200)
    deal_start_date = models.DateTimeField(null=True)
    deal_end_date = models.DateTimeField(null=True)

    class Meta:
        ordering = ('title',)

    def __str__(self):
        return self.title
    
    def dollars_off(self):
        return "{:.0f}".format(Decimal(self.original_price) - Decimal(self.deal_price))

    def get_deal_price_pretty(self):
        return "{:.0f}".format(Decimal(self.deal_price))

    def amazon_link(self):
        return format_html('<a target="_blank" href="%s">Amazon</a>' % (self.url))
        admin_link.short_description = 'Link'

    def search_wired(self):
        term = "https://www.google.com/search?q=%s" % (str(self.title))
        term = term+"+site%3Awired.com"
        return format_html("<a target='_blank' href='%s'>wired search</a>" % (term))
        admin_link.short_description = 'Link'

    def get_airtable_code(self):
        return format_html("<a target='_blank' href='/code/%s'>Get code</a>" % (self.pk))
        admin_link.short_description = 'Link'

    def search_ccc(self):
        return format_html("<a target='_blank' href='https://camelcamelcamel.com/product/%s'>Deal?</a>" % (self.asin))
        admin_link.short_description = 'Link'


class REIDeal(models.Model):
    brand_str = models.CharField(max_length=200)
    category = models.CharField(max_length=200)
    title = models.CharField(max_length=200, blank=True, null=True)
    deal_price = models.CharField(max_length=20, null=True)
    original_price = models.CharField(max_length=20, null=True)
    discount_percent_str = models.CharField(max_length=20, null=True)
    url = models.CharField(max_length=200)
    generate_deal = models.BooleanField(default=False)

    class Meta:
        ordering = ('title',)


    def get_deal_price_pretty(self):
        try:
            return "{:.0f}".format(Decimal(self.deal_price))
        except:
            return None
    
    def dollars_off(self):
        try:
            return "{:.0f}".format(Decimal(self.original_price) - Decimal(self.deal_price))
        except:
            return None
    
    def rei_link(self):
        return format_html('<a target="_blank" href="https://%s">REI</a>' % (self.url))
        admin_link.short_description = 'Link'
    
    def __str__(self):
        return self.title
    

class MyDeal(models.Model):
    title = models.CharField(max_length=200, blank=True, null=True)
    lookup_title = models.CharField(max_length=200, blank=True, null=True)
    asin = models.CharField(max_length=200, blank=True, null=True)
    brand = models.CharField(max_length=200, blank=True, null=True)
    retailer = models.CharField(max_length=200, blank=True, null=True)
    secondary_retailer = models.CharField(max_length=200, blank=True, null=True)
    tertiary_retailer = models.CharField(max_length=200, blank=True, null=True)
    url = models.CharField(max_length=200)
    secondary_url = models.CharField(max_length=200, blank=True, null=True)
    tertiary_url = models.CharField(max_length=200, blank=True, null=True)
    body = models.TextField(null=True, blank=True)
    blurb = models.TextField(null=True)
    added = models.BooleanField(default=False)

    class Meta:
        ordering = ('title',)

    def __str__(self):
        return self.title

    def amazon_link(self):
        return format_html('<a target="_blank" href="%s">%s</a>' % (self.url, self.asin))
        admin_link.short_description = 'Link'

    def search_wired(self):
        term = "https://www.google.com/search?q=%s" % (str(self.lookup_title))
        term = term+"+site%3Awired.com"
        return format_html("<a target='_blank' href='%s'>wired search</a>" % (term))
        admin_link.short_description = 'Link'

    def search_ccc(self):
        return format_html("<a target='_blank' href='https://camelcamelcamel.com/product/%s'>Deal?</a>" % (self.asin))
        admin_link.short_description = 'Link'
"""

for deal in Deal.objects.all():
    if deal.promo_type == "Lightning Deal":
        deal.delete()

with open(path) as f:
    reader = csv.reader(f)
    count = 0
    for row in reader:
        if count > 0
            print(row)
        count = count+1

with open(path) as f:
    reader = csv.reader(f)
    count = 0
    for row in reader:
        print(row)
        count = count+1


import csv
from datetime import datetime
path = "pdelectronicsdata.csv"
with open(path) as f:
    reader = csv.reader(f)
    count = 0
    for row in reader:
        if count > 0:
            print(row)
            if row[21] == "Y":
                prime = True
            else:
                prime = False
            _, created = Deal.objects.get_or_create(
                asin=row[0],
                category=row[8],
                brand_str=row[10],
                title=row[1],
                prime_only=prime,
                promo_type=row[23],
                deal_price= row[29],
                original_price = row[30],
                discount_percent_str=row[35],
                url=row[42],
                deal_start_date=datetime.strptime(row[24], "%m/%d/%Y")
                deal_end_date=datetime.strptime(row[25], "%m/%d/%Y")
            )
        count = count+1



b, created = Brand.objects.get_or_create(
    name = deal.brand_str
)
deal.brand = b
deal.save()




















with open(path) as f:
    reader = csv.reader(f)
    count = 0
    for row in reader:
        if count > 0
        if row[5] == "Y":
            prime = True
        else:
            prime = False
        _, created = Deal.objects.get_or_create(
            print(row)
            asin=row[0],
            cateogry="New",
            brand=row[7],
            title=row[18],
            prime_only=row[19],
            promo_type=row[21],
            deal_price= row[29],
            original_price = row[31],
            discount_percent=row[32],
            url=row[34],
        )

            title, prime_only, promo_type, deal_price, discount_percent, url)


        _, created = Deal.objects.get_or_create(
            asin=row[0],
            cateogry=row[1],
            brand=row[2],
            title=row[4],
            prime_only=row[5],
            prime_only=row[6],
            deal_price=row[10],
            discount_percent=row[13],
            url=row[14],
        )
        # creates a tuple of the new object or
        # current object and a boolean of if it was created
"""

"""

parse airtable csv:

import tldextract, csv
path = ''

with open(path) as f:
    reader = csv.reader(f)
    count = 0
    for row in reader:
        if count > 0:
            domain = tldextract.extract(row[2])
            retailer = domain.domain.capitalize()
            d = '#### [%s](%s)\n\n+++button-group\n\n[%s](%s "%s"){: target="_blank"}\n\n+++\n\n%s\n\n' %(str(row[0]).strip(), row[2], retailer, row[2], retailer, row[4])
            with open('out.txt', 'a') as the_file:
                the_file.write(d)
            print(d)
        count = count+1


import tldextract, csv
path = ''

with open(path) as f:
    reader = csv.reader(f)
    count = 0
    for row in reader:
        if count > 0:
            domain = tldextract.extract(row[2])
            retailer = domain.domain.capitalize()
            title = str(row[0].strip())
            try:
                stitle = title.split('for')[0]
            except:
                stitle=title
            try:
                asin = str(row[2]).split("/dp/")[1]
            except:
                asin = ''
            d, created = MyDeal.objects.get_or_create(
                title = str(row[0].strip()),
                lookup_title = stitle,
                asin = asin,
                retailer = retailer,
                url = str(row[2].strip()),
                body = row[4]
            )
        count = count+1



import csv
path = "rei.csv"
with open(path) as f:
    reader = csv.reader(f)
    count = 0
    for row in reader:
        if count > 0:
            print(row)
            try:

            if row[4] == "Y":
                prime = True
            else:
                prime = False
            _, created = Deal.objects.get_or_create(
                asin=row[0],
                category=row[1],
                brand_str=row[2],
                title=row[3],
                prime_only=prime,
                promo_type=row[5],
                deal_price= row[9],
                original_price = row[10],
                discount_percent_str=row[12],
                url=row[13],
            )
        count = count+1




+++button-group

[Amazon]({{object.url}} "Amazon"){: target="_blank"}

+++
"""

"""
asin = 3
deal_type = 4
name = 6
url = 7
deal price = 9
price = 8
percent_discount_str = 10
prime only = 11




from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By

driver = webdriver.Firefox()
driver.get("http://www.python.org")



"""

"""

REI Importer


with open(path) as f:
    reader = csv.reader(f)
    count = 0
    for row in reader:
        _, created = REIDeal.objects.get_or_create(
            category=row[3],
            brand_str=row[5],
            title=row[8],
            deal_price= row[12],
            original_price = row[11],
            discount_percent_=row[13],
            url=row[15],
        )
"""