diff options
-rw-r--r-- | app/ebay/__init__.py | 0 | ||||
-rw-r--r-- | app/ebay/admin.py | 14 | ||||
-rw-r--r-- | app/ebay/migrations/0001_initial.py | 50 | ||||
-rw-r--r-- | app/ebay/migrations/0002_auto_20161231_1401.py | 25 | ||||
-rw-r--r-- | app/ebay/migrations/0003_auto_20161231_1419.py | 24 | ||||
-rw-r--r-- | app/ebay/migrations/__init__.py | 0 | ||||
-rw-r--r-- | app/ebay/models.py | 64 |
7 files changed, 177 insertions, 0 deletions
diff --git a/app/ebay/__init__.py b/app/ebay/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/app/ebay/__init__.py diff --git a/app/ebay/admin.py b/app/ebay/admin.py new file mode 100644 index 0000000..52d14ea --- /dev/null +++ b/app/ebay/admin.py @@ -0,0 +1,14 @@ +from django.contrib import admin + +from .models import Item, Price, TrackedItem + + +@admin.register(Item) +class ItemAdmin(admin.ModelAdmin): + list_display = ('name', 'currently_own', 'purchase_price') + + +@admin.register(TrackedItem) +class TrackedItemAdmin(admin.ModelAdmin): + list_display = ('title', 'get_latest_price' ) + diff --git a/app/ebay/migrations/0001_initial.py b/app/ebay/migrations/0001_initial.py new file mode 100644 index 0000000..3d2d34e --- /dev/null +++ b/app/ebay/migrations/0001_initial.py @@ -0,0 +1,50 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.9 on 2016-12-31 11:43 +from __future__ import unicode_literals + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + ] + + operations = [ + migrations.CreateModel( + name='Item', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('url', models.CharField(max_length=200)), + ('title', models.CharField(max_length=200)), + ('purchase_price', models.IntegerField()), + ('currently_own', models.BooleanField(default=False)), + ('sold', models.BooleanField(default=False)), + ('sale_price', models.IntegerField()), + ], + ), + migrations.CreateModel( + name='Price', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('date', models.DateTimeField()), + ('price', models.IntegerField()), + ], + ), + migrations.CreateModel( + name='TrackedItem', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('url', models.CharField(max_length=200)), + ('title', models.CharField(max_length=200)), + ], + ), + migrations.AddField( + model_name='price', + name='item', + field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='ebay.TrackedItem'), + ), + ] diff --git a/app/ebay/migrations/0002_auto_20161231_1401.py b/app/ebay/migrations/0002_auto_20161231_1401.py new file mode 100644 index 0000000..46f5a12 --- /dev/null +++ b/app/ebay/migrations/0002_auto_20161231_1401.py @@ -0,0 +1,25 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.9 on 2016-12-31 14:01 +from __future__ import unicode_literals + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('ebay', '0001_initial'), + ] + + operations = [ + migrations.AlterField( + model_name='price', + name='date', + field=models.DateTimeField(blank=True), + ), + migrations.AlterField( + model_name='price', + name='price', + field=models.DecimalField(decimal_places=2, max_digits=6), + ), + ] diff --git a/app/ebay/migrations/0003_auto_20161231_1419.py b/app/ebay/migrations/0003_auto_20161231_1419.py new file mode 100644 index 0000000..1f05859 --- /dev/null +++ b/app/ebay/migrations/0003_auto_20161231_1419.py @@ -0,0 +1,24 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.9 on 2016-12-31 14:19 +from __future__ import unicode_literals + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('ebay', '0002_auto_20161231_1401'), + ] + + operations = [ + migrations.AlterModelOptions( + name='price', + options={'get_latest_by': 'date', 'ordering': ('-date',)}, + ), + migrations.RenameField( + model_name='item', + old_name='title', + new_name='name', + ), + ] diff --git a/app/ebay/migrations/__init__.py b/app/ebay/migrations/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/app/ebay/migrations/__init__.py diff --git a/app/ebay/models.py b/app/ebay/models.py new file mode 100644 index 0000000..4338197 --- /dev/null +++ b/app/ebay/models.py @@ -0,0 +1,64 @@ +import datetime +import re +from django.contrib.gis.db import models +from bs4 import BeautifulSoup +import requests + +class Item(models.Model): + name = models.CharField(max_length=200) + purchase_price = models.IntegerField() + url = models.CharField(max_length=200) + currently_own = models.BooleanField(blank=True, default=False) + sold = models.BooleanField(blank=True, default=False) + sale_price = models.IntegerField() + + def save(self, **kwargs): + super(Item, self).save() + + def __str__(self): + return self.name + + +class TrackedItem(models.Model): + url = models.CharField(max_length=200) + title = models.CharField(max_length=200) + + def save(self, **kwargs): + super(TrackedItem, self).save() + + @property + def get_latest_price(self): + return self.price_set.latest().price + + def __str__(self): + return self.title + + +class Price(models.Model): + date = models.DateTimeField(blank=True) + price = models.DecimalField(max_digits=6, decimal_places=2) + item = models.ForeignKey(TrackedItem) + + class Meta: + ordering = ('-date',) + get_latest_by = 'date' + + def __str__(self): + return str(self.date) + + def save(self, **kwargs): + if not self.pk: + self.date = datetime.datetime.now() + super(Price, self).save() + + +def update_tracked_item_price(item): + r = requests.get(item.url) + soup = BeautifulSoup(r.text, "lxml") + numbers = re.compile('\d+(?:\.\d+)?') + price = soup.find(itemprop="price").get_text() + price = numbers.findall(price)[0] + l, created = Price.objects.get_or_create( + price=price, + item=item + ) |