def post(self, request, *args, **kwargs): article_id = self.request.POST.get("article_id") comment = self.request.POST.get("comment") article = Article.objects.get(pk=article_id) new_comment = article.comments.create(author=request.user, content=comment) likes_count = new_comment.get_likes() dislikes_count = new_comment.get_dislikes() dt = datetime.now() df = DateFormat(dt) tf = TimeFormat(dt) new_comment_timestamp = ( df.format(get_format("DATE_FORMAT")) + ", " + tf.format(get_format("TIME_FORMAT")) ) data = [ { "author": new_comment.author.get_full_name(), "comment": new_comment.content, "comment_id": new_comment.pk, "comment_likes": likes_count, "comment_dislikes": dislikes_count, "timestamp": new_comment_timestamp, "slug": article.slug, } ] return JsonResponse(data, safe=False)
def _batch_update(): translation.activate(settings.LANGUAGE_CODE) requests = {} gte_date = timezone.now() - timedelta(days=1) updates = {} message_type = ContentType.objects.get_for_model(FoiMessage) for comment in Comment.objects.filter(content_type=message_type, submit_date__gte=gte_date): try: message = FoiMessage.objects.get(pk=comment.object_pk) if not message.request_id in requests: requests[message.request_id] = message.request updates.setdefault(message.request_id, []) tf = TimeFormat(comment.submit_date) updates[message.request_id].append( ( comment.submit_date, _("%(time)s: New comment by %(name)s") % {"time": tf.format(_(settings.TIME_FORMAT)), "name": comment.name}, ) ) except FoiMessage.DoesNotExist: pass # send out update on comments to request users for req_id, request in requests.items(): if not updates[req_id]: continue sorted_events = sorted(updates[req_id], key=lambda x: x[0]) event_string = "\n".join([x[1] for x in sorted_events]) request.send_update(event_string) for event in FoiEvent.objects.filter(timestamp__gte=gte_date).select_related("request"): if event.event_name in ("message_received", "message_sent"): continue if not event.request_id in requests: requests[event.request_id] = event.request updates.setdefault(event.request_id, []) tf = TimeFormat(event.timestamp) updates[event.request_id].append( (event.timestamp, _("%(time)s: %(text)s") % {"time": tf.format(_("TIME_FORMAT")), "text": event.as_text()}) ) # Send out update on comments and event to followers for req_id, request in requests.items(): if not updates[req_id]: continue updates[req_id].sort(key=lambda x: x[0]) event_string = "\n".join([x[1] for x in updates[req_id]]) followers = FoiRequestFollower.objects.filter(request=request) for follower in followers: follower.send_update( _("The following happend in the last 24 hours:\n%(events)s") % {"events": event_string} )
def __str__(self): if self.start.date() == self.end.date(): return '{0} {1}-{2}'.format( DateFormat(self.start).format(settings.DATE_FORMAT), TimeFormat(self.start).format(settings.TIME_FORMAT), TimeFormat(self.end).format(settings.TIME_FORMAT), ) return '{0}-{1}'.format( DateFormat(self.start).format(settings.DATETIME_FORMAT), DateFormat(self.end).format(settings.DATETIME_FORMAT), )
def get_ampm_designators(): am = TimeFormat(datetime.time(11, 00, 00)) pm = TimeFormat(datetime.time(13, 00, 00)) result = {} result['AM'] = {"1": am.format('A'), "2": am.format('a'), "3": am.format('A')} result['PM'] = {"1": pm.format('A'), "2": pm.format('a'), "3": pm.format('A')} return result
def get_human_time(self): slots = self.slots.all() slots_human = [] # TODO: Clean up, inprove english. # Fridays 9pm to 10pm # Mondays & Wednesdays 10:30am to 11am # Weekdays 10:30am to 11am and 10pm to 11pm for slot in slots: time_display = TimeFormat(slot.from_time).format('g:i a') relative = ((slot.day - datetime.now().weekday()) + 7) % 7 relative_word = '' if relative == 0: relative_word = 'Today, and every' elif relative == 1: relative_word = 'Tomorrow, and every' slots_human.append('{} {day}{relative_coma} at {time}'.format( relative_word, day=slot.get_day_display(), relative_coma=',' if relative == 1 or relative == 0 else '', time=time_display)) return ', '.join(slots_human)
def _batch_update(update_requester=True, update_follower=True): event_black_list = ("message_received", "message_sent", 'set_concrete_law',) translation.activate(settings.LANGUAGE_CODE) requests = {} users = {} gte_date = timezone.now() - timedelta(days=1) updates = {} message_type = ContentType.objects.get_for_model(FoiMessage) for comment in Comment.objects.filter(content_type=message_type, submit_date__gte=gte_date): try: message = FoiMessage.objects.get(pk=comment.object_pk) if message.request_id not in requests: requests[message.request_id] = message.request updates.setdefault(message.request_id, []) tf = TimeFormat(comment.submit_date) updates[message.request_id].append( ( comment.submit_date, _("%(time)s: New comment by %(name)s") % { "time": tf.format(_(settings.TIME_FORMAT)), "name": comment.name }, comment.user_id ) ) except FoiMessage.DoesNotExist: pass if update_requester: requester_updates = defaultdict(dict) # send out update on comments to request users for req_id, request in iteritems(requests): if not request.user.is_active: continue if not request.user.email: continue if not updates[req_id]: continue if not any([x for x in updates[req_id] if x[2] != request.user_id]): continue sorted_events = sorted(updates[req_id], key=lambda x: x[0]) requester_updates[request.user][request] = { 'events': [x[1] for x in sorted_events] } for user, request_dict in iteritems(requester_updates): FoiRequest.send_update(request_dict, user=user) if update_follower: # update followers for event in FoiEvent.objects.filter(timestamp__gte=gte_date).select_related("request"): if event.event_name in event_black_list: continue if event.request_id not in requests: requests[event.request_id] = event.request updates.setdefault(event.request_id, []) tf = TimeFormat(event.timestamp) updates[event.request_id].append( ( event.timestamp, _("%(time)s: %(text)s") % { "time": tf.format(_("TIME_FORMAT")), "text": event.as_text() }, event.user_id ) ) # Send out update on comments and event to followers follower_updates = defaultdict(dict) for req_id, request in iteritems(requests): if not updates[req_id]: continue updates[req_id].sort(key=lambda x: x[0]) followers = FoiRequestFollower.objects.filter( request=request).select_related('user') for follower in followers: if follower.user is None and not follower.confirmed: continue if follower.user and ( not follower.user.is_active or not follower.user.email): continue if not request.is_visible(None): continue if not any([x for x in updates[req_id] if x[2] != follower.user_id]): continue users[follower.user_id] = follower.user ident = follower.user_id or follower.email follower_updates[ident][request] = { 'unfollow_link': follower.get_unfollow_link(), 'events': [x[1] for x in updates[req_id]] } for user_id, req_event_dict in iteritems(follower_updates): user = users.get(user_id) email = None if user is None: email = user_id FoiRequestFollower.send_update(req_event_dict, user=user, email=email)
def _batch_update(): event_black_list = ( "message_received", "message_sent", 'set_concrete_law', ) translation.activate(settings.LANGUAGE_CODE) requests = {} gte_date = timezone.now() - timedelta(days=1) updates = {} message_type = ContentType.objects.get_for_model(FoiMessage) for comment in Comment.objects.filter(content_type=message_type, submit_date__gte=gte_date): try: message = FoiMessage.objects.get(pk=comment.object_pk) if not message.request_id in requests: requests[message.request_id] = message.request updates.setdefault(message.request_id, []) tf = TimeFormat(comment.submit_date) updates[message.request_id].append( (comment.submit_date, _("%(time)s: New comment by %(name)s") % { "time": tf.format(_(settings.TIME_FORMAT)), "name": comment.name }, comment.user_id)) except FoiMessage.DoesNotExist: pass # send out update on comments to request users for req_id, request in requests.iteritems(): if not updates[req_id]: continue if not any([x for x in updates[req_id] if x[2] != request.user_id]): continue sorted_events = sorted(updates[req_id], key=lambda x: x[0]) event_string = "\n".join([x[1] for x in sorted_events]) request.send_update(event_string) for event in FoiEvent.objects.filter( timestamp__gte=gte_date).select_related("request"): if event.event_name in event_black_list: continue if not event.request_id in requests: requests[event.request_id] = event.request updates.setdefault(event.request_id, []) tf = TimeFormat(event.timestamp) updates[event.request_id].append( (event.timestamp, _("%(time)s: %(text)s") % { "time": tf.format(_("TIME_FORMAT")), "text": event.as_text() }, event.user_id)) # Send out update on comments and event to followers for req_id, request in requests.items(): if not updates[req_id]: continue updates[req_id].sort(key=lambda x: x[0]) event_string = "\n".join([x[1] for x in updates[req_id]]) followers = FoiRequestFollower.objects.filter(request=request) for follower in followers: if not any( [x for x in updates[req_id] if x[2] != follower.user_id]): continue follower.send_update( _("The following happend in the last 24 hours:\n%(events)s") % {"events": event_string})
def _batch_update(update_requester=True, update_follower=True, since=None): if since is None: since = timezone.now() - timedelta(days=1) event_black_list = ( "message_received", "message_sent", 'set_concrete_law', ) translation.activate(settings.LANGUAGE_CODE) requests = {} users = {} updates = {} message_type = ContentType.objects.get_for_model(FoiMessage) comments = Comment.objects.filter(content_type=message_type, submit_date__gte=since) for comment in comments: try: message = FoiMessage.objects.get(pk=comment.object_pk) if message.request_id not in requests: requests[message.request_id] = message.request updates.setdefault(message.request_id, []) tf = TimeFormat(comment.submit_date) updates[message.request_id].append( (comment.submit_date, _("%(time)s: New comment by %(name)s") % { "time": tf.format(_(settings.TIME_FORMAT)), "name": comment.user_name }, comment.user_id)) except FoiMessage.DoesNotExist: pass if update_requester: requester_updates = defaultdict(dict) # send out update on comments to request users for req_id, request in requests.items(): if not request.user.is_active: continue if not request.user.email: continue if not updates[req_id]: continue if not any([x for x in updates[req_id] if x[2] != request.user_id]): continue sorted_events = sorted(updates[req_id], key=lambda x: x[0]) requester_updates[request.user][request] = { 'events': [x[1] for x in sorted_events] } for user, request_dict in requester_updates.items(): FoiRequest.send_update(request_dict, user=user) if update_follower: # update followers for event in FoiEvent.objects.filter( timestamp__gte=since).select_related("request"): if event.event_name in event_black_list: continue if event.request_id not in requests: requests[event.request_id] = event.request updates.setdefault(event.request_id, []) tf = TimeFormat(event.timestamp) updates[event.request_id].append( (event.timestamp, _("%(time)s: %(text)s") % { "time": tf.format(_("TIME_FORMAT")), "text": event.as_text() }, event.user_id)) # Send out update on comments and event to followers follower_updates = defaultdict(dict) for req_id, request in requests.items(): if not updates[req_id]: continue updates[req_id].sort(key=lambda x: x[0]) followers = FoiRequestFollower.objects.filter( request=request).select_related('user') for follower in followers: if follower.user is None and not follower.confirmed: continue if follower.user and (not follower.user.is_active or not follower.user.email): continue if not request.is_public(): continue if not any( [x for x in updates[req_id] if x[2] != follower.user_id]): continue users[follower.user_id] = follower.user ident = follower.user_id or follower.email follower_updates[ident][request] = { 'unfollow_link': follower.get_unfollow_link(), 'events': [x[1] for x in updates[req_id]] } for user_id, req_event_dict in follower_updates.items(): user = users.get(user_id) email = None if user is None: email = user_id FoiRequestFollower.send_update(req_event_dict, user=user, email=email)