class GroupForm(djangoforms.ModelForm): class Meta: model = Group fields = ['title', 'introduction'] title = fields.CharField(label = _("Group Title"), min_length=2,\ max_length=10) introduction = fields.CharField(label = _("Group Introductioin"),\ widget=forms.Textarea, min_length=2, max_length = 2000)
class TopicForm(djangoforms.ModelForm): """docstring for TopicForm""" class Meta: model = GroupTopic fields = ['title', 'content'] title = fields.CharField(label = _('Topic Title'), min_length=2,\ max_length=50) content = fields.CharField(label = _('Topic Content'), min_length=5,\ widget=forms.Textarea, max_length=5000)
class UserForm(djangoforms.ModelForm): """docstring for UserForm""" class Meta: model = User fields = ["fullname", "email", "status_message", "introduction"] fullname = fields.CharField(label = _('Full Name'), min_length=1,\ max_length=17) status_message = fields.CharField(label = _("Status Message"), min_length=1,\ max_length=70, required=False) email = fields.EmailField(label=_('Email')) introduction = fields.CharField(label = _('Self Introduction'), min_length=1,\ widget=forms.Textarea, max_length=5000)
class Site(db.Model): """a faked object""" site_name = db.StringProperty(required=True, default=_("Daoshicha.com")) site_slogan = db.StringProperty(required=True, \ default= _("Want to be the best open source SNS!")) @classmethod @cache_result('globalsite-instance', 240) def get_instance(cls): """docstring for get_instance""" instance = super(Site, cls).all().get() if not instance: instance = Site() instance.put() return instance
class UserStreamForm(djangoforms.ModelForm): """docstring for UserStreamForm""" class Meta: model = UserStream fields = ['content'] content = fields.CharField(label=_('Content of stream'), max_length=140)
class PostForm(djangoforms.ModelForm): """docstring for PostForm""" class Meta: model = GroupPost fields = ['content'] content = fields.CharField(label = _('Post Content'), min_length=1,\ widget=forms.Textarea, max_length=2000)
def profile(self, request): """The user profile settings page""" # user image setting if request.get('image_url', ''): self.user.photo_url = request.get('image_url') self.user.put() site_message = _("Your photo has been changed.:-)") form = UserForm(instance=self.user) photo_form = ProfilePhotoForm() photo_upload_url = photo.create_upload_url() back_url = request.path logging.debug("form %s", self.user.to_dict()) return template('page_user_profile.html', locals())
class ProfilePhotoForm(forms.Form): """docstring for ProfilePhoto""" photo = fields.ImageField(label=_("Profile Picture"))
def pretty_time(time): """ Get a datetime object or a int() Epoch timestamp and return a pretty string like 'an hour ago', 'Yesterday', '3 months ago', 'just now', etc This method is derived from the stackoverflow post: http://stackoverflow.com/questions/1551382/python-user-friendly-time-format """ from datetime import datetime now = datetime.now() if type(time) is int: diff = now - datetime.fromtimestamp(time) elif not time: diff = now - now else: diff = now - time second_diff = diff.seconds day_diff = diff.days if day_diff < 0: return '' if day_diff == 0: if second_diff < 10: return _("just now") if second_diff < 60: return str(second_diff) + _(" seconds ago") if second_diff < 120: return _("a minute ago") if second_diff < 3600: return str( second_diff / 60 ) + _(" minutes ago") if second_diff < 7200: return _("an hour ago") if second_diff < 86400: return str( second_diff / 3600 ) + _(" hours ago") if day_diff == 1: return _("Yesterday") if day_diff < 7: return str(day_diff) + _(" days ago") if day_diff < 31: return str(day_diff/7) + _(" weeks ago") if day_diff < 365: return str(day_diff/30) + _(" months ago") return str(day_diff/365) + _(" years ago")
def pretty_time(time): """ Get a datetime object or a int() Epoch timestamp and return a pretty string like 'an hour ago', 'Yesterday', '3 months ago', 'just now', etc This method is derived from the stackoverflow post: http://stackoverflow.com/questions/1551382/python-user-friendly-time-format """ from datetime import datetime now = datetime.now() if type(time) is int: diff = now - datetime.fromtimestamp(time) elif not time: diff = now - now else: diff = now - time second_diff = diff.seconds day_diff = diff.days if day_diff < 0: return '' if day_diff == 0: if second_diff < 10: return _("just now") if second_diff < 60: return str(second_diff) + _(" seconds ago") if second_diff < 120: return _("a minute ago") if second_diff < 3600: return str(second_diff / 60) + _(" minutes ago") if second_diff < 7200: return _("an hour ago") if second_diff < 86400: return str(second_diff / 3600) + _(" hours ago") if day_diff == 1: return _("Yesterday") if day_diff < 7: return str(day_diff) + _(" days ago") if day_diff < 31: return str(day_diff / 7) + _(" weeks ago") if day_diff < 365: return str(day_diff / 30) + _(" months ago") return str(day_diff / 365) + _(" years ago")
class UserStreamUI(ModelUI): """docstring for UserStreamUI""" def __init__(self, user_stream_info): super(UserStreamUI, self).__init__(user_stream_info) self.user_stream_info = user_stream_info self.user = user_stream_info.user self.current_user = get_current_user() def _home(self, query, request, new_vars={}): """docstring for _home""" limit = int(request.get('limit', 20)) offset = int(request.get('offset', 0)) groupinfo = UserGroupInfo.get_by_user(self.user) joined_groups = groupinfo.get_recent_joined_groups() stream_form = UserStreamForm() page = Page(query=query, limit=limit, offset=offset, request=request) streams = page.data() following_users = self.user_stream_info.recent_following_users() follower_users = self.user_stream_info.recent_follower_users() for stream in streams: logging.debug("stream %s", stream) post_url = "/u/%d" % self.user.key().id() var_dict = locals() var_dict.update(new_vars) return var_dict def home(self, request): """docstring for home""" if self.user_stream_info.can_view_following(self.current_user): return redirect("/u/%d/following" % self.user.key().id()) return redirect("/u/%d/all" % self.user.key().id()) @check_permission('view_all', _("Can't visit given user's homepage")) def home_all(self, request): """docstring for home""" query = UserStream.latest_by_author(self.user) return template('page_user_stream_view_all.html', self._home(query, request)) @check_permission('view_following', _("Can't visit given user's homepage")) def home_following(self, request): """docstring for home_following""" query = UserStream.latest_by_subscriber(self.user) return template('page_user_stream_view_following.html', self._home(query, request)) @check_permission('view_mention', _("Can't view given user's mentions")) def home_mention(self, request): """docstring for home_mention""" keyword = "@" + self.user.fullname query = UserStream.latest_by_keyword(keyword) return template('page_user_stream_view_mention.html', self._home(query, request)) @check_permission('create_stream', _("Can't create stream for user")) def home_post(self, request): """docstring for home_post""" stream_form = UserStreamForm(data=request.POST) if stream_form.is_valid(): new_stream = stream_form.save(commit=False) self.user_stream_info.create_stream(new_stream) return back() @check_permission('follow', _("Can't follow the user")) def follow(self): """docstring for follow""" self.user_stream_info.follow(self.current_user) return back() @check_permission('unfollow', _("Can't unfollow given user")) def unfollow(self): """docstring for unfollow""" self.user_stream_info.unfollow(self.current_user) return back()