Skip to content

Commit

Permalink
Initial commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
pflarr committed Jul 25, 2011
0 parents commit cb472c4
Show file tree
Hide file tree
Showing 33 changed files with 770 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
0.0
---

- Initial version
2 changes: 2 additions & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
include *.txt *.ini *.cfg *.rst
recursive-include basic_site *.ico *.png *.css *.gif *.jpg *.pt *.txt *.mak *.mako *.js *.html *.xml
4 changes: 4 additions & 0 deletions README.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
basic_site README



25 changes: 25 additions & 0 deletions basic_site.egg-info/PKG-INFO
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
Metadata-Version: 1.0
Name: basic-site
Version: 0.0
Summary: basic_site
Home-page: UNKNOWN
Author: UNKNOWN
Author-email: UNKNOWN
License: UNKNOWN
Description: basic_site README





0.0
---

- Initial version

Keywords: web wsgi bfg pylons pyramid
Platform: UNKNOWN
Classifier: Programming Language :: Python
Classifier: Framework :: Pylons
Classifier: Topic :: Internet :: WWW/HTTP
Classifier: Topic :: Internet :: WWW/HTTP :: WSGI :: Application
29 changes: 29 additions & 0 deletions basic_site.egg-info/SOURCES.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
CHANGES.txt
MANIFEST.in
README.txt
development.ini
production.ini
setup.cfg
setup.py
basic_site/__init__.py
basic_site/models.py
basic_site/tests.py
basic_site/views.py
basic_site.egg-info/PKG-INFO
basic_site.egg-info/SOURCES.txt
basic_site.egg-info/dependency_links.txt
basic_site.egg-info/entry_points.txt
basic_site.egg-info/not-zip-safe
basic_site.egg-info/paster_plugins.txt
basic_site.egg-info/requires.txt
basic_site.egg-info/top_level.txt
basic_site/static/favicon.ico
basic_site/static/footerbg.png
basic_site/static/headerbg.png
basic_site/static/ie6.css
basic_site/static/middlebg.png
basic_site/static/pylons.css
basic_site/static/pyramid-small.png
basic_site/static/pyramid.png
basic_site/static/transparent.gif
basic_site/templates/mytemplate.pt
1 change: 1 addition & 0 deletions basic_site.egg-info/dependency_links.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

3 changes: 3 additions & 0 deletions basic_site.egg-info/entry_points.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[paste.app_factory]
main = basic_site:main

1 change: 1 addition & 0 deletions basic_site.egg-info/not-zip-safe
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

1 change: 1 addition & 0 deletions basic_site.egg-info/paster_plugins.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pyramid
6 changes: 6 additions & 0 deletions basic_site.egg-info/requires.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
pyramid
SQLAlchemy
transaction
repoze.tm2>=1.0b1
zope.sqlalchemy
WebError
1 change: 1 addition & 0 deletions basic_site.egg-info/top_level.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
basic_site
20 changes: 20 additions & 0 deletions basic_site/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from pyramid.config import Configurator
from pyramid.session import UnencryptedCookieSessionFactoryConfig
from sqlalchemy import engine_from_config

from basic_site.models import initialize_sql

def main(global_config, **settings):
""" This function returns a Pyramid WSGI application.
"""
engine = engine_from_config(settings, 'sqlalchemy.')
initialize_sql(engine)
session_factory = UnencryptedCookieSessionFactoryConfig()
config = Configurator(settings=settings, session_factory=session_factory)
config.add_static_view('static', 'basic_site:static')
config.add_route('Main', '/')
config.add_view('basic_site.views.Main',
route_name='home',
renderer='templates/main.mako')
return config.make_wsgi_app()

165 changes: 165 additions & 0 deletions basic_site/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
from sqlalchemy import create_engine, Column, ForeignKey
from sqlalchemy.orm import sessionmaker, scoped_session
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.types import String, DateTime, Integer, Boolean

from zope.sqlalchemy import ZopeTransactionExtension

