Skip to content
This repository was archived by the owner on Feb 3, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 55 additions & 4 deletions quotefault/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from flask import Flask, render_template, request, flash, session, make_response
from flask_pyoidc.flask_pyoidc import OIDCAuthentication
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy import func

app = Flask(__name__)
# look for a config file to associate with a db/port/ip/servername
Expand Down Expand Up @@ -52,6 +53,24 @@ def __init__(self, submitter, quote, speaker):
self.speaker = speaker


class Vote(db.Model):
id = db.Column(db.Integer, primary_key=True, nullable=False)
quote_id = db.Column(db.ForeignKey("quote.id"))
voter = db.Column(db.String(200), nullable=False)
direction = db.Column(db.Integer, nullable=False)
updated_time = db.Column(db.DateTime, nullable=False)

quote = db.relationship(Quote)
test = db.UniqueConstraint("quote_id", "voter")

# initialize a row for the Vote table
def __init__(self, quote_id, voter, direction):
self.quote_id = quote_id
self.voter = voter
self.direction = direction
self.updated_time = datetime.now()


def get_metadata():
uuid = str(session["userinfo"].get("sub", ""))
uid = str(session["userinfo"].get("preferred_username", ""))
Expand Down Expand Up @@ -89,6 +108,30 @@ def settings():
return render_template('bootstrap/settings.html', metadata=metadata)


@app.route('/vote', methods=['POST'])
@auth.oidc_auth
def make_vote():
# submitter will grab UN from OIDC when linked to it
submitter = session['userinfo'].get('preferred_username', '')

quote = request.form['quote_id']
direction = request.form['direction']

existing_vote = Vote.query.filter_by(voter=submitter, quote_id=quote).first()
if existing_vote is None:
vote = Vote(quote, submitter, direction)
db.session.add(vote)
db.session.commit()
return '200'
Copy link
Contributor

Choose a reason for hiding this comment

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

Flask responses have a couple different forms (docs).

If you return a string, it will form a response with that string as the body and a status code of 200 OK.
If you want to specify the status code or headers, you can return a tuple in either of the following forms:

  • (body, status, headers)
  • (body, headers)

This means that when you return '201', it gives a response with a body of '201' and a status code of 200 OK

You could return a JSON success message:

        return jsonify(success=True)

For line 132, you can use a similar style while specifying the response code:

		return jsonify(success=True), 201

There are a few other ways you could achieve this, this is just the one I think of first.

elif existing_vote.direction != direction:
existing_vote.direction = direction
existing_vote.updated_time = datetime.now()
db.session.commit()
return '200'
else:
return '201'


@app.route('/settings', methods=['POST'])
@auth.oidc_auth
def update_settings():
Expand Down Expand Up @@ -164,7 +207,7 @@ def submit():
def get():
metadata = get_metadata()
metadata['submitter'] = request.args.get('submitter') # get submitter from url query string
metadata['speaker'] = request.args.get('speaker') # get submitter from url query string
metadata['speaker'] = request.args.get('speaker') # get speaker from url query string

if metadata['speaker'] is not None and metadata['submitter'] is not None:
quotes = Quote.query.order_by(Quote.quote_time.desc()).filter(Quote.submitter == metadata['submitter'],
Expand All @@ -174,19 +217,27 @@ def get():
elif metadata['speaker'] is not None:
quotes = Quote.query.order_by(Quote.quote_time.desc()).filter(Quote.speaker == metadata['speaker']).all()
else:
quotes = Quote.query.order_by(Quote.quote_time.desc()).limit(20).all() # collect all quote rows in the Quote db
# quotes = Quote.query.order_by(Quote.quote_time.desc()).limit(20).all()

# returns tuples with a quote and its net vote value
quotes = db.session.query(Quote, func.sum(Vote.direction).label('votes')).outerjoin(Vote).group_by(Quote).order_by(
Quote.quote_time.desc()).limit(20).all()

user_votes = db.session.query(Vote).filter(Vote.voter == metadata['submitter']).all()

if request.cookies.get('flag'):
return render_template(
'flag/storage.html',
quotes=quotes,
metadata=metadata
metadata=metadata,
user_votes=user_votes
)
else:
return render_template(
'bootstrap/storage.html',
quotes=quotes,
metadata=metadata
metadata=metadata,
user_votes=user_votes
)


Expand Down
2 changes: 2 additions & 0 deletions quotefault/static/css/quotefaultcss.css
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,5 @@ body{
.contentContainer a:hover{
color: #d1d6d6;
}


6 changes: 6 additions & 0 deletions quotefault/static/css/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -313,3 +313,9 @@ div.version{
#get_more {
width: 100%;
}

.upvote-meta-stackoverflow{
text-align: right;
float: right;
justify-content: right;
}
150 changes: 150 additions & 0 deletions quotefault/static/css/votes/upvote.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
div.upvote {
text-align: center;
}

div.upvote a.upvote-enabled {
cursor: pointer;
}

div.upvote a {
color: transparent;
background-image: url('images/sprites-stackoverflow.png?v=1');
background-repeat: no-repeat;
overflow: hidden;
display: block;
margin: 0 auto;
width: 41px;
height: 25px;
}

div.upvote span.count {
display: block;
font-size: 24px;
font-family: Arial, Liberation, Sans, DejaVu Sans, sans-serif;
color: #555;
text-align: center;
line-height: 1;
}

div.upvote a.upvote {
background-position: 0px -265px;
}

div.upvote a.upvote.upvote-on {
background-position: 0px -230px;
}

div.upvote a.downvote {
background-position: 0px -300px;
}

div.upvote a.downvote.downvote-on {
background-position: 0px -330px;
}

div.upvote a.star {
width: 33px;
height: 31px;
background-position: 0px -150px;
}

div.upvote a.star.star-on {
background-position: 0px -190px;
}

div.upvote-serverfault a {
background-image: url('images/sprites-serverfault.png?v=1');
}

div.upvote-meta-stackoverflow a {
background-image: url('images/sprites-meta-stackoverflow.png?v=1');
}

div.upvote-superuser a {
background-image: url('images/sprites-superuser.png?v=1');
}

div.upvote-unix a {
background-image: url('images/sprites-unix.png?v=1');
width: 42px;
height: 27px;
}

div.upvote-unix a.upvote {
background-position: 0px -237px;
}

div.upvote-unix a.upvote.upvote-on {
background-position: 0px -198px;
}

div.upvote-unix a.downvote {
background-position: 0px -281px;
}

div.upvote-unix a.downvote.downvote-on {
background-position: 0px -319px;
}

div.upvote-unix a.star {
width: 42px;
height: 30px;
background-position: 0px -126px;
}

div.upvote-unix a.star.star-on {
background-position: 0px -164px;
}

div.upvote-unix span.count {
color: #333;
line-height: 15px;
padding: 9px 0;
font-family: "DejaVu Sans Mono","Bitstream Vera Sans Mono","Courier New",Courier,Consolas,"Andale Mono WT","Andale Mono","Lucida Console","Lucida Sans Typewriter",monospace;
text-shadow: 1px 1px 0 #ffffff;
font-weight: bold;
margin: 0;
border: 0;
vertical-align: baseline;
}

div.upvote-programmers a {
background-image: url('images/sprites-programmers.png?v=1');
width: 42px;
height: 20px;
}

div.upvote-programmers a.upvote {
background-position: 5px -248px;
}

div.upvote-programmers a.upvote.upvote-on {
background-position: 5px -211px;
}

div.upvote-programmers a.downvote {
background-position: 5px -282px;
}

div.upvote-programmers a.downvote.downvote-on {
background-position: 5px -320px;
}

div.upvote-programmers a.star {
width: 42px;
height: 30px;
background-position: 6px -128px;
}

div.upvote-programmers a.star.star-on {
background-position: 6px -166px;
}

div.upvote-programmers span.count {
color: #333;
line-height: 15px;
padding: 5px 0 7px;
font-size: 20px;
font-weight: bold;
font-family: Tahoma,Geneva,Arial,sans-serif;
}
Loading