Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cash register tracking #18

Merged
merged 13 commits into from
Feb 21, 2017

Conversation

ElinSwedin
Copy link
Member

No description provided.

@kjagiello kjagiello added this to the 2.0.0 milestone Feb 17, 2017
@codecov
Copy link

codecov bot commented Feb 17, 2017

Codecov Report

Merging #18 into develop will increase coverage by 0.89%.
The diff coverage is 97.19%.

@@             Coverage Diff             @@
##           develop      #18      +/-   ##
===========================================
+ Coverage    86.79%   87.69%   +0.89%     
===========================================
  Files           71       73       +2     
  Lines         2204     2381     +177     
  Branches       114      125      +11     
===========================================
+ Hits          1913     2088     +175     
  Misses         275      275              
- Partials        16       18       +2
Impacted Files Coverage Δ
src/foobar/urls.py 60% <ø> (ø)
src/wallet/api.py 100% <100%> (ø)
src/foobar/wallet/api.py 100% <100%> (ø)
src/foobar/api.py 95.23% <100%> (+1.29%)
src/foobar/tests/test_api.py 100% <100%> (ø)
src/foobar/enums.py 100% <100%> (ø)
src/foobar/forms.py 100% <100%> (ø)
src/wallet/tests/test_api.py 100% <100%> (ø)
src/foobar/tests/test_views.py 100% <100%> (ø)
src/foobar/admin.py 60.74% <77.77%> (+1.21%)
... and 5 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 7ab2048...c844b5a. Read the comment docs.

@flaeppe
Copy link
Member

flaeppe commented Feb 18, 2017

WalletCorrection model could have a clean(self) function to check if amount == 0 otherwise it is not considered a correction?

@@ -24,6 +24,16 @@ def get_balance(owner_id, currency):
return wallet_obj, wallet_obj.balance


def set_balance(owner_id, new_balance, reference=None):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function should probably be decorated with @transaction.atomic. We want to avoid possible race conditions (no other wallet transactions should be able to sneak in between get_balance and withdraw/deposit).

def has_add_permission(self, request):
return False

def get_form(self, request, obj=None, **kwargs):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Debug code? Should be cleaned up.

amount=-amount,
reference=reference
)
trx = 2
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No hardcoded values. Should be replaced with fobar.enums.TrxType.WITHDRAW. The same applies to the other occurrences of hardcoded values in this function.

data = self.cleaned_data['deposit_or_withdraw']
wallet, balance = api.get_balance(self.owner_id)
if data.amount < 0 and -data > balance:
raise forms.ValidationError("Not enough founds")
Copy link
Member

@kjagiello kjagiello Feb 17, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The string should be translated using following method: https://docs.djangoproject.com/en/1.10/topics/i18n/translation/#internationalization-in-python-code. Also, the convention in our codebase is to use single quotes for strings.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

founds is misspelled, should be funds

@@ -86,3 +86,24 @@ def total(self):

def __str__(self):
return str(self.id)


class WalletCorrection(UUIDModel, TimeStampedModel):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The name could probably be improved. What are we representing here actually? It seems that it can be both either correction or withdrawal/deposit. The name should reflect that.

{% csrf_token %}
{{ form_class.as_p }}
</div>
<input type="submit" name="save_correction" action="" id="correction_save" style="float:right">
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What function does "action" have on the input field?

)
self.assertEqual(correction_obj.wallet.owner_id, wallet_obj.owner_id)
self.assertEqual(correction_obj.trx_type.value, 0)
self.assertEqual(correction_obj.post_balance.amount, 1000)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this really right? You set the balance of the wallet to 1200 in your call on row 211, but the post_balance is 1000. Shouldn't be 1200?

)
self.assertEqual(correction_obj.wallet.owner_id, wallet_obj.owner_id)
self.assertEqual(correction_obj.trx_type.value, 1)
self.assertEqual(correction_obj.post_balance.amount, 1000)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your initial balance is 1000 SEK. You deposit 100 SEK. The post_balance should be 1100 SEK.

owner_id=wallet_obj.owner_id
)
self.assertEqual(correction_obj.wallet.owner_id, wallet_obj.owner_id)
self.assertEqual(correction_obj.trx_type.value, 1)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Those should not be hardcoded. Use enums.TrxType.DEPOSIT instead,

class TrxType(enum.Enum):
CORRECTION = 0
DEPOSIT = 1
WITHDRAW = 2
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we follow @kjagiello's idea of changing the name for WalletCorrection the name here might be appropriate to change as well.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WITHDRAW should probably be changed too from being a verb to a noun: WITHDRAWAL.

import uuid


class Migration(migrations.Migration):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens with old transactions that exist in the system?

I assume that each new transaction will generate a Correction/Log but should/can we generate an instance for the old ones?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are no old correction transactions, so no data migration is needed.