from z3c.bcrypt import BcryptPasswordManager
manager = BcryptPasswordManager()

import sqlalchemy.orm

DBSession = scoped_session(sessionmaker(extension=ZopeTransactionExtension()))
Base = declarative_base()

def initialize_sql(engine):
DBSession.configure(bind=engine)
Base.metadata.bind = engine
Base.metadata.create_all(engine)

class User(Base):
__tablename__ = 'Users'
uid = Column(String(10), primary_key=True)
pw_hash = Column(String())
fullname = Column(String())

def __init__(self, name, pw, fullname):
self.uid = name[:10]
self.pw_hash = manager.encodePassword(pw)
self.fullname = fullname

def check_pw(self, passwd):
return manager.checkPassword(self.pw_hash, passwd)

def change_pw(self, old, new):
"""Verifies the old pw before changing it to new. Returns True if
successful."""
if manager.checkPassword(self.pw_hash, old):
self.pw_hash = manager.encodePassword(pw)
return True
else:
return False

class UserSession(Base):
__tablename__ = 'Sessions'
id = Column(String(), primary_key=True)
last_used = Column(DateTime(), nullable=False)
ip = Column(Integer(), nullable=False)
user = Column(String(10), ForeignKey(User.uid))

def __init__(self, ip):
now = datetime.datetime.utcnow()
md5 = hashlib.md5(ip)
md5.update(randome.randint(1,0xffffffff))
self.id = md5.hexdigest()
self.last_used = now
self.ip = ip

class Post(Base):
__tablename__ = 'Post'
id = Column(Integer(), primary_key=True)
created = Column(DateTime(), nullable=False)
creator = Column(String(10), nullable=False)
sticky = Column(Boolean(), nullable=False)
contents = Column(String(), nullable=False)

def __init__(self, creator, title, contents, sticky=False):
if created:
self.created = created
else:
self.created = datetime.datetime.utcnow()
self.creator = creator
self.sticky = sticky
self.title = title[:30]
self.contents = contents

def edit(self, user, new_title, new_content):
"""Edit this post, and record the change in the history."""
session = DBSession()
hist = Post_History(user, self)
self.title = new_title[:30]
self.content = new_content
session.add(hist)

class Post_History(Base):
id = Column(Integer(), nullable=False)
created = Column(DateTime(), nullable=False)
creator = Column(String(10), nullable=False)
sticky = Column(Boolean(), nullable=False)
title = Column(String(30), nullable=False)
contents = Column(String(), nullable=False)
changed_by = Column(String(10), ForiegnKey(Users.uid), nullable=False)
changed_on = Column(DateTime(), nullable=False)

def __init__(self, editor_uid, post):
for col in post.__table__.c:
self.__table__[col] = post.__table__.c[col]
self.changed_by = editor_uid
self.changed_on = datetime.datetime.utcnow()

def restore(self, user):
"""Restore this history item's content."""
session = DBSession()
try:
post = session.query(Post)\
.filter(Post.id = self.id)\
.one()
post.edit(user, self.title, self.content)
except sqlalchemy.orm.exc.NoResultFound:
# Recreate it if it doesn't still exist
post = Post(user, self.title, self.contents, self.created)
session.add(page)
session.flush()

class Page(Base):
__tablename__ = 'Page'
name = Column(String(15), primary_key=True)
created = Column(DateTime(), nullable=False)
creator = Column(String(10), nullable=False)
contents = Column(String(), nullable=False)

def __init__(self, creator, name, contents, created=None):
if created:
self.created = created
else:
self.created = datetime.datetime.utcnow()
self.name = name[:15]
self.creator = creator
self.contents = contents

def edit(self, new_name, new_content, user):
"""Edit this post, and record the change in the history."""
session = DBSession()
hist = Page_History(user, self)
self.name = new_name
self.content = new_content
session.add(hist)

