Skip to content

Commit

Permalink
Playing around
Browse files Browse the repository at this point in the history
  • Loading branch information
Carles Boils Gisbert committed Mar 9, 2017
1 parent ef2e87c commit a642170
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 48 deletions.
34 changes: 18 additions & 16 deletions geokey/socialinteractions/models.py
Expand Up @@ -57,7 +57,7 @@ def create(cls, name, description, project, socialaccounts, creator):
Return
------
geokey.socialinteractions.models.SocialInteraction.
"""
if socialaccounts:
socialinteraction = cls(
Expand Down Expand Up @@ -136,24 +136,24 @@ def post_social_media(sender, instance, created, **kwargs):
"""This function post/tweet to social media when a new Observation
is added.
At the same time adds a new comment on the observaction with the link to
redirect
redirect
"""
if created:
project = instance.project
socialinteractions_all = project.socialinteractions.all()
url = 'www.acb.com/admin/projects/{project_id}/contributions/{subset_id}/'
url = 'local/{project_id}/contributions/{subset_id}/'
link = url.format(project_id=project.id,subset_id=instance.id)

for socialinteraction in socialinteractions_all:
text_to_post = socialinteraction.text_to_post
replacements = {
"$project$": project.name,
"$link$":link
"$link$": link
}

for key, replacement in replacements.iteritems():
text_to_post = text_to_post.replace(key, replacement)

print "text to post", text_to_post
for socialaccount in socialinteraction.socialaccounts.all():

provider = socialaccount.provider
Expand All @@ -170,24 +170,24 @@ def post_social_media(sender, instance, created, **kwargs):
text_to_post,
app)
comment_txt = 'https://twitter.com/{user_name}/status/{tweet_id}'.format(
user_name=screen_name,
tweet_id=tweet_id
)
user_name=screen_name,
tweet_id=tweet_id
)
Comment.objects.create(
text=comment_txt,
commentto=instance,
creator=socialaccount.user
)



def check_provider(provider,access_token,text_to_post,app):
"""This function checks the provider.
Parameters:
------------
provider : str
provider of the social account
provider : str
provider of the social account
access_token: str - SocialToken Object
access token for the social account and user
text_to_post: str
Expand All @@ -196,8 +196,8 @@ def check_provider(provider,access_token,text_to_post,app):
returns
--------
tweet_id : str
tweet identifier
tweet_id : str
tweet identifier
screen_aname: str
screen name user by twitter user
Expand All @@ -212,9 +212,11 @@ def check_provider(provider,access_token,text_to_post,app):
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
access_token_all = access_token
access_token = access_token_all.token
access_token_secret = access_token_all.token_secret
access_token_secret = access_token_all.token_secret
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
#print "api", api
tweet_back = api.update_status(text_to_post)
#print "tweet_back", tweet_back

return tweet_back.id, tweet_back.author.screen_name
return tweet_back.id, tweet_back.author.screen_name
37 changes: 20 additions & 17 deletions geokey/socialinteractions/views.py
Expand Up @@ -294,7 +294,7 @@ def post(self, request, project_id, socialinteraction_id):
data = request.POST
context = self.get_context_data(project_id, socialinteraction_id)
socialinteraction = context.get('socialinteraction')

socialinteraction.text_to_post = data.get('text_post')
socialinteraction.save()

Expand Down Expand Up @@ -504,6 +504,7 @@ def post(self, request, project_id):

data = request.POST
text_pull = data.get("text_pull")
print "oOOOO", text_pull
socialaccount_id= data.get('socialaccount_id')
socialaccount = SocialAccount.objects.get(id=socialaccount_id)
provider = socialaccount.provider
Expand All @@ -513,11 +514,11 @@ def post(self, request, project_id):
account__user=socialaccount.user,
account__provider=app.provider
)


all_tweets = pull_from_social_media(provider,access_token,text_pull,app)
context = self.get_context_data(project_id)

context = self.get_context_data(project_id)

context['all_tweets'] = all_tweets

Expand Down Expand Up @@ -599,11 +600,11 @@ def post(self, request, project_id):
account__id = socialaccount.id,
account__user=socialaccount.user,
account__provider=app.provider
)
)

all_tweets = pull_from_social_media_workshop(provider,access_token,text_pull,app)
geometry = all_tweets[0]['geometry']
point = 'POINT(' + str(geometry['coordinates'][0]) + ' ' + str(geometry['coordinates'][1]) +')'
point = 'POINT(' + str(geometry['coordinates'][0]) + ' ' + str(geometry['coordinates'][1]) +')'
new_loc = Location.objects.create(
geometry=point,
creator=socialaccount.user
Expand All @@ -612,7 +613,7 @@ def post(self, request, project_id):

for geo_tweet in all_tweets:
coordinates = geo_tweet['geometry']['coordinates']
point = 'POINT(' + str(coordinates[0]) + ' ' + str(coordinates[1]) +')'
point = 'POINT(' + str(coordinates[0]) + ' ' + str(coordinates[1]) +')'
new_loc = Location.objects.create(
geometry=point,
creator=socialaccount.user
Expand Down Expand Up @@ -642,10 +643,10 @@ def pull_from_social_media_workshop(provider,access_token,text_to_pull,app):
"""
Pull data from the timeline when social account has been mentioned and
with specific text or hastag.
Parameters
-----------
provider = str
provider = str
provider of the social account
access_token: str - SocialToken Object
Expand All @@ -656,7 +657,7 @@ def pull_from_social_media_workshop(provider,access_token,text_to_pull,app):
Returns
--------
all_tweets: array
all_tweets: array
array of tweet objects
"""
Expand All @@ -667,7 +668,7 @@ def pull_from_social_media_workshop(provider,access_token,text_to_pull,app):
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
access_token_all = access_token
access_token = access_token_all.token
access_token_secret = access_token_all.token_secret
access_token_secret = access_token_all.token_secret
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)

Expand All @@ -689,6 +690,7 @@ def pull_from_social_media_workshop(provider,access_token,text_to_pull,app):
new_contribution['geometry'] = mention.coordinates
if 'media' in mention.entities: ## gets when is media attached to it
for image in mention.entities['media']:
print "yes"
new_contribution['url'] = image['url']

all_tweets.append(new_contribution)
Expand All @@ -706,13 +708,14 @@ def pull_from_social_media(provider,access_token,text_to_pull,app):
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
access_token_all = access_token
access_token = access_token_all.token
access_token_secret = access_token_all.token_secret
access_token_secret = access_token_all.token_secret
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)

try:
# tweets_all = api.mentions_timeline(count=100)
tweets_all = api.search(q=text_to_pull)
print "ofoao0", text_to_pull
tweets_all = api.search(q=text_to_pull, count=100)
except:
print "You are not autheticate"

Expand All @@ -724,7 +727,7 @@ def pull_from_social_media(provider,access_token,text_to_pull,app):
new_contribution['user'] = mention.user.name
new_contribution['created_at'] = mention.created_at
if mention.coordinates: ## checks if there are coorindates
if text_to_pull in mention.text:
if text_to_pull in mention.text:
#new_contribution['text'] = mention.text
geotype = mention.coordinates['type']
lon = mention.coordinates['coordinates'][1]
Expand Down Expand Up @@ -763,4 +766,4 @@ def pull_from_social_media(provider,access_token,text_to_pull,app):
# if text_to_pull in feed['message']:
# print "Yeeepa", feed['created_time']

return all_tweets
return all_tweets
Expand Up @@ -50,7 +50,7 @@ <h3 class="header">
{%if socialaccount == '' %}
<p>Unfortunately, you cannot create new social interactions because any social media has been logged on for this project.</p>
{% else %}
<input type="checkbox" name="socialaccounts" value="{{ socialaccount.id }}"> <img class="socialaccount_provider" src="{% static 'img/providers/' %}{{ socialaccount.provider }}.svg" alt="{{ socialaccount.get_provider.name }} social provider" /> {{ socialaccount.get_provider_account }}
<input type="checkbox" name="socialaccounts" value="{{ socialaccount.id }}"> <img class="#socialaccount_provider" src="{% static 'img/providers/' %}{{ socialaccount.provider }}.svg" alt="{{ socialaccount.get_provider.name }} social provider" /> {{ socialaccount.get_provider_account }}
{% endif %}
{% endfor %}
<ul>
Expand All @@ -67,4 +67,12 @@ <h3 class="header">

{% block libraries %}
<script src="/static/js/admin.ui.forms.validate.js"></script>
<style type="text/css">
.socialaccount_provider
{
height:15px;
width:15px;

}
</style>
{% endblock %}
20 changes: 6 additions & 14 deletions geokey/templates/socialinteractions/socialinteraction_pull.html
Expand Up @@ -39,27 +39,19 @@ <h3 class="header">
<li>
<h4>
{% if project.islocked %}<span class="glyphicon glyphicon-lock" aria-hidden="true"></span>{% endif %}

<img class="socialaccount_provider" src="{% static 'img/providers/' %}{{ sa.provider }}.svg" alt="{{ sa.get_provider.name }} social provider" /> {{sa.get_provider_account}}
</h4>
<form id="settings" method="POST" action="{% url 'admin:socialinteraction_pull' project.id %}" novalidate>
{% csrf_token %}
{% csrf_token %}
<input hidden name="socialaccount_id" value="{{sa.id}}">
<div class="form-group">
<label for="pull_text" class="control-label">Text to search</label>
<label for="text_pull" class="control-label">Text to search</label>
<input type="text" class="form-control input-lg" id="text_pull" name="text_pull" value="#Project2" placeholder="e.g: #Project2" required />
</div>
<div class="form-group">
<label for="coordinates" class="control-label">Coordinates</label>
<input type="text" class="form-control input-lg" id="text_pull" name="text_pull" value="50.56 2.333" placeholder="e.g: 50.56 2.333" required />
</div>
<div class="list-group-item">
<select class="form-control" name="category">
<option value="-1">Select category</option>
{% for category in project.categories.all %}
<option value="category.id">{{category.name}}</option>
{% endfor %}
</select>
<input type="text" class="form-control input-lg" id="coordinates" name="coordinates" value="50.56 2.333" placeholder="e.g: 50.56 2.333" required />
</div>
<div class="form-group">
<label for="hola" class="col-sm-3 control-label">Get data from (date)</label>
Expand All @@ -86,7 +78,7 @@ <h4>
{% if forloop.first %}
<ul class="list-unstyled overview-list">
{% endif %}
<li> {{t.text}} create by {{t.user_name}} at {{t.created_at}} {% if t.url %} <a href={{ t.url}}>URL </a>{% endif %} {% if t.geometry %} <button type="submit" class="btn btn-success btn-sm">Create Contribution</button> {% endif %}</li>
<li> {{t.text}} create by {{t.user}} at {{t.created_at}} {% if t.url %} <a href={{ t.url}}>URL </a>{% endif %} {% if t.geometry %} <button type="submit" class="btn btn-success btn-sm">Create Contribution</button> {% endif %}</li>
{% if forloop.last %}
</ul>
{% endif %}
Expand Down Expand Up @@ -122,7 +114,7 @@ <h4>
<h3 class="header">Social interaction post to social mediasdjsdjksdjksgnk sdgkl sdgkldsg</h3>
<h6 class="header">Social accounts associated: {% for sa in socialinteraction.socialaccounts.all %} {% load static %} <img id="socialaccount_provider" src="{% static 'img/providers/' %}{{ sa.provider }}.svg" alt="{{ sa.get_provider.name }} social provider" /> {%if sa.provider == 'twitter' %}@{% endif%}{{sa.get_provider_account}}{% endfor %} </h6>
<form id="settings" method="POST" action="{% url 'admin:socialinteraction_settings' project.id socialinteraction.id %}" novalidate>
{% csrf_token %}
{% csrf_token %}
<div class="form-group">
<label for="post_text" class="control-label">Text to post</label>
<textarea class="form-control input-lg" rows="5" id="post_text" name="post_text"></textarea>
Expand Down

0 comments on commit a642170

Please sign in to comment.