Commits

Anonymous committed 0978e83

some old stuff i found while cleaning out netbook

  • Participants
  • Parent commits e7885f3

Comments (0)

Files changed (5)

File dino/hearth/fixtures/hearth.yaml

--   fields: {amount: 20.0, financial_relation: 1, user: 1}
+-   fields:
+        amount: 20.0
+        financial_relation: 1
+        user: 1
     model: hearth.userfinancialrelation
     pk: 1
--   fields: {amount: 20.0, financial_relation: 1, user: 2}
+-   fields:
+        amount: 20.0
+        financial_relation: 1
+        user: 2
     model: hearth.userfinancialrelation
     pk: 2
--   fields: {amount: 30.0, financial_relation: 2, user: 2}
+-   fields:
+        amount: 30.0
+        financial_relation: 2
+        user: 2
     model: hearth.userfinancialrelation
     pk: 3
--   fields: {amount: 30.0, financial_relation: 3, user: 2}
+-   fields:
+        amount: 30.0
+        financial_relation: 3
+        user: 2
     model: hearth.userfinancialrelation
     pk: 4
--   fields: {amount: 0.0, financial_relation: 3, user: 1}
+-   fields:
+        amount: 0.0
+        financial_relation: 3
+        user: 1
     model: hearth.userfinancialrelation
     pk: 5
--   fields: {amount: 20.0, financial_relation: 4, user: 1}
+-   fields:
+        amount: 20.0
+        financial_relation: 4
+        user: 1
     model: hearth.userfinancialrelation
     pk: 6
--   fields: {amount: 40.0, financial_relation: 4, user: 2}
+-   fields:
+        amount: 40.0
+        financial_relation: 4
+        user: 2
     model: hearth.userfinancialrelation
     pk: 7
 -   fields: {}
         tags: []
     model: hearth.transaction
     pk: 4
-

File dino/hearth/forms.py

+# FIXME: is anything like this still needed?
 from django import forms
 from dino.hearth.models import Transaction
 

File dino/hearth/models.py

     @property
     def short_summary(self):
         """The summary attribute, shortened to no more than 11 characters."""
-        if len(self.summary) < 12:
+        limit = 15
+        if len(self.summary) <= limit:
             return self.summary
-        return self.summary[:7] + " ..."
+        return self.summary[:(limit - 4)] + " ..."
 
 
 class UserFinancialRelation(models.Model):
         except IndexError:
             return None
 
+    @classmethod
+    def filter_exclusively_to(cls, users):
+        # Couldn't find any better way to express this (and, of
+        # course, *only* get Txs whose User set is equal to users).
+        queryset = cls.objects.all()
+        # Restrict to FinancialRelations with User sets containing all Users
+        # in users, (not just with one).
+        for user in set(users):
+            queryset = queryset.filter(userfinancialrelation__user=user)
+        # Include User set length in query, as "user_count".
+        user_count = models.Count('userfinancialrelation')
+        queryset = queryset.annotate(user_count=user_count)
+        # Restrict to FinancialRelations with User sets of same length as users.
+        queryset = queryset.filter(user_count=len(users))
+        return queryset
+
     def __repr__(self):
         return "<{0} {1}: {2!r}>".format(cname(self), self.pk, self.pres())
 
         """Return a mapping of the total contributions of each given User
         over all Transactions involving those Users.
         """
-        # Couldn't find any better way to express this (and, of
-        # course, *only* get Txs whose User set is equal to users).
-        queryset = cls.objects.all()
-        for user in users:
-            queryset = queryset.filter(userfinancialrelation__user=user)
-        # Still have to protect against including users
-        # involved in a valid transaction but not in users.
-        return [(user, amount) for user, amount in cls.sum_over(queryset)
-                if user in users]
+        queryset = cls.filter_exclusively_to(users)
+        return cls.sum_over(queryset)
 
     def _pres(self, user):
         # FIXME: does this make sense for a single Tx?
     """A tracked FinancialRelation model representing a sum over all
     preceeding Transactions.
     """
+    current = models.BooleanField(default=True)
+
     @classmethod
     def create_new_for(cls, *users):
         """Calculate, create and return the Balance between the given Users."""