class Page_History(Base):
name = Column(String(15), nullable=False)
created = Column(DateTime(), nullable=False)
creator = Column(String(10), nullable=False)
contents = Column(String(), nullable=False)
changed_by = Column(String(10), ForiegnKey(Users.uid), nullable=False)
changed_on = Column(DateTime(), nullable=False)

def __init__(self, editor_uid, page):
for col in page.__table__.c:
self.__table__[col] = page.__table__.c[col]
self.changed_by = editor_uid
self.changed_on = datetime.datetime.utcnow()

def restore(self, user):
"""Restore this history item's content."""
session = DBSession()
try:
page = session.query(Page)\
.filter(Page.id = self.id)\
.one()
page.edit(self.content, user)
except sqlalchemy.orm.exc.NoResultFound:
# Recreate it if it doesn't still exist
page = Page(user, self.name, self.contents, self.created)
session.add(page)
session.flush()
94 changes: 94 additions & 0 deletions basic_site/security.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import datetime

from models import UserSession, User

class Auth:
session_expire = datetime.timedelta(hours=2)
def __init__(self, request):
self.error = None
self.user = None
self.session = None
self.ip = None

if 'HTTP_COOKIE' in env:
try:
cookie = request.cookie
except:
cookie = None
else:
cookie = None

if cookie:
sessionkey = cookie.get('session').value
else:
sessionkey = None

ip = env.get('REMOTE_ADDR', 0)
if ip:
try:
ip = ip.split('.')
ip = map(int, ip)
ip = ip[0] << 24 + ip[1] << 16 + ip[2] << 8 + ip[3]
except:
ip = 0

if ip == 0:
return

if sessionkey:
q = session.query(UserSession)\
.filter(UserSession.id == sessionkey)\
.filter(UserSession.ip == ip)\
.filter(UserSession.last_used >
(now - self.session_expire))
try:
user_sess = q.one()
except:
# Couldn't find a matching session
sessionkey = None

# If they don't have a session key already, make them one.
if not sessionkey:
user_sess = UserSession(ip)
session.add(user_sess)
self.ip = ip
self.session = user_sess.id
request.response.set_cookie('session', self.session)
return

q = session.query(UserSession)
q.filter(UserSession.last_used < (now - self.session_expire))
q.delete()

# At this point, we should have a user_sess object and a
# session key. Now we'll check to see if the session key is logged in,
# or if the user is trying to log in.
request.response.set_cookie('session', self.session)
self.session = sessionkey
self.ip = ip
user_sess.last_used = now
session.commit()
if user_sess.user:
# Already logged in
self.user = user_sess.user
return

# If they aren't trying to log in, we're done.
if 'user' not in form or 'passwd' not in form:
return

uid = form.get_first('user')
passwd = form.get_first('passwd')
try:
user = session.query(User).filter(User.uid = uid).one()
except:
self.error = "No such user: %s" % uid
return

if user.check_pw(passwd):
self.user = uid
else:
self.error = "Invalid Password"

def logged_in(self):
return self.user is not None
Binary file added basic_site/static/favicon.ico
Binary file not shown.
Binary file added basic_site/static/footerbg.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added basic_site/static/headerbg.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 8 additions & 0 deletions basic_site/static/ie6.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
* html img,
* html .png{position:relative;behavior:expression((this.runtimeStyle.behavior="none")&&(this.pngSet?this.pngSet=true:(this.nodeName == "IMG" && this.src.toLowerCase().indexOf('.png')>-1?(this.runtimeStyle.backgroundImage = "none",
this.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + this.src + "',sizingMethod='image')",
this.src = "static/transparent.gif"):(this.origBg = this.origBg? this.origBg :this.currentStyle.backgroundImage.toString().replace('url("','').replace('")',''),
this.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + this.origBg + "',sizingMethod='crop')",
this.runtimeStyle.backgroundImage = "none")),this.pngSet=true)
);}
#wrap{display:table;height:100%}
Binary file added basic_site/static/middlebg.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit cb472c4

Please sign in to comment.