Skip to content
This repository was archived by the owner on Sep 26, 2022. It is now read-only.
Open
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ db.sqlite
*.mo
*.log
*.tmp.*
*.swp
*~
\#.*\#
*.iml
.idea/
__pycache__
Expand Down
33 changes: 33 additions & 0 deletions alembic/versions/4d51c84f8dca_create_notify_table.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
"""create notify table

Revision ID: 4d51c84f8dca
Revises: 4ad568a7a84e
Create Date: 2014-02-26 13:26:20.556780

"""

# revision identifiers, used by Alembic.
revision = '4d51c84f8dca'
down_revision = '4ad568a7a84e'

from datetime import datetime
from alembic import op
import sqlalchemy as sa


def upgrade():
op.create_table(
'notify',
sa.Column('id', sa.Integer, primary_key=True),
sa.Column('account_id', sa.Integer, nullable=False),
sa.Column('topic_id', sa.Integer, index=True, nullable=False),
sa.Column('reason', sa.String(100), nullable=False),
sa.Column('is_viewed', sa.String(100), default=0, nullable=False),
sa.Column('created', sa.DateTime, default=datetime.utcnow),
)
op.add_column('account', sa.Column('notify_count', sa.Integer, default=0))


def downgrade():
op.drop_table('notify')
op.drop_column('account', 'notify_count')
3 changes: 2 additions & 1 deletion june/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ def rendering_time(response):


def register_routes(app):
from .handlers import front, account, node, topic, user, admin
from .handlers import front, account, node, topic, user, admin, notify
app.register_blueprint(notify.bp, url_prefix='/notify')
app.register_blueprint(account.bp, url_prefix='/account')
app.register_blueprint(node.bp, url_prefix='/node')
app.register_blueprint(topic.bp, url_prefix='/topic')

Choose a reason for hiding this comment

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

Is return app at the end of func register_routes necessary?

Copy link
Contributor

Choose a reason for hiding this comment

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

no

Choose a reason for hiding this comment

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

so why not remove

Expand Down
30 changes: 30 additions & 0 deletions june/handlers/notify.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# coding: utf-8

from flask import Blueprint, g, request
from flask import render_template, abort
from ..helpers import force_int
from ..models import Notify
from ..models import fill_with_topics, fill_with_users
from ..utils.user import require_login


__all__ = ['bp']

bp = Blueprint('notify', __name__)


@bp.route('/')
@require_login
def notifies():
"""
The topics list page.
"""
page = force_int(request.args.get('page', 1), 0)
if not page:
return abort(404)
paginator = Notify.query.filter_by(
account_id=g.user.id, is_viewed=0).order_by(
Notify.created.desc()).paginate(page)
paginator.items = fill_with_users(paginator.items)
paginator.items = fill_with_topics(paginator.items)
return render_template('notify/notify.html', paginator=paginator)
14 changes: 13 additions & 1 deletion june/handlers/topic.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from flask import url_for
from flask.ext.babel import gettext as _
from ..helpers import force_int, limit_request
from ..models import db, Node, Account
from ..models import db, Node, Account, Notify
from ..models import Topic, Reply, LikeTopic
from ..models import fill_topics, fill_with_users
from ..forms import TopicForm, ReplyForm
Expand Down Expand Up @@ -132,6 +132,18 @@ def view(uid):
topic.author = author
topic.node = node

if g.user.notify_count != 0:
args = {'account_id': g.user.id,
'topic_id': topic.id,
'is_viewed': 0}
notifies = Notify.query.filter_by(**args).all()
g.user.notify_count -= len(notifies)
db.session.add(g.user)
for i in notifies:
i.is_viewed = 1
db.session.add(i)
db.session.commit()

