Skip to content

Commit

Permalink
Merge pull request tsoporan#6 from scjudd/develop
Browse files Browse the repository at this point in the history
template/manage.py magic!
  • Loading branch information
Titus Soporan committed Sep 16, 2011
2 parents 15d1606 + 2e004c8 commit e4a4ff1
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 32 deletions.
5 changes: 5 additions & 0 deletions cyclonejet/templates/base.html
Expand Up @@ -25,6 +25,11 @@
<body>
<div id="container">
<header>
{% if session['logged_in'] %}
<p>Welcome {{ session['username'] }}! <a href="{{ url_for('frontend.logout') }}">Logout</a>?</p>
{% else %}
<p><a href="{{ url_for('frontend.login') }}">Login</a></p>
{% endif %}
<nav>
<ul>
<li><a href=""></a></li>
Expand Down
2 changes: 1 addition & 1 deletion cyclonejet/templates/index.html
@@ -1,6 +1,6 @@
{% extends 'base.html' %}

{% block title %}}home{% endblock %}
{% block title %}home{% endblock %}

{% block content %}

Expand Down
26 changes: 12 additions & 14 deletions cyclonejet/templates/login.html
Expand Up @@ -5,21 +5,19 @@
{% block content %}

<form method="post" action="">
{% for field in form %}
{% for field in form %}

{% if field.name == 'csrf' %}
{# do nothing i.e don't display this label #}
{% else %}
{{ field.label }}
{% endif %}

{{ field }}

{% if field.errors %}
<ul class='errors'>
{% for error in field.errors %}<li>{{ error }}{% endfor %}
</ul>
{% endif %}
{% if field.name != 'csrf' %}
{{ field.label }}
{% endif %}

{{ field }}

{% if field.errors %}
<ul class='errors'>
{% for error in field.errors %}<li>{{ error }}{% endfor %}
</ul>
{% endif %}

{% endfor %}
<input type="submit" value="Login">
Expand Down
1 change: 1 addition & 0 deletions cyclonejet/views/frontend.py
Expand Up @@ -36,6 +36,7 @@ def login():
else:
flash("You've been logged in")
session['uid'] = user.id
session['username'] = user.username
session['logged_in'] = True
return redirect(url_for('.index'))

Expand Down
63 changes: 46 additions & 17 deletions manage.py
Expand Up @@ -5,7 +5,7 @@
from flaskext.script import Manager
from cyclonejet import create_app
from cyclonejet.extensions import db
from cyclonejet.models import User, Anime
from cyclonejet.models import User, Anime, Manga

app = create_app('cyclonejet')
manager = Manager(app)
Expand All @@ -18,17 +18,15 @@ def create_db():
def drop_db():
db.drop_all()

@manager.command
def populate_anime(fp):
""" Populate the Anime database from json. """
def import_json(fp, model_class):

import os
assert os.path.exists(fp), "That file path doesn't seem to exist."

try:
import simplejson
animes = simplejson.load(open(fp))
except: #this needs to be json
entries = simplejson.load(open(fp))
except: # this needs to be json
raise

user = User.query_class.create_user(
Expand All @@ -37,27 +35,58 @@ def populate_anime(fp):
password='changeme'
)

for a in animes:
anime = Anime(
title = a['title'],
description = a['description'],
)
anime.user = user
db.session.add(anime)

for e in entries:

# this could be cleaned up a bit once we have the mangareader
# spider returning cleaner data - @scjudd

if model_class.__name__ == 'Manga':
try:
description = e['description'][0],
except IndexError:
description = e['description'],

entry = model_class(
title = e['title'],
description = description,
year = e['year'],
author = e['author']
)
elif model_class.__name__ == 'Anime':
entry = model_class(
title = e['title'],
description = e['description']
)
else:
raise

entry.user = user
db.session.add(entry)

try:
db.session.commit()
except Exception, e: #these are most-likely dupes
except Exception, e: # these are most-likely dupes
print(e.message)
db.session.rollback()

print('Added anime: {}'.format(anime.title))
try:
print('Added {}: {}'.format(entry.__class__.__name__, entry.title))
except UnicodeEncodeError:
print('Added {}: {}'.format(entry.__class__.__name__, '[Error: UnicodeEncodeError]'))

print('All done!')


@manager.command
def populate_anime(fp):
""" Populate the Anime database from json. """
import_json(fp, Anime)


@manager.command
def populate_manga(fp):
pass
""" Populate the Manga database from json. """
import_json(fp, Manga)


#prefill our shell with some defaults
Expand Down

0 comments on commit e4a4ff1

Please sign in to comment.