diff options
Diffstat (limited to 'app/deals')
-rw-r--r-- | app/deals/__init__.py | 0 | ||||
-rw-r--r-- | app/deals/admin.py | 16 | ||||
-rw-r--r-- | app/deals/build.py | 110 | ||||
-rw-r--r-- | app/deals/importer.py | 107 | ||||
-rw-r--r-- | app/deals/migrations/0001_initial.py | 32 | ||||
-rw-r--r-- | app/deals/migrations/0002_deal_original_price_alter_deal_deal_price.py | 23 | ||||
-rw-r--r-- | app/deals/migrations/0003_alter_deal_deal_price_alter_deal_original_price.py | 23 | ||||
-rw-r--r-- | app/deals/migrations/0004_alter_deal_discount_percent.py | 18 | ||||
-rw-r--r-- | app/deals/migrations/0005_deal_discount_percent_num.py | 18 | ||||
-rw-r--r-- | app/deals/migrations/__init__.py | 0 | ||||
-rw-r--r-- | app/deals/models.py | 79 |
11 files changed, 426 insertions, 0 deletions
diff --git a/app/deals/__init__.py b/app/deals/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/app/deals/__init__.py diff --git a/app/deals/admin.py b/app/deals/admin.py new file mode 100644 index 0000000..8c93ead --- /dev/null +++ b/app/deals/admin.py @@ -0,0 +1,16 @@ +from django.contrib import admin +from django import forms +from django.contrib.gis.admin import OSMGeoAdmin +from django.contrib.contenttypes.admin import GenericStackedInline + +from django_admin_listfilter_dropdown.filters import DropdownFilter, RelatedDropdownFilter, ChoiceDropdownFilter +from .models import Deal + + +@admin.register(Deal) +class DealAdmin(OSMGeoAdmin): + list_display = ('brand', 'title', 'category', 'deal_price', 'original_price', 'discount_percent', 'promo_type', 'amazon_link', 'search_wired') + search_fields = ['title', ] + list_filter = ('category', 'promo_type', ('brand', DropdownFilter),) + + diff --git a/app/deals/build.py b/app/deals/build.py new file mode 100644 index 0000000..47a6efe --- /dev/null +++ b/app/deals/build.py @@ -0,0 +1,110 @@ +from django.urls import reverse +from django.apps import apps +from builder.base import BuildNew +from itertools import chain + +from django.conf import settings +from .models import PostType + + +class BuildSrc(BuildNew): + + def get_model_queryset(self): + return self.model.objects.filter(post_type=PostType.SRC).filter(status__exact=1).order_by('-pub_date') + + def build(self): + self.build_list_view( + base_path=reverse("src:list"), + paginate_by=50 + ) + self.build_detail_view() + + +class BuildGuide(BuildNew): + + def get_model_queryset(self): + return self.model.objects.filter(post_type__in=[PostType.FIELD_TEST, PostType.REVIEW]).filter(status__exact=1).order_by('-pub_date') + + def build(self): + self.build_list_view( + base_path=reverse("guides:guide-base"), + paginate_by=50 + ) + self.build_detail_view() + + +class BuildFieldNotes(BuildNew): + + def get_model_queryset(self): + return self.model.objects.filter(post_type=PostType.FIELD_NOTE).filter(status__exact=1).order_by('-pub_date') + + def build(self): + self.build_detail_view() + self.build_list_view( + base_path=reverse("fieldnotes:list"), + paginate_by=24 + ) + self.build_year_view("fieldnotes:list_year") + self.build_month_view("fieldnotes:list_month") + + +class BuildJrnl(BuildNew): + ''' + Write jrnl to disk + ''' + def get_model_queryset(self): + return self.model.objects.filter(post_type=PostType.JRNL).filter(status__exact=1).order_by('-pub_date') + + def build(self): + self.build_list_view( + base_path=reverse("jrnl:list"), + paginate_by=24 + ) + self.build_year_view("jrnl:list_year") + self.build_month_view("jrnl:list_month") + self.build_detail_view() + self.build_location_view() + self.build_latest() + + def build_arc(self): + self.build_list_view( + base_path=reverse("jrnl:list"), + paginate_by=24 + ) + self.build_year_view("jrnl:list_year") + self.build_month_view("jrnl:list_month") + self.build_location_view() + + def build_location_view(self): + c = apps.get_model('locations', 'Country') + r = apps.get_model('locations', 'Region') + countries = c.objects.filter(visited=True) + regions = r.objects.all() + locations = list(chain(countries, regions)) + for c in locations: + try: + qs = self.model.objects.filter( + status__exact=1, + post_type=PostType.JRNL, + location__state__country=c + ) + except: + qs = self.model.objects.filter( + status__exact=1, + post_type=PostType.JRNL, + location__state__country__lux_region=c.id + ) + print(c) + pages = self.get_pages(qs, 24) + for page in range(pages): + base_path = reverse("jrnl:list_country", kwargs={'slug': c.slug, 'page': page + 1}) + response = self.client.get(base_path) + print(response.content) + if page == 0: + self.write_file(base_path, response.content) + else: + self.write_file(base_path, response.content) + + def build_latest(self): + response = self.client.get('/jrnl/latest/') + self.write_file(reverse("jrnl:latest"), response.content) diff --git a/app/deals/importer.py b/app/deals/importer.py new file mode 100644 index 0000000..7ed4782 --- /dev/null +++ b/app/deals/importer.py @@ -0,0 +1,107 @@ +for e in essaysold: + if e.featured_image: + feat = e.featured_image + else: + feat = None + if e.meta_description: + meta = e.meta_description + else: + meta = "need meta" + new, created = Post.objects.get_or_create( + old_id=e.pk, + post_type=2, + title=e.title, + subtitle=e.sub_title, + dek=e.dek, + slug=e.slug, + prologue_markdown=e.preamble, + body_markdown=e.body_markdown, + pub_date=e.pub_date, + enable_comments=e.enable_comments, + status=e.status, + meta_description=meta, + originally_published_by=e.originally_published_by, + originally_published_by_url=e.originally_published_by_url, + featured_image=feat, + has_video=e.has_video, + epilogue_markdown=e.afterword, + ) + print(created) + + + +# migrate jrnl to posts +for e in Entry.objects.all(): + if e.meta_description: + meta_description = e.meta_description + else: + meta_description = "needs" + if e.image: + old_image = e.image + else: + old_image = None + p, created = Post.objects.get_or_create( + old_id=e.pk, + title = e.title, + short_title = '', + subtitle = e.subtitle, + slug = e.slug, + body_markdown = e.body_markdown, + body_html = e.body_html, + dek = e.dek, + meta_description = meta_description, + pub_date = e.pub_date, + enable_comments = e.enable_comments, + status = e.status, + featured_image = e.featured_image, + post_type = PostType.JRNL, + template_name = e.template_name, + has_video = e.has_video, + point = e.point, + location = e.location, + old_image=old_image + ) + for b in e.books.all(): + c = Book.objects.get( + slug=b.slug, + title=b.title, + ) + p.books.add(c) + for f in e.field_notes.all(): + c = Post.objects.get( + slug=f.slug, + title=f.title, + ) + p.field_notes.add(c) + p.save() + +#Then after they're all in there: +ctype = ContentType.objects.get(app_label='posts',model='post') +oldctype = ContentType.objects.get(app_label='jrnl',model='entry') +for e in Entry.objects.all(): + p = Post.objects.get(title=e.title,old_id=e.id) + if e.related: + for t in e.related.all(): + if t.model_name == oldctype: + tp = ctype + else: + tp = t.model_name + c = RelatedPost.objects.get( + model_name=tp, + title=t.title, + slug=t.slug, + pub_date=t.pub_date + ) + p.related.add(c) + p.save() + +# Then to port comments: +ctype = ContentType.objects.get(app_label='posts',model='post') +oldctype = ContentType.objects.get(app_label='jrnl',model='entry') +for c in Comment.objects.filter(content_type=oldctype): + e = Entry.objects.get(pk=c.object_pk) + p = Post.objects.get(title=e.title,old_id=e.id) + c.object_pk = p.pk + c.content_type = ctype + print("%s --> %s" %(c.content_object,p)) + c.save() diff --git a/app/deals/migrations/0001_initial.py b/app/deals/migrations/0001_initial.py new file mode 100644 index 0000000..af6edd7 --- /dev/null +++ b/app/deals/migrations/0001_initial.py @@ -0,0 +1,32 @@ +# Generated by Django 4.2.2 on 2023-06-29 15:43 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + ] + + operations = [ + migrations.CreateModel( + name='Deal', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('asin', models.CharField(max_length=200)), + ('category', models.CharField(max_length=200)), + ('brand', models.CharField(max_length=200)), + ('title', models.CharField(blank=True, max_length=200, null=True)), + ('prime_only', models.BooleanField(default=True)), + ('promo_type', models.CharField(blank=True, max_length=200)), + ('deal_price', models.DecimalField(decimal_places=2, max_digits=6)), + ('discount_percent', models.DecimalField(decimal_places=2, max_digits=6)), + ('url', models.CharField(max_length=200)), + ], + options={ + 'ordering': ('title',), + }, + ), + ] diff --git a/app/deals/migrations/0002_deal_original_price_alter_deal_deal_price.py b/app/deals/migrations/0002_deal_original_price_alter_deal_deal_price.py new file mode 100644 index 0000000..ef7f544 --- /dev/null +++ b/app/deals/migrations/0002_deal_original_price_alter_deal_deal_price.py @@ -0,0 +1,23 @@ +# Generated by Django 4.2.2 on 2023-06-29 16:32 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('deals', '0001_initial'), + ] + + operations = [ + migrations.AddField( + model_name='deal', + name='original_price', + field=models.CharField(max_length=6, null=True), + ), + migrations.AlterField( + model_name='deal', + name='deal_price', + field=models.CharField(max_length=6, null=True), + ), + ] diff --git a/app/deals/migrations/0003_alter_deal_deal_price_alter_deal_original_price.py b/app/deals/migrations/0003_alter_deal_deal_price_alter_deal_original_price.py new file mode 100644 index 0000000..76a2112 --- /dev/null +++ b/app/deals/migrations/0003_alter_deal_deal_price_alter_deal_original_price.py @@ -0,0 +1,23 @@ +# Generated by Django 4.2.2 on 2023-06-29 16:34 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('deals', '0002_deal_original_price_alter_deal_deal_price'), + ] + + operations = [ + migrations.AlterField( + model_name='deal', + name='deal_price', + field=models.CharField(max_length=12, null=True), + ), + migrations.AlterField( + model_name='deal', + name='original_price', + field=models.CharField(max_length=12, null=True), + ), + ] diff --git a/app/deals/migrations/0004_alter_deal_discount_percent.py b/app/deals/migrations/0004_alter_deal_discount_percent.py new file mode 100644 index 0000000..d296217 --- /dev/null +++ b/app/deals/migrations/0004_alter_deal_discount_percent.py @@ -0,0 +1,18 @@ +# Generated by Django 4.2.2 on 2023-06-29 16:35 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('deals', '0003_alter_deal_deal_price_alter_deal_original_price'), + ] + + operations = [ + migrations.AlterField( + model_name='deal', + name='discount_percent', + field=models.CharField(max_length=12, null=True), + ), + ] diff --git a/app/deals/migrations/0005_deal_discount_percent_num.py b/app/deals/migrations/0005_deal_discount_percent_num.py new file mode 100644 index 0000000..1405d84 --- /dev/null +++ b/app/deals/migrations/0005_deal_discount_percent_num.py @@ -0,0 +1,18 @@ +# Generated by Django 4.2.2 on 2023-06-29 17:07 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('deals', '0004_alter_deal_discount_percent'), + ] + + operations = [ + migrations.AddField( + model_name='deal', + name='discount_percent_num', + field=models.FloatField(null=True), + ), + ] diff --git a/app/deals/migrations/__init__.py b/app/deals/migrations/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/app/deals/migrations/__init__.py diff --git a/app/deals/models.py b/app/deals/models.py new file mode 100644 index 0000000..b7d77d6 --- /dev/null +++ b/app/deals/models.py @@ -0,0 +1,79 @@ +import datetime +import os + +from django.dispatch import receiver +from django.contrib.gis.db import models +from django.utils.html import format_html, format_html_join + +class Deal(models.Model): + asin = models.CharField(max_length=200) + category = models.CharField(max_length=200) + brand = 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=12, null=True) + original_price = models.CharField(max_length=12, null=True) + discount_percent = models.CharField(max_length=12, null=True) + discount_percent_num = models.FloatField(null=True) + url = models.CharField(max_length=200) + + 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.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' +""" +import csv +path = "pdelectronicsdata.csv" +with open(path) as f: + reader = csv.reader(f) + count = 0 + for row in reader: + price = f'{num(row[10]):.2f}' + if row[5] == "Y": + prime = True + else: + prime = False + _, created = Deal.objects.get_or_create( + print(row) + count = 1 + asin=row[0], + cateogry=row[1], + brand=row[2], + title=row[4], + prime_only=row[5], + promo_type=row[6], + deal_price= Decimal(price), + discount_percent=row[13], + url=row[14], + ) + print(row[0], row[1], row[2], row[4],row[5],row[6],row[10],row[13],row[14]) + + 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 +""" |