Guest User

Untitled

a guest
Mar 30th, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.49 KB | None | 0 0
  1. # JS
  2.  
  3. import Axios from 'axios';
  4.  
  5. // get api url of the repos, call with Axios
  6. // and add description and stars count updated
  7.  
  8. // links = all span elements with api url inside
  9. const links = document.querySelectorAll('.repoApiUrl');
  10. links.forEach(
  11. (elementNode) => {
  12. // link = text content/api url to call
  13. const link = elementNode.textContent;
  14. // starsEl = span element with star icon to add stars count
  15. const starsEl = elementNode.previousElementSibling;
  16. // descEl = element to add description
  17. const descEl = elementNode.parentElement.parentElement.lastElementChild;
  18.  
  19. Axios.get(link).then(
  20. (res) => {
  21. // create text not with stars and description
  22. // and add in the element node
  23. const data = res.data;
  24. const stars = document.createTextNode(data.stargazers_count);
  25. const desc = document.createTextNode(data.description);
  26. starsEl.appendChild(stars);
  27. descEl.appendChild(desc);
  28. }
  29. )
  30. }
  31. )
  32.  
  33. // listenner to send form on click in icon
  34. // to delete repository
  35. const deleteBtns = document.querySelectorAll('.cardrepos__del-btn');
  36. deleteBtns.forEach(
  37. (deleteBtn) => {
  38. deleteBtn.addEventListener('click',
  39. () => {
  40. const parentForm = deleteBtn.parentElement;
  41. parentForm.submit();
  42. }
  43. )
  44. }
  45. )
  46.  
  47. // Toggle Menu
  48. const menuMobile = document.querySelector('.navbar__menu');
  49. const toggleBtn = document.querySelector('.navbar__toggle');
  50.  
  51. function toggleMenu() {
  52.  
  53. if (toggleBtn.innerText == 'menu') {
  54. toggleBtn.innerText = 'close';
  55. menuMobile.classList.add('open');
  56. } else {
  57. toggleBtn.innerText = 'menu';
  58. menuMobile.classList.remove('open');
  59. }
  60. }
  61.  
  62. toggleBtn.addEventListener('click',
  63. toggleMenu
  64. )
  65.  
  66. # view example
  67.  
  68. class RepoCreateView(generic.CreateView):
  69. template_name = "main/repo_add.html"
  70. success_url = reverse_lazy("main:repo_add")
  71. model = Repo
  72. fields = ("url",)
  73.  
  74. def get_context_data(self, **kwargs):
  75. kwargs["repos"] = Repo.objects.filter(user=self.request.user).order_by("name")
  76. return super().get_context_data(**kwargs)
  77.  
  78. def form_valid(self, form):
  79. form.instance.user = self.request.user
  80. data = get_github_data(form.instance.url)
  81. form.instance.name = data["name"]
  82. form.instance.owner = data["owner"]["login"]
  83. form.instance.url_api = data["url"]
  84.  
  85. return super().form_valid(form)
  86.  
  87. # utils example
  88.  
  89. import requests, json, re
  90.  
  91. def get_github_data(url):
  92. # convert github link to
  93. # github api link and transform
  94. # json response in python dict
  95. url = re.sub("//", "//api.", url)
  96. url = re.sub("com/", "com/repos/", url)
  97. if url.endswith("/"):
  98. url = url[:-1]
  99. res = requests.get(url)
  100. obj = json.loads(res.content)
  101.  
  102. return obj
  103.  
  104. # model example
  105. User = get_user_model()
  106.  
  107.  
  108. class Repo(models.Model):
  109. user = models.ForeignKey(User, on_delete=models.CASCADE, related_name="repos")
  110. url = models.URLField(max_length=600)
  111. url_api = models.URLField(max_length=600)
  112. slug = AutoSlugField(populate_from=["user__username","name", "owner"], overwrite=True)
  113. name = models.CharField(max_length=200)
  114. owner = models.CharField(max_length=200)
  115.  
  116. def __str__(self):
  117. return f"{self.name} - {self.owner}"
Advertisement
Add Comment
Please, Sign In to add comment