+        # TODO: start transaction here.
+        # Set all previous applicable Balances as not "current".
+        cls.filter_exclusively_to(users).filter(current=True).update(current=False)
         balance = cls()
         balance.save()
         for user, amount in Transaction.sum_for(*users):
             balance.userfinancialrelation_set.create(user=user, amount=amount)
+        # TODO: end transaction here.
         return balance
 
     def _pres(self, user):

File dino/hearth/urls.py

 
 urlpatterns = patterns('hearth.views',
     url(r'^$', 'index', name='hearth-index'),
-    #url(r'^register$', hearth_views.register),
     url(r'^transactions/$', 'transaction_list'),
     url(r'^transactions/new$', 'transaction_create', name='hearth-new-tx'),
     url(r'^transactions/(?P<pk>\d+)/$', 'transaction_view'),

File dino/hearth/views.py

-from django.core import urlresolvers
+from django.core.urlresolvers import reverse
 from django.shortcuts import redirect, render_to_response
 
 from dino.hearth.models import Balance, Transaction
-from dino.hearth.forms import TransactionForm
-from dino import util
+#from dino.hearth.forms import TransactionForm
+from dino.util import http_allows
 
 
-@util.http_allows('GET', 'POST')
-def register(request):
-    user = users.get_current_user()
-    if request.method == 'POST':
-        RegisteredUser(
-            key_name=user.user_id(),
-            email=user.email(),
-            nickname=user.nickname(),
-        ).put()
-        return redirect(urlresolvers.reverse(index))
-    return render_to_response('hearth/register.html')
-
-
-@util.http_allows('GET')
+@http_allows('GET')
 def index(request):
     # TODO: Add context processor to add users.get_current_user,
     # TODO: create_login_url and create_logout_url to current path to
     # TODO: context for all pages (to use in base template).
     # TODO: And for non-index pages, auto-redirect to login page if needed --
     # TODO: http://code.google.com/appengine/docs/python/config/appconfig.html#Requiring_Login_or_Administrator_Status
-    user = users.get_current_user()
     balances = None
-    if user:
-        r_user = RegisteredUser.get_by_key_name(user.user_id())
-        if r_user:
-            balances = Balance.all().filter(
-                        'user_pair', r_user.key().name()
-                       ).order('-modified')
+    if request.user.is_authenticated():
+        balances = Balance.objects.filter(
+            userfinancialrelation__user=request.user,
+            current=True,
+        ).order_by('-modified')
     return render_to_response('hearth/index.html',
                               {'balances': balances,
-                               'tx_form': TransactionForm()})
+                                  })#'tx_form': TransactionForm()})
 
 
-@util.http_allows('GET', 'POST')
+@http_allows('GET', 'POST')
 def transaction_create(request):
     if request.method == 'POST':
         form = TransactionForm(request.POST)
         if form.is_valid():
             tx = form.save()
-            return redirect(urlresolvers.reverse(transaction_view,
-                                                 kwargs={'pk': tx.key().id()}))
+            return redirect(reverse(transaction_view,
+                                    kwargs={'pk': tx.key().id()}))
     else:
         form = TransactionForm()
     return render_to_response('hearth/transaction_form.html', {'form': form})
 
 
-@util.http_allows('GET')
+@http_allows('GET')
 def transaction_list(request):
     return render_to_response('hearth/transaction_list.html',
                               {'txs': Transaction.all().fetch(100)})
 
 
-@util.http_allows('GET')
+@http_allows('GET')
 def transaction_view(request, pk):
     tx = util.get_object_or_404(pk, Transaction)
     return render_to_response('hearth/transaction_view.html', {'tx': tx})
 
 
-@util.http_allows('GET', 'POST')
+@http_allows('GET', 'POST')
 def transaction_edit(request, pk):
     tx = util.get_object_or_404(pk, Transaction)
     if request.method == 'POST':
         form = TransactionForm(request.POST, instance=tx)
         if form.is_valid():
             form.save()
-            return redirect(urlresolvers.reverse(transaction_view,
-                                                 kwargs={'pk': pk}))
+            return redirect(reverse(transaction_view,
+                                    kwargs={'pk': pk}))
     else:
         form = TransactionForm(instance=tx)
     return render_to_response('hearth/transaction_form.html',