Anonymous avatar Anonymous committed f3db9f3

groups initial coding

Comments (0)

Files changed (6)

newsmeme/forms.py

 is_username = regexp(USERNAME_RE, 
                      message=_("You can only use letters, numbers or dashes"))
 
+
+class GroupForm(Form):
+
+    name = TextField(_("Name of your group"),
+        validators=[
+            required(message=_("Name is required"))
+        ])
+
+    about = TextField(_("About your group"),
+        validators=[
+            required(message=_("Some group detail required"))
+        ])
+
 class ContactForm(Form):
 
     name = TextField(_("Your name"), 

newsmeme/models.py

     email_alerts = db.Column(db.Boolean, default=False)
     followers = db.Column(DenormalizedText)
     following = db.Column(DenormalizedText)
+    groups = db.Column(DenormalizedText)
 
     _password = db.Column("password", db.String(80))
     _openid = db.Column("openid", db.String(80), unique=True)
     def get_friends(self):
         return User.query.filter(User.id.in_(self.friends))
 
+    def get_groups(self):
+        return Group.query.filter(db.or_(Group.owner_id==self.id,
+                                         Group.id.in_(self.groups)))
+
     def can_receive_mail(self, user):
         return self.receive_email and self.is_friend(user)
 
         return "http://www.gravatar.com/avatar/%s.jpg?s=%d" % (
             self.gravatar, size)
 
-    
+
+class Group(db.Model):
+
+    __tablename__ = "groups"
+
+    id = db.Column(db.Integer, primary_key=True)
+    name = db.Column(db.Unicode(100), unique=True)
+    about = db.Column(db.UnicodeText)
+    date_created = db.Column(db.DateTime, default=datetime.now)
+    owner_id = db.Column(db.Integer, db.ForeignKey(User.id), nullable=False)
+    members = db.Column(DenormalizedText)
+
+    owner = db.relation(User, innerjoin=True, lazy="joined")
+
+    __mapper_args__ = {'order_by' : name}
+
+    def is_member(self, user):
+        return user and user.id == self.owner_id or user.id in self.members
+
+    def can_join(self, user):
+        return user and not self.is_member(user)
+
+    def can_leave(self, user):
+        return user and self.is_member(user) and user.id != self.owner_id
+
+    def join(self, user):
+        self.members.append(user.id)
+        user.groups.append(self.id)
+
+    def leave(self, user):
+        self.members.remove(user.id)
+        user.groups.remove(self.id)
+
+    def get_members(self):
+        return User.query.filter(User.id.in_(self.members))
+
+
 class PostQuery(BaseQuery):
 
     def jsonify(self):
 
         deferred_cols = ("description", 
                          "tags",
+                         "group.name",
                          "author.email",
                          "author.password",
                          "author.activation_key",
                           db.ForeignKey(User.id, ondelete='CASCADE'), 
                           nullable=False)
     
+    group_id = db.Column(db.Integer,
+                         db.ForeignKey(Group.id, ondelete='CASCADE'))
+
     title = db.Column(db.Unicode(200))
     description = db.Column(db.UnicodeText)
     link = db.Column(db.String(250))
     _tags = db.Column("tags", db.UnicodeText)
 
     author = db.relation(User, innerjoin=True, lazy="joined")
-    
+    group = db.relation(Group, innerjoin=True, lazy="joined")
+
     __mapper_args__ = {'order_by' : id.desc()}
 
     def __init__(self, *args, **kwargs):

newsmeme/production_settings.py

+DEBUG = True
+
+SQLALCHEMY_DATABASE_URI = "postgresql://newsmeme:test@localhost/newsmeme" 
+#SQLALCHEMY_ECHO = False
+SQLALCHEMY_ECHO = True
+
+DEFAULT_MAIL_SENDER = "support@thenewsmeme.com"
+MAIL_SERVER = 'smtp.webfaction.com'
+MAIL_USERNAME = 'danjac354'
+MAIL_PASSWORD = 'm0nk3Y1972'
+
+ADMINS = ['danjac354@gmail.com']
+
+RECAPTCHA_PUBLIC_KEY = "6LcNlbsSAAAAAEpwDkMLGIpZE8iUMXOerzcMySLY"
+RECAPTCHA_PRIVATE_KEY = "6LcNlbsSAAAAAATQxj3O-NQIlQPp1l-XtRvo_mqw"
+
+GOOGLE_TRACKING_CODE = "UA-17762728-1"
+

newsmeme/templates/base.html

                 <li class="first">{{ tabbed_link('hot', _('hot'), url_for('frontend.index')) }}</li>
                 <li>{{ tabbed_link('latest', _('new'), url_for('frontend.latest')) }}</li>
                 <li>{{ tabbed_link('deadpool', _('deadpool'), url_for('frontend.deadpool')) }}</li>
+                <li>{{ tabbed_link('groups', _('groups'), url_for('groups.index')) }}</li>
                 <li>{{ tabbed_link('submit', _('submit'), url_for('frontend.submit')) }}</li>
                 <li class="last">
                     <form id="search" method="GET" action="{{ url_for('frontend.search') }}">

newsmeme/views/groups.py

+from flask import Module
+
+groups = Module(__name__)
+
+@groups.route("/")
+def index():
+    pass # render all groups
+
+
+@groups.route("/add/")
+def add_group():
+    pass # add a new group
+
+
+@groups.route("/<int:group_id>/")
+def group(group_id):
+    pass # group posts
+
+
+@groups.route("/<int:group_id>/members/")
+def members(group_id):
+    pass
+
+@groups.route("/<int:group_id>/edit/")
+def edit_group(group_id):
+    pass # group posts
+
+
+@groups.route("/<int:group_id>/delete/")
+def delete_group(group_id):
+    pass # group posts
+
+
+@groups.route("/<int:group_id>/join/")
+def join_group(group_id):
+    pass # group posts
+
+
+@groups.route("/<int:group_id>/leave/")
+def leave_group(group_id):
+    pass # group posts

newsmeme/views/user.py

                            following=following)
 
 
+@user.route("/<username>/groups/")
+@keep_login_url
+def groups(username):
+
+    user = User.query.filter_by(username=username).first_or_404()
+
+    num_posts = Post.query.filter_by(author_id=user.id).\
+        restricted(g.user).count()
+
+    num_comments = Comment.query.filter_by(author_id=user.id).\
+        restricted(g.user).count()
+ 
+    groups = user.get_groups()
+
+    return render_template("user/groups.html",
+                           user=user,
+                           num_posts=num_posts,
+                           num_comments=num_comments,
+                           groups=groups)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.