Created
November 19, 2014 14:14
-
-
Save allyjweir/502d584b5732a9cf23b9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#In forms.py | |
class WebForm(forms.Form): | |
url = forms.URLField(label='Link') | |
class Meta: | |
model = Datapoint | |
fields = ('uploaded_by', 'name', 'file', 'url', 'description', 'author', 'source', 'publish_date',) | |
#My current attempt in views.py | |
class DatapointWebUploadView(LoginRequiredMixin, FormView): | |
template_name = 'datapoint/datapoint_web_upload_form.html' | |
form_class = WebForm | |
success_url = reverse_lazy('datapoint:list') | |
''' | |
- Run newspaper over the link | |
- Fill the fields out of form.instance (see file upload filefield for example) with this information | |
- Redirect to the datapoint's page | |
''' | |
def form_valid(self, form): | |
self.object = form.save(commit=False) | |
article = web_import.get_article(form.cleaned_data['url']) | |
logger.debug('got article') | |
screenshot = web_import.get_screenshot(form.cleaned_data['url']) | |
logger.debug('got screenshot') | |
screenshot_file = open(screenshot, 'r') | |
logger.debug('opened screenshot') | |
self.object.uploaded_by = self.request.user | |
self.object.name = article.title | |
self.object.description = article.summary | |
self.object.author = article.authors | |
self.object.file = screenshot_file | |
logger.debug('saved stuff to object') | |
self.object.save() | |
return super(DatapointWebUploadView, self).form_valid(form) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment