This repository has been archived by the owner on Feb 8, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 308
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3923 from gratipay/team-edit
Edit teams.
- Loading branch information
Showing
10 changed files
with
3,861 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import zipfile | ||
from cStringIO import StringIO | ||
|
||
import requests | ||
|
||
def imgize(image, image_type): | ||
large = None | ||
small = None | ||
crops = requests.post( 'http://gip.rocks/v1', | ||
data=image, | ||
headers={'Content-Type': image_type} | ||
) | ||
if crops.status_code == 200: | ||
zf = zipfile.ZipFile(StringIO(crops.content)) | ||
large = zf.open('160').read() | ||
small = zf.open('48').read() | ||
|
||
return crops.status_code, large, small |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
Gratipay.edit_team = {} | ||
|
||
Gratipay.edit_team.initForm = function() { | ||
$form = $("#edit-team"); | ||
$buttons = $form.find("button"); // submit and cancel btns | ||
$form.submit(Gratipay.edit_team.submitForm); | ||
} | ||
|
||
Gratipay.edit_team.submitForm = function(e) { | ||
e.preventDefault(); | ||
|
||
var data = new FormData($form[0]); | ||
|
||
$buttons.prop("disabled", true); | ||
$.ajax({ | ||
url: $form.attr("action"), | ||
type: $form.attr("method"), | ||
data: data, | ||
dataType: 'json', | ||
processData: false, | ||
contentType: false, | ||
success: function(d) { | ||
Gratipay.notification("Successfully edited team.", 'success'); | ||
setTimeout(function() { | ||
window.location.href = "../"; | ||
}, 1000); | ||
}, | ||
error: [Gratipay.error, function () { $buttons.prop("disabled", false); }] | ||
}); | ||
} |
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
from aspen import Response | ||
|
||
from gratipay.utils import get_team | ||
from gratipay.utils.images import imgize | ||
|
||
[---] | ||
|
||
def valid_url(url): | ||
return any(map(url.lower().startswith, ('http://', 'https://'))) | ||
|
||
request.allow('POST') | ||
|
||
field_names = { | ||
'name': 'Team Name', | ||
'image': 'Image', | ||
'product_or_service': 'Product or Service', | ||
'homepage': 'Homepage', | ||
'onboarding_url': 'Self-onboarding Documentation URL', | ||
'todo_url': 'To-do URL', | ||
} | ||
|
||
if user.ANON: | ||
raise Response(401, _("You need to log in to access this page.")) | ||
|
||
team = get_team(state) | ||
|
||
if team.is_closed: | ||
raise Response(403, _("You can't edit a closed team.")) | ||
|
||
if team.is_approved is False: # for teams under review, is_approved is None. | ||
raise Response(403, _("You can't edit a rejected team.")) | ||
|
||
if not user.ADMIN and user.participant.username != team.owner: | ||
raise Response(403, _("You are not authorized to access this page.")) | ||
|
||
data = request.body | ||
fields = {} | ||
image = None | ||
|
||
for field in data.keys(): | ||
if field in field_names: | ||
if field == 'image': | ||
image = data[field].value # file upload | ||
image_type = data[field].type | ||
if image and image_type not in ('image/png', 'image/jpeg'): | ||
raise Response(400, _("Please upload a PNG or JPG image.")) | ||
else: | ||
value = data.get(field, '').strip(' ') | ||
if not value: | ||
raise Response(400, _("Please fill out the '{}' field.", field_names[field])) | ||
|
||
if (field in ('homepage', 'onboarding_url', 'todo_url') | ||
and not valid_url(value)): | ||
raise Response(400, | ||
_( "Please enter an http[s]:// URL for the '{}' field." | ||
, field_names[field] | ||
)) | ||
fields[field] = value | ||
|
||
if fields: | ||
team.update(**fields) | ||
|
||
if image: | ||
code, large, small = imgize(image, image_type) | ||
if code == 200: | ||
team.save_image(image, large, small, image_type) | ||
elif code == 413: | ||
raise Response(400, _("Please upload an image smaller than 100 kB.")) | ||
elif code == 415: | ||
raise Response(400, _("Please upload a PNG or JPG image.")) | ||
else: | ||
raise Response(500, _("Sorry, there was a problem saving your image. Please try again.")) | ||
|
||
[---] application/json via json_dump | ||
team.to_dict() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
from aspen import Response | ||
|
||
from gratipay.utils import get_team | ||
|
||
[---] | ||
request.allow('GET') | ||
|
||
if user.ANON: | ||
raise Response(401, _("You need to log in to access this page.")) | ||
|
||
team = get_team(state) | ||
|
||
if team.is_closed: | ||
raise Response(403, _("You can't edit a closed team.")) | ||
|
||
if team.is_approved is False: # for teams under review, is_approved is None. | ||
raise Response(403, _("You can't edit a rejected team.")) | ||
|
||
if not user.ADMIN and user.participant.username != team.owner: | ||
raise Response(403, _("You are not authorized to access this page.")) | ||
|
||
title = _("Edit your team") | ||
banner = _("Edit") | ||
suppress_sidebar = True | ||
|
||
[---] text/html | ||
{% extends "templates/base.html" %} | ||
|
||
{% block scripts %} | ||
<script>$(document).ready(Gratipay.edit_team.initForm);</script> | ||
{% endblock %} | ||
|
||
{% block content %} | ||
<style> | ||
textarea { | ||
width: 100%; | ||
height: 200px; | ||
} | ||
input[type="file"] { | ||
margin-left: 10px; | ||
} | ||
</style> | ||
|
||
<form action="edit.json" method="POST" id = "edit-team"> | ||
<input type="hidden" name="csrf_token" value="{{ csrf_token }}"> | ||
|
||
<label><h2>{{ _("Team Name") }}</h2></label> | ||
<input type="text" name="name" value="{{team.name}}" required autofocus> | ||
|
||
<label><h2>{{ _("Image") }}</h2></label> | ||
<img src="{{ team.get_image_url('small') }}" align="middle"> | ||
<input type="file" name="image" accept="image/png, image/jpeg"> | ||
|
||
<label><h2>{{ _("Product or Service") }}</h2></label> | ||
<textarea name="product_or_service" required>{{team.product_or_service}}</textarea> | ||
|
||
<label><h2>{{ _("Homepage") }}</h2></label> | ||
<input type="text" name="homepage" value="{{team.homepage}}" required> | ||
|
||
<label><h2>{{ _("Self-onboarding Documentation URL") }}</h2></label> | ||
<input type="text" name="onboarding_url" value="{{team.onboarding_url}}" required> | ||
|
||
<label><h2>{{ _("To-do URL") }}</h2></label> | ||
<input type="text" name="todo_url" value="{{team.todo_url}}" required> | ||
|
||
<br> | ||
<br> | ||
<button type="submit">{{ _("Modify") }}</button> | ||
<button onclick="window.location='../';return false;">{{ _("Cancel") }}</button> | ||
|
||
</form> | ||
{% endblock %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters