Skip to content

Commit cd7e05c

Browse files
committed
Updated command for adding sniper data and wzrd URL
1 parent ac6a5a8 commit cd7e05c

File tree

7 files changed

+128
-14
lines changed

7 files changed

+128
-14
lines changed

main/admin.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,6 @@
66
admin.site.register(Contributor)
77
admin.site.register(Contribution)
88
admin.site.register(CSSDependency)
9-
admin.site.register(JSDependency)
9+
admin.site.register(JSDependency)
10+
admin.site.register(Snippet)
11+
admin.site.register(SniperData)

main/management/commands/updatecomponents.py

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,17 @@
1717
'''
1818

1919
def get_commit_hash(commits_url):
20-
# import ast
21-
2220
commits_url = commits_url.split('{')[0] + '?client_id=' + GITHUB_CLIENT_ID + '&client_secret=' + GITHUB_CLIENT_SECRET
23-
print commits_url
21+
print (commits_url)
2422
response = urllib.urlopen(commits_url)
2523
data = json.loads(response.read())[0]
2624
return data['sha']
2725

26+
### store the dependency urls and snippet urls
2827
def update_visualizations(component, commit_hash):
2928
github_url_list = component.github_url.split('?')[0].split('/')
3029
owner = github_url_list[4]
3130
repo_name = github_url_list[5]
32-
# print "https://cdn.rawgit.com/" + str(owner) + '/' + str(repo_name) + "/" + commit_hash + "/package.json"
3331
try:
3432
sniper_data = json.load(urllib.urlopen("https://cdn.rawgit.com/" + str(owner) +
3533
'/' + str(repo_name) + "/" + commit_hash + "/package.json"))["sniper"]
@@ -78,14 +76,42 @@ def update_visualizations(component, commit_hash):
7876
try:
7977
no_browserify = sniper_data['noBrowserify']
8078
sniperData.no_browserify = no_browserify
79+
except:
80+
pass
81+
try:
8182
if no_browserify:
8283
sniperData.wzrd_url = '#'
8384
else:
84-
sniperData.wzrd_url = "https://wzrd.in/bundle/" + component.name
85+
sniperData.wzrd_url = "https://wzrd.in/bundle/" + component.name
86+
except:
87+
sniperData.wzrd_url = "https://wzrd.in/bundle/" + component.name
88+
try:
89+
snippets_dir_name = sniper_data['snippets'][0]
90+
sniperData.snippets_dir_name = snippets_dir_name
8591
except:
8692
pass
8793
sniperData.save()
8894

95+
### For Snippets URLs
96+
try:
97+
print ("https://api.github.com/repos/" + str(owner) + "/" + str(repo_name) + "/contents/" + sniperData.snippets_dir_name + "?ref=master&client_id="
98+
+ GITHUB_CLIENT_ID + "&client_secret=" + GITHUB_CLIENT_SECRET)
99+
snippets_data = urllib.urlopen("https://api.github.com/repos/" + str(owner) + "/" + str(repo_name) + "/contents/" + sniperData.snippets_dir_name + "?ref=master&client_id="
100+
+ GITHUB_CLIENT_ID + "&client_secret=" + GITHUB_CLIENT_SECRET)
101+
snippets = json.loads(snippets_data.read())
102+
for snippet in snippets:
103+
if not snippet['name'].endswith('.js'):
104+
continue
105+
url = "https://cdn.rawgit.com/" + str(owner) + '/' + str(repo_name) + "/" + commit_hash + "/" + sniperData.snippets_dir_name + "/" + snippet['name']
106+
try:
107+
req_snippet = Snippet.objects.get(name=snippet['name'], sniperData=sniperData)
108+
req_snippet.url = url
109+
req_snippet.save()
110+
except:
111+
Snippet.objects.create(name=snippet['name'], sniperData=sniperData, url=url)
112+
except:
113+
pass
114+
89115
def get_github_data(github_url):
90116
response = urllib.urlopen(github_url)
91117
data = json.load(response)
@@ -106,8 +132,7 @@ def get_contributors_data(contributors_url):
106132
return data
107133

108134
def get_downloads(downloads_url):
109-
# import ast
110-
print downloads_url
135+
print (downloads_url)
111136
response = urllib.urlopen(downloads_url)
112137
downloads = 0
113138
data = ast.literal_eval(response.read())
@@ -126,10 +151,10 @@ def handle(self, *args, **options):
126151
component_data = component['package']
127152
try:
128153
_component = Component.objects.get(name=component_data['name'])
129-
print 'exists'
154+
print ('exists')
130155
except:
131156
_component = Component.objects.create(name=component_data['name'])
132-
print _component.name
157+
print (_component.name)
133158
try:
134159
_component.version = component_data['version']
135160
except:
@@ -182,7 +207,7 @@ def handle(self, *args, **options):
182207
_component.save()
183208

184209
if _component.github_url:
185-
print _component.github_url
210+
print (_component.github_url)
186211
try:
187212
github_data = get_github_data(_component.github_url)
188213
except:
@@ -222,7 +247,7 @@ def handle(self, *args, **options):
222247
# except:
223248
# pass
224249
_component.save()
225-
print str(github_data['contributors_url']) + '?client_id=' + GITHUB_CLIENT_ID + '&client_secret=' + GITHUB_CLIENT_SECRET
250+
print (str(github_data['contributors_url']) + '?client_id=' + GITHUB_CLIENT_ID + '&client_secret=' + GITHUB_CLIENT_SECRET)
226251
try:
227252
contributors_data = get_contributors_data(str(github_data['contributors_url']) + '?client_id=' + GITHUB_CLIENT_ID + '&client_secret=' + GITHUB_CLIENT_SECRET)
228253
except:

main/migrations/0014_snippet.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# -*- coding: utf-8 -*-
2+
# Generated by Django 1.11.13 on 2018-06-26 06:32
3+
from __future__ import unicode_literals
4+
5+
from django.db import migrations, models
6+
import django.db.models.deletion
7+
8+
9+
class Migration(migrations.Migration):
10+
11+
dependencies = [
12+
('main', '0013_sniperdata'),
13+
]
14+
15+
operations = [
16+
migrations.CreateModel(
17+
name='Snippet',
18+
fields=[
19+
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
20+
('url', models.URLField()),
21+
('name', models.CharField(max_length=50)),
22+
('sniperData', models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, to='main.SniperData')),
23+
],
24+
),
25+
]
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# -*- coding: utf-8 -*-
2+
# Generated by Django 1.11.13 on 2018-06-26 06:54
3+
from __future__ import unicode_literals
4+
5+
from django.db import migrations, models
6+
7+
8+
class Migration(migrations.Migration):
9+
10+
dependencies = [
11+
('main', '0014_snippet'),
12+
]
13+
14+
operations = [
15+
migrations.AddField(
16+
model_name='sniperdata',
17+
name='snippets_dir_name',
18+
field=models.CharField(default='snippets', max_length=50),
19+
),
20+
]

main/models.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,15 @@ class SniperData(models.Model):
9797
no_browserify = models.BooleanField(default=False)
9898
wzrd_url = models.URLField(null=True)
9999
component = models.OneToOneField(Component, on_delete=models.SET_NULL, null=True)
100+
snippets_dir_name = models.CharField(max_length=50, default='snippets')
101+
102+
def __unicode__(self):
103+
return str(self.component)
104+
105+
class Snippet(models.Model):
106+
url = models.URLField()
107+
name = models.CharField(max_length=50)
108+
sniperData = models.ForeignKey(SniperData, null=True, on_delete=models.SET_NULL)
100109

101110
def __unicode__(self):
102-
return str(self.component)
111+
return str(self.sniperData.component)

main/serializers.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,4 +103,28 @@ class ContributionSerializer(serializers.ModelSerializer):
103103

104104
class Meta:
105105
model = Contribution
106-
fields = ('contributor', 'contributions', 'id')
106+
fields = ('contributor', 'contributions', 'id')
107+
108+
class JSDependencySerializer(serializers.ModelSerializer):
109+
110+
class Meta:
111+
model = JSDependency
112+
fields = ('js_url',)
113+
114+
class CSSDependencySerializer(serializers.ModelSerializer):
115+
116+
class Meta:
117+
model = CSSDependency
118+
fields = ('css_url',)
119+
120+
class SniperDataSerializer(serializers.ModelSerializer):
121+
122+
class Meta:
123+
model = SniperData
124+
fields = ('wzrd_url',)
125+
126+
class SnippetSerializer(serializers.ModelSerializer):
127+
128+
class Meta:
129+
model = Snippet
130+
fields = ('url', 'name')

main/views.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,18 @@ def component_details(request, url_name):
3737
component = Component.objects.get(url_name=url_name)
3838
details = DetailComponentSerializer(component, context={'request':request})
3939
contributions = ContributionSerializer(component.contributions, many=True)
40+
js_dependencies = JSDependencySerializer(component.jsdependency_set.all(), many=True)
41+
css_dependencies = CSSDependencySerializer(component.cssdependency_set.all(), many=True)
42+
sniper_data = component.sniperdata
43+
sniper = SniperDataSerializer(sniper_data)
44+
snippets = SnippetSerializer(sniper_data.snippet_set.all(), many=True)
4045
return JsonResponse({
4146
'details' : details.data,
4247
'contributors' : contributions.data,
48+
'js_dependencies' : js_dependencies.data,
49+
'css_dependencies' : css_dependencies.data,
50+
'sniper_data' : sniper.data,
51+
'snippets' : snippets.data,
4352
})
4453

4554
@staff_member_required

0 commit comments

Comments
 (0)