if g.user:
topic.like = LikeTopic.query.filter_by(
account_id=g.user.id, topic_id=uid
Expand Down
13 changes: 13 additions & 0 deletions june/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from .account import *
from .node import *
from .topic import *
from .notify import *
from flask.ext.sqlalchemy import models_committed


Expand Down Expand Up @@ -34,6 +35,13 @@ def fill_with_nodes(items):
return items


def fill_with_topics(items):
tids = set(map(lambda o: o.topic_id, items))
topics = Topic.query.filter_in(Topic.id, tids)
items = map(lambda o: _attach_topic(o, topics.get(o.topic_id)), items)
return items


def get_by_ids(model, uids):
if not len(uids):
return {}
Expand Down Expand Up @@ -86,6 +94,11 @@ def _attach_node(item, node):
return item


def _attach_topic(item, topic):
item.topic = topic
return item


def _clear_cache(sender, changes):
for model, operation in changes:
if isinstance(model, Account) and operation != 'update':
Expand Down
1 change: 1 addition & 0 deletions june/models/account.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class Account(db.Model, SessionMixin):

created = db.Column(db.DateTime, default=datetime.utcnow)
token = db.Column(db.String(20))
notify_count = db.Column(db.Integer, default=0)

def __init__(self, **kwargs):
self.token = self.create_token(16)
Expand Down
21 changes: 21 additions & 0 deletions june/models/notify.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# coding: utf-8

from datetime import datetime
from ._base import db, SessionMixin

__all__ = ['Notify']


class Notify(db.Model, SessionMixin):
id = db.Column(db.Integer, primary_key=True)
account_id = db.Column(db.Integer, nullable=False)
topic_id = db.Column(db.Integer, index=True, nullable=False)
reason = db.Column(db.String(100), nullable=False)
is_viewed = db.Column(db.String(100), default=0, nullable=False)
created = db.Column(db.DateTime, default=datetime.utcnow)

def __str__(self):
return self.id

def __repr__(self):
return '<Notify: %s>' % self.id
24 changes: 24 additions & 0 deletions june/models/topic.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
# coding: utf-8

import re
from datetime import datetime
from ._base import db
from .account import Account
from .notify import Notify
from .node import Node, NodeStatus


Expand Down Expand Up @@ -153,6 +155,28 @@ def save(self, user=None, topic=None):
topic.updated = datetime.utcnow()
db.session.add(topic)

if topic.account_id != user.id:
args = {'account_id': topic.account_id,
'topic_id': topic.id,
'reason': 'reply'}
notify = Notify(**args)
db.session.add(notify)
creator = Account.query.get(topic.account_id)
creator.notify_count += 1
db.session.add(creator)

for user_ated in set(re.findall('@([0-9a-z]+)', self.content)):
u = Account.query.filter_by(username=user_ated).first()
if not u or u.id in [topic.account_id, user.id]:
continue
args = {'account_id': u.id,
'topic_id': topic.id,
'reason': 'at'}
notify = Notify(**args)
db.session.add(notify)
u.notify_count += 1
db.session.add(u)

db.session.add(self)
db.session.commit()
return self
Expand Down
20 changes: 20 additions & 0 deletions june/templates/notify/notify.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{% extends "layout.html" %}
{% from "snippet/macro.html" import notify_columns, pagination %}

{% block subtitle %}{{ _('Notify') }}{% endblock %}

{% block content %}
<div class="panel panel-default">
<div class="panel-heading clearfix">
<h3 class="panel-title">{{ _('Unread Message') }}</h3>
</div>
<div class="panel-columns">
{{ notify_columns(paginator.items) }}
</div>
{% if paginator.pages > 1 %}
<div class="panel-footer">
{{ pagination(paginator, url_for('notify.notifies')) }}
</div>
{% endif %}
</div>
{% endblock %}
27 changes: 27 additions & 0 deletions june/templates/snippet/macro.html
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,26 @@ <h3 class="title">
{% endif %}
{% endmacro %}

{% macro render_notify(item) %}
<div class="item">
<p> user <a href="{{ url_for('user.view', username=item.user.username) }}"> {{item.user.username}}</a> {{item.reason}} you on topic <a href="{{ url_for('topic.view', uid=item.topic.id) }}"> {{item.topic.title}} </a> </p>
</div>
{% endmacro %}

{% macro notify_columns(notifies) %}
{% if notifies %}
<div class="panel-columns">
{% for item in notifies %}
{{ render_notify(item) }}
{% endfor %}
</div>
{% else %}
<div class="panel-body">
<p>{{ _('There is no notify yet.') }}</p>
</div>
{% endif %}
{% endmacro %}

{% macro pagination(paginator, url) %}
<ul class="pagination pagination-centered">
{% for page in paginator.iter_pages() %}
Expand All @@ -108,5 +128,12 @@ <h3 class="title">
</li>
<li {% if current == 'user' %}class="active"{% endif %}><a href="{{ url_for('user.users') }}">{{ _('Users') }}</a>
</li>
{% if g.user %}
<li {% if current == 'notify' %}class="active"{% endif %}><a href="{{ url_for('notify.notifies') }}">
{% if g.user.notify_count == 0 %}
{{ _('Notify') }} {% else %}
{{ _('Notify (%s)' % g.user.notify_count) }} {% endif %}
</a> </li>
{% endif %}
</ul>
{% endmacro %}