def end_challenge(self, latest): send_email(latest, CHALLENGE_END, theme=latest.theme) print 'PYWEEK BOT: SENT CHALLENGE END EMAIL' log_event( type="challenge-end", target=latest, challenge_number=latest.number, challenge_title=latest.title, theme=latest.theme, ) print 'PYWEEK BOT: Posted to activity log'
def challenge_countdown(self, latest, current_date): remaining = (latest.end_utc().date() - current_date).days log_event( type="challenge-day", target=latest, days_remaining=remaining, challenge_number=latest.number, challenge_title=latest.title, theme=latest.theme, ) print 'PYWEEK BOT: Posted to activity log'
def start_challenge(self, latest): theme = latest.setTheme() # this also closes the poll send_email(latest, CHALLENGE_START, theme=theme) print 'PYWEEK BOT: SENT CHALLENGE START EMAIL' log_event( type="challenge-start", target=latest, challenge_number=latest.number, challenge_title=latest.title, theme=theme, poll_id=latest.theme_poll.id, ) print 'PYWEEK BOT: Posted to activity log'
def message_add(request): is_anon = request.user.is_anonymous if is_anon: return HttpResponseRedirect('/login/') is_super = not is_anon and request.user.is_superuser previewed = False content = title = '' if is_super: cls = StickyDiaryForm else: cls = DiaryForm if request.POST: form = cls(request.POST) if form.is_valid(): content = form.cleaned_data['content'] previewed = True # do the save diary = models.DiaryEntry() diary.content = content diary.user = request.user diary.actor = request.user diary.title = form.cleaned_data['title'] if is_super: diary.sticky = form.cleaned_data.get('sticky', False) diary.created = datetime.datetime.utcnow() diary.save() desc, truncated = summarise(diary.content) log_event( type='new-thread', user=request.user, target=diary, title=diary.title, id=diary.id, description=desc, description_truncated=truncated, ) messages.success(request, 'Entry saved!') return HttpResponseRedirect(f'/d/{diary.id}/') else: form = cls() return render(request, 'message_add.html', { 'previewed': previewed, 'content': content, 'title': title, 'form': cls, })
def entry_add(request, challenge_id): challenge = get_object_or_404(models.Challenge, pk=challenge_id) if not challenge.isRegoOpen(): return HttpResponseRedirect(f"/{challenge_id}/") if challenge.isCompFinished(): messages.error(request, 'Entry registration closed') return HttpResponseRedirect(f"/{challenge_id}/") if request.method == 'POST': f = AddEntryForm(request.POST) f.current_user = request.user.username if f.is_valid(): entry = f.instance entry.challenge = challenge entry.user = request.user members = f.cleaned_data['users'] entry.save() entry.users.set(models.User.objects.filter(username__in=members)) short_description, truncated = summarise(entry.description) log_event( type='new-entry', challenge=entry.challenge.number, team=entry.title, members=list(members), name=entry.name, description=short_description, description_truncated=truncated, ) messages.success(request, 'Entry created!') return HttpResponseRedirect(f"/e/{entry.name}/") else: f = AddEntryForm(initial={'users': request.user.username}) return render(request, 'challenge/entry_add.html', { 'challenge': challenge, 'form': f, 'is_member': True, 'is_owner': True, } )
def query_github(self, entry): repo = entry.github_repo url = f'https://api.github.com/repos/{repo}/commits' headers = {} if self.github_token: headers['Authorization'] = f'token {self.github_token}' resp = requests.get(url, headers=headers) if resp.status_code != 200: self.stdout.write( f"Error {resp.status_code} polling {url}" ) return resp_json = resp.json() if not resp_json: return users = set() commits = [] for commit in resp.json(): sha = commit['sha'] if sha == entry.head_sha: break msg = commit['commit']['message'].splitlines()[0] users.add(commit['committer']['login']) commits.append({ 'msg': msg, 'sha': sha, }) if not commits: return users = User.objects.filter(userprofile__github_username__in=users) entry.head_sha = commits[0]['sha'] log_event( type="github-push", repo=entry.github_repo, users=[u.username for u in users], commits=commits, game=entry.display_title, name=entry.name, ) entry.save()
def begin_theme_voting(self, latest): latest.theme_poll.is_open = True latest.theme_poll.is_hidden = False latest.theme_poll.save() options = list(latest.theme_poll.option_set.all()) themes = '\n'.join(['- %s' % o.text for o in options]) url = 'http://pyweek.org/p/%s/' % latest.theme_poll.id print 'PYWEEK BOT: SENDING VOTING EMAIL' send_email(latest, VOTING_START, themes=themes, poll_url=url) print 'PYWEEK BOT: SENT VOTING EMAIL' log_event( type="theme-voting", target=latest.theme_poll, challenge_number=latest.number, challenge_title=latest.title, options=[o.text for o in options], ) print 'PYWEEK BOT: Posted to activity log'
def _give_award(challenge, user, entry, award): # Don't do anything if award has already been given. if models.EntryAward.objects.filter(award=award, entry=entry).count(): return False entryaward = models.EntryAward(challenge=challenge, creator=user, entry=entry, award=award) entryaward.save() log_event( 'gave-award', user=user, target=entryaward, entry_id=entry.name, entry_name=entry.display_title, award_name=award.description, award_url=award.content.name, ) return True
def entry_diary(request, entry_id): is_anon = request.user.is_anonymous if is_anon: return HttpResponseRedirect('/login/') entry = get_object_or_404(models.Entry, pk=entry_id) is_member = request.user in entry.users.all() if not is_member: messages.error(request, "You're not allowed to add diary entries!") return HttpResponseRedirect(f'/e/{entry_id}/') challenge = entry.challenge is_super = not is_anon and request.user.is_superuser previewed = False content = title = '' if is_super: cls = StickyDiaryForm else: cls = DiaryForm if request.POST: form = cls(request.POST) if form.is_valid(): content = form.cleaned_data['content'] previewed = True # do the save diary = models.DiaryEntry() diary.entry = entry diary.challenge = challenge diary.content = content diary.user = request.user diary.actor = request.user diary.title = form.cleaned_data['title'] diary.created = datetime.datetime.utcnow() if is_super: diary.sticky = form.cleaned_data.get('sticky', False) diary.save() desc, truncated = summarise(diary.content) log_event( type='new-diary', user=request.user, target=diary, title=diary.title, entry={ 'title': diary.entry.display_title, 'name': diary.entry.name, }, description=desc, description_truncated=truncated, ) messages.success(request, 'Entry saved!') return HttpResponseRedirect(f'/d/{diary.id}/') else: form = cls() return render( request, 'challenge/add_diary.html', { 'previewed': previewed, 'content': content, 'title': title, 'form': form, 'challenge': challenge, 'entry': entry, 'diary_entries': entry.diaryentry_set.all(), 'is_member': True, 'is_owner': entry.user == request.user, })
def entry_upload(request, entry_id): if request.user.is_anonymous: return HttpResponseRedirect('/login/') entry = get_object_or_404(models.Entry, pk=entry_id) challenge = entry.challenge is_member = request.user in entry.users.all() if not is_member or not entry.isUploadOpen(): messages.error(request, "You're not allowed to upload files!") return HttpResponseRedirect(f'/e/{entry_id}/') if request.method == 'POST': f = FileForm(request.POST, request.FILES) else: f = FileForm() info = { 'challenge': challenge, 'entry': entry, 'files': entry.file_set.all(), 'is_member': True, 'is_owner': entry.user == request.user, 'form': f, } # just display the form? if not f.is_valid(): return render(request, 'challenge/entry_file.html', info) # make sure user isn't sneeeky if f.cleaned_data['is_final'] and not challenge.isFinalUploadOpen(): f._errors['is_final'] = f.error_class( ["Final uploads are not allowed now."]) return render(request, 'challenge/entry_file.html', info) # avoid dupes if os.path.exists( os.path.join(MEDIA_ROOT, str(challenge.number), entry.name, request.FILES['content'].name)): f._errors['content'] = f.error_class( ["File with that filename already exists."]) return render(request, 'challenge/entry_file.html', info) file = models.File( challenge=challenge, entry=entry, user=request.user, created=datetime.datetime.utcnow(), content=request.FILES['content'], description=html2text(f.cleaned_data['description']), is_final=f.cleaned_data['is_final'], is_screenshot=f.cleaned_data['is_screenshot'], thumb_width=0, ) file.save() if file.is_final: entry.has_final = True entry.save() if file.is_screenshot: try: _make_thumbnail(file) except IOError as e: # XXX need feedback with custom error "file is not an image" msg = e.args[0] if not msg.startswith('cannot identify image file'): raise messages.error(request, 'File is not an image') return render(request, 'challenge/entry_file.html', info) log_event( type='new-screenshot', user=request.user, target=file, challenge=challenge.number, game=entry.display_title, name=entry.name, description=file.description, role=file.get_image_role(), ) messages.success(request, 'File added!') return HttpResponseRedirect(f'/e/{entry_id}/')