Skip to content
This repository has been archived by the owner on Feb 8, 2018. It is now read-only.

Commit

Permalink
Profile page barely loading with postgres.orm
Browse files Browse the repository at this point in the history
  • Loading branch information
chadwhitacre committed Aug 14, 2013
1 parent 200d12f commit a25a4bb
Show file tree
Hide file tree
Showing 9 changed files with 108 additions and 80 deletions.
53 changes: 25 additions & 28 deletions gittip/models/team.py → gittip/models/_mixin_team.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
"""Teams on Gittip are plural participants with members.
"""
from decimal import Decimal

from aspen.utils import typecheck
from postgres import RealDictCursor


"""
class MemberLimitReached(Exception): pass


class Team(object):
class MixinTeam(object):
"""This class provides methods for working with a Participant as a Team.
:param Participant participant: the underlying :py:class:`~gittip.participant.Participant` object for this team
Expand All @@ -20,12 +24,12 @@ def __init__(self, participant):
def show_as_team(self, user):
"""Return a boolean, whether to show this participant as a team.
"""
if not self.participant.IS_PLURAL:
if not self.IS_PLURAL:
return False
if user.ADMIN:
return True
if not self.get_members():
if self != user:
if self != user.participant:
return False
return True

Expand All @@ -34,7 +38,7 @@ def add_member(self, member):
"""
assert self.IS_PLURAL
if len(self.get_members()) == 149:
raise self.MemberLimitReached
raise MemberLimitReached
self.__set_take_for(member, Decimal('0.01'), self)

def remove_member(self, member):
Expand All @@ -58,7 +62,7 @@ def get_take_last_week_for(self, member):
assert self.IS_PLURAL
membername = member.username if hasattr(member, 'username') \
else member['username']
rec = gittip.db.one_or_zero("""
return self.db.one_or_zero("""
SELECT amount
FROM transfers
Expand All @@ -67,25 +71,17 @@ def get_take_last_week_for(self, member):
(SELECT ts_start FROM paydays ORDER BY ts_start DESC LIMIT 1)
ORDER BY timestamp DESC LIMIT 1
""", (self.username, membername))

if rec is None:
return Decimal('0.00')
else:
return rec['amount']
""", (self.username, membername), zero=Decimal('0.00'))

def get_take_for(self, member):
"""Return a Decimal representation of the take for this member, or 0.
"""
assert self.IS_PLURAL
rec = gittip.db.one_or_zero( "SELECT take FROM current_memberships "
"WHERE member=%s AND team=%s"
, (member.username, self.username)
)
if rec is None:
return Decimal('0.00')
else:
return rec['take']
return self.db.one_or_zero( "SELECT take FROM current_memberships "
"WHERE member=%s AND team=%s"
, (member.username, self.username)
, zero=Decimal('0.00')
)

def compute_max_this_week(self, last_week):
"""2x last week's take, but at least a dollar.
Expand All @@ -96,7 +92,11 @@ def set_take_for(self, member, take, recorder):
"""Sets member's take from the team pool.
"""
assert self.IS_PLURAL
from gittip.models.user import User # lazy to avoid circular import

# lazy import to avoid circular import
from gittip.security.user import User
from gittip.models.participant import Participant

