def add_spud_comment(request): """ Adds a comment to a SPUD (KrowdIO post) that tags Spudder entities :param request: a POST request :return: response from KrowdIO or HttpResponseNotAllowed """ if request.method == 'POST': entity = KrowdIOStorage.GetOrCreateForCurrentUserRole( user_role=request.current_role) spud_id = request.POST.get('spud_id') tags = request.POST.getlist('tags[]') text = "" fan = request.current_role.entity for t in tags: tag = FanFollowingEntityTag.objects.get(fan=fan, tag=t) text += "@%s%s " % (tag.entity_type, tag.entity_id) if tag.entity_type == 'Team': team = TeamPage.objects.get(id=tag.entity_id) admin = TeamAdministrator.objects.get(team_page=team) if admin.entity_type == 'student': stu = Student.objects.get(id=admin.entity_id) team_tagged_in_spud(stu) json = post_comment(entity, spud_id, text) return HttpResponse(simplejson.dumps(json)) else: return HttpResponseNotAllowed(['POST'])
def add_spud_from_fan(self, spud): spud_text = ' '.join([s.encode('ascii', 'ignore') for s in spud.expanded_data['text']]) tagged_entities = [] tags = FanFollowingEntityTag.objects.filter(fan=self.role.entity) logging.debug("SpudsController add_spud_from_fan: Found %s tags for fan: %s" % ( len(tags or []), self.role.entity.id)) for tag in tags: if tag.tag and tag.tag in spud_text: mention = "@%s%s" % (tag.entity_type, tag.entity_id) logging.debug("SpudsController add_spud_from_fan: Adding mention: %s" % mention) tagged_entities.append(mention) if tag.entity_type == 'Team': try: team = TeamPage.objects.get(id=tag.entity_id) except TeamPage.DoesNotExist: logging.error("SpudsController add_spud_from_fan: No Team with id: %s" % tag.entity_id) continue # Check if there are Student points that need awarding try: admin = TeamAdministrator.objects.get(team_page=team) if admin.entity_type == 'student': stu = Student.objects.get(id=admin.entity_id) team_tagged_in_spud(stu) except Exception as e: logging.error("%s" % e) # Also mention any associated venues for association in TeamVenueAssociation.objects.filter(team_page=team): mention = "@%s%s" % (EntityController.ENTITY_VENUE, association.venue.id) logging.debug( "SpudsController add_spud_from_fan: Adding associated venue mention: %s" % mention) tagged_entities.append(mention) user_text = '@%s%s %s' % (RoleController.ENTITY_FAN, self.role.entity.id, ' '.join(tagged_entities)) logging.debug("SpudsController add_spud_from_fan: Adding spud with user_text: %s" % user_text) data = { 'type': 'image', 'url': spud.expanded_data['image']['standard_resolution']['url'], 'title': spud_text, 'usertext': user_text, 'extra': spud.data} krowd_io_storage = KrowdIOStorage.GetOrCreateForCurrentUserRole(self.role) post_spud(krowd_io_storage, data) spud.state = SpudFromSocialMedia.STATE_ACCEPTED spud.save()