def user_post_save(sender, instance, created, **kwargs): user = instance # check for profile try: p = user.get_profile() ## user already has a profile except: ## user has no profile - create it p = UserProfile.create(user) if not p.welcome_email and user.email and user.first_name: ## write welcome email password = User.objects.make_random_password(10) docmail.docmail(user.email, _("Welcome to DocMan!"), "welcome", Context({ 'user': user, 'password': password, 'domain': DOMAIN })); p.welcome_email=True p.save() user.set_password(password) user.save()
def save_comment_view(request, document_id): document = get_object_or_404(Document, Q(pk=document_id)) text = request.POST.get('comment') if document.is_etherpad: raise Http404 if len(text) < 5 or len(text) > 2000: messages.add_message(request, messages.ERROR, _('Your comment is too short (or far too long).')) return HttpResponseRedirect(reverse("document", args=[document.pk])) else: c = DocumentComment(pub_date=datetime.datetime.now(), author=request.user, text=text, document=document) c.save() for subscriber in document.subscribers.all(): if subscriber.pk != request.user.pk: docmail.docmail(subscriber.email, "[%s] Neuer Kommentar" % document.name, "document_new_comment", Context({ 'user': subscriber, 'document': document, 'text': text, 'author' : request.user, 'domain': settings.DOMAIN })); messages.add_message(request, messages.SUCCESS, _('Your comment has been saved!')) return HttpResponseRedirect(reverse("document", args=[document.pk]))
def reset_password(profile): user = User.objects.get(pk=profile.user.pk) password = User.objects.make_random_password(10) docmail.docmail(user.email, _("New DocMan Password"), "reset_password", Context({ 'user': user, 'password': password, 'domain': DOMAIN })); user.set_password(password) user.save()
def test_upload_view(request): # we find these in our request header if 'HTTP_DOCUMENTID' in request.META: document_id = request.META["HTTP_DOCUMENTID"] else: document_id = False if 'HTTP_COURSEID' in request.META: course_id = request.META["HTTP_COURSEID"] else: course_id = False # we always expect base64-encoded data content = request.raw_post_data #content = b64decode(request.raw_post_data) if not content: return # whooops - something went wrong! contentFile = ContentFile(content) contentFile.name = request.META['HTTP_UP_FILENAME'] try: contentFile.name = unicode(contentFile.name, 'ascii') except UnicodeError: contentFile.name = unicode(contentFile.name, 'utf-8') else: pass if not document_id: try: course = get_object_or_404(Course.objects.distinct('pk'), Q(pk=course_id), Q(scopes__users__id=request.user.pk)) except: response = {'success' : False, 'error' : _("Course couldn't be loaded - please try again!")} return HttpResponse(simplejson.dumps(response)) else: try: document = Document.objects.get(Q(pk=document_id)) if document.is_etherpad: raise except: response = {'success' : False, 'error' : _("Document couln't be loaded - please try again!")} return HttpResponse(simplejson.dumps(response)) try: if int(contentFile.size) > MAX_UPLOAD_SIZE: response = {'success' : False, 'error' : _('The file is too big!')} return HttpResponse(simplejson.dumps(response)) # we do not want a filetype restriction any longer #content_type = request.META['HTTP_UP_TYPE'] #if content_type in CONTENT_TYPES: #else: # response = {'success' : False, 'error' : _('The file type %s is not supported.') % content_type} # return HttpResponse(simplejson.dumps(response)) except: response = {'success' : False, 'error' : _("An unexpected error occured while uploading the file.")} return HttpResponse(simplejson.dumps(response)) if not document_id: try: document = Document(name=contentFile.name, pub_date=datetime.datetime.now(), author=request.user, course=course) document.save() # auto-subscribe to document document.subscribers.add(request.user) revision = DocumentRevision(document=document, pub_date=datetime.datetime.now(), uploaded_by=request.user) revision.file.save(slugify(document.name) + '.' + document.name.rpartition('.')[len(document.name.rpartition('.'))-1], contentFile) for subscriber in course.subscribers.all(): if subscriber.pk != request.user.pk: docmail.docmail(subscriber.email, "[%s] Neues Dokument" % course.name, "course_new_document", Context({ 'user': subscriber, 'document': document, 'author' : request.user, 'course' : course, 'domain': settings.DOMAIN })); except: raise response = {'success' : False, 'error' : _("An unexpected error occured while uploading the file.")} return HttpResponse(simplejson.dumps(response)) else: try: #document.name = contentFile.name document.pub_date = datetime.datetime.now() document.save() revision = DocumentRevision(document=document, pub_date=datetime.datetime.now(), uploaded_by=request.user) revision.file.save(slugify(contentFile.name) + '.' + contentFile.name.rpartition('.')[len(contentFile.name.rpartition('.'))-1], contentFile) for subscriber in document.subscribers.all(): if subscriber.pk != request.user.pk: docmail.docmail(subscriber.email, "[%s] Neue Revision" % document.name, "document_new_revision", Context({ 'user': subscriber, 'document': document, 'author' : request.user, 'domain': settings.DOMAIN })); except: response = {'success' : False, 'error' : _("An unexpected error occured while uploading the file.")} return HttpResponse(simplejson.dumps(response)) # we do really want to know what the mime type is command = smart_str(u'file %s' % revision.file.path) mime = os.popen(command).read() if mime.find('PDF document') > 0: revision.type = 'pdf' elif mime.find('text') > 0: revision.type = 'txt' elif mime.find('image') > 0: revision.type = 'image' elif mime.find('Rich Text') > 0: revision.type = 'rtf' elif mime.find('Excel') > 0: revision.type = 'xls' elif mime.find('PowerPoint') > 0: revision.type = 'ppt' elif mime.find('Word') > 0: revision.type = 'doc' elif mime.find('MPEG') > 0: revision.type = 'audio' #elif mime.find('Zip archive') > 0: # revision.type = 'zip' else: if revision.file.name.find('pages') > 0: revision.type = 'doc' elif revision.file.name.find('key') > 0: revision.type = 'ppt' elif revision.file.name.find('ppt') > 0: revision.type = 'ppt' elif revision.file.name.find('xls') > 0: revision.type = 'xls' elif revision.file.name.find('xlsx') > 0: revision.type = 'xls' elif revision.file.name.find('doc') > 0: revision.type = 'doc' elif revision.file.name.find('docx') > 0: revision.type = 'doc' elif revision.file.name.find('odp') > 0: revision.type = 'ppt' elif revision.file.name.find('rar') > 0: revision.type = 'zip' elif revision.file.name.find('zip') > 0: revision.type = 'zip' else: # default type: file ending revision.type = document.name.rpartition('.')[len(document.name.rpartition('.'))-1] try: raw_formats = settings.RAW_FORMATS if document.name.rpartition('.')[len(document.name.rpartition('.'))-1] == 'pdf': print "detected pdf, opening.." try: pdf = pyPdf.PdfFileReader(open(revision.file.path, "rb")) raw = "" for page in pdf.pages: raw = "%s %s" % (raw, page.extractText()) revision.raw = raw except: pass elif document.name.rpartition('.')[len(document.name.rpartition('.'))-1] in raw_formats: revision.raw = revision.file.read() except: pass """if revision.raw: #tags = [slugify(word) for word in set(revision.raw.split(" ")) if len(word) > 10] #tags = tags[:5] # get most common words from raw text try: tags = get_most_common_word(revision.raw.split(), n=10, min_length=10) except:""" tags = {} revision.save() # create preview if pdf or image file # we need a pk to do this, so call this after .save() if revision.type == "image" or revision.type == "pdf": revision.create_preview() print "upload done!" # clear cache cache.clear() if document_id: messages.add_message(request, messages.SUCCESS, _("The new revision <strong>%s</strong> has been uploaded successfully." % document.name)) response = {'revision': True, 'success' : True, 'ok' : _("The new revision <strong>%s</strong> has been uploaded successfully." % document.name), 'tags': ' '.join(tags),} else: response = {'documentName' : document.name, 'documentId' : document.pk, 'success' : True, 'ok' : _("The Document <strong>%s</strong> has been uploaded successfully.") % document.name, 'tags': ' '.join(tags),} return HttpResponse(simplejson.dumps(response))
def ie_upload_view(request): if request.POST.get('document_id'): document_id = request.POST.get('document_id') else: document_id = False if request.POST.get('course_id'): course_id = request.POST.get('course_id') else: messages.add_message(request, messages.ERROR, _('An unknown error occurred while trying to upload your file. Please try again later!')) return HttpResponseRedirect(request.META.get('HTTP_REFERER')) try: contentFile = request.FILES['file'] except: messages.add_message(request, messages.ERROR, _('No File selected!')) return HttpResponseRedirect(request.META.get('HTTP_REFERER')) if not document_id: u_name = request.POST.get('name') u_tags = request.POST.get('tags') u_desc = request.POST.get('desc') if not request.user.get_profile().is_lecturer: is_lecturer_visible = request.POST.get("is_lecturer_visible") """try: contentFile.name = unicode(contentFile.name, 'ascii') except UnicodeError: contentFile.name = unicode(contentFile.name, 'utf-8') else: pass""" if not document_id: try: course = get_object_or_404(Course.objects.distinct('pk'), Q(pk=course_id), Q(scopes__users__id=request.user.pk)) except: messages.add_message(request, messages.ERROR, _("Course couldn't be loaded - please try again!")) return HttpResponseRedirect(request.META.get('HTTP_REFERER')) else: try: document = Document.objects.get(Q(pk=document_id)) if document.is_etherpad: raise except: messages.add_message(request, messages.ERROR, _("Document couln't be loaded - please try again!")) return HttpResponseRedirect(request.META.get('HTTP_REFERER')) try: if int(contentFile.size) > MAX_UPLOAD_SIZE: messages.add_message(request, messages.ERROR, _("The file is too big!")) return HttpResponseRedirect(request.META.get('HTTP_REFERER')) except: messages.add_message(request, messages.ERROR, _("An unexpected error occured while uploading the file.")) return HttpResponseRedirect(request.META.get('HTTP_REFERER')) if not document_id: try: document = Document(name=contentFile.name, pub_date=datetime.datetime.now(), author=request.user, course=course) document.save() # auto-subscribe to document document.subscribers.add(request.user) revision = DocumentRevision(document=document, pub_date=datetime.datetime.now(), uploaded_by=request.user) revision.file.save(slugify(document.name) + '.' + document.name.rpartition('.')[len(document.name.rpartition('.'))-1], contentFile) for subscriber in course.subscribers.all(): if subscriber.pk != request.user.pk: docmail.docmail(subscriber.email, "[%s] Neues Dokument" % course.name, "course_new_document", Context({ 'user': subscriber, 'document': document, 'author' : request.user, 'course' : course, 'domain': settings.DOMAIN })); except: raise messages.add_message(request, messages.ERROR, _("An unexpected error occured while uploading the file.")) return HttpResponseRedirect(request.META.get('HTTP_REFERER')) else: try: #document.name = contentFile.name document.pub_date = datetime.datetime.now() document.save() revision = DocumentRevision(document=document, pub_date=datetime.datetime.now(), uploaded_by=request.user) revision.file.save(slugify(contentFile.name) + '.' + contentFile.name.rpartition('.')[len(contentFile.name.rpartition('.'))-1], contentFile) for subscriber in document.subscribers.all(): if subscriber.pk != request.user.pk: docmail.docmail(subscriber.email, "[%s] Neue Revision" % document.name, "document_new_revision", Context({ 'user': subscriber, 'document': document, 'author' : request.user, 'domain': settings.DOMAIN })); except: messages.add_message(request, messages.ERROR, _("An unexpected error occured while uploading the file.")) return HttpResponseRedirect(request.META.get('HTTP_REFERER')) if not document_id: # filter tags tags = u_tags.lower().strip() tags = re.sub("[^a-z0-9-, ]", "", tags) document.desc = u_desc if u_name: document.name = u_name document.tags = tags if not request.user.get_profile().is_lecturer: if is_lecturer_visible: document.is_lecturer_visible = True else: document.is_lecturer_visible = False document.save() # we do really want to know what the mime type is command = smart_str(u'file %s' % revision.file.path) mime = os.popen(command).read() if mime.find('PDF document') > 0: revision.type = 'pdf' elif mime.find('text') > 0: revision.type = 'txt' elif mime.find('image') > 0: revision.type = 'image' elif mime.find('Rich Text') > 0: revision.type = 'rtf' elif mime.find('Excel') > 0: revision.type = 'xls' elif mime.find('PowerPoint') > 0: revision.type = 'ppt' elif mime.find('Word') > 0: revision.type = 'doc' elif mime.find('MPEG') > 0: revision.type = 'audio' #elif mime.find('Zip archive') > 0: # revision.type = 'zip' else: if revision.file.name.find('pages') > 0: revision.type = 'doc' elif revision.file.name.find('key') > 0: revision.type = 'ppt' elif revision.file.name.find('ppt') > 0: revision.type = 'ppt' elif revision.file.name.find('xls') > 0: revision.type = 'xls' elif revision.file.name.find('xlsx') > 0: revision.type = 'xls' elif revision.file.name.find('doc') > 0: revision.type = 'doc' elif revision.file.name.find('docx') > 0: revision.type = 'doc' elif revision.file.name.find('odp') > 0: revision.type = 'ppt' elif revision.file.name.find('rar') > 0: revision.type = 'zip' elif revision.file.name.find('zip') > 0: revision.type = 'zip' else: # default type: file ending revision.type = document.name.rpartition('.')[len(document.name.rpartition('.'))-1] try: raw_formats = settings.RAW_FORMATS if document.name.rpartition('.')[len(document.name.rpartition('.'))-1] == 'pdf': print "detected pdf, opening.." try: pdf = pyPdf.PdfFileReader(open(revision.file.path, "rb")) raw = "" for page in pdf.pages: raw = "%s %s" % (raw, page.extractText()) revision.raw = raw except: pass elif document.name.rpartition('.')[len(document.name.rpartition('.'))-1] in raw_formats: revision.raw = revision.file.read() except: pass revision.save() # create preview if pdf or image file # we need a pk to do this, so call this after .save() if revision.type == "image" or revision.type == "pdf": revision.create_preview() print "upload done!" # clear cache cache.clear() if document_id: messages.add_message(request, messages.SUCCESS, _("The new revision <strong>%s</strong> has been uploaded successfully." % document.name)) return HttpResponseRedirect(request.META.get('HTTP_REFERER')) else: messages.add_message(request, messages.SUCCESS, _("The Document <strong>%s</strong> has been uploaded successfully.") % document.name) return HttpResponseRedirect(reverse("document", args=[document.pk]))