Esempio n. 1
0
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))
Esempio n. 2
0
    def _store_hashtags(self, model_instance, hashtags):

        for hashtag in hashtags:
            h = Hashtag()
            if model_instance.__class__.__name__ == "Reply":
                h.reply = model_instance
            elif model_instance.__class__.__name__ == "TrumpStatus":
                h.trump_status = model_instance
            else:
                h.retweet = model_instance

            h.hashtag = hashtag['text']
            h.save()
Esempio n. 3
0
def agregar_hashtag(request):
  if request.method == 'POST':
    if request.POST.get('hashtag',False):
      h = True
    else:
      h = False
    if request.POST.get('dias',False):
      try:
        dias = int(request.POST.get('dias'))
      except:
        dias = 7
    else:
      return redirect('/')
    a = Hashtag(nombre=request.POST['cuenta'], hashtag = h)
    a.save()
    a.hasta = a.creado + timedelta(days=dias)
    a.save()
  return redirect('/')
Esempio n. 4
0
def extract_hash_tags(tmp_tweet):
	set_of_hastags = set(part[1:] for part in tmp_tweet.text.split() if part.startswith('#'))
	for hashtag in set_of_hastags:
		# check if hashtag already exists
		hashtag_object = None
		relate_hastag_to_tweet = None
		try:
			# look if there's already the hastag in the db
			hashtag_object = Hashtag.objects.get(name=hashtag)
		except:
			# if there isn't create a new hastag object in the db
			hashtag_object = Hashtag(name=hashtag, type=2, mentioned=0)
			hashtag_object.save()

		# create a new relationship between the hashtag and the tweet
		relate_hastag_to_tweet = TweetRelatedHashtags(tweet=tmp_tweet, hashtag=hashtag_object)
		relate_hastag_to_tweet.save()
		# update number of mentions for this hashtag
		update_number_of_mentions(hashtag_object)
Esempio n. 5
0
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'})
Esempio n. 6
0
File: views.py Progetto: feakuru/web
def save_question_from_form(qForm, user):
	if qForm.is_valid():
	    title = qForm.cleaned_data['title']
	    text = qForm.cleaned_data['text']
	    date = datetime.now()
	    author = user
	    q = Question(title=title, text=text,
	    	timeStamp=date
	    	#author=Profile.objects.get(user=author.id).id
	    )
	    q.save()
	    for tag in qForm.cleaned_data['tags']:
	    	try:
	    		htag = Hashtag.objects.get(tag=tag)
	    		q.hashtags.add(htag)
	    	except Hashtag.DoesNotExist:
	    		htag = Hashtag(tag=tag)
	    		htag.save()
	    		q.hashtags.add(htag)

	    q.save()
	    return True
	else:
		return False