@transaction.atomic
def calculate_correction(new_balance, owner_id, user, reference=None):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm the reference should be saved only in WalletCorrection. Then WalletCorrect.id should be saved as reference in WalletTransaction. That way, WalletTransactions can be traced back to specific WalletCorrections.

@kjagiello
Copy link
Member

The code is not compatible with Django 1.9 anymore, but I don't think it is an issue. I've got some changes in workings that are not compatible with Django 1.9 and as we are the only ones using this project at the moment, I propose we drop the 1.9 compatibility.

@codecov-io
Copy link

codecov-io commented Feb 20, 2017

Codecov Report

Merging #18 into develop will increase coverage by 0.83%.
The diff coverage is 95.85%.

@@             Coverage Diff             @@
##           develop      #18      +/-   ##
===========================================
+ Coverage    86.88%   87.71%   +0.83%     
===========================================
  Files           71       73       +2     
  Lines         2218     2410     +192     
  Branches       115      126      +11     
===========================================
+ Hits          1927     2114     +187     
- Misses         275      278       +3     
- Partials        16       18       +2
Impacted Files Coverage Δ
src/foobar/urls.py 60% <ø> (ø)
src/foobar/forms.py 100% <100%> (ø)
src/wallet/api.py 100% <100%> (ø)
src/wallet/tests/test_api.py 100% <100%> (ø)
src/foobar/wallet/api.py 100% <100%> (ø)
src/foobar/tests/test_views.py 100% <100%> (ø)
src/foobar/api.py 95.4% <100%> (+1.46%)
src/foobar/tests/test_api.py 100% <100%> (ø)
src/foobar/enums.py 100% <100%> (ø)
src/foobar/admin.py 60.14% <66.66%> (+0.62%)
... and 5 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update a663d96...9d01b87. Read the comment docs.

'pre_balance'
)
ordering = ('-date_created',)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should add a Meta-class here and specify verbose_name and verbose_name_plural.

@@ -192,6 +192,25 @@ def changelist_view(self, request, extra_context=None):
return response


@admin.register(models.WalletLogEntry)
class WalletLogEntryAdmin(admin.ModelAdmin):
list_display = ('date_created',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should reorder the columns so it is old balance, amount and then new balance. image

@@ -192,6 +192,25 @@ def changelist_view(self, request, extra_context=None):
return response


@admin.register(models.WalletLogEntry)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Everything here should be read-only. Also, nobody should be able to delete log entries.
image

@@ -29,10 +29,10 @@ def set_balance(owner_id, new_balance, reference=None):
wallet, old_balance = get_balance(owner_id, new_balance.currency)
difference = new_balance - old_balance
if difference.amount < 0:
withdraw(owner_id, -difference, reference)
return withdraw(owner_id, -difference, reference), difference
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Second return value should be -difference to become identical to the object's amount

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is actually correct.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Never mind. My mistake.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is okay. No problem.

)
readonly_fields = list_display
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should keep the comment field writeable? To avoid cluttering the timeline by forcing any spelling mistakes or similar to updated by creating a new log entry.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea 👍🏻

@kjagiello
Copy link
Member

image
It would be really nice to get some colors there. At a glance, you don't really see which entries are losses and which are "gains".

…redit wallet and added colors to wallet log entry change list

class CorrectionForm(forms.Form):
balance = MoneyField(label='Balance', min_value=0)
comment = forms.CharField(label='Comment', max_length=250, required=False)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The max length of the comment field should reflect the max length of the corresponding database field.

'amount',
'post_balance'
)
readonly_fields = list_display
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image
The wallet field is not readonly which means someone could mess with us and attach a log entry to some other wallet. We should either make it read only or ideally simple hide it from the form.

@kjagiello
Copy link
Member

LGTM 👍

@@ -92,7 +92,7 @@ class WalletLogEntry(UUIDModel, TimeStampedModel):
user = models.ForeignKey(User, null=True, blank=True)
trx_type = EnumIntegerField(enums.TrxType,
default=enums.TrxType.CORRECTION)
wallet = models.ForeignKey('wallet.Wallet', related_name='log_entries')
wallet = models.ForeignKey('wallet.Wallet', related_name='log_entries', editable=False)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a heads up, because settings editable=False should trigger a migration. In case it does, we are missing a migration file, test running python manage.py makemigrations to see if it generates a new migration file.

For the admin panel, if you haven't figured it out already, the same effect as setting editable=False here would be applied by including the field in the ModelAdmins readonly_fields.

@kjagiello kjagiello merged commit 115b417 into uppsaladatavetare:develop Feb 21, 2017
@kjagiello kjagiello changed the title Feature/cashdeposits Ability to track the state of the cash register Feb 23, 2017
@kjagiello kjagiello changed the title Ability to track the state of the cash register Cash register tracking Feb 23, 2017
@ElinSwedin ElinSwedin deleted the feature/cashdeposits branch March 2, 2017 13:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants