def createCivi(request): ''' USAGE: use this function to insert a new connected civi into the database. Text POST: group creator topic category title body type reference (optional) at (optional) and_negative (optional) and_positive (optional) :return: (200, ok) (400, missing required parameter) (500, internal error) ''' civi = Civi() data = { 'group_id': request.POST.get('group', ''), 'creator_id': request.POST.get('creator', ''), 'topic_id': request.POST.get('topic', ''), 'title': request.POST.get('title', ''), 'body': request.POST.get('body', ''), 'type': request.POST.get('type', ''), 'visits': 0, 'votes_neutral': 0, 'votes_positive1': 0, 'votes_positive2': 0, 'votes_negative1': 0, 'votes_negative2': 0, 'reference_id': request.POST.get('reference', ''), 'at_id': request.POST.get('at', ''), 'and_negative_id': request.POST.get('and_negative', ''), 'and_positive_id': request.POST.get('and_positive', ''), } try: civi = Civi(**data) hashtags = request.POST.get('hashtags', '') split = [x.strip() for x in hashtags.split(',')] for str in split: if not Hashtag.objects.filter(title=str).exists(): hash = Hashtag(title=str) hash.save() else: hash = Hashtag.objects.get(title=str) civi.hashtags.add(hash.id) civi.save() return HttpResponse() except Exception as e: return HttpResponseServerError(reason=str(e))
def createCivi(request): ''' USAGE: use this function to insert a new connected civi into the database. :return: (200, ok) (400, missing required parameter) (500, internal error) ''' a = Account.objects.get(user=request.user) thread_id = request.POST.get('thread_id') data = { 'author': Account.objects.get(user=request.user), 'title': request.POST.get('title', ''), 'body': request.POST.get('body', ''), 'c_type': request.POST.get('c_type', ''), 'thread': Thread.objects.get(id=thread_id) } try: civi = Civi(**data) civi.save() # hashtags = request.POST.get('hashtags', '') # split = [x.strip() for x in hashtags.split(',')] # for str in split: # if not Hashtag.objects.filter(title=str).exists(): # hash = Hashtag(title=str) # hash.save() # else: # hash = Hashtag.objects.get(title=str) # # civi.hashtags.add(hash.id) links = request.POST.getlist('links[]', '') if links: for civi_id in links: linked_civi = Civi.objects.get(id=civi_id) civi.linked_civis.add(linked_civi) # If response related_civi = request.POST.get('related_civi', '') if related_civi: # parent_civi = Civi.objects.get(id=related_civi) # parent_civi.links.add(civi) parent_civi = Civi.objects.get(id=related_civi) parent_civi.responses.add(civi) if parent_civi.author.user.username != request.user.username: notify.send( request.user, # Actor User recipient=parent_civi.author.user, # Target User verb=u'responded to your civi', # Verb action_object=civi, # Action Object target=civi.thread, # Target Object popup_string="{user} responded to your civi in {thread}".format(user=a.full_name, thread=civi.thread.title), link="/{}/{}".format("thread", thread_id) ) else: #not a reply, a regular civi c_qs = Civi.objects.filter(thread_id=thread_id) accounts = Account.objects.filter(pk__in=c_qs.distinct('author').values_list('author', flat=True)) data = { "command": "add", "data": json.dumps(civi.dict_with_score(a.id)), } # channels_Group("thread-%s" % thread_id).send({ # "text": json.dumps(data), # }) for act in accounts: if act.user.username != request.user.username: notify.send( request.user, # Actor User recipient=act.user, # Target User verb=u'created a new civi', # Verb action_object=civi, # Action Object target=civi.thread, # Target Object popup_string="{user} created a new civi in the thread {thread}".format(user=a.full_name, thread=civi.thread.title), link="/{}/{}".format("thread", thread_id) ) return JsonResponse({'data' : civi.dict_with_score(a.id)}) except Exception as e: return HttpResponseServerError(reason=str(e))
def addCivi(request): ''' takes in civi info and adds it to the database :param request: :return: ''' civi = Civi() civi.id = Civi.objects.all().order_by("-id")[0].id + 1 civi.author_id = request.POST.get('author_id', '') civi.article_id = request.POST.get('article_id', '') civi.title = request.POST.get('title', '') civi.body = request.POST.get('body', '') civi.type = request.POST.get('type', '') civi.REFERENCE_id = request.POST.get('reference_id', '') civi.AT_id = request.POST.get('at_id', '') civi.AND_NEGATIVE_id = request.POST.get('and_negative_id', '') civi.AND_POSITIVE_id = request.POST.get('and_positive_id', '') civi.save() hashtags = request.POST.get('hashtags', '') split = [x.strip() for x in hashtags.split(',')] for str in split: if len(Hashtag.objects.filter(title=str)) == 0: hash = Hashtag(title=str) hash.save() else: hash = Hashtag.objects.filter(title=str)[0]#get the first element civi.hashtags.add(hash.id) civi.save() return JsonResponse({'result': 'success'})