Skip to content

Commit

Permalink
Working on getting get_scope and get_aggregate to function'
Browse files Browse the repository at this point in the history
  • Loading branch information
robdox committed Jul 12, 2018
1 parent e07a581 commit 7f68fcb
Show file tree
Hide file tree
Showing 25 changed files with 381 additions and 347 deletions.
19 changes: 13 additions & 6 deletions easy_scoping/DjangoEasyScoping/ScopingMixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,18 @@ class ScopingQuerySet(models.QuerySet):
def __getattr__(self, attr):
for plugin in ['scopes', 'aggregates']:
if attr in getattr(self.model, '__%s__'%plugin):
def scoped_query(*args, **kwargs):
def plugin_query(*args, **kwargs):
return getattr(self.model, '__%s__'%plugin)[attr](self, *args, **kwargs)
return scoped_query
return plugin_query
raise AttributeError('Queryset for %s has no attribute %s'%(self.model, attr))

def get_scope(self, name):
return self.model.get_scope(name)

def get_aggregate(self, name):
return self.model.get_aggregate(name)


class ScopingMixin(object):

@classmethod
Expand All @@ -16,22 +23,22 @@ def scopes(cls):
setattr(cls, '__scopes__', dict())
return cls.__scopes__

# This method fails, this is me working on fixing it
@classmethod
def get_scope(cls, name):
if hasattr(cls, '__scopes__') and name in cls.scopes():
print(cls.__scopes__)
print(dir(cls))
return cls.__scopes__[name]
return getattr(cls.objects.all(), name)

@classmethod
def aggregates(cls):
if not getattr(cls, '__aggregates__', None):
setattr(cls, '__aggregates__', dict())
return cls.__aggregates__

# This method also fails, this is the code given from you
@classmethod
def get_aggregate(cls, name):
if hasattr(cls, '__aggregates__')and name in cls.aggregates():
if hasattr(cls, '__aggregates__') and name in cls.aggregates():
return cls.__aggregates__[name]

@classmethod
Expand Down
13 changes: 5 additions & 8 deletions easy_scoping/easy_scoping/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
'django.contrib.messages',
'django.contrib.staticfiles',
'widgets',
'purchases',
]

MIDDLEWARE = [
Expand Down Expand Up @@ -87,20 +88,16 @@

AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation. \
UserAttributeSimilarityValidator',
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation. \
MinimumLengthValidator',
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation. \
CommonPasswordValidator',
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation. \
NumericPasswordValidator',
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]

Expand Down
Empty file.
15 changes: 15 additions & 0 deletions easy_scoping/purchases/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from django.contrib import admin
from .models import Purchase

class WidgetInLine(admin.TabularInline):
model = Purchase.items.through


class PurchaseAdmin(admin.ModelAdmin):
model = Purchase
list_display = ('sale_date', 'sale_price', 'profit')
list_filter = ('sale_date', 'sale_price', 'profit')
inlines = [WidgetInLine]


admin.site.register(Purchase, PurchaseAdmin)
5 changes: 5 additions & 0 deletions easy_scoping/purchases/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from django.apps import AppConfig


class PurchasesConfig(AppConfig):
name = 'purchases'
31 changes: 31 additions & 0 deletions easy_scoping/purchases/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.10.6 on 2018-07-11 17:33
from __future__ import unicode_literals

import DjangoEasyScoping.ScopingMixin
import datetime
from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

initial = True

dependencies = [
('widgets', '0005_auto_20180711_1733'),
]

operations = [
migrations.CreateModel(
name='Purchase',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('sale_date', models.DateTimeField(default=datetime.datetime.now)),
('sale_price', models.DecimalField(blank=True, decimal_places=2, default=0, max_digits=10)),
('profit', models.DecimalField(blank=True, decimal_places=2, default=0, max_digits=10)),
('items', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='widgets.Widget')),
],
bases=(DjangoEasyScoping.ScopingMixin.ScopingMixin, models.Model),
),
]
25 changes: 25 additions & 0 deletions easy_scoping/purchases/migrations/0002_auto_20180711_1741.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.10.6 on 2018-07-11 17:41
from __future__ import unicode_literals

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('widgets', '0005_auto_20180711_1733'),
('purchases', '0001_initial'),
]