typecheck( member, Participant
, take, Decimal
, recorder, (Participant, User)
Expand All @@ -113,7 +113,7 @@ def set_take_for(self, member, take, recorder):
def __set_take_for(self, member, take, recorder):
assert self.IS_PLURAL
# XXX Factored out for testing purposes only! :O Use .set_take_for.
gittip.db.run("""
self.db.run("""
INSERT INTO memberships (ctime, member, team, take, recorder)
VALUES ( COALESCE (( SELECT ctime
Expand All @@ -133,19 +133,19 @@ def __set_take_for(self, member, take, recorder):

def get_members(self):
assert self.IS_PLURAL
return list(gittip.db.all("""
return self.db.all("""
SELECT member AS username, take, ctime, mtime
FROM current_memberships
WHERE team=%s
ORDER BY ctime DESC
""", (self.username,)))
""", (self.username,), cursor_factory=RealDictCursor)

def get_teams_membership(self):
assert self.IS_PLURAL
TAKE = "SELECT sum(take) FROM current_memberships WHERE team=%s"
total_take = gittip.db.one_or_zero(TAKE, (self.username,))['sum']
total_take = self.db.one_or_zero(TAKE, (self.username,))['sum']
total_take = 0 if total_take is None else total_take
team_take = max(self.get_dollars_receiving() - total_take, 0)
membership = { "ctime": None
Expand Down Expand Up @@ -178,6 +178,3 @@ def get_memberships(self, current_user):
member['balance'] = balance
member['percentage'] = (amount / budget) if budget > 0 else 0
return members



2 changes: 1 addition & 1 deletion gittip/models/community.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def get_list_for(user):
:database: One SELECT, multiple rows
"""
if user is None or user.ANON:
if user is None:
member_test = "false"
sort_order = 'DESC'
params = ()
Expand Down
58 changes: 22 additions & 36 deletions gittip/models/participant.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from psycopg2 import IntegrityError
from postgres.orm import Model
from gittip.models._mixin_elsewhere import MixinElsewhere
from gittip.models._mixin_team import MixinTeam
from gittip.utils import canonicalize


Expand All @@ -31,16 +32,20 @@
NANSWERS_THRESHOLD = 0 # configured in wireup.py


class Participant(Model, MixinElsewhere):
class Participant(Model, MixinElsewhere, MixinTeam):
"""Represent a Gittip participant.
"""

typname = 'participants'

def __eq__(self, other):
if not isinstance(other, Participant):
return False
return self.username == other.username

def __ne__(self, other):
if not isinstance(other, Participant):
return False
return self.username != other.username


Expand Down Expand Up @@ -113,17 +118,13 @@ def set_session_expires(self, expires):
# Number
# ======

def is_singular(self):
rec = gittip.db.one_or_zero("SELECT number FROM participants "
"WHERE username = %s", (self.username,))

return rec['number'] == 'singular'

def is_plural(self):
rec = gittip.db.one_or_zero("SELECT number FROM participants "
"WHERE username = %s", (self.username,))
@property
def IS_SINGULAR(self):
return self.number == 'singular'

return rec['number'] == 'plural'
@property
def IS_PLURAL(self):
return self.number == 'plural'


def get_teams(self):
Expand Down Expand Up @@ -344,15 +345,13 @@ def get_tip_to(self, tippee):

# XXX

def get_dollars_receiving(self):
return sum(tip.amount for tip in self.valid_tips_receiving) + Decimal('0.00')
def get_dollars_receiving(self):
"""Return a Decimal.
"""

BACKED = """\
SELECT sum(amount) AS dollars_receiving
SELECT sum(amount)
FROM ( SELECT DISTINCT ON (tipper)
amount
, tipper
Expand All @@ -366,16 +365,10 @@ def get_dollars_receiving(self):
) AS foo
"""
rec = gittip.db.one_or_zero(BACKED, (self.username,))
if rec is None:
amount = None
else:
amount = rec['dollars_receiving'] # might be None

if amount is None:
amount = Decimal('0.00')

return amount
return self.db.one_or_zero( BACKED
, (self.username,)
, zero=Decimal('0.00')
)


def get_dollars_giving(self):
Expand All @@ -384,7 +377,7 @@ def get_dollars_giving(self):

BACKED = """\
SELECT sum(amount) AS dollars_giving
SELECT sum(amount)
FROM ( SELECT DISTINCT ON (tippee)
amount
, tippee
Expand All @@ -398,16 +391,10 @@ def get_dollars_giving(self):
) AS foo
"""
rec = gittip.db.one_or_zero(BACKED, (self.username,))
if rec is None:
amount = None
else:
amount = rec['dollars_giving'] # might be None

if amount is None:
amount = Decimal('0.00')

return amount
return self.db.one_or_zero( BACKED
, (self.username,)
, zero=Decimal('0.00')
)


def get_number_of_backers(self):
Expand Down Expand Up @@ -755,7 +742,6 @@ class UsernameIsRestricted(ProblemChangingUsername): pass
class UsernameAlreadyTaken(ProblemChangingUsername): pass

class TooGreedy(Exception): pass
class MemberLimitReached(Exception): pass
class NoSelfTipping(Exception): pass
class BadAmount(Exception): pass

Expand Down
45 changes: 45 additions & 0 deletions gittip/utils/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import gittip
from aspen import Response
from aspen.utils import typecheck
from tornado.escape import linkify
Expand Down Expand Up @@ -275,3 +276,47 @@ def canonicalize(path, base, canonical, given):

def plural(i, singular="", plural="s"):
return singular if i == 1 else plural


def get_participant(request, restrict=True):
"""Given a Request, raise Response or return Participant.
If user is not None then we'll restrict access to owners and admins.
"""
user = request.context['user']
slug = request.line.uri.path['username']

if restrict:
if user.ANON:
request.redirect(u'/%s/' % slug)

participant = gittip.db.one_or_zero( "SELECT participants.*::participants "
"FROM participants "
"WHERE username_lower=%s"
, (slug.lower(),)
)

if participant is None:
raise Response(404)

canonicalize(request.line.uri.path.raw, '/', participant.username, slug)

if participant.claimed_time is None:

# This is a stub participant record for someone on another platform who
# hasn't actually registered with Gittip yet. Let's bounce the viewer
# over to the appropriate platform page.

to = participant.resolve_unclaimed()
if to is None:
raise Response(404)
request.redirect(to)

if restrict:
if participant != user.participant:
if not user.ADMIN:
raise Response(403)

return participant

10 changes: 5 additions & 5 deletions templates/connected-accounts.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ <h2>Connected Accounts</h2>
</td>
<td class="account-details">
{% if twitter_account is None %}
{% if not user.ANON and user == participant %}
{% if not user.ANON and user.participant == participant %}
Connect a <a href="{{ twitter.oauth_url(website, u'connect') }}">Twitter account</a>.
{% else %}
No Twitter account connected.
Expand All @@ -31,7 +31,7 @@ <h2>Connected Accounts</h2>
</td>
<td class="account-details">
{% if github_account is None %}
{% if not user.ANON and user == participant %}
{% if not user.ANON and user.participant == participant %}
Connect a <a href="{{ github.oauth_url(website, u'connect') }}">GitHub account</a>.
{% else %}
No GitHub account connected.
Expand All @@ -54,7 +54,7 @@ <h2>Connected Accounts</h2>
</td>
<td class="account-details">
{% if bitbucket_account is None %}
{% if not user.ANON and user == participant %}
{% if not user.ANON and user.participant == participant %}
Connect a <a href="{{ bitbucket.oauth_url(website, u'connect') }}">Bitbucket account</a>.
{% else %}
No Bitbucket account connected.
Expand All @@ -78,7 +78,7 @@ <h2>Connected Accounts</h2>
</td>
<td class="account-details">
{% if bountysource_account is None %}
{% if not user.ANON and user == participant %}
{% if not user.ANON and user.participant == participant %}
Connect a <a href="{{ bountysource.oauth_url(website, participant) }}">Bountysource account</a>.
{% else %}
No Bountysource account connected.
Expand All @@ -101,7 +101,7 @@ <h2>Connected Accounts</h2>
<img src="/assets/balanced-avatar.png" />
</td>
<td class="account-details">
{% if user == participant %}
{% if user.participant == participant %}

<a href="/credit-card.html">Credit card</a>:
<!--
Expand Down
6 changes: 3 additions & 3 deletions templates/participant.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
{% if g > r and not participant.anonymous %}
<h2 class="pad-sign">{{ participant.username }} gives</h2>
<div class="number">
{% if user == participant %}
{% if user.participant == participant %}
$<span class="total-giving">{{ giving }}</span>
{% else %}
${{ giving }}
Expand All @@ -30,7 +30,7 @@ <h2 class="pad-sign">{{ participant.username }} receives</h2>
week{% if g > 0 %}, and gives
{% if participant.anonymous %}
anonymously
{% elif user == participant %}
{% elif user.participant == participant %}
$<span class="total-giving">{{ giving }}</span>
{% else %}
${{ giving }}
Expand All @@ -55,7 +55,7 @@ <h2>{{ participant.username }}
</tr>
</table>

{% if (participant != user) and participant.accepts_tips %}
{% if (participant != user.participant) and participant.accepts_tips %}
{% include "participant.tip.html" %}
{% end %}

Expand Down
Loading

0 comments on commit a25a4bb

Please sign in to comment.