Skip to content

Commit 5a24a41

Browse files
authored
Merge pull request #34 from GiorginoSerbuciano/patches-v.0.1.0
Patching up minor things.
2 parents 73a83c9 + e4e4bec commit 5a24a41

File tree

6 files changed

+21
-18
lines changed

6 files changed

+21
-18
lines changed

README.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Tech Basics Companion v0.1.0-alpha
22

3+
![GitHub](https://img.shields.io/github/license/giorginoserbuciano/techbasicscompanion?style=flat-square)
4+
![GitHub last commit](https://img.shields.io/github/last-commit/giorginoserbuciano/techbasicscompanion?style=flat-square)
5+
![GitHub top language](https://img.shields.io/github/languages/top/giorginoserbuciano/techbasicscompanion?style=flat-square)
6+
7+
A Flask-SQLAlchemy website for sharing Python-related projects among Tech Basics students and alumni. Projects are linked to their respective Github repositories, from which information about the project is drawn through the Github API and presented on the site.
8+
39
_This project is built from scratch, following CoreyMS's Flask-SQLAlchemy tutorial series. You can find these code snippets [forked on my GitHub profile](https://github.com/GiorginoSerbuciano/code_snippets/tree/master/Python/Flask_Blog)._
410

511
Before starting work on TBCOMP, I had been planning to repurpose one of my other applications, DesktopSort, into a Python code analyser/file manipulation software, but I ultimately decided against that for the following reasons:
@@ -49,8 +55,7 @@ It turns out that silent failures are actually very easy to cause in WTForms; no
4955
</tr>
5056
<tr>
5157
<td>`field_name.data`</td>
52-
<td>The data returned from field `field_name`, seen from inside the context of a form.</tr>
53-
</tr>
58+
<td>The data returned from field `field_name`, seen from inside the context of a form.</tr>
5459
<tr>
5560
<td>`field_name`</td>
5661
<td>The field entity.</td>
@@ -110,6 +115,6 @@ Not all problems were equally educative. I had set up the password reset email s
110115

111116
A brief note on using GitHub: If you look at the commits on the project, you'll notice that my interaction with GitHub got more sophisiticated at some point in March. This is when I figured out how pushing, pulling, stashing, branching, etc. all work, so I've been using them to the greatest extent that I can. It's actually turning out to be very useful in a number of ways, the most important of which is that I can now commit changes very precisely; I sometimes commit one file at a time if the change I have made is significant only to that file. I'm also getting into the habit of creating issues and working on branches. All of this helps me navigate the project's history much quicker&mdash;which also makes it easier to document&mdash;and lets me reverse experimentation on one file without reversing what already worked well in 50 others.
112117

113-
I would like to look at a very recent example of an issue that really stretched my nerves, namely #8 Project creators cannot set contributors. In this issue, I dealt with many-to-many relationships between Flask-SQLAlchemy models for the first time. I'm going to use a slash to mirror the task: The task was to assign users as contributors to projects,/and to assign projects with contributors which were users. The left side of the slash was necessary in order to allow multiple users to be contributors of projects, while the right side allows projects to keep a record of which users are contributing to it for look-up purposes. The reason why I want to attribute multiple contributors to each project (and not just one author) is because, first of all, it's not impossible that more than one person works on a project&mdash;it's actually quite common in Tech Basics seminars, as it turns out&mdash;, so I want multiple people to be able to create posts on the project's page (yet to be implemented) and to edit projects (I have not yet figured out what the privileges of a project admin should be, if any).
118+
An issue that really stretched my nerves was #8 Project creators cannot set contributors. In this issue, I dealt with many-to-many relationships between Flask-SQLAlchemy models for the first time. I'm going to use a slash to mirror the task: The task was to assign users as contributors to projects,/and to assign projects with contributors which were users. The left side of the slash was necessary in order to allow multiple users to be contributors of projects, while the right side allows projects to keep a record of which users are contributing to it for look-up purposes. The reason why I want to attribute multiple contributors to each project (and not just one author) is because, first of all, it's not impossible that more than one person works on a project&mdash;it's actually quite common in Tech Basics seminars, as it turns out&mdash;, so I want multiple people to be able to create posts on the project's page (yet to be implemented) and to edit projects. I have not yet figured out what the privileges of a project admin should be, if any).
114119

115120

tbcompanion/accounts/routes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ def password_reset_request():
109109
form=form)
110110

111111

112-
@accounts.route('/passwordReset/<token>', methods=['POST'])
112+
@accounts.route('/passwordReset/<token>', methods=['GET','POST'])
113113
def password_reset(token):
114114
"""Password reset link redirects to this page. Resets user's password. """
115115

tbcompanion/models.py

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,30 +7,27 @@
77
from tbcompanion import db, login_man
88

99
contributor_table = db.Table('contributor_table',
10-
db.Column('user_id', db.Integer, db.ForeignKey('user.id')),
11-
db.Column('project_id', db.Integer, db.ForeignKey('project.id'))
12-
)
10+
db.Column('user_id', db.Integer, db.ForeignKey('user.id')),
11+
db.Column('project_id', db.Integer, db.ForeignKey('project.id'))
12+
)
13+
1314

1415
@login_man.user_loader
15-
def load_user(id):
16-
return User.query.get(int(id))
16+
def load_user(user_id):
17+
return User.query.get(int(user_id))
1718

1819

1920
class User(db.Model, UserMixin):
20-
2121
post_author = db.relationship('Post', backref='author', lazy=True)
2222
project_admin = db.relationship('Project', backref='admin', lazy=True)
23-
# projects = db.relationship('Project', secondary="contributor_table", lazy=True)
24-
#sqlalchemy.exc.ArgumentError: Error creating backref 'contributor' on relationship 'Project.contributor_id': property of that name exists on mapper 'mapped class User->user'
25-
23+
2624
id = db.Column(db.Integer, primary_key=True)
2725
username = db.Column(db.String(20), unique=True, nullable=False)
2826
email = db.Column(db.String(120), unique=True, nullable=False)
2927
image_file = db.Column(db.String(20), nullable=False, default='profile_pics\default.jpg')
3028
password = db.Column(db.String(60), nullable=False)
3129
is_admin = db.Column(db.Boolean)
3230

33-
3431
def __repr__(self):
3532
return self.username
3633

@@ -47,6 +44,7 @@ def validate_reset_token(token):
4744
return None
4845
return User.query.get(user_id)
4946

47+
5048
class Post(db.Model):
5149
id = db.Column(db.Integer, primary_key=True)
5250
title = db.Column(db.String(120), nullable=False)
@@ -61,7 +59,6 @@ def __repr__(self):
6159

6260

6361
class Project(db.Model):
64-
6562
contributors = db.relationship('User', secondary='contributor_table', backref='contribution_id')
6663

6764
id = db.Column(db.Integer, primary_key=True)
@@ -75,4 +72,3 @@ class Project(db.Model):
7572
def __repr__(self):
7673
return "Project('{}','{}','{}')".format(
7774
self.title, self.date, self.content)
78-

tbcompanion/posts/forms.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def validate_project_id(self, project_id):
3030

3131
"""Raise ValidationError if the project exists but the current user is not a contributor,
3232
or if the project does not exist."""
33-
if project and project.contributor != current_user:
33+
if project and current_user not in project.contributors:
3434
raise ValidationError('You are not contributing to this project. '
3535
'Please leave a comment on the project\'s page instead.')
3636
elif not project:

tbcompanion/posts/routes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ def update_post(post_id):
4949

5050
post_query = Post.query.get_or_404(post_id)
5151

52-
if post_query.author != current_user: # Only the post's author can edit it..
52+
if post_query.author != current_user: # Only the post's author can edit it.
5353
abort(403)
5454

5555
form = PostForm()

tbcompanion/templates/home.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
</fieldset>
2828
</form>
2929

30+
<a href="{{ url_for('.github_authorization') }}">To avoid API errors, authorize this app by following this link.</a>
31+
3032
{% endblock content %}
3133

3234
{% block feed %}

0 commit comments

Comments
 (0)