operations = [
migrations.RemoveField(
model_name='purchase',
name='items',
),
migrations.AddField(
model_name='purchase',
name='items',
field=models.ManyToManyField(to='widgets.Widget'),
),
]
18 changes: 18 additions & 0 deletions easy_scoping/purchases/migrations/0003_auto_20180711_2003.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 2.0.6 on 2018-07-11 20:03

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('purchases', '0002_auto_20180711_1741'),
]

operations = [
migrations.AlterField(
model_name='purchase',
name='items',
field=models.ManyToManyField(blank=True, to='widgets.Widget'),
),
]
23 changes: 23 additions & 0 deletions easy_scoping/purchases/migrations/0004_auto_20180711_2237.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Generated by Django 2.0.6 on 2018-07-11 22:37

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('purchases', '0003_auto_20180711_2003'),
]

operations = [
migrations.AlterField(
model_name='purchase',
name='profit',
field=models.FloatField(blank=True, default=0),
),
migrations.AlterField(
model_name='purchase',
name='sale_price',
field=models.FloatField(blank=True, default=0),
),
]
Empty file.
52 changes: 52 additions & 0 deletions easy_scoping/purchases/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import django
from django.db import models
from widgets.models import Widget
from DjangoEasyScoping.ScopingMixin import ScopingMixin, ScopingQuerySet


class Purchase(ScopingMixin, models.Model):
items = models.ManyToManyField(Widget, blank=True)
sale_date = models.DateTimeField(default=django.utils.timezone.now)
sale_price = models.FloatField(default=0,
blank=True)
profit = models.FloatField(default=0,
blank=True)

objects = ScopingQuerySet.as_manager()

def get_items(self):
return self.items.all()

def get_item_count(self):
return self.items.all().count()

def get_sale_date(self):
return self.sale_date

def get_sale_price(self):
return self.sale_price

def get_cost(self):
from django.db.models import Sum
return round(self.items.aggregate(cost=Sum('cost'))['cost'], 2)

def get_profit(self):
return self.profit

def set_sale_price(self):
cost_plus_profit= 1.1
cost = self.get_cost()
self.sale_price = round(cost*cost_plus_profit, 2)

def set_profit(self):
profit_margin = .1
cost = self.get_cost()
self.profit = round(cost*profit_margin, 2)

def save(self, *args, **kwargs):
if self.id:
for item in Widget.objects.filter(purchase=self):
self.items.add(item)
self.set_sale_price()
self.set_profit()
super(Purchase, self).save(*args, **kwargs)
3 changes: 3 additions & 0 deletions easy_scoping/purchases/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.shortcuts import render

# Create your views here.
23 changes: 23 additions & 0 deletions easy_scoping/tests/factories.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import datetime as dt
import pytz
import random
import factory
import factory.fuzzy as fuz
from purchases.models import Purchase
from widgets.models import Widget


class PurchaseFactory(factory.django.DjangoModelFactory):
class Meta:
model = Purchase

sale_date = fuz.FuzzyDateTime(dt.datetime(1950, 1, 1, tzinfo=pytz.UTC))

@factory.post_generation
def items(self, create, purchased, **kwargs):
amt_purchased = random.randint(1, 10)
purchased = Widget.objects.all().order_by('?')[:amt_purchased]

if create and purchased:
for item in purchased:
self.items.add(item)
1 change: 0 additions & 1 deletion easy_scoping/tests/fixtures/filter_test_data.json

This file was deleted.

1 change: 0 additions & 1 deletion easy_scoping/tests/fixtures/random_test_data.json

This file was deleted.

1 change: 1 addition & 0 deletions easy_scoping/tests/fixtures/widgets_combo.json

Large diffs are not rendered by default.

76 changes: 0 additions & 76 deletions easy_scoping/tests/test_aggregation.py

This file was deleted.

Loading

0 comments on commit 7f68fcb

Please sign in